-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
public-holidays.js
122 lines (98 loc) · 2.98 KB
/
public-holidays.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
const ical = require('node-ical')
const path = require('path')
const DatabaseConnection = require('./database/DatabaseConnection')
const config = require('./config')
const utils = require('./utils')
const database = new DatabaseConnection(config.databaseURL, config.databaseName)
let gtfsTimetables
database.connect(async err => {
gtfsTimetables = database.getCollection('gtfs timetables')
})
let rawEvents = ical.sync.parseFile(path.join(__dirname, 'additional-data/vic-holidays.ics'))
let events = Object.values(rawEvents).slice(1)
let eventCache = {}
let dayCache = {}
let nightNetworkRunning = {}
function editName(name) {
if (name === 'Saturday before Easter Sunday') return 'Easter Saturday'
if (name === 'Friday before the AFL Grand Final') return 'AFL Grand Final Friday'
return name
}
events.filter(e => e.type === 'VEVENT').forEach(event => {
try {
let start = event.start.toISOString()
let day = utils.parseTime(start)
let name = editName(event.summary)
eventCache[utils.getYYYYMMDD(day)] = name
} catch (e) {
global.loggers.error.err('Failed to load holiday data', event)
}
})
function getPublicHolidayName(time) {
return eventCache[utils.getYYYYMMDD(time)]
}
async function waitForDB() {
for (let i = 0; i < 4; i++) {
if (!gtfsTimetables) await utils.sleep(500)
if (gtfsTimetables) break
}
}
async function getPHDayOfWeek(time) {
await waitForDB()
let day = utils.getYYYYMMDD(time)
if (eventCache[day]) {
if (dayCache[day]) {
return dayCache[day]
} else {
let count782 = await gtfsTimetables.countDocuments({
operationDays: day,
routeGTFSID: '4-782',
mode: 'bus',
destination: 'Frankston Railway Station/Young Street'
})
let hasFlinders = await gtfsTimetables.countDocuments({
operationDays: day,
routeGTFSID: '4-782',
mode: 'bus',
origin: 'Wood Street/Cook Street',
})
let phDay
if (count782 === 15) phDay = 'Saturday'
if (count782 === 14) {
if (hasFlinders) phDay = 'Weekday'
else phDay = 'Sunday'
}
dayCache[day] = phDay
return phDay
}
} else {
return null
}
}
async function getDayOfWeek(time) {
let phDay = await getPHDayOfWeek(time)
let regularDay = utils.getDayOfWeek(time)
if (!phDay || phDay === 'Weekday') return regularDay
if (phDay === 'Saturday') return 'Sat'
else return 'Sun'
}
async function isNightNetworkRunning(time) {
await waitForDB()
let day = utils.getYYYYMMDD(time)
if (nightNetworkRunning[day]) return nightNetworkRunning[day]
let train = await gtfsTimetables.findDocument({
operationDays: day,
mode: 'metro train',
'stopTimings.0.departureTimeMinutes': {
$gte: 1500 // 25 * 60 = 1am
}
})
nightNetworkRunning[day] = !!train // If timetable exists means NN running, else not running
return nightNetworkRunning[day]
}
module.exports = {
getPublicHolidayName,
getPHDayOfWeek,
getDayOfWeek,
isNightNetworkRunning
}