ticks.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package plumbing
  2. import (
  3. "time"
  4. "gopkg.in/src-d/go-git.v4"
  5. "gopkg.in/src-d/go-git.v4/plumbing"
  6. "gopkg.in/src-d/go-git.v4/plumbing/object"
  7. "gopkg.in/src-d/hercules.v10/internal/core"
  8. )
  9. // TicksSinceStart provides relative tick information for every commit.
  10. // It is a PipelineItem.
  11. type TicksSinceStart struct {
  12. core.NoopMerger
  13. TickSize time.Duration
  14. remote string
  15. tick0 *time.Time
  16. previousTick int
  17. commits map[int][]plumbing.Hash
  18. l core.Logger
  19. }
  20. const (
  21. // DependencyTick is the name of the dependency which DaysSinceStart provides - the number
  22. // of ticks since the first commit in the analysed sequence.
  23. DependencyTick = "tick"
  24. // FactCommitsByTick contains the mapping between day indices and the corresponding commits.
  25. FactCommitsByTick = "TicksSinceStart.Commits"
  26. // FactTickSize contains the time.Duration of each tick.
  27. FactTickSize = "TicksSinceStart.TickSize"
  28. // ConfigTicksSinceStartTickSize sets the size of each 'tick' in hours.
  29. ConfigTicksSinceStartTickSize = "TicksSinceStart.TickSize"
  30. // DefaultTicksSinceStartTickSize is the default number of hours in each 'tick' (24*hour = 1day).
  31. DefaultTicksSinceStartTickSize = 24
  32. )
  33. // Name of this PipelineItem. Uniquely identifies the type, used for mapping keys, etc.
  34. func (ticks *TicksSinceStart) Name() string {
  35. return "TicksSinceStart"
  36. }
  37. // Provides returns the list of names of entities which are produced by this PipelineItem.
  38. // Each produced entity will be inserted into `deps` of dependent Consume()-s according
  39. // to this list. Also used by core.Registry to build the global map of providers.
  40. func (ticks *TicksSinceStart) Provides() []string {
  41. return []string{DependencyTick}
  42. }
  43. // Requires returns the list of names of entities which are needed by this PipelineItem.
  44. // Each requested entity will be inserted into `deps` of Consume(). In turn, those
  45. // entities are Provides() upstream.
  46. func (ticks *TicksSinceStart) Requires() []string {
  47. return []string{}
  48. }
  49. // ListConfigurationOptions returns the list of changeable public properties of this PipelineItem.
  50. func (ticks *TicksSinceStart) ListConfigurationOptions() []core.ConfigurationOption {
  51. return []core.ConfigurationOption{{
  52. Name: ConfigTicksSinceStartTickSize,
  53. Description: "How long each 'tick' represents in hours.",
  54. Flag: "tick-size",
  55. Type: core.IntConfigurationOption,
  56. Default: DefaultTicksSinceStartTickSize},
  57. }
  58. }
  59. // Configure sets the properties previously published by ListConfigurationOptions().
  60. func (ticks *TicksSinceStart) Configure(facts map[string]interface{}) error {
  61. if l, exists := facts[core.ConfigLogger].(core.Logger); exists {
  62. ticks.l = l
  63. }
  64. if val, exists := facts[ConfigTicksSinceStartTickSize].(int); exists {
  65. ticks.TickSize = time.Duration(val) * time.Hour
  66. } else {
  67. ticks.TickSize = DefaultTicksSinceStartTickSize * time.Hour
  68. }
  69. if ticks.commits == nil {
  70. ticks.commits = map[int][]plumbing.Hash{}
  71. }
  72. facts[FactCommitsByTick] = ticks.commits
  73. facts[FactTickSize] = ticks.TickSize
  74. return nil
  75. }
  76. // Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
  77. // calls. The repository which is going to be analysed is supplied as an argument.
  78. func (ticks *TicksSinceStart) Initialize(repository *git.Repository) error {
  79. ticks.l = core.NewLogger()
  80. if ticks.TickSize == 0 {
  81. ticks.TickSize = DefaultTicksSinceStartTickSize * time.Hour
  82. }
  83. ticks.tick0 = &time.Time{}
  84. ticks.previousTick = 0
  85. if len(ticks.commits) > 0 {
  86. keys := make([]int, len(ticks.commits))
  87. for key := range ticks.commits {
  88. keys = append(keys, key)
  89. }
  90. for _, key := range keys {
  91. delete(ticks.commits, key)
  92. }
  93. }
  94. ticks.remote = core.GetSensibleRemote(repository)
  95. return nil
  96. }
  97. // Consume runs this PipelineItem on the next commit data.
  98. // `deps` contain all the results from upstream PipelineItem-s as requested by Requires().
  99. // Additionally, DependencyCommit is always present there and represents the analysed *object.Commit.
  100. // This function returns the mapping with analysis results. The keys must be the same as
  101. // in Provides(). If there was an error, nil is returned.
  102. func (ticks *TicksSinceStart) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  103. commit := deps[core.DependencyCommit].(*object.Commit)
  104. index := deps[core.DependencyIndex].(int)
  105. if index == 0 {
  106. // first iteration - initialize the file objects from the tree
  107. // our precision is 1 day
  108. tick0 := commit.Committer.When
  109. if tick0.Unix() < 631152000 { // 01.01.1990, that was 30 years ago
  110. ticks.l.Warnf("suspicious committer timestamp in %s > %s: %d",
  111. ticks.remote, commit.Hash.String(), tick0.Unix())
  112. }
  113. *ticks.tick0 = FloorTime(tick0, ticks.TickSize)
  114. }
  115. tick := int(commit.Committer.When.Sub(*ticks.tick0) / ticks.TickSize)
  116. if tick < ticks.previousTick {
  117. // rebase works miracles, but we need the monotonous time
  118. tick = ticks.previousTick
  119. }
  120. ticks.previousTick = tick
  121. tickCommits := ticks.commits[tick]
  122. if tickCommits == nil {
  123. tickCommits = []plumbing.Hash{}
  124. }
  125. exists := false
  126. if commit.NumParents() > 0 {
  127. for i := range tickCommits {
  128. if tickCommits[len(tickCommits)-i-1] == commit.Hash {
  129. exists = true
  130. break
  131. }
  132. }
  133. }
  134. if !exists {
  135. ticks.commits[tick] = append(tickCommits, commit.Hash)
  136. }
  137. return map[string]interface{}{DependencyTick: tick}, nil
  138. }
  139. // Fork clones this PipelineItem.
  140. func (ticks *TicksSinceStart) Fork(n int) []core.PipelineItem {
  141. return core.ForkCopyPipelineItem(ticks, n)
  142. }
  143. // FloorTime is the missing implementation of time.Time.Floor() - round to the nearest less than or equal.
  144. func FloorTime(t time.Time, d time.Duration) time.Time {
  145. // We have check if the regular rounding resulted in Floor() + d.
  146. result := t.Round(d)
  147. if result.After(t) {
  148. result = result.Add(-d)
  149. }
  150. return result
  151. }
  152. func init() {
  153. core.Registry.Register(&TicksSinceStart{})
  154. }