burndown.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. package hercules
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "os"
  7. "sort"
  8. "unicode/utf8"
  9. "github.com/gogo/protobuf/proto"
  10. "github.com/sergi/go-diff/diffmatchpatch"
  11. "gopkg.in/src-d/go-git.v4"
  12. "gopkg.in/src-d/go-git.v4/plumbing"
  13. "gopkg.in/src-d/go-git.v4/plumbing/object"
  14. "gopkg.in/src-d/go-git.v4/utils/merkletrie"
  15. "gopkg.in/src-d/hercules.v3/pb"
  16. "gopkg.in/src-d/hercules.v3/yaml"
  17. )
  18. // BurndownAnalyser allows to gather the line burndown statistics for a Git repository.
  19. type BurndownAnalysis struct {
  20. // Granularity sets the size of each band - the number of days it spans.
  21. // Smaller values provide better resolution but require more work and eat more
  22. // memory. 30 days is usually enough.
  23. Granularity int
  24. // Sampling sets how detailed is the statistic - the size of the interval in
  25. // days between consecutive measurements. It is usually a good idea to set it
  26. // <= Granularity. Try 15 or 30.
  27. Sampling int
  28. // TrackFiles enables or disables the fine-grained per-file burndown analysis.
  29. // It does not change the top level burndown results.
  30. TrackFiles bool
  31. // The number of developers for which to collect the burndown stats. 0 disables it.
  32. PeopleNumber int
  33. // Debug activates the debugging mode. Analyse() runs slower in this mode
  34. // but it accurately checks all the intermediate states for invariant
  35. // violations.
  36. Debug bool
  37. // Repository points to the analysed Git repository struct from go-git.
  38. repository *git.Repository
  39. // globalStatus is the current daily alive number of lines; key is the number
  40. // of days from the beginning of the history.
  41. globalStatus map[int]int64
  42. // globalHistory is the weekly snapshots of globalStatus.
  43. globalHistory [][]int64
  44. // fileHistories is the weekly snapshots of each file's status.
  45. fileHistories map[string][][]int64
  46. // peopleHistories is the weekly snapshots of each person's status.
  47. peopleHistories [][][]int64
  48. // files is the mapping <file path> -> *File.
  49. files map[string]*File
  50. // matrix is the mutual deletions and self insertions.
  51. matrix []map[int]int64
  52. // people is the people's individual time stats.
  53. people []map[int]int64
  54. // day is the most recent day index processed.
  55. day int
  56. // previousDay is the day from the previous sample period -
  57. // different from DaysSinceStart.previousDay.
  58. previousDay int
  59. // references IdentityDetector.ReversedPeopleDict
  60. reversedPeopleDict []string
  61. }
  62. type BurndownResult struct {
  63. GlobalHistory [][]int64
  64. FileHistories map[string][][]int64
  65. PeopleHistories [][][]int64
  66. PeopleMatrix [][]int64
  67. reversedPeopleDict []string
  68. sampling int
  69. granularity int
  70. }
  71. const (
  72. ConfigBurndownGranularity = "Burndown.Granularity"
  73. ConfigBurndownSampling = "Burndown.Sampling"
  74. ConfigBurndownTrackFiles = "Burndown.TrackFiles"
  75. ConfigBurndownTrackPeople = "Burndown.TrackPeople"
  76. ConfigBurndownDebug = "Burndown.Debug"
  77. )
  78. func (analyser *BurndownAnalysis) Name() string {
  79. return "Burndown"
  80. }
  81. func (analyser *BurndownAnalysis) Provides() []string {
  82. return []string{}
  83. }
  84. func (analyser *BurndownAnalysis) Requires() []string {
  85. arr := [...]string{"file_diff", "changes", "blob_cache", "day", "author"}
  86. return arr[:]
  87. }
  88. func (analyser *BurndownAnalysis) ListConfigurationOptions() []ConfigurationOption {
  89. options := [...]ConfigurationOption{{
  90. Name: ConfigBurndownGranularity,
  91. Description: "How many days there are in a single band.",
  92. Flag: "granularity",
  93. Type: IntConfigurationOption,
  94. Default: 30}, {
  95. Name: ConfigBurndownSampling,
  96. Description: "How frequently to record the state in days.",
  97. Flag: "sampling",
  98. Type: IntConfigurationOption,
  99. Default: 30}, {
  100. Name: ConfigBurndownTrackFiles,
  101. Description: "Record detailed statistics per each file.",
  102. Flag: "burndown-files",
  103. Type: BoolConfigurationOption,
  104. Default: false}, {
  105. Name: ConfigBurndownTrackPeople,
  106. Description: "Record detailed statistics per each developer.",
  107. Flag: "burndown-people",
  108. Type: BoolConfigurationOption,
  109. Default: false}, {
  110. Name: ConfigBurndownDebug,
  111. Description: "Validate the trees on each step.",
  112. Flag: "burndown-debug",
  113. Type: BoolConfigurationOption,
  114. Default: false},
  115. }
  116. return options[:]
  117. }
  118. func (analyser *BurndownAnalysis) Configure(facts map[string]interface{}) {
  119. if val, exists := facts[ConfigBurndownGranularity].(int); exists {
  120. analyser.Granularity = val
  121. }
  122. if val, exists := facts[ConfigBurndownSampling].(int); exists {
  123. analyser.Sampling = val
  124. }
  125. if val, exists := facts[ConfigBurndownTrackFiles].(bool); exists {
  126. analyser.TrackFiles = val
  127. }
  128. if people, exists := facts[ConfigBurndownTrackPeople].(bool); people {
  129. if val, exists := facts[FactIdentityDetectorPeopleCount].(int); exists {
  130. analyser.PeopleNumber = val
  131. analyser.reversedPeopleDict = facts[FactIdentityDetectorReversedPeopleDict].([]string)
  132. }
  133. } else if exists {
  134. analyser.PeopleNumber = 0
  135. }
  136. if val, exists := facts[ConfigBurndownDebug].(bool); exists {
  137. analyser.Debug = val
  138. }
  139. }
  140. func (analyser *BurndownAnalysis) Flag() string {
  141. return "burndown"
  142. }
  143. func (analyser *BurndownAnalysis) Initialize(repository *git.Repository) {
  144. if analyser.Granularity <= 0 {
  145. fmt.Fprintln(os.Stderr, "Warning: adjusted the granularity to 30 days")
  146. analyser.Granularity = 30
  147. }
  148. if analyser.Sampling <= 0 {
  149. fmt.Fprintln(os.Stderr, "Warning: adjusted the sampling to 30 days")
  150. analyser.Sampling = 30
  151. }
  152. analyser.repository = repository
  153. analyser.globalStatus = map[int]int64{}
  154. analyser.globalHistory = [][]int64{}
  155. analyser.fileHistories = map[string][][]int64{}
  156. analyser.peopleHistories = make([][][]int64, analyser.PeopleNumber)
  157. analyser.files = map[string]*File{}
  158. analyser.matrix = make([]map[int]int64, analyser.PeopleNumber)
  159. analyser.people = make([]map[int]int64, analyser.PeopleNumber)
  160. analyser.day = 0
  161. analyser.previousDay = 0
  162. }
  163. func (analyser *BurndownAnalysis) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  164. sampling := analyser.Sampling
  165. if sampling == 0 {
  166. sampling = 1
  167. }
  168. author := deps["author"].(int)
  169. analyser.day = deps["day"].(int)
  170. delta := (analyser.day / sampling) - (analyser.previousDay / sampling)
  171. if delta > 0 {
  172. analyser.previousDay = analyser.day
  173. gs, fss, pss := analyser.groupStatus()
  174. analyser.updateHistories(gs, fss, pss, delta)
  175. }
  176. cache := deps["blob_cache"].(map[plumbing.Hash]*object.Blob)
  177. treeDiffs := deps["changes"].(object.Changes)
  178. fileDiffs := deps["file_diff"].(map[string]FileDiffData)
  179. for _, change := range treeDiffs {
  180. action, err := change.Action()
  181. if err != nil {
  182. return nil, err
  183. }
  184. switch action {
  185. case merkletrie.Insert:
  186. err = analyser.handleInsertion(change, author, cache)
  187. case merkletrie.Delete:
  188. err = analyser.handleDeletion(change, author, cache)
  189. case merkletrie.Modify:
  190. err = analyser.handleModification(change, author, cache, fileDiffs)
  191. }
  192. if err != nil {
  193. return nil, err
  194. }
  195. }
  196. return nil, nil
  197. }
  198. // Finalize() returns the list of snapshots of the cumulative line edit times
  199. // and the similar lists for every file which is alive in HEAD.
  200. // The number of snapshots (the first dimension >[]<[]int64) depends on
  201. // Analyser.Sampling (the more Sampling, the less the value); the length of
  202. // each snapshot depends on Analyser.Granularity (the more Granularity,
  203. // the less the value).
  204. func (analyser *BurndownAnalysis) Finalize() interface{} {
  205. gs, fss, pss := analyser.groupStatus()
  206. analyser.updateHistories(gs, fss, pss, 1)
  207. for key, statuses := range analyser.fileHistories {
  208. if len(statuses) == len(analyser.globalHistory) {
  209. continue
  210. }
  211. padding := make([][]int64, len(analyser.globalHistory)-len(statuses))
  212. for i := range padding {
  213. padding[i] = make([]int64, len(analyser.globalStatus))
  214. }
  215. analyser.fileHistories[key] = append(padding, statuses...)
  216. }
  217. peopleMatrix := make([][]int64, analyser.PeopleNumber)
  218. for i, row := range analyser.matrix {
  219. mrow := make([]int64, analyser.PeopleNumber+2)
  220. peopleMatrix[i] = mrow
  221. for key, val := range row {
  222. if key == MISSING_AUTHOR {
  223. key = -1
  224. } else if key == SELF_AUTHOR {
  225. key = -2
  226. }
  227. mrow[key+2] = val
  228. }
  229. }
  230. return BurndownResult{
  231. GlobalHistory: analyser.globalHistory,
  232. FileHistories: analyser.fileHistories,
  233. PeopleHistories: analyser.peopleHistories,
  234. PeopleMatrix: peopleMatrix,
  235. reversedPeopleDict: analyser.reversedPeopleDict,
  236. }
  237. }
  238. func (analyser *BurndownAnalysis) Serialize(result interface{}, binary bool, writer io.Writer) error {
  239. burndownResult := result.(BurndownResult)
  240. if binary {
  241. return analyser.serializeBinary(&burndownResult, writer)
  242. }
  243. analyser.serializeText(&burndownResult, writer)
  244. return nil
  245. }
  246. func (analyser *BurndownAnalysis) Deserialize(pbmessage []byte) (interface{}, error) {
  247. msg := pb.BurndownAnalysisResults{}
  248. err := proto.Unmarshal(pbmessage, &msg)
  249. if err != nil {
  250. return nil, err
  251. }
  252. result := BurndownResult{}
  253. convertCSR := func(mat *pb.BurndownSparseMatrix) [][]int64 {
  254. res := make([][]int64, mat.NumberOfRows)
  255. for i := 0; i < int(mat.NumberOfRows); i++ {
  256. res[i] = make([]int64, mat.NumberOfColumns)
  257. for j := 0; j < int(mat.NumberOfColumns); j++ {
  258. res[i][j] = int64(mat.Rows[i].Columns[j])
  259. }
  260. }
  261. return res
  262. }
  263. result.GlobalHistory = convertCSR(msg.Project)
  264. result.FileHistories = map[string][][]int64{}
  265. for _, mat := range msg.Files {
  266. result.FileHistories[mat.Name] = convertCSR(mat)
  267. }
  268. result.reversedPeopleDict = make([]string, len(msg.People))
  269. result.PeopleHistories = make([][][]int64, len(msg.People))
  270. for i, mat := range msg.People {
  271. result.PeopleHistories[i] = convertCSR(mat)
  272. result.reversedPeopleDict[i] = mat.Name
  273. }
  274. result.PeopleMatrix = make([][]int64, msg.PeopleInteraction.NumberOfRows)
  275. for i := 0; i < len(result.PeopleMatrix); i++ {
  276. result.PeopleMatrix[i] = make([]int64, msg.PeopleInteraction.NumberOfColumns)
  277. for j := int(msg.PeopleInteraction.Indptr[i]); j < int(msg.PeopleInteraction.Indptr[i+1]); j++ {
  278. result.PeopleMatrix[i][msg.PeopleInteraction.Indices[j]] = msg.PeopleInteraction.Data[j]
  279. }
  280. }
  281. result.sampling = int(msg.Sampling)
  282. result.granularity = int(msg.Granularity)
  283. return result, nil
  284. }
  285. func (analyser *BurndownAnalysis) MergeResults(
  286. r1, r2 interface{}, c1, c2 *CommonAnalysisResult) interface{} {
  287. bar1 := r1.(BurndownResult)
  288. bar2 := r2.(BurndownResult)
  289. merged := BurndownResult{}
  290. if bar1.sampling < bar2.sampling {
  291. merged.sampling = bar1.sampling
  292. } else {
  293. merged.sampling = bar2.sampling
  294. }
  295. if bar1.granularity < bar2.granularity {
  296. merged.granularity = bar1.granularity
  297. } else {
  298. merged.granularity = bar2.granularity
  299. }
  300. people := map[string]int{}
  301. for _, id := range bar1.reversedPeopleDict {
  302. people[id] = len(people)
  303. }
  304. for _, id := range bar2.reversedPeopleDict {
  305. if _, exists := people[id]; !exists {
  306. people[id] = len(people)
  307. }
  308. }
  309. merged.reversedPeopleDict = make([]string, len(people))
  310. for name, index := range people {
  311. merged.reversedPeopleDict[index] = name
  312. }
  313. // interpolate to daily and sum
  314. _ = bar1
  315. _ = bar2
  316. panic("not implemented")
  317. // return merged
  318. }
  319. func mergeMatrices(m1, m2 [][]int64, granularity1, sampling1, granularity2, sampling2 int,
  320. c1, c2 *CommonAnalysisResult) {
  321. commonMerged := CommonAnalysisResult{}
  322. commonMerged.Merge(c1)
  323. commonMerged.Merge(c2)
  324. size := (commonMerged.EndTime - commonMerged.BeginTime) / (3600 * 24)
  325. daily := make([][]float32, size)
  326. for i := range daily {
  327. daily[i] = make([]float32, size)
  328. }
  329. addMatrix(m1, granularity1, sampling1, daily,
  330. int(c1.BeginTime-commonMerged.BeginTime)/(3600*24))
  331. addMatrix(m2, granularity2, sampling2, daily,
  332. int(c2.BeginTime-commonMerged.BeginTime)/(3600*24))
  333. // convert daily to [][]int64
  334. }
  335. func addMatrix(matrix [][]int64, granularity, sampling int, daily [][]float32, offset int) {
  336. /*
  337. daily_matrix = numpy.zeros(
  338. (matrix.shape[0] * granularity, matrix.shape[1] * sampling),
  339. dtype=numpy.float32)
  340. */
  341. // Determine the maximum number of bands; the actual one may be larger but we do not care
  342. maxCols := 0
  343. for _, row := range matrix {
  344. if maxCols < len(row) {
  345. maxCols = len(row)
  346. }
  347. }
  348. // Ported from labours.py load_burndown()
  349. for y := 0; y < maxCols; y++ {
  350. for x := 0; x < len(matrix); x++ {
  351. if (y+1)*granularity <= x*sampling {
  352. // interpolate
  353. var previous int64
  354. if x > 0 && y < len(matrix[x-1]) {
  355. previous = matrix[x-1][y]
  356. }
  357. for i := 0; i < sampling; i++ {
  358. var value float32
  359. if y < len(matrix[x]) {
  360. value = (float32(previous) +
  361. float32((matrix[x][y]-previous)*int64(i))/float32(sampling)) / float32(granularity)
  362. } else {
  363. value = float32(previous) *
  364. (float32(1) - float32(i)/float32(sampling)) / float32(granularity)
  365. }
  366. for j := y * granularity; j < (y+1)*granularity; j++ {
  367. daily[j+offset][x*sampling+i+offset] += value
  368. }
  369. }
  370. } else if y*granularity <= (x+1)*sampling && y < len(matrix[x]) {
  371. // fill constant
  372. for suby := y*granularity + offset; suby < (y+1)*granularity+offset; suby++ {
  373. for subx := suby; subx < (x+1)*sampling+offset; subx++ {
  374. daily[suby][subx] += float32(matrix[x][y]) / float32(granularity)
  375. }
  376. }
  377. }
  378. }
  379. }
  380. }
  381. func (analyser *BurndownAnalysis) serializeText(result *BurndownResult, writer io.Writer) {
  382. fmt.Fprintln(writer, " granularity:", analyser.Granularity)
  383. fmt.Fprintln(writer, " sampling:", analyser.Sampling)
  384. yaml.PrintMatrix(writer, result.GlobalHistory, 2, "project", true)
  385. if len(result.FileHistories) > 0 {
  386. fmt.Fprintln(writer, " files:")
  387. keys := sortedKeys(result.FileHistories)
  388. for _, key := range keys {
  389. yaml.PrintMatrix(writer, result.FileHistories[key], 4, key, true)
  390. }
  391. }
  392. if len(result.PeopleHistories) > 0 {
  393. fmt.Fprintln(writer, " people_sequence:")
  394. for key := range result.PeopleHistories {
  395. fmt.Fprintln(writer, " - "+yaml.SafeString(result.reversedPeopleDict[key]))
  396. }
  397. fmt.Fprintln(writer, " people:")
  398. for key, val := range result.PeopleHistories {
  399. yaml.PrintMatrix(writer, val, 4, result.reversedPeopleDict[key], true)
  400. }
  401. fmt.Fprintln(writer, " people_interaction: |-")
  402. yaml.PrintMatrix(writer, result.PeopleMatrix, 4, "", false)
  403. }
  404. }
  405. func (analyser *BurndownAnalysis) serializeBinary(result *BurndownResult, writer io.Writer) error {
  406. message := pb.BurndownAnalysisResults{
  407. Granularity: int32(analyser.Granularity),
  408. Sampling: int32(analyser.Sampling),
  409. Project: pb.ToBurndownSparseMatrix(result.GlobalHistory, "project"),
  410. }
  411. if len(result.FileHistories) > 0 {
  412. message.Files = make([]*pb.BurndownSparseMatrix, len(result.FileHistories))
  413. keys := sortedKeys(result.FileHistories)
  414. i := 0
  415. for _, key := range keys {
  416. message.Files[i] = pb.ToBurndownSparseMatrix(
  417. result.FileHistories[key], key)
  418. i++
  419. }
  420. }
  421. if len(result.PeopleHistories) > 0 {
  422. message.People = make(
  423. []*pb.BurndownSparseMatrix, len(result.PeopleHistories))
  424. for key, val := range result.PeopleHistories {
  425. message.People[key] = pb.ToBurndownSparseMatrix(val, result.reversedPeopleDict[key])
  426. }
  427. message.PeopleInteraction = pb.DenseToCompressedSparseRowMatrix(result.PeopleMatrix)
  428. }
  429. serialized, err := proto.Marshal(&message)
  430. if err != nil {
  431. return err
  432. }
  433. writer.Write(serialized)
  434. return nil
  435. }
  436. func sortedKeys(m map[string][][]int64) []string {
  437. keys := make([]string, 0, len(m))
  438. for k := range m {
  439. keys = append(keys, k)
  440. }
  441. sort.Strings(keys)
  442. return keys
  443. }
  444. func checkClose(c io.Closer) {
  445. if err := c.Close(); err != nil {
  446. panic(err)
  447. }
  448. }
  449. func (analyser *BurndownAnalysis) packPersonWithDay(person int, day int) int {
  450. if analyser.PeopleNumber == 0 {
  451. return day
  452. }
  453. result := day
  454. result |= person << 14
  455. // This effectively means max 16384 days (>44 years) and (131072 - 2) devs
  456. return result
  457. }
  458. func (analyser *BurndownAnalysis) unpackPersonWithDay(value int) (int, int) {
  459. if analyser.PeopleNumber == 0 {
  460. return MISSING_AUTHOR, value
  461. }
  462. return value >> 14, value & 0x3FFF
  463. }
  464. func (analyser *BurndownAnalysis) updateStatus(
  465. status interface{}, _ int, previous_time_ int, delta int) {
  466. _, previous_time := analyser.unpackPersonWithDay(previous_time_)
  467. status.(map[int]int64)[previous_time] += int64(delta)
  468. }
  469. func (analyser *BurndownAnalysis) updatePeople(people interface{}, _ int, previous_time_ int, delta int) {
  470. old_author, previous_time := analyser.unpackPersonWithDay(previous_time_)
  471. if old_author == MISSING_AUTHOR {
  472. return
  473. }
  474. casted := people.([]map[int]int64)
  475. stats := casted[old_author]
  476. if stats == nil {
  477. stats = map[int]int64{}
  478. casted[old_author] = stats
  479. }
  480. stats[previous_time] += int64(delta)
  481. }
  482. func (analyser *BurndownAnalysis) updateMatrix(
  483. matrix_ interface{}, current_time int, previous_time int, delta int) {
  484. matrix := matrix_.([]map[int]int64)
  485. new_author, _ := analyser.unpackPersonWithDay(current_time)
  486. old_author, _ := analyser.unpackPersonWithDay(previous_time)
  487. if old_author == MISSING_AUTHOR {
  488. return
  489. }
  490. if new_author == old_author && delta > 0 {
  491. new_author = SELF_AUTHOR
  492. }
  493. row := matrix[old_author]
  494. if row == nil {
  495. row = map[int]int64{}
  496. matrix[old_author] = row
  497. }
  498. cell, exists := row[new_author]
  499. if !exists {
  500. row[new_author] = 0
  501. cell = 0
  502. }
  503. row[new_author] = cell + int64(delta)
  504. }
  505. func (analyser *BurndownAnalysis) newFile(
  506. author int, day int, size int, global map[int]int64, people []map[int]int64,
  507. matrix []map[int]int64) *File {
  508. statuses := make([]Status, 1)
  509. statuses[0] = NewStatus(global, analyser.updateStatus)
  510. if analyser.TrackFiles {
  511. statuses = append(statuses, NewStatus(map[int]int64{}, analyser.updateStatus))
  512. }
  513. if analyser.PeopleNumber > 0 {
  514. statuses = append(statuses, NewStatus(people, analyser.updatePeople))
  515. statuses = append(statuses, NewStatus(matrix, analyser.updateMatrix))
  516. day = analyser.packPersonWithDay(author, day)
  517. }
  518. return NewFile(day, size, statuses...)
  519. }
  520. func (analyser *BurndownAnalysis) handleInsertion(
  521. change *object.Change, author int, cache map[plumbing.Hash]*object.Blob) error {
  522. blob := cache[change.To.TreeEntry.Hash]
  523. lines, err := CountLines(blob)
  524. if err != nil {
  525. if err.Error() == "binary" {
  526. return nil
  527. }
  528. return err
  529. }
  530. name := change.To.Name
  531. file, exists := analyser.files[name]
  532. if exists {
  533. return errors.New(fmt.Sprintf("file %s already exists", name))
  534. }
  535. file = analyser.newFile(
  536. author, analyser.day, lines, analyser.globalStatus, analyser.people, analyser.matrix)
  537. analyser.files[name] = file
  538. return nil
  539. }
  540. func (analyser *BurndownAnalysis) handleDeletion(
  541. change *object.Change, author int, cache map[plumbing.Hash]*object.Blob) error {
  542. blob := cache[change.From.TreeEntry.Hash]
  543. lines, err := CountLines(blob)
  544. if err != nil {
  545. if err.Error() == "binary" {
  546. return nil
  547. }
  548. return err
  549. }
  550. name := change.From.Name
  551. file := analyser.files[name]
  552. file.Update(analyser.packPersonWithDay(author, analyser.day), 0, 0, lines)
  553. delete(analyser.files, name)
  554. return nil
  555. }
  556. func (analyser *BurndownAnalysis) handleModification(
  557. change *object.Change, author int, cache map[plumbing.Hash]*object.Blob,
  558. diffs map[string]FileDiffData) error {
  559. file, exists := analyser.files[change.From.Name]
  560. if !exists {
  561. // this indeed may happen
  562. return analyser.handleInsertion(change, author, cache)
  563. }
  564. // possible rename
  565. if change.To.Name != change.From.Name {
  566. err := analyser.handleRename(change.From.Name, change.To.Name)
  567. if err != nil {
  568. return err
  569. }
  570. }
  571. thisDiffs := diffs[change.To.Name]
  572. if file.Len() != thisDiffs.OldLinesOfCode {
  573. fmt.Fprintf(os.Stderr, "====TREE====\n%s", file.Dump())
  574. return errors.New(fmt.Sprintf("%s: internal integrity error src %d != %d %s -> %s",
  575. change.To.Name, thisDiffs.OldLinesOfCode, file.Len(),
  576. change.From.TreeEntry.Hash.String(), change.To.TreeEntry.Hash.String()))
  577. }
  578. // we do not call RunesToDiffLines so the number of lines equals
  579. // to the rune count
  580. position := 0
  581. pending := diffmatchpatch.Diff{Text: ""}
  582. apply := func(edit diffmatchpatch.Diff) {
  583. length := utf8.RuneCountInString(edit.Text)
  584. if edit.Type == diffmatchpatch.DiffInsert {
  585. file.Update(analyser.packPersonWithDay(author, analyser.day), position, length, 0)
  586. position += length
  587. } else {
  588. file.Update(analyser.packPersonWithDay(author, analyser.day), position, 0, length)
  589. }
  590. if analyser.Debug {
  591. file.Validate()
  592. }
  593. }
  594. for _, edit := range thisDiffs.Diffs {
  595. dump_before := ""
  596. if analyser.Debug {
  597. dump_before = file.Dump()
  598. }
  599. length := utf8.RuneCountInString(edit.Text)
  600. debug_error := func() {
  601. fmt.Fprintf(os.Stderr, "%s: internal diff error\n", change.To.Name)
  602. fmt.Fprintf(os.Stderr, "Update(%d, %d, %d (0), %d (0))\n", analyser.day, position,
  603. length, utf8.RuneCountInString(pending.Text))
  604. if dump_before != "" {
  605. fmt.Fprintf(os.Stderr, "====TREE BEFORE====\n%s====END====\n", dump_before)
  606. }
  607. fmt.Fprintf(os.Stderr, "====TREE AFTER====\n%s====END====\n", file.Dump())
  608. }
  609. switch edit.Type {
  610. case diffmatchpatch.DiffEqual:
  611. if pending.Text != "" {
  612. apply(pending)
  613. pending.Text = ""
  614. }
  615. position += length
  616. case diffmatchpatch.DiffInsert:
  617. if pending.Text != "" {
  618. if pending.Type == diffmatchpatch.DiffInsert {
  619. debug_error()
  620. return errors.New("DiffInsert may not appear after DiffInsert")
  621. }
  622. file.Update(analyser.packPersonWithDay(author, analyser.day), position, length,
  623. utf8.RuneCountInString(pending.Text))
  624. if analyser.Debug {
  625. file.Validate()
  626. }
  627. position += length
  628. pending.Text = ""
  629. } else {
  630. pending = edit
  631. }
  632. case diffmatchpatch.DiffDelete:
  633. if pending.Text != "" {
  634. debug_error()
  635. return errors.New("DiffDelete may not appear after DiffInsert/DiffDelete")
  636. }
  637. pending = edit
  638. default:
  639. debug_error()
  640. return errors.New(fmt.Sprintf("diff operation is not supported: %d", edit.Type))
  641. }
  642. }
  643. if pending.Text != "" {
  644. apply(pending)
  645. pending.Text = ""
  646. }
  647. if file.Len() != thisDiffs.NewLinesOfCode {
  648. return errors.New(fmt.Sprintf("%s: internal integrity error dst %d != %d",
  649. change.To.Name, thisDiffs.NewLinesOfCode, file.Len()))
  650. }
  651. return nil
  652. }
  653. func (analyser *BurndownAnalysis) handleRename(from, to string) error {
  654. file, exists := analyser.files[from]
  655. if !exists {
  656. return errors.New(fmt.Sprintf("file %s does not exist", from))
  657. }
  658. analyser.files[to] = file
  659. delete(analyser.files, from)
  660. return nil
  661. }
  662. func (analyser *BurndownAnalysis) groupStatus() ([]int64, map[string][]int64, [][]int64) {
  663. granularity := analyser.Granularity
  664. if granularity == 0 {
  665. granularity = 1
  666. }
  667. day := analyser.day
  668. day++
  669. adjust := 0
  670. if day%granularity != 0 {
  671. adjust = 1
  672. }
  673. global := make([]int64, day/granularity+adjust)
  674. var group int64
  675. for i := 0; i < day; i++ {
  676. group += analyser.globalStatus[i]
  677. if (i % granularity) == (granularity - 1) {
  678. global[i/granularity] = group
  679. group = 0
  680. }
  681. }
  682. if day%granularity != 0 {
  683. global[len(global)-1] = group
  684. }
  685. locals := make(map[string][]int64)
  686. if analyser.TrackFiles {
  687. for key, file := range analyser.files {
  688. status := make([]int64, day/granularity+adjust)
  689. var group int64
  690. for i := 0; i < day; i++ {
  691. group += file.Status(1).(map[int]int64)[i]
  692. if (i % granularity) == (granularity - 1) {
  693. status[i/granularity] = group
  694. group = 0
  695. }
  696. }
  697. if day%granularity != 0 {
  698. status[len(status)-1] = group
  699. }
  700. locals[key] = status
  701. }
  702. }
  703. peoples := make([][]int64, len(analyser.people))
  704. for key, person := range analyser.people {
  705. status := make([]int64, day/granularity+adjust)
  706. var group int64
  707. for i := 0; i < day; i++ {
  708. group += person[i]
  709. if (i % granularity) == (granularity - 1) {
  710. status[i/granularity] = group
  711. group = 0
  712. }
  713. }
  714. if day%granularity != 0 {
  715. status[len(status)-1] = group
  716. }
  717. peoples[key] = status
  718. }
  719. return global, locals, peoples
  720. }
  721. func (analyser *BurndownAnalysis) updateHistories(
  722. globalStatus []int64, file_statuses map[string][]int64, people_statuses [][]int64, delta int) {
  723. for i := 0; i < delta; i++ {
  724. analyser.globalHistory = append(analyser.globalHistory, globalStatus)
  725. }
  726. to_delete := make([]string, 0)
  727. for key, fh := range analyser.fileHistories {
  728. ls, exists := file_statuses[key]
  729. if !exists {
  730. to_delete = append(to_delete, key)
  731. } else {
  732. for i := 0; i < delta; i++ {
  733. fh = append(fh, ls)
  734. }
  735. analyser.fileHistories[key] = fh
  736. }
  737. }
  738. for _, key := range to_delete {
  739. delete(analyser.fileHistories, key)
  740. }
  741. for key, ls := range file_statuses {
  742. fh, exists := analyser.fileHistories[key]
  743. if exists {
  744. continue
  745. }
  746. for i := 0; i < delta; i++ {
  747. fh = append(fh, ls)
  748. }
  749. analyser.fileHistories[key] = fh
  750. }
  751. for key, ph := range analyser.peopleHistories {
  752. ls := people_statuses[key]
  753. for i := 0; i < delta; i++ {
  754. ph = append(ph, ls)
  755. }
  756. analyser.peopleHistories[key] = ph
  757. }
  758. }
  759. func init() {
  760. Registry.Register(&BurndownAnalysis{})
  761. }