/
stats.go
89 lines (79 loc) · 2.32 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package discern
import (
"github.com/grd/statistics"
"github.com/hahnicity/go-discern/config"
"sort"
"time"
)
const FIVE_DAYS_AGO int64 = 60 * 60 * 24 * 5
// Find all companies with abnormally high activity within the last
// 5 days.
func FindRecentDates(wr *WikiResponse, f float64) (dates map[string]int) {
dates = FindDates(wr, f)
now := time.Now().Unix()
for date, _ := range dates {
parsed, err := time.Parse(config.TimeConf, date)
if err != nil {
panic(err)
}
ut := parsed.Unix()
if now - ut > FIVE_DAYS_AGO {
delete(dates, date)
}
}
return
}
// Find the dates where a given wiki page experienced abnormally high
// activity
func FindDates(wr *WikiResponse, viewPercentile float64) (dates map[string]int ) {
vals := GetValuesFromMap(wr.Yearly)
sort.Sort(vals)
result := statistics.QuantileFromSortedData(vals, viewPercentile)
dates = GetKeysGE(wr.Yearly, result)
return
}
// Find the mean number of views for a wikipedia page
func FindMeanViews(wr *WikiResponse) (mean int) {
vals := GetValuesFromMap(wr.Yearly)
mean = int(statistics.Mean(vals))
return
}
// Find the companies with the highest number of mean views according to a
// mean view percentile
func FindHighestMeans(means map[string]int, meanPercentile float64) (stats map[string]int) {
vals := GetValuesFromMap(means)
result := statistics.QuantileFromSortedData(vals, meanPercentile)
stats = GetKeysGE(means, result)
return
}
// Convert a slice of floats to a Values struct
func FloatsToValues(floats []float64) (values *Values) {
values = new(Values)
for _, f := range floats {
*values = append(*values, int(f))
}
return
}
// Get all keys mapped to their values in a map whose values are greater than or equal
// to some float
func GetKeysGE(Map map[string]int, val float64) map[string]int {
for k, v := range Map {
if v < int(val) {
delete(Map, k)
}
}
return Map
}
func GetValuesFromMap(Map map[string]int) *Values {
v := &Values{}
for _, j := range Map {
*v = append(*v, j)
}
return v
}
func JoinViewsMaps(map1 map[string]int, map2 map[string]int) map[string]int {
for k, v := range map2 {
map1[k] = v
}
return map1
}