imports_printer.go 6.4 KB

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