imports_printer.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package leaves
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "log"
  7. "sort"
  8. "github.com/gogo/protobuf/proto"
  9. imports2 "github.com/src-d/imports"
  10. "gopkg.in/src-d/go-git.v4"
  11. "gopkg.in/src-d/go-git.v4/plumbing"
  12. "gopkg.in/src-d/hercules.v10/internal/core"
  13. "gopkg.in/src-d/hercules.v10/internal/pb"
  14. "gopkg.in/src-d/hercules.v10/internal/plumbing/identity"
  15. "gopkg.in/src-d/hercules.v10/internal/plumbing/imports"
  16. "gopkg.in/src-d/hercules.v10/internal/yaml"
  17. )
  18. // ImportsPerDeveloper collects imports per developer.
  19. type ImportsPerDeveloper struct {
  20. core.NoopMerger
  21. core.OneShotMergeProcessor
  22. // imports is the mapping from dev indexes to languages to import names to usage counts.
  23. imports map[int]map[string]map[string]int
  24. // reversedPeopleDict references IdentityDetector.ReversedPeopleDict
  25. reversedPeopleDict []string
  26. l core.Logger
  27. }
  28. // ImportsPerDeveloperResult is returned by Finalize() and represents the analysis result.
  29. type ImportsPerDeveloperResult struct {
  30. // Imports is the mapping from dev indexes to languages to import names to usage counts.
  31. Imports map[int]map[string]map[string]int
  32. // reversedPeopleDict references IdentityDetector.ReversedPeopleDict
  33. reversedPeopleDict []string
  34. }
  35. // Name of this PipelineItem. Uniquely identifies the type, used for mapping keys, etc.
  36. func (ipd *ImportsPerDeveloper) Name() string {
  37. return "ImportsPerDeveloper"
  38. }
  39. // Provides returns the list of names of entities which are produced by this PipelineItem.
  40. // Each produced entity will be inserted into `deps` of dependent Consume()-s according
  41. // to this list. Also used by core.Registry to build the global map of providers.
  42. func (ipd *ImportsPerDeveloper) Provides() []string {
  43. return []string{}
  44. }
  45. // Requires returns the list of names of entities which are needed by this PipelineItem.
  46. // Each requested entity will be inserted into `deps` of Consume(). In turn, those
  47. // entities are Provides() upstream.
  48. func (ipd *ImportsPerDeveloper) Requires() []string {
  49. return []string{imports.DependencyImports, identity.DependencyAuthor}
  50. }
  51. // ListConfigurationOptions returns the list of changeable public properties of this PipelineItem.
  52. func (ipd *ImportsPerDeveloper) ListConfigurationOptions() []core.ConfigurationOption {
  53. return []core.ConfigurationOption{}
  54. }
  55. // Flag for the command line switch which enables this analysis.
  56. func (ipd *ImportsPerDeveloper) Flag() string {
  57. return "imports-per-dev"
  58. }
  59. // Description returns the text which explains what the analysis is doing.
  60. func (ipd *ImportsPerDeveloper) Description() string {
  61. return "Whenever a file is changed or added, we extract the imports from it and increment " +
  62. "their usage for the commit author."
  63. }
  64. // Configure sets the properties previously published by ListConfigurationOptions().
  65. func (ipd *ImportsPerDeveloper) Configure(facts map[string]interface{}) error {
  66. if l, exists := facts[core.ConfigLogger].(core.Logger); exists {
  67. ipd.l = l
  68. }
  69. ipd.reversedPeopleDict = facts[identity.FactIdentityDetectorReversedPeopleDict].([]string)
  70. return nil
  71. }
  72. // Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
  73. // calls. The repository which is going to be analysed is supplied as an argument.
  74. func (ipd *ImportsPerDeveloper) Initialize(repository *git.Repository) error {
  75. ipd.l = core.NewLogger()
  76. ipd.imports = map[int]map[string]map[string]int{}
  77. ipd.OneShotMergeProcessor.Initialize()
  78. return nil
  79. }
  80. // Consume runs this PipelineItem on the next commit data.
  81. // `deps` contain all the results from upstream PipelineItem-s as requested by Requires().
  82. // Additionally, DependencyCommit is always present there and represents the analysed *object.Commit.
  83. // This function returns the mapping with analysis results. The keys must be the same as
  84. // in Provides(). If there was an error, nil is returned.
  85. func (ipd *ImportsPerDeveloper) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
  86. if deps[core.DependencyIsMerge].(bool) {
  87. // we ignore merge commits
  88. // TODO(vmarkovtsev): handle them better
  89. return nil, nil
  90. }
  91. author := deps[identity.DependencyAuthor].(int)
  92. imps := deps[imports.DependencyImports].(map[plumbing.Hash]imports2.File)
  93. aimps := ipd.imports[author]
  94. if aimps == nil {
  95. aimps = map[string]map[string]int{}
  96. ipd.imports[author] = aimps
  97. }
  98. for _, file := range imps {
  99. limps := aimps[file.Lang]
  100. if limps == nil {
  101. limps = map[string]int{}
  102. aimps[file.Lang] = limps
  103. }
  104. for _, imp := range file.Imports {
  105. limps[imp]++
  106. }
  107. }
  108. return nil, nil
  109. }
  110. // Finalize returns the result of the analysis. Further Consume() calls are not expected.
  111. func (ipd *ImportsPerDeveloper) Finalize() interface{} {
  112. return ImportsPerDeveloperResult{Imports: ipd.imports, reversedPeopleDict: ipd.reversedPeopleDict}
  113. }
  114. // Fork clones this PipelineItem.
  115. func (ipd *ImportsPerDeveloper) Fork(n int) []core.PipelineItem {
  116. return core.ForkSamePipelineItem(ipd, n)
  117. }
  118. // Serialize converts the analysis result as returned by Finalize() to text or bytes.
  119. // The text format is YAML and the bytes format is Protocol Buffers.
  120. func (ipd *ImportsPerDeveloper) Serialize(result interface{}, binary bool, writer io.Writer) error {
  121. importsResult := result.(ImportsPerDeveloperResult)
  122. if binary {
  123. return ipd.serializeBinary(&importsResult, writer)
  124. }
  125. ipd.serializeText(&importsResult, writer)
  126. return nil
  127. }
  128. func (ipd *ImportsPerDeveloper) serializeText(result *ImportsPerDeveloperResult, writer io.Writer) {
  129. devs := make([]int, 0, len(result.Imports))
  130. for dev := range result.Imports {
  131. devs = append(devs, dev)
  132. }
  133. sort.Ints(devs)
  134. for _, dev := range devs {
  135. imps := result.Imports[dev]
  136. obj, err := json.Marshal(imps)
  137. if err != nil {
  138. log.Panicf("Could not serialize %v: %v", imps, err)
  139. }
  140. fmt.Fprintf(writer, " - %s: %s\n", yaml.SafeString(result.reversedPeopleDict[dev]), string(obj))
  141. }
  142. }
  143. func (ipd *ImportsPerDeveloper) serializeBinary(result *ImportsPerDeveloperResult, writer io.Writer) error {
  144. message := pb.ImportsPerDeveloperResults{
  145. Imports: make([]*pb.ImportsPerDeveloper, len(result.Imports)),
  146. AuthorIndex: result.reversedPeopleDict,
  147. }
  148. for key, vals := range result.Imports {
  149. dev := &pb.ImportsPerDeveloper{Languages: map[string]*pb.ImportsPerLanguage{}}
  150. message.Imports[key] = dev
  151. for lang, counts := range vals {
  152. counts64 := map[string]int64{}
  153. dev.Languages[lang] = &pb.ImportsPerLanguage{Counts: counts64}
  154. for imp, n := range counts {
  155. counts64[imp] = int64(n)
  156. }
  157. }
  158. }
  159. serialized, err := proto.Marshal(&message)
  160. if err != nil {
  161. return err
  162. }
  163. _, err = writer.Write(serialized)
  164. return err
  165. }
  166. func init() {
  167. core.Registry.Register(&ImportsPerDeveloper{})
  168. }