-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
116 lines (101 loc) · 2.54 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
/*
- Transform data using streams
- Data taken from https://www.kaggle.com/jboysen/london-crime/
- Columns: lsoa_code,borough,major_category,minor_category,value,year,month
- Data ommited for brevity
*/
import { createReadStream } from 'fs'
import parse from "csv-parse"
import { Transform,PassThrough, pipeline } from "stream"
const csvParser = parse({ columns: true })
const FILE = "london_crime_by_lsoa.csv"
/*
//
// TRANSFORM CLASSES
//
//
*/
//filter by crime category
class FilterByCrime extends Transform {
constructor(major_category, options = {}){
options.objectMode = true
super(options)
this.major_category = major_category
}
_transform(record,enc,cb){
if(record.major_category === this.major_category){
this.push(record)
}
cb()
}
}
//filter by location
class FilterByLocation extends Transform {
constructor(borough, options = {}){
options.objectMode = true
super(options)
this.borough = borough
}
_transform(record,enc,cb){
if(record.borough === this.borough){
this.push(record)
}
cb()
}
}
//filter by year
class FilterByYear extends Transform {
constructor(year, options = {}){
options.objectMode = true
super(options)
this.year = year
}
_transform(record,enc,cb){
if(record.year === this.year){
this.push(record)
}
cb()
}
}
/*
//
//
// METHODS
//
//
*/
function main(){
const yearSet = new Set()
const crimeMajorCategorySet = new Set()
const locationSet = new Set()
pipeline(
createReadStream(FILE),
csvParser,
/*
Collect unique values of each column
Takes awhile as the data must stream
through hundreds of thousands of lines of data
*/
new Transform({ //Collect set Transforms
objectMode: true,
transform(record, enc,cb){
console.log(record)
yearSet.add(record.year)
crimeMajorCategorySet.add(record.major_category)
locationSet.add(record.borough)
cb()
}
}),
//Apply Filters Here
new FilterByYear("2010"),
new FilterByLocation("Lambeth"),
// map sets to object
// use map to solve lowest recurring value such as
//--crime that was committed the most in the area
//-- safest location of the year, etc
(err)=>{
console.log(err)
}
)
}
main()