-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute_all_stats.js
64 lines (60 loc) · 1.95 KB
/
compute_all_stats.js
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
const admin = require("firebase-admin");
admin.initializeApp({
credential: admin.credential.cert("../others/serviceAccountKey.json"),
databaseURL: "https://project-coswara.firebaseio.com"
})
const db = admin.firestore();
const getDateStringWithOffset = function (dateString, offset) {
const dateStringISO = `${dateString}T00:00:00.000Z`;
const dataObject = new Date(dateStringISO);
dataObject.setDate(dataObject.getDate() + offset);
return dataObject.toISOString().substring(0, 10);
};
const startDateString = '2020-04-10'
for(const i of Array(28).keys()) {
const dateString = getDateStringWithOffset(startDateString, i)
const dateSummary = {
'visited': 0,
'completed': 0,
'positive_mild': 0,
'positive_moderate': 0,
'recovered_full': 0,
'resp_illness_not_identified': 0,
'no_resp_illness_exposed': 0,
'healthy': 0,
'web_users': 0,
'android_users': 0,
'ios_users': 0,
'gender_male': 0,
'gender_female': 0,
'gender_other': 0
}
const locationSummary = {}
console.log('Processing date:', dateString)
db.collection('USER_METADATA').doc(dateString).collection('DATA').get().then((snapshots) => {
snapshots.forEach((doc) => {
const userData = doc.data();
const deviceType = userData['dT'] || 'web'
dateSummary.visited += 1
if (userData['iF']) {
dateSummary.completed += 1
dateSummary[userData['covid_status']] += 1
dateSummary[`${deviceType}_users`] += 1
dateSummary[`gender_${userData['g']}`] += 1
locationSummary[userData['l_c']] = (locationSummary['l_c'] ? locationSummary['l_c'] : 0) + 1
}
})
const batch = db.batch();
batch.set(
db.collection('USER_METADATA').doc(dateString),
dateSummary
);
batch.set(
db.collection('USER_METADATA').doc(dateString).collection('STATS').doc('l_c'),
locationSummary
);
batch.commit().then(() => {
console.log('Updated stats:', dateString)
})
})
}