-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
430 lines (392 loc) · 15.6 KB
/
script.js
File metadata and controls
430 lines (392 loc) · 15.6 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/**
* Calculates statistics about ORCA card usage from a CSV file and displays the
* results on a webpage.
*/
function parseCSV(csvText) {
// Handle CSV with multi-line quoted fields
const rows = [];
let currentRow = [];
let currentField = '';
let insideQuotes = false;
for (let i = 0; i < csvText.length; i++) {
const char = csvText[i];
const nextChar = csvText[i + 1];
if (char === '"') {
if (insideQuotes && nextChar === '"') {
// Escaped quote
currentField += '"';
i++; // Skip next quote
} else {
// Toggle quote state
insideQuotes = !insideQuotes;
}
} else if (char === ',' && !insideQuotes) {
currentRow.push(currentField);
currentField = '';
} else if ((char === '\n' || (char === '\r' && nextChar === '\n')) && !insideQuotes) {
if (char === '\r') i++; // Skip \n in \r\n
currentRow.push(currentField);
if (currentRow.length > 1 || currentRow[0] !== '') {
rows.push(currentRow);
}
currentRow = [];
currentField = '';
} else {
currentField += char;
}
}
// Don't forget the last field/row
if (currentField || currentRow.length > 0) {
currentRow.push(currentField);
if (currentRow.length > 1 || currentRow[0] !== '') {
rows.push(currentRow);
}
}
return rows;
}
function runScript(exampleFile = false, year = null) {
const yearInput = document.getElementById('year-input');
const targetYear = String(year ?? yearInput?.value ?? '');
const fileInput = document.getElementById('csvFileInput');
const file = exampleFile
? "test-file.csv"
: fileInput?.files?.[0] ?? null;
if (!file) {
console.error('No file selected.');
return;
}
if (typeof file === 'string') {
fetch(file)
.then(response => {
if (!response.ok) throw new Error(`Failed to fetch ${file}: ${response.statusText}`);
return response.text();
})
.then(csvText => {
const rows = parseCSV(csvText);
const statistics = calculateRouteTotals(rows, targetYear);
displayStats(statistics);
})
.catch(err => console.error(err));
} else {
const reader = new FileReader();
reader.onload = function (e) {
const csvText = e.target.result;
const rows = parseCSV(csvText);
const statistics = calculateRouteTotals(rows, targetYear);
displayStats(statistics);
};
reader.readAsText(file);
}
}
/**
* Takes in statistics about ORCA card usage and updates the webpage to display the results.
* @param {Object} statistics
*/
function displayStats(statistics) {
// Statistics = [[route numbers], number of taps, topRoutes, topStops, topDates, sortedRouteCount, sortedStopCount, sortedBusCount, topBuses, targetYear]
const output = document.getElementById('stats-output');
// Check to see if statistics are valid before trying to display them
if (!statistics || statistics.length === 0 || Object.entries(statistics[0]).length === 0) {
console.error(`No data available for ${statistics[10]}.`);
output.innerHTML = (
`<div class="dialog dialog--error">
<i class="fa-solid fa-triangle-exclamation" aria-hidden="true"></i>
<p>No data available for ${statistics[10]}.</p>
</div>`
)
return;
}
output.innerHTML = (
`<div class="result" id="wrapped-result">
<div class="result-header">
<div class="stat-row">
<h3>ORCA Wrapped ${statistics[10]}</h3>
</div>
</div>
<div class="headline-stats">
<div class="stat-row">
<h2 class="number-stat">${Object.entries(statistics[0]).length}</h2><h2> transit routes ridden</h2>
</div>
<div class="stat-row">
<h2 class="number-stat">${statistics[1]}</h2><h2>card taps</h2>
</div>
<div class="stat-row">
<h2 class="number-stat">${Object.entries(statistics[2]).length}</h2><h2> stops visited</h2>
</div>
<div class="stat-row">
<h3>Busiest day: </h3><h3 class="number-stat">${statistics[5][0][0]} (${statistics[5][0][1]} trips)</h3>
</div>
</div>
<div class="details-container">
<div class="list-container">
<div class="stat-row">
<p>Top 5 Routes:</p>
</div>
<div>
<ul>
${statistics[3].map(([key, value]) => `<li>${key} | ${value} trips</li>`).join("")}
</ul>
</div>
</div>
<div class="list-container">
<div class="stat-row">
<p>Top 5 Stops:</p>
</div>
<div>
<ul>
${statistics[4].map(([key, value]) => `<li>${key} | ${value} taps</li>`).join("")}
</ul>
</div>
</div>
</div>
<div class="result-footer">
<p>Created on moshobo.github.io/orca-wrapped</p>
</div>
</div>
<div>
<button onClick="saveAsImage()">Download</button>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script>
<script src="script.js"></script>
<div>
<div class="list-container list-container-no-background">
<div class="stat-row">
<h3>All Routes</h3>
</div>
<div>
<ul>
${statistics[6].map(([key, value]) => `<li>${key} | ${value} trips</li>`).join("")}
</ul>
</div>
</div>
<div class="list-container list-container-no-background">
<div class="stat-row">
<h3>All Stops</h3>
</div>
<div>
<ul>
${statistics[7].map(([key, value]) => `<li>${key} | ${value} taps</li>`).join("")}
</ul>
</div>
</div>
<div class="list-container list-container-no-background">
<div class="stat-row">
<h3>All Busses</h3>
</div>
<div>
<ul>
${statistics[8].map(([key, value]) => `<li>#${key} | ${value} trips</li>`).join("")}
</ul>
</div>
</div>
</div>`
)
}
/**
* Takes rows of data and the target year of interest and calculates key statistics
* @param {Array<Array<string>>} rows
* @param {string} targetYear
* @returns {Object}
*/
function calculateRouteTotals(rows, targetYear) {
const headers = rows[0];
const dataRows = rows.slice(1);
const locationIndex = headers.indexOf('Location');
const activityIndex = headers.indexOf('Activity');
const dateIndex = headers.indexOf('Date');
const filteredRows = dataRows.filter(row => {
// Guard against malformed/empty rows in the CSV
if (!row || row.length === 0) return false;
const activityCell = row[activityIndex];
const dateCell = row[dateIndex];
// Skip rows where required cells are missing
if (!activityCell || !dateCell) return false;
const activity = activityCell.split(', ')[0];
const date = dateCell;
const year = date.split("/")[2]; // Extract year from "MM/dd/YYYY" format
return (
(activity === "Transfer" || activity === "Boarding" || activity === "ClientFare") &&
year === targetYear
);
});
let stopCount = {}
var routeCount = {}
var dateCount = {}
var busCount = {}
var paymentTerminalsWSF = ['Seattle', 'Edmonds', 'Fauntleroy', 'Southworth', 'Point Defiance', 'Mukilteo', 'Port Townsend', 'Anacortes']
filteredRows.forEach(row => {
const locationArray = row[locationIndex].split(': ') // ['Line','4 ..., Stop', '23rd...']
let routeLongName = null
let stop = null
let date = null
// Parse data out of Location column
const split_array = row[locationIndex].split(', Stop: ')
if (split_array.length === 2) { // Bus, Light Rail, or Washington State Ferry (WSF)
routeLongName = split_array[0].split(': ')[1]
stop = split_array[1]
if (stop === "WSF") {
// This is the only route that charges both directions, so the stop can't be determined from the route
if (routeLongName === 'Point Townsend - Coupeville' || routeLongName === 'Coupeville - Point Townsend') {
stop = 'Point Townsend or Coupeville'
}
// Other routes only charge on one end of the route, so the stop can be determined from the route name
else {
let stops = routeLongName.split(' - ')
stops = stops.map(s => s.trim());
const terminal = stops.find(s => paymentTerminalsWSF.includes(s));
if (terminal) {
stop = terminal;
}
}
}
// Get bus number from activity column, if possible
activity = row[activityIndex]
const busMatch = activity.match(/Bus number:\s*(\d+)/i)
if (busMatch) {
const busNumber = busMatch[1]
if (busNumber in busCount) {
busCount[busNumber] = busCount[busNumber] + 1
} else {
busCount[busNumber] = 1
}
}
} else if (locationArray.length === 2) { // Bus without Stop or Fast Ferry
routeLongName = (locationArray[1])
} else if (locationArray.length === 3) { // KCM Water taxi
routeLongName = (locationArray[1] + ' ' + locationArray[2])
} else { // Washington State Ferry or other
routeLongName = locationArray
if (locationArray[0] === "Washington State Ferry (WSF)") {
routeLongName = "Washington State Ferry (WSF), undefined route"
}
}
date = row[dateIndex]
if (routeLongName in routeCount) {
routeCount[routeLongName] = routeCount[routeLongName] + 1
} else {
routeCount[routeLongName] = 1
}
if (stop != null && stop in stopCount) {
stopCount[stop] = stopCount[stop] + 1
} else if (stop != null) {
stopCount[stop] = 1
}
if (date in dateCount) {
dateCount[date] = dateCount[date] + 1
} else {
dateCount[date] = 1
}
});
const sortedRouteCount = Object.entries(routeCount).sort(([, valueA], [, valueB]) => valueB - valueA);
const sortedStopCount = Object.entries(stopCount).sort(([, valueA], [, valueB]) => valueB - valueA); // Maybe sort this to not include "None"
const sortedDateCount = Object.entries(dateCount).sort(([, valueA], [, valueB]) => valueB - valueA);
const sortedBusCount = Object.entries(busCount).sort(([, valueA], [, valueB]) => valueB - valueA);
const topRoutes = sortedRouteCount.slice(0, 5);
const topStops = sortedStopCount.slice(0, 5);
const topDates = sortedDateCount.slice(0, 1);
const topBuses = sortedBusCount.slice(0, 1);
return [
routeCount,
filteredRows.length,
stopCount,
topRoutes,
topStops,
topDates,
sortedRouteCount,
sortedStopCount,
sortedBusCount,
topBuses,
targetYear
]
}
/**
* Saves the statistics HTML as an PNG image file.
*/
function saveAsImage() {
const fileInput = document.getElementById('csvFileInput')
const file = fileInput.files[0];
const yearInput = document.getElementById('year-input');
const targetYear = yearInput.value;
if (file) {
const reader = new FileReader();
reader.onload = function (e) {
const csvText = e.target.result;
const rows = parseCSV(csvText);
statistics = calculateRouteTotals(rows, targetYear)
printResult(statistics)
};
reader.readAsText(file);
}
setTimeout(() => {
const element = document.getElementById("wrapped-result-printed");
element.style.visibility = "visible";
html2canvas(element).then((canvas) => {
const image = canvas.toDataURL("image/png");
const link = document.createElement("a");
link.href = image;
link.download = "wrapped-result.png";
link.click();
});
element.style.visibility = "hidden";
}, 500);
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
/**
* Creates the HTML for the statistics in a print-friendly format that isn't
* dependent on the user's current viewport size. Inserts result into the DOM
* @param {Object} statistics
*/
function printResult(statistics) {
// Statistics = [[route numbers], number of taps, [Stop Names], topRoutes, topStops]
const output = document.getElementById('result-printed');
output.innerHTML = (
`<div class="result" id="wrapped-result-printed">
<div class="result-header">
<div class="stat-row">
<h3>ORCA Wrapped ${statistics[10]}</h3>
</div>
</div>
<div class="headline-stats">
<div class="stat-row">
<h2 class="number-stat">${Object.entries(statistics[0]).length}</h2><h2> transit routes ridden</h2>
</div>
<div class="stat-row">
<h2 class="number-stat">${statistics[1]}</h2><h2>card taps</h2>
</div>
<div class="stat-row">
<h2 class="number-stat">${Object.entries(statistics[2]).length}</h2><h2> stops visited</h2>
</div>
<div class="stat-row">
<h3>Busiest day: </h3><h3 class="number-stat">${statistics[5][0][0]} (${statistics[5][0][1]} trips)</h3>
</div>
</div>
<div class="details-container">
<div class="list-container">
<div class="stat-row">
<p>Top 5 Routes:</p>
</div>
<div>
<ul>
${statistics[3].map(([key, value]) => `<li>${key} | ${value} trips</li>`).join("")}
</ul>
</div>
</div>
<div class="list-container">
<div class="stat-row">
<p>Top 5 Stops:</p>
</div>
<div>
<ul>
${statistics[4].map(([key, value]) => `<li>${key} | ${value} taps</li>`).join("")}
</ul>
</div>
</div>
</div>
<div class="result-footer">
<p>Created on moshobo.github.io/orca-wrapped</p>
</div>
</div>`
)
}