This repository has been archived by the owner on Aug 19, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
export-aggregate.go
207 lines (168 loc) · 5.25 KB
/
export-aggregate.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"container/ring"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
"github.com/gocarina/gocsv"
)
var (
path = flag.String("path", "", "path to the export json files")
country = flag.String("country", "", "country to to analyze")
loc = time.UTC
)
const isoDateFormat = "2006-01-02"
// ExposureNotificationExport was auto-generated using https://mholt.github.io/json-to-go/
type ExposureNotificationExport struct {
StartTimestamp int `json:"start_timestamp"`
EndTimestamp int `json:"end_timestamp"`
Region string `json:"region"`
BatchNum int `json:"batch_num"`
BatchSize int `json:"batch_size"`
SignatureInfos []struct {
VerificationKeyVersion string `json:"verification_key_version"`
VerificationKeyID string `json:"verification_key_id"`
SignatureAlgorithm string `json:"signature_algorithm"`
} `json:"signature_infos"`
Keys []ExposureNotificationExportKey `json:"keys"`
}
type ExposureNotificationExportKey struct {
KeyData string `json:"key_data"`
TransmissionRiskLevel int `json:"transmission_risk_level"`
RollingStartIntervalNumber int `json:"rolling_start_interval_number"`
RollingPeriod int `json:"rolling_period"`
ReportType int `json:"report_type"`
DaysSinceOnsetOfSymptoms int `json:"days_since_onset_of_symptoms"`
}
type DailyKeyCount struct {
Date string `json:"date" csv:"date"`
NewKeysCount int `json:"new_key_count" csv:"new_key_count"`
KeysTotal int `json:"total_keys" csv:"total_keys"`
NewKeysInLast14Days int `json:"new_keys_in_last_14_days" csv:"new_keys_in_last_14_days"`
NonExpiredKeys int `json:"non_expired_keys" csv:"non_expired_keys"`
Keys []ExposureNotificationExportKey `json:"-" csv:"-"`
}
func getDailyNewKeyCount(date string) ([]ExposureNotificationExportKey, error) {
fileName := fmt.Sprintf("%s/%s.json", *path, date)
export, err := readExportJSON(fileName)
if err != nil {
return nil, err
}
return export.Keys, nil
}
func readExportJSON(fileName string) (*ExposureNotificationExport, error) {
blob, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, err
}
data := ExposureNotificationExport{}
err = json.Unmarshal([]byte(blob), &data)
if err != nil {
return nil, err
}
return &data, nil
}
func getStartDate() time.Time {
var startDate time.Time
files, err := ioutil.ReadDir(*path)
if err != nil {
panic(err)
}
for _, f := range files {
fn := f.Name()
if strings.HasSuffix(fn, ".json") {
dateName := strings.TrimSuffix(fn, ".json")
startDate, err = time.Parse(isoDateFormat, dateName)
if err == nil {
break
}
}
}
if startDate.IsZero() {
panic("Could not determine start date")
}
return startDate
}
func getDailyKeyCounts() []DailyKeyCount {
startDate := getStartDate()
dailyKeyCounts := make([]DailyKeyCount, 0)
newKeysInLast14days := ring.New(14)
for i := 0; i < newKeysInLast14days.Len(); i++ {
newKeysInLast14days.Value = make([]ExposureNotificationExportKey, 0)
newKeysInLast14days = newKeysInLast14days.Next()
}
date := startDate
runningTotal := 0
for {
dateIso := date.Format(isoDateFormat)
fmt.Println("Counting keys on:", dateIso)
dailyKeys, err := getDailyNewKeyCount(dateIso)
if err != nil {
}
n := len(dailyKeys)
runningTotal += n
newKeysInLast14days.Value = dailyKeys
sum := 0
nonExpiredKeys := 0
twoWeeksAgo := date.AddDate(0, 0, -14)
newKeysInLast14days.Do(func(p interface{}) {
keys := p.([]ExposureNotificationExportKey)
sum += len(keys)
for _, k := range keys {
if k.getStart().After(twoWeeksAgo) {
nonExpiredKeys++
// fmt.Println("active:", k.getIsoDate(), k.getStart(), k)
}
}
fmt.Println(dateIso, twoWeeksAgo, ":", nonExpiredKeys, "of", sum)
})
dailyKeyCounts = append(dailyKeyCounts, DailyKeyCount{
Date: dateIso,
NewKeysCount: n,
KeysTotal: runningTotal,
NewKeysInLast14Days: sum,
NonExpiredKeys: nonExpiredKeys,
})
date = date.AddDate(0, 0, 1)
newKeysInLast14days = newKeysInLast14days.Next()
if !date.Before(time.Now().In(loc).Truncate(24 * time.Hour)) {
break
}
}
return dailyKeyCounts
}
func writeJSON(data interface{}, fileName string) {
jsonBlob, err := json.MarshalIndent(data, "", " ")
if err != nil {
panic(err)
}
err = ioutil.WriteFile(fileName, jsonBlob, 0644)
}
func writeCSV(data interface{}, fileName string) {
csvFile, err := os.Create(fileName)
if err != nil {
panic(err)
}
defer csvFile.Close()
err = gocsv.MarshalFile(data, csvFile)
if err != nil {
panic(err)
}
}
func (k *ExposureNotificationExportKey) getStart() time.Time {
return time.Unix(int64(k.RollingStartIntervalNumber)*600, 0) // 10-minute slot since unix epoch
}
func (k *ExposureNotificationExportKey) getIsoDate() string {
return k.getStart().Format(isoDateFormat)
}
func main() {
flag.Parse()
dailyKeyCounts := getDailyKeyCounts()
writeJSON(dailyKeyCounts, *path+"/keycount.json")
writeCSV(dailyKeyCounts, *path+"/keycount.csv")
writeChart(dailyKeyCounts, *path+"/keycount.png", *country)
}