-
Notifications
You must be signed in to change notification settings - Fork 0
/
oakland_domestic_violence.js
71 lines (63 loc) · 2.07 KB
/
oakland_domestic_violence.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
/*
Crimewatch
Past 90 days
https://data.oaklandnet.com/Public-Safety/CrimeWatch-Maps-Past-90-Days/ym6k-rx7a
*/
var http = require("http");
var _ = require("underscore");
var oakland_crimewatch_endpoint_url = "http://data.oaklandnet.com/resource/ym6k-rx7a.json";
function printIncident(crimeIncident) {
console.log(crimeIncident.crimetype + " : " + crimeIncident.Description);
}
function fetchdata(cb) {
var request = http.get(oakland_crimewatch_endpoint_url, function(response) {
var body = "";
response.on("data", function(chunk) {
body += chunk;
});
response.on("end", function() {
if (response.statusCode === 200) {
try {
var responseArray = JSON.parse(body);
cb(null, responseArray);
} catch(error) {
cb(error, null);
}
} else {
console.log("response code is: " + response.statusCode);
}
});
});
}
function printDomesticViolenceIncidentsTotal() {
console.log("Oakland Crimewatch - the past 90 days");
printIncidentsByPoliceBeat("DOMESTIC VIOLENCE");
}
function printIncidentsByPoliceBeat(crimeType){
var crimes = fetchdata(function (error, responseArray) {
if (error == null) {
var crimeTotals = {};
_.each(responseArray, function(crimeIncident) {
var crimeTypeValue = crimeIncident.crimetype;
var policeBeat = crimeIncident.policebeat;
if (crimeTypeValue === crimeType) {
if (crimeTotals[policeBeat]) {
crimeTotals[policeBeat]++;
} else {
crimeTotals[policeBeat] = 1;
}
}
});
for (var key in crimeTotals) {
console.log("Police Precinct " + key + " : " + crimeTotals[key] + " domestic violence incidents");
}
} else {
print(error);
}
});
}
function printError(error) {
console.log(error.message);
}
module.exports.printIncidentsByPoliceBeat = printIncidentsByPoliceBeat;
module.exports.printDomesticViolenceIncidentsTotal = printDomesticViolenceIncidentsTotal;