-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
247 lines (225 loc) · 6.23 KB
/
server.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
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
var http = require('http')
, fs = require('fs')
, url = require('url')
, qs = require('querystring')
, port = 8080
var MAX_OPACITY = 0.95
var MIN_OPACITY = 0.1
var NUMBER_OF_BEATS = 277
var server = http.createServer (function (req, res) {
var uri = url.parse(req.url)
switch( uri.pathname ) {
case '/':
case '/index.html':
sendFile(res, 'index.html')
break
case '/core_map.js':
sendFile(res, 'core_map.js', 'application/javascript')
break
case '/core_tabs.js':
sendFile(res, 'core_tabs.js', 'application/javascript')
break
case '/core_populate.js':
sendFile(res, 'core_populate.js', 'application/javascript')
break
case '/core_scroller.js':
sendFile(res, 'core_scroller.js', 'application/javascript')
break
case '/styles.css':
sendFile(res, 'styles.css', 'text/css')
break
case '/data/NOV2016.json':
sendFile(res, 'data/NOV2016.json', 'application/json')
break
case '/data/police_beats.json':
sendFile(res, 'data/police_beats.json', 'application/json')
break
case '/imgs/city.png':
sendFile(res, 'imgs/city.png', 'image/png')
break
case '/imgs/arson.png':
sendFile(res, 'imgs/arson.png', 'image/png')
break
case '/imgs/beer.png':
sendFile(res, 'imgs/beer.png', 'image/png')
break
case '/imgs/car.png':
sendFile(res, 'imgs/car.png', 'image/png')
break
case '/imgs/cop.png':
sendFile(res, 'imgs/cop.png', 'image/png')
break
case '/imgs/drugs.png':
sendFile(res, 'imgs/drugs.png', 'image/png')
break
case '/imgs/foot.png':
sendFile(res, 'imgs/foot.png', 'image/png')
break
case '/imgs/gamb.png':
sendFile(res, 'imgs/gamb.png', 'image/png')
break
case '/imgs/guns.png':
sendFile(res, 'imgs/guns.png', 'image/png')
break
case '/imgs/hit.png':
sendFile(res, 'imgs/hit.png', 'image/png')
break
case '/imgs/kid.png':
sendFile(res, 'imgs/kid.png', 'image/png')
break
case '/imgs/kiss.png':
sendFile(res, 'imgs/kiss.png', 'image/png')
break
case '/imgs/murder.png':
sendFile(res, 'imgs/murder.png', 'image/png')
break
case '/imgs/peace.png':
sendFile(res, 'imgs/peace.png', 'image/png')
break
case '/imgs/rape.png':
sendFile(res, 'imgs/rape.png', 'image/png')
break
case '/imgs/theft.png':
sendFile(res, 'imgs/theft.png', 'image/png')
break
case '/imgs/play.png':
sendFile(res, 'imgs/play.png', 'image/png')
break
case '/imgs/pause.png':
sendFile(res, 'imgs/pause.png', 'image/png')
break
case '/get':
processCrimeData(res, uri, 'application/json')
break
case '/readme.md':
case '/README.md':
sendFile(res, 'README.md', 'text/markdown')
break
default:
res.writeHead(404, {'Content-type': "text/html"})
ret = '<meta http-equiv="refresh" content="2;url=/" />';
ret += '<h3>404 - File not found :(</h3>'
res.end(ret)
}
})
server.listen(process.env.PORT || port)
console.log(port)
function sendFile(res, filename, contentType) {
contentType = contentType || 'text/html'
fs.readFile(filename, function(error, content) {
res.writeHead(200, {'Content-type': contentType})
res.end(content, 'utf-8')
})
}
//Process the query if the Crime JSON is asked
function processCrimeData(res,uri, contentType) {
if (uri.query) {
var parsed = qs.parse(uri.query)
if (parsed.type != null)
getProcessedJSON(res, parsed, contentType)
else
res.end('incorrect query')
} else
res.end('no query provided')
}
function getProcessedJSON (res, parsed, contentType) {
// If all of the crime info is requested
if (parsed.type == 'all') {
var allCrimeJSON = getBeatSizeInitialJSON();
fs.readdir('data/BeatJSONData/', function(err, filenames) {
if (err) {
console.log(err);
return
}
filenames.forEach(function(filename) {
var contents = fs.readFileSync('data/BeatJSONData/' + filename);
var jsonContent = JSON.parse(contents);
for (var key in jsonContent) {
if (jsonContent.hasOwnProperty(key)) {
if (key!="__beatordering") {
allCrimeJSON[key] = addListToList(allCrimeJSON[key], jsonContent[key])
}
else {
allCrimeJSON[key] = jsonContent[key].slice()
}
}
}
})
for (var key in allCrimeJSON) {
if (allCrimeJSON.hasOwnProperty(key)) {
if (key!="__beatordering") {
allCrimeJSON[key] = relativeValueOfList(allCrimeJSON[key])
}
}
}
res.writeHead(200, {'Content-type': contentType})
res.end(JSON.stringify(allCrimeJSON))
})
} else {
var crimeJSON = {}
if(!fs.existsSync('data/BeatJSONData/' + parsed.type + '.json')) {
res.writeHead(200, {'Content-type': contentType})
res.end(JSON.stringify(crimeJSON))
}
else {
var contents = fs.readFileSync('data/BeatJSONData/' + parsed.type + '.json')
crimeJSON = JSON.parse(contents)
for (var key in crimeJSON) {
if (crimeJSON.hasOwnProperty(key)) {
if (key!="__beatordering") {
crimeJSON[key] = relativeValueOfList(crimeJSON[key])
}
}
}
res.writeHead(200, {'Content-type': contentType})
res.end(JSON.stringify(crimeJSON))
}
}
}
//Finds the relative value of list specified by max and min opacitites
function relativeValueOfList(listToRelate) {
var max = -1
var newList = []
for (var i = 0; i < listToRelate.length; i++) {
if(listToRelate[i] > max) {
max = listToRelate[i]
}
}
for (var i = 0; i < listToRelate.length; i++) {
newList.push(Math.round(((((MAX_OPACITY-MIN_OPACITY)*listToRelate[i])/max)+ MIN_OPACITY)*100)/100)
}
return newList
}
//Add list2 iterms to list1 and return list1
function addListToList(list1, list2) {
for(var i = 0; i < list1.length; i++) {
list1[i] = list1[i] + list2[i]
}
return list1
}
//Makes a JSON will all the required keys and values as list of 0s
function getBeatSizeInitialJSON() {
var allCrimeJSON = {}
var beatSizeList = []
for (var i = 0; i < NUMBER_OF_BEATS; i++) {
beatSizeList.push(0)
}
allCrimeJSON["__beatordering"] = []
//Initialize the JSON to have all the proper keys
for (var i = 2013; i <=2016; i++) {
var jLimit = 12
if (i == 2016){
jLimit = 11
}
for (var j = 1; j <=jLimit; j++) {
if (j > 9) {
var key = (j.toString() + "_" + i.toString());
}
else {
var key = ("0" + j.toString() + "_" + i.toString());
}
allCrimeJSON[key] = beatSizeList.slice();
}
}
return allCrimeJSON
}