analytics.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 serve
  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 statsForInt64s(data []int64) [4]float64 {
  100. var res [4]float64
  101. if len(data) == 0 {
  102. return res
  103. }
  104. sort.Slice(data, func(a, b int) bool {
  105. return data[a] < data[b]
  106. })
  107. res[0] = float64(data[int(float64(len(data))*0.05)])
  108. res[1] = float64(data[len(data)/2])
  109. res[2] = float64(data[int(float64(len(data))*0.95)])
  110. res[3] = float64(data[len(data)-1])
  111. return res
  112. }
  113. func statsForFloats(data []float64) [4]float64 {
  114. var res [4]float64
  115. if len(data) == 0 {
  116. return res
  117. }
  118. sort.Float64s(data)
  119. res[0] = data[int(float64(len(data))*0.05)]
  120. res[1] = data[len(data)/2]
  121. res[2] = data[int(float64(len(data))*0.95)]
  122. res[3] = data[len(data)-1]
  123. return res
  124. }
  125. func group(by func(string) string, as []analytic, perGroup int, otherPct float64) []analytic {
  126. var res []analytic
  127. next:
  128. for _, a := range as {
  129. group := by(a.Key)
  130. for i := range res {
  131. if res[i].Key == group {
  132. res[i].Count += a.Count
  133. res[i].Percentage += a.Percentage
  134. if len(res[i].Items) < perGroup {
  135. res[i].Items = append(res[i].Items, a)
  136. }
  137. continue next
  138. }
  139. }
  140. res = append(res, analytic{
  141. Key: group,
  142. Count: a.Count,
  143. Percentage: a.Percentage,
  144. Items: []analytic{a},
  145. })
  146. }
  147. sort.Sort(analyticList(res))
  148. if otherPct > 0 {
  149. // Groups with less than otherPCt go into "Other"
  150. other := analytic{
  151. Key: "Other",
  152. }
  153. for i := 0; i < len(res); i++ {
  154. if res[i].Percentage < otherPct || res[i].Key == "Other" {
  155. other.Count += res[i].Count
  156. other.Percentage += res[i].Percentage
  157. res = append(res[:i], res[i+1:]...)
  158. i--
  159. }
  160. }
  161. if other.Count > 0 {
  162. res = append(res, other)
  163. }
  164. }
  165. return res
  166. }
  167. func byVersion(s string) string {
  168. parts := strings.Split(s, ".")
  169. if len(parts) >= 2 {
  170. return strings.Join(parts[:2], ".")
  171. }
  172. return s
  173. }
  174. func byPlatform(s string) string {
  175. parts := strings.Split(s, "-")
  176. if len(parts) >= 2 {
  177. return parts[0]
  178. }
  179. return s
  180. }
  181. var numericGoVersion = regexp.MustCompile(`^go[0-9]\.[0-9]+`)
  182. func byCompiler(s string) string {
  183. if m := numericGoVersion.FindString(s); m != "" {
  184. return m
  185. }
  186. return "Other"
  187. }
  188. func versionLess(a, b string) bool {
  189. arel, apre := versionParts(a)
  190. brel, bpre := versionParts(b)
  191. minlen := len(arel)
  192. if l := len(brel); l < minlen {
  193. minlen = l
  194. }
  195. for i := 0; i < minlen; i++ {
  196. if arel[i] != brel[i] {
  197. return arel[i] < brel[i]
  198. }
  199. }
  200. // Longer version is newer, when the preceding parts are equal
  201. if len(arel) != len(brel) {
  202. return len(arel) < len(brel)
  203. }
  204. if apre != bpre {
  205. // "(+dev)" versions are ahead
  206. if apre == plusStr {
  207. return false
  208. }
  209. if bpre == plusStr {
  210. return true
  211. }
  212. return apre < bpre
  213. }
  214. // don't actually care how the prerelease stuff compares for our purposes
  215. return false
  216. }
  217. // Split a version as returned from transformVersion into parts.
  218. // "1.2.3-beta.2" -> []int{1, 2, 3}, "beta.2"}
  219. func versionParts(v string) ([]int, string) {
  220. parts := strings.SplitN(v[1:], " ", 2) // " (+dev)" versions
  221. if len(parts) == 1 {
  222. parts = strings.SplitN(parts[0], "-", 2) // "-rc.1" type versions
  223. }
  224. fields := strings.Split(parts[0], ".")
  225. release := make([]int, len(fields))
  226. for i, s := range fields {
  227. v, _ := strconv.Atoi(s)
  228. release[i] = v
  229. }
  230. var prerelease string
  231. if len(parts) > 1 {
  232. prerelease = parts[1]
  233. }
  234. return release, prerelease
  235. }