More details version/platform stats

This commit is contained in:
Jakob Borg
2016-05-30 09:52:38 +02:00
parent ed45c43033
commit 82621f250c
3 changed files with 107 additions and 54 deletions

View File

@@ -1,16 +1,26 @@
package main
import "sort"
import (
"sort"
"strings"
)
type analytic struct {
Key string
Count int
Percentage float64
Items []analytic `json:",omitempty"`
}
type analyticList []analytic
func (l analyticList) Less(a, b int) bool {
if l[a].Key == "Others" {
return true
}
if l[b].Key == "Others" {
return false
}
return l[b].Count < l[a].Count // inverse
}
@@ -33,7 +43,11 @@ func analyticsFor(ss []string, cutoff int) []analytic {
l := make([]analytic, 0, len(m))
for k, c := range m {
l = append(l, analytic{k, c, 100 * float64(c) / float64(t)})
l = append(l, analytic{
Key: k,
Count: c,
Percentage: 100 * float64(c) / float64(t),
})
}
sort.Sort(analyticList(l))
@@ -43,7 +57,11 @@ func analyticsFor(ss []string, cutoff int) []analytic {
for _, i := range l[cutoff:] {
c += i.Count
}
l = append(l[:cutoff], analytic{"Others", c, 100 * float64(c) / float64(t)})
l = append(l[:cutoff], analytic{
Key: "Others",
Count: c,
Percentage: 100 * float64(c) / float64(t),
})
}
return l
@@ -76,3 +94,47 @@ func statsForFloats(data []float64) [4]float64 {
res[3] = data[len(data)-1]
return res
}
func group(by func(string) string, as []analytic, perGroup int) []analytic {
var res []analytic
next:
for _, a := range as {
group := by(a.Key)
for i := range res {
if res[i].Key == group {
res[i].Count += a.Count
res[i].Percentage += a.Percentage
if len(res[i].Items) < perGroup {
res[i].Items = append(res[i].Items, a)
}
continue next
}
}
res = append(res, analytic{
Key: group,
Count: a.Count,
Percentage: a.Percentage,
Items: []analytic{a},
})
}
sort.Sort(analyticList(res))
return res
}
func byVersion(s string) string {
parts := strings.Split(s, ".")
if len(parts) >= 2 {
return strings.Join(parts[:2], ".")
}
return s
}
func byPlatform(s string) string {
parts := strings.Split(s, "-")
if len(parts) >= 2 {
return parts[0]
}
return s
}