-
Notifications
You must be signed in to change notification settings - Fork 2
/
data.js
207 lines (188 loc) · 5.25 KB
/
data.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
// Script to produce data for open.canada.ca
// Switch this to 'production' once the prod data service is live
// NB: If the latest data is not available from production, you may need to
// download it from TEST, put it in a JSON file in public/data, change the
// downloaded data file in RouteComputations to the new JSON file, and change
// this environment setting to 'development'
process.env.NODE_ENV = 'production'
// NB: this list omits elements which are not used by the visualization, and
// which are not really of interest to end uers such as database IDs.
const csvHeaderNamesInOrder = [
'IncidentNumber',
'Latitude',
'Longitude',
'ApproximateVolumeM3',
'ReportedDate',
'ReportedYear',
'IncidentType_EN_LIST',
'IncidentType_FR_LIST',
'IncidentStatus_EN',
'IncidentStatus_FR',
'CompanyName_EN',
'CompanyName_FR',
'NearestPopulationCenter_EN',
'NearestPopulationCenter_FR',
'ProvinceName_EN',
'ProvinceName_FR',
'SubstanceName_EN',
'SubstanceName_FR',
'ReleaseType_EN',
'ReleaseType_FR',
'PipelinePhase_EN',
'PipelinePhase_FR',
'WerePipelineSystemComponentsInvolved',
'PipelineComponent_EN_LIST',
'PipelineComponent_FR_LIST',
'WhatHappened_EN_LIST',
'WhatHappened_FR_LIST',
'WhyItHappened_EN_LIST',
'WhyItHappened_FR_LIST',
]
//trKey contains key information for the data stored in translation table
//trKey: 'not-applicable' is the one whose translation is not available
const csvHeaderNamesInOrder_FR = {
IncidentNumber: {
header: 'Incidents',
trKey: 'not-applicable'
},
Latitude: {
header: 'Latitude',
trKey: 'not-applicable'
},
Longitude:{
header: 'Longitude',
trKey: 'not-applicable'
},
ApproximateVolumeM3:{
header: 'Volume approx. rejeté',
trKey: 'volumeCategory'
},
ReportedDate:{
header: 'Date/année du signalement',
trKey: 'not-applicable'
},
IncidentType_FR_LIST:{
header: 'Type d’incident',
trKey: 'incidentTypes'
},
IncidentStatus_FR:{
header: 'État',
trKey: 'status'
},
CompanyName_FR:{
header: 'Société',
trKey: 'not-applicable'
},
NearestPopulationCenter_FR:{
header: 'Centre de population le plus près',
trKey: 'not-applicable'
},
ProvinceName_FR:{
header: 'Provinces',
trKey: 'province'
},
SubstanceName_FR:{
header: 'Substance',
trKey: 'substance'
},
ReleaseType_FR:{
header: ' Type de rejet',
trKey: 'releaseType'
},
PipelinePhase_FR:{
header: 'Étape du cycle de vie',
trKey: 'pipelinePhase'
},
WerePipelineSystemComponentsInvolved:{
header: 'Des composantes du réseau ont-elles été en cause?',
trKey: 'pipelineSystemComponentsInvolved'
},
PipelineComponent_FR_LIST:{
header: 'Composantes en cause',
trKey: 'not-applicable'
},
WhatHappened_FR_LIST:{
header: 'Ce qui s’est passé',
trKey: 'whatHappened'
},
WhyItHappened_FR_LIST:{
header: 'Causes',
trKey: 'whyItHappened'
}
}
const csvHeaderNamesInOrder_EN = [
'IncidentNumber',
'Latitude',
'Longitude',
'ApproximateVolumeM3',
'ReportedDate',
'ReportedYear',
'IncidentType_EN_LIST',
'IncidentStatus_EN',
'CompanyName_EN',
'NearestPopulationCenter_EN',
'ProvinceName_EN',
'SubstanceName_EN',
'ReleaseType_EN',
'PipelinePhase_EN',
'WerePipelineSystemComponentsInvolved',
'PipelineComponent_EN_LIST',
'WhatHappened_EN_LIST',
'WhyItHappened_EN_LIST',
]
const Fs = require('fs')
const D3 = require('d3')
const DataLoader = require('./app/DataLoader.js')
const Store = require('./app/Store.js')
const Tr = require('./app/TranslationTable.js')
const store = Store()
let location
if (process.env.NODE_ENV === 'development') {
location = {
origin: 'http://localhost:3001',
pathname: '/pipeline-incidents/',
}
}
else if (process.env.NODE_ENV === 'production') {
location = {
origin: 'https://apps2.cer-rec.gc.ca',
pathname: '/pipeline-incidents/',
}
}
const dataLoadPromise = DataLoader.loadFromDataService(store, location)
dataLoadPromise.then( () => {
const outputData = store.getState().data.map( incident => incident.get('originalData'))
//Prepend UTF 8 Byte Order Mark during CSV generation so
//that programs like Microsft Excel can recognize the encoding
//and use that encoding while opening the file
const byteOrderMark = '\ufeff'
//English
Fs.writeFile(
'Incident Visualization Data_EN.csv',
byteOrderMark + D3.csvFormat(outputData.toJS(), csvHeaderNamesInOrder_EN),
() => {}
)
//French
let dataFields = Object.keys(csvHeaderNamesInOrder_FR)
const frenchHeader = dataFields.map((index) => csvHeaderNamesInOrder_FR[index]['header'])
Fs.writeFile(
'Incident Visualization Data_FR.csv',
byteOrderMark + D3.csvFormatRows([frenchHeader].concat((outputData.toJS()).map(function(columns) {
return dataFields.map((dataFieldName)=> {
let dataValue = columns[dataFieldName]
const trKey = csvHeaderNamesInOrder_FR[dataFieldName]['trKey']
if(trKey !== 'not-applicable')
{
Tr.getIn(['categories', trKey]).entrySeq().forEach(category => {
if(dataValue === category[1].get('en')){
dataValue = category[1].get('fr')
return
}
})
}
return dataValue
})
}))),
() => {}
)
})