analytics.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. // Copyright (C) 2018 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package main
  7. import (
  8. "regexp"
  9. "sort"
  10. "strconv"
  11. "strings"
  12. )
  13. type analytic struct {
  14. Key string
  15. Count int
  16. Percentage float64
  17. Items []analytic `json:",omitempty"`
  18. }
  19. type analyticList []analytic
  20. func (l analyticList) Less(a, b int) bool {
  21. if l[a].Key == "Others" {
  22. return false
  23. }
  24. if l[b].Key == "Others" {
  25. return true
  26. }
  27. return l[b].Count < l[a].Count // inverse
  28. }
  29. func (l analyticList) Swap(a, b int) {
  30. l[a], l[b] = l[b], l[a]
  31. }
  32. func (l analyticList) Len() int {
  33. return len(l)
  34. }
  35. // Returns a list of frequency analytics for a given list of strings.
  36. func analyticsFor(ss []string, cutoff int) []analytic {
  37. m := make(map[string]int)
  38. t := 0
  39. for _, s := range ss {
  40. m[s]++
  41. t++
  42. }
  43. l := make([]analytic, 0, len(m))
  44. for k, c := range m {
  45. l = append(l, analytic{
  46. Key: k,
  47. Count: c,
  48. Percentage: 100 * float64(c) / float64(t),
  49. })
  50. }
  51. sort.Sort(analyticList(l))
  52. if cutoff > 0 && len(l) > cutoff {
  53. c := 0
  54. for _, i := range l[cutoff:] {
  55. c += i.Count
  56. }
  57. l = append(l[:cutoff], analytic{
  58. Key: "Others",
  59. Count: c,
  60. Percentage: 100 * float64(c) / float64(t),
  61. })
  62. }
  63. return l
  64. }
  65. // Find the points at which certain penetration levels are met
  66. func penetrationLevels(as []analytic, points []float64) []analytic {
  67. sort.Slice(as, func(a, b int) bool {
  68. return versionLess(as[b].Key, as[a].Key)
  69. })
  70. var res []analytic
  71. idx := 0
  72. sum := 0.0
  73. for _, a := range as {
  74. sum += a.Percentage
  75. if sum >= points[idx] {
  76. a.Count = int(points[idx])
  77. a.Percentage = sum
  78. res = append(res, a)
  79. idx++
  80. if idx == len(points) {
  81. break
  82. }
  83. }
  84. }
  85. return res
  86. }
  87. func statsForInts(data []int) [4]float64 {
  88. var res [4]float64
  89. if len(data) == 0 {
  90. return res
  91. }
  92. sort.Ints(data)
  93. res[0] = float64(data[int(float64(len(data))*0.05)])
  94. res[1] = float64(data[len(data)/2])
  95. res[2] = float64(data[int(float64(len(data))*0.95)])
  96. res[3] = float64(data[len(data)-1])
  97. return res
  98. }
  99. func statsForFloats(data []float64) [4]float64 {
  100. var res [4]float64
  101. if len(data) == 0 {
  102. return res
  103. }
  104. sort.Float64s(data)
  105. res[0] = data[int(float64(len(data))*0.05)]
  106. res[1] = data[len(data)/2]
  107. res[2] = data[int(float64(len(data))*0.95)]
  108. res[3] = data[len(data)-1]
  109. return res
  110. }
  111. func group(by func(string) string, as []analytic, perGroup int) []analytic {
  112. var res []analytic
  113. next:
  114. for _, a := range as {
  115. group := by(a.Key)
  116. for i := range res {
  117. if res[i].Key == group {
  118. res[i].Count += a.Count
  119. res[i].Percentage += a.Percentage
  120. if len(res[i].Items) < perGroup {
  121. res[i].Items = append(res[i].Items, a)
  122. }
  123. continue next
  124. }
  125. }
  126. res = append(res, analytic{
  127. Key: group,
  128. Count: a.Count,
  129. Percentage: a.Percentage,
  130. Items: []analytic{a},
  131. })
  132. }
  133. sort.Sort(analyticList(res))
  134. return res
  135. }
  136. func byVersion(s string) string {
  137. parts := strings.Split(s, ".")
  138. if len(parts) >= 2 {
  139. return strings.Join(parts[:2], ".")
  140. }
  141. return s
  142. }
  143. func byPlatform(s string) string {
  144. parts := strings.Split(s, "-")
  145. if len(parts) >= 2 {
  146. return parts[0]
  147. }
  148. return s
  149. }
  150. var numericGoVersion = regexp.MustCompile(`^go[0-9]\.[0-9]+`)
  151. func byCompiler(s string) string {
  152. if m := numericGoVersion.FindString(s); m != "" {
  153. return m
  154. }
  155. return "Other"
  156. }
  157. func versionLess(a, b string) bool {
  158. arel, apre := versionParts(a)
  159. brel, bpre := versionParts(b)
  160. minlen := len(arel)
  161. if l := len(brel); l < minlen {
  162. minlen = l
  163. }
  164. for i := 0; i < minlen; i++ {
  165. if arel[i] != brel[i] {
  166. return arel[i] < brel[i]
  167. }
  168. }
  169. // Longer version is newer, when the preceding parts are equal
  170. if len(arel) != len(brel) {
  171. return len(arel) < len(brel)
  172. }
  173. if apre != bpre {
  174. // "(+dev)" versions are ahead
  175. if apre == plusStr {
  176. return false
  177. }
  178. if bpre == plusStr {
  179. return true
  180. }
  181. return apre < bpre
  182. }
  183. // don't actually care how the prerelease stuff compares for our purposes
  184. return false
  185. }
  186. // Split a version as returned from transformVersion into parts.
  187. // "1.2.3-beta.2" -> []int{1, 2, 3}, "beta.2"}
  188. func versionParts(v string) ([]int, string) {
  189. parts := strings.SplitN(v[1:], " ", 2) // " (+dev)" versions
  190. if len(parts) == 1 {
  191. parts = strings.SplitN(parts[0], "-", 2) // "-rc.1" type versions
  192. }
  193. fields := strings.Split(parts[0], ".")
  194. release := make([]int, len(fields))
  195. for i, s := range fields {
  196. v, _ := strconv.Atoi(s)
  197. release[i] = v
  198. }
  199. var prerelease string
  200. if len(parts) > 1 {
  201. prerelease = parts[1]
  202. }
  203. return release, prerelease
  204. }