main.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2017 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // summarize is a tool for summarizing the results of gnostic_analyze runs.
  15. package main
  16. import (
  17. "encoding/json"
  18. "fmt"
  19. "io/ioutil"
  20. "os"
  21. "path"
  22. "path/filepath"
  23. "sort"
  24. "github.com/googleapis/gnostic/plugins/gnostic-analyze/statistics"
  25. )
  26. // Results are collected in this global slice.
  27. var stats []statistics.DocumentStatistics
  28. // walker is called for each summary file found.
  29. func walker(p string, info os.FileInfo, err error) error {
  30. basename := path.Base(p)
  31. if basename != "summary.json" {
  32. return nil
  33. }
  34. data, err := ioutil.ReadFile(p)
  35. if err != nil {
  36. return err
  37. }
  38. var s statistics.DocumentStatistics
  39. err = json.Unmarshal(data, &s)
  40. if err != nil {
  41. return err
  42. }
  43. stats = append(stats, s)
  44. return nil
  45. }
  46. func printFrequencies(m map[string]int) {
  47. for _, pair := range rankByCount(m) {
  48. fmt.Printf("%6d %s\n", pair.Value, pair.Key)
  49. }
  50. }
  51. func rankByCount(frequencies map[string]int) pairList {
  52. pl := make(pairList, len(frequencies))
  53. i := 0
  54. for k, v := range frequencies {
  55. pl[i] = pair{k, v}
  56. i++
  57. }
  58. sort.Sort(sort.Reverse(pl))
  59. return pl
  60. }
  61. type pair struct {
  62. Key string
  63. Value int
  64. }
  65. type pairList []pair
  66. func (p pairList) Len() int { return len(p) }
  67. func (p pairList) Less(i, j int) bool { return p[i].Value < p[j].Value }
  68. func (p pairList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  69. func main() {
  70. // Collect all statistics in the current directory and its subdirectories.
  71. stats = make([]statistics.DocumentStatistics, 0)
  72. filepath.Walk(".", walker)
  73. // Compute some interesting properties.
  74. apisWithAnonymousOperations := 0
  75. apisWithAnonymousObjects := 0
  76. apisWithAnonymousAnything := 0
  77. opFrequencies := make(map[string]int, 0)
  78. parameterTypeFrequencies := make(map[string]int, 0)
  79. resultTypeFrequencies := make(map[string]int, 0)
  80. definitionFieldTypeFrequencies := make(map[string]int, 0)
  81. definitionArrayTypeFrequencies := make(map[string]int, 0)
  82. definitionPrimitiveTypeFrequencies := make(map[string]int, 0)
  83. for _, api := range stats {
  84. if api.Operations["anonymous"] != 0 {
  85. apisWithAnonymousOperations++
  86. }
  87. if len(api.AnonymousObjects) > 0 {
  88. apisWithAnonymousObjects++
  89. }
  90. if len(api.AnonymousOperations) > 0 {
  91. apisWithAnonymousAnything++
  92. if len(api.AnonymousObjects) > 0 {
  93. fmt.Printf("%s has anonymous operations and objects\n", api.Name)
  94. } else {
  95. fmt.Printf("%s has anonymous operations\n", api.Name)
  96. }
  97. } else {
  98. if len(api.AnonymousObjects) > 0 {
  99. apisWithAnonymousAnything++
  100. fmt.Printf("%s has anonymous objects\n", api.Name)
  101. } else {
  102. fmt.Printf("%s has no anonymous operations or objects\n", api.Name)
  103. }
  104. }
  105. for k, v := range api.Operations {
  106. opFrequencies[k] += v
  107. }
  108. for k, v := range api.ParameterTypes {
  109. parameterTypeFrequencies[k] += v
  110. }
  111. for k, v := range api.ResultTypes {
  112. resultTypeFrequencies[k] += v
  113. }
  114. for k, v := range api.DefinitionFieldTypes {
  115. definitionFieldTypeFrequencies[k] += v
  116. }
  117. for k, v := range api.DefinitionArrayTypes {
  118. definitionArrayTypeFrequencies[k] += v
  119. }
  120. for k, v := range api.DefinitionPrimitiveTypes {
  121. definitionPrimitiveTypeFrequencies[k] += v
  122. }
  123. }
  124. // Report the results.
  125. fmt.Printf("\n")
  126. fmt.Printf("Collected information on %d APIs.\n\n", len(stats))
  127. fmt.Printf("APIs with anonymous operations: %d\n", apisWithAnonymousOperations)
  128. fmt.Printf("APIs with anonymous objects: %d\n", apisWithAnonymousObjects)
  129. fmt.Printf("APIs with anonymous anything: %d\n", apisWithAnonymousAnything)
  130. fmt.Printf("\nOperation frequencies:\n")
  131. printFrequencies(opFrequencies)
  132. fmt.Printf("\nParameter type frequencies:\n")
  133. printFrequencies(parameterTypeFrequencies)
  134. fmt.Printf("\nResult type frequencies:\n")
  135. printFrequencies(resultTypeFrequencies)
  136. fmt.Printf("\nDefinition object field type frequencies:\n")
  137. printFrequencies(definitionFieldTypeFrequencies)
  138. fmt.Printf("\nDefinition array type frequencies:\n")
  139. printFrequencies(definitionArrayTypeFrequencies)
  140. fmt.Printf("\nDefinition primitive type frequencies:\n")
  141. printFrequencies(definitionPrimitiveTypeFrequencies)
  142. }