This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
175 lines (156 loc) · 5.45 KB
/
index.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
175
/**
*
* NN-Police-Socrata
*
* Created by Jason Wilson <[email protected]>
*
* 9/30/17.
*
* MIT License
*/
// index.js
// Call required modules
const CronJob = require('cron').CronJob
const csv = require('csv-parse')
const express = require('express')
const server = express()
const path = require('path')
// Load local ENV file if ENV variables not set
if (!process.env.API_KEY) {
require('dotenv').config()
}
// Use PUG as the view engine
server.set('view engine', 'pug')
// Set pug template path
server.set('views', path.join(__dirname, 'public/views'));
// constants
const port = process.env.PORT
const sourceData = {
ACCIDENTS: {
URI: 'https://gis2.nngov.com/ssrs/report/?rs:Name=/12-Police/Daily_Accidents_Public&rs:Command=Render&rs:Format=CSV',
cronPattern: '0 0 0-23/12 * * *' // Run automatically at Midnight and Noon
},
DAILY_ARRESTS: {
URI: 'https://gis2.nngov.com/ssrs/report/?rs:Name=/12-Police/Daily_Arrests_Public&rs:Command=Render&rs:Format=CSV',
cronPattern: '0 5 0-23/12 * * *' // Run automatically at five after Midnight and Noon
},
DAILY_JUVENILE: {
URI: 'https://gis2.nngov.com/ssrs/report/?rs:Name=/12-Police/Daily_Juvenile_Report_Public&rs:Command=Render&rs:Format=CSV',
cronPattern: '0 10 0-23/12 * * *' // Run automatically at ten after Midnight and Noon
},
DAILY_OFFENSES: {
URI: 'https://gis2.nngov.com/ssrs/report/?rs:Name=/12-Police/Daily_Offenses_Public&rs:Command=Render&rs:Format=CSV',
cronPattern: '0 15 0-23/12 * * *' // Run automatically at quarter after Midnight and Noon
},
DAILY_FIELD_CONTACTS: {
URI: 'https://gis2.nngov.com/ssrs/report/?rs:Name=/12-Police/Daily_Field_Contacts_Public&rs:Command=Render&rs:Format=CSV',
cronPattern: '0 20 0-23/12 * * *' // Run automatically at twenty after Midnight and Noon
},
THEFT_FROM_VEHICLE: {
URI: 'https://gis2.nngov.com/ssrs/report/?rs:Name=/12-Police/Daily_Theft_From_Vehicle_Public&rs:Command=Render&rs:Format=CSV',
cronPattern: '0 25 0-23/12 * * *' // Run automatically at twenty-five after Midnight and Noon
},
TOW_IMPOUND: {
URI: 'https://gis2.nngov.com/ssrs/report/?rs:Name=/12-Police/NNPD_Tow_Impound&rs:Command=Render&rs:Format=CSV',
cronPattern: '0 30 0-23/12 */7 * *' // Run automatically at half past Midnight and Noon every seven days
}
}
// Variables
let crons = [] // Provide an array to house the cron jobs
// Helper Methods
const syndicate = function (source) {
console.log(`I'll syndicate ${source} once I'm fully coded`)
// Fetch the csv file
// Log some csv data to verify file recieved
// Eventually translate the file to socrata format
}
// Define Cron Jobs and save them to crons
Object.keys(sourceData).forEach(job => {
crons[job] = new CronJob(
sourceData[job].cronPattern,
function () {
console.log(`[DATA COLLECTION] ${job} - Job Started ${new Date().toString()}`)
syndicate(sourceData[job].URI) // go and get the data file and push it to Socrata
},
function () {
console.log(`[DATA COLLECTION] ${job} - Job Stopped ${new Date().toString()}`)
},
false, // Do not start automatically, wait for start command
'America/New_York' // timezone
)
})
// Server for handling admin requests
server.param('key', (req, res, next, key) => {
// Verify key match
if (key === process.env.API_KEY) {
req.lock = { unlocked: true, msg: `API key validated` }
} else {
req.lock = { unlocked: false, msg: `Incorrect key value` }
}
return next()
})
server.get('/:key/:cron/start', function (req, res) {
if (req.lock.unlocked) {
try {
crons[req.params.cron].start()
res.send(`${req.lock.msg}, starting ${req.params.cron}...`)
} catch (err) {
res.send(`There was a problem starting ${req.params.cron}, the error received was: \n ${err}`)
}
} else {
res.send(`${req.lock.msg}, no actions will be performed.`)
}
})
server.get('/:key/:cron/stop', function (req, res) {
if (req.lock.unlocked) {
try {
crons[req.params.cron].stop()
res.send(`${req.lock.msg}, stopping ${req.params.cron}...`)
} catch (err) {
res.send(`There was a problem stopping ${req.params.cron}, the error received was: \n ${err}`)
}
} else {
res.send(`${req.lock.msg}, no actions will be performed.`)
}
})
server.get('/:key/start', function (req, res) {
if (req.lock.unlocked) {
Object.keys(crons).forEach(job => {
try {
crons[job].start()
console.log(`${job} started: ${crons[job].running}`)
} catch (err) {
console.log(`There was a problem starting the jobs, the error received was: \n ${err}`)
}
})
res.send(`${req.lock.msg}, starting all crons...`)
} else {
res.send(`${req.lock.msg}, no actions will be performed.`)
}
})
server.get('/:key/stop', function (req, res) {
if (req.lock.unlocked) {
Object.keys(crons).forEach(job => {
try {
crons[job].stop()
} catch (err) {
console.log(`There was a problem starting the jobs, the error received was: \n ${err}`)
}
})
res.send(`${req.lock.msg}, stopping all crons...`)
} else {
res.send(`${req.lock.msg}, no actions will be performed.`)
}
})
server.get('/:key', function (req, res) { // Help Page
if (req.lock.unlocked) {
res.render(`help`, {
jobs: Object.keys(sourceData)
})
} else {
res.send(`${req.lock.msg}, no actions will be performed.`)
}
})
server.listen(port, function () {
console.log(`Code4HR NN Police Open Data transport listening on port ${port}!`)
})