-
Notifications
You must be signed in to change notification settings - Fork 0
/
monthly.js
174 lines (155 loc) · 4.34 KB
/
monthly.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
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
'use strict';
const { NO_TAG } = require('./tag-special');
const {
getMonthlyRecords
} = require ('./open-baltimore');
const { monthlySummaryTweet, monthlyByViolationsTweets, worstDriverTweets } = require('./text-parsing');
const { publishTweet } = require('./twitter');
module.exports = {
getPrevMonthStats,
publishStats
};
async function publishStats() {
const summaryData = await getPrevMonthStats();
const summaryTweets = [
monthlySummaryTweet(summaryData),
...monthlyByViolationsTweets(summaryData)
];
const worstTweets = [
...worstDriverTweets(summaryData)
];
let replyToId = undefined;
const summaryTweetsAsyncIterable = {
[Symbol.asyncIterator]() {
return {
next() {
if (summaryTweets.length) {
return publishTweet(summaryTweets.shift(), replyToId)
.then(id_str => {
return id_str;
});
} else {
return Promise.resolve({
done: true
});
}
}
};
}
};
for await (let id_str of summaryTweetsAsyncIterable) {
replyToId = id_str;
}
// reset replyToId
replyToId = undefined;
const worstTweetsAsyncIterable = {
[Symbol.asyncIterator]() {
return {
next() {
if (worstTweets.length) {
const worstTweet = worstTweets.shift();
console.log(worstTweet);
return publishTweet(worstTweet, replyToId)
.then(id_str => {
return id_str;
});
} else {
return Promise.resolve({
done: true
});
}
}
};
}
};
for await (let id_str of worstTweetsAsyncIterable) {
replyToId = id_str;
}
return { statusCode: 200, body: '' };
}
async function getPrevMonthStats() {
const now = new Date();
const month = now.getMonth();
// JS months start with 0, and ESRI API months start with 1.
// JS month will be equivalent of previous month.
// If 0, set to the previous December.
const monthToSearch = month > 0 ? month : 12;
const year = now.getFullYear();
const yearToSearch = monthToSearch > month ? year - 1 : year;
const rawData = await getMonthlyRecords(yearToSearch, monthToSearch);
const totalFines = rawData.reduce((acc, val) => acc += +val.violFine, 0);
const worst = worstTags(countByTag(rawData.filter(d => !NO_TAG.includes(d.tag))));
return Promise.resolve({
month: monthToSearch,
year: yearToSearch,
numViolations: rawData.length,
totalFines,
worst,
violationTotals: totalsByViolation(rawData)
});
}
function totalsByViolation(data) {
const groupObj = data.reduce((acc, val) => {
const violCode = val.violCode;
if (Object.prototype.hasOwnProperty.call(acc, violCode)) {
acc[violCode].count += 1;
} else {
acc[violCode] = {
violCode,
count: 1
};
}
return acc;
}, {});
return Object.values(groupObj)
.sort((a, b) => {
if(a.violCode > b.violCode) return 1;
else if (a.violCode === b.violCode) return 0;
else return -1;
});
}
function countByTag(data) {
const groupObj = data.reduce((acc, val) => {
const plate = `${val.state} ${val.tag}`.toUpperCase();
if (Object.prototype.hasOwnProperty.call(acc, plate)) {
acc[plate].count += 1;
acc[plate].totalFines += +val.violFine;
} else {
acc[plate] = {
plate: plate,
count: 1,
totalFines: +val.violFine,
violationTotals: {}
};
}
if (Object.prototype.hasOwnProperty.call(acc[plate].violationTotals, val.violCode)) {
acc[plate].violationTotals[val.violCode].count += 1;
} else {
acc[plate].violationTotals[val.violCode] = {
violCode: val.violCode,
count: 1
};
}
return acc;
}, {});
return Object.values(groupObj).map(go => {
go.violationTotals = Object.values(go.violationTotals);
return go;
});
}
function worstTags(data) {
data.sort((a, b) => {
if (a.count > b.count) return -1;
else if (a.count === b.count) return 0;
else return 1;
});
const worstTags = [ data[0] ];
for (let i = 1; i < data.length; ++i) {
if (data[i].count === worstTags[worstTags.length - 1]) {
worstTags.push(data[i]);
} else {
break; // We've broken the tie, stop looping
}
}
return worstTags;
}