1
- class IndicatorHandler {
1
+ class IndicatorHandler {
2
2
constructor ( ) {
3
3
this . indicators = { } ;
4
4
}
5
5
6
6
fluviewIndicatorsMapping = {
7
7
"wili" : "%wILI" ,
8
- "ili" : "%ILI" ,
8
+ "ili" : "%ILI" ,
9
9
}
10
10
11
11
fluSurvRegions = [
@@ -122,6 +122,57 @@ class IndicatorHandler{
122
122
} ) ;
123
123
}
124
124
125
+ getCovidcastIndicators ( ) {
126
+ var covidcastIndicators = [ ] ;
127
+ this . indicators . forEach ( ( indicator ) => {
128
+ if ( indicator [ "_endpoint" ] === "covidcast" ) {
129
+ covidcastIndicators . push ( indicator ) ;
130
+ }
131
+ } ) ;
132
+ return covidcastIndicators ;
133
+ }
134
+
135
+ getFluviewIndicators ( ) {
136
+ var fluviewIndicators = [ ] ;
137
+ this . indicators . forEach ( ( indicator ) => {
138
+ if ( indicator [ "_endpoint" ] === "fluview" ) {
139
+ fluviewIndicators . push ( indicator ) ;
140
+ }
141
+ }
142
+ ) ;
143
+ return fluviewIndicators ;
144
+ }
145
+
146
+ getFromToDate ( startDate , endDate , timeType ) {
147
+ if ( timeType === "week" ) {
148
+ $ . ajax ( {
149
+ url : "get_epiweek/" ,
150
+ type : 'POST' ,
151
+ async : false ,
152
+ data : {
153
+ csrfmiddlewaretoken : csrf_token ,
154
+ start_date : startDate ,
155
+ end_date : endDate ,
156
+ } ,
157
+ success : function ( result ) {
158
+ startDate = result . start_date ;
159
+ endDate = result . end_date ;
160
+ }
161
+ } )
162
+ }
163
+ return [ startDate , endDate ] ;
164
+ }
165
+
166
+
167
+ sendAsyncAjaxRequest ( url , data ) {
168
+ var request = $ . ajax ( {
169
+ url : url ,
170
+ type : "GET" ,
171
+ data : data ,
172
+ } )
173
+ return request ;
174
+ }
175
+
125
176
showFluviewRegions ( ) {
126
177
var fluviewRegionSelect = `
127
178
<div class="row margin-top-1rem">
@@ -132,13 +183,15 @@ class IndicatorHandler{
132
183
<select id="fluviewRegions" name="fluviewRegions" class="form-select" multiple="multiple"></select>
133
184
</div>
134
185
</div>`
135
- $ ( "#otherEndpointLocations" ) . append ( fluviewRegionSelect )
136
- $ ( "#fluviewRegions" ) . select2 ( {
137
- placeholder : "Select ILINet Location(s)" ,
138
- data : this . fluviewRegions ,
139
- allowClear : true ,
140
- width : '100%' ,
141
- } ) ;
186
+ if ( $ ( "#otherEndpointLocations" ) . length ) {
187
+ $ ( "#otherEndpointLocations" ) . append ( fluviewRegionSelect )
188
+ $ ( "#fluviewRegions" ) . select2 ( {
189
+ placeholder : "Select ILINet Location(s)" ,
190
+ data : this . fluviewRegions ,
191
+ allowClear : true ,
192
+ width : '100%' ,
193
+ } ) ;
194
+ }
142
195
}
143
196
144
197
generateEpivisCustomTitle ( indicator , geoValue ) {
@@ -151,19 +204,18 @@ class IndicatorHandler{
151
204
return epivisCustomTitle ;
152
205
}
153
206
154
- plotData ( ) {
207
+ plotData ( ) {
155
208
var dataSets = { } ;
156
209
var covidCastGeographicValues = $ ( '#geographic_value' ) . select2 ( 'data' ) ;
157
210
var fluviewRegions = $ ( '#fluviewRegions' ) . select2 ( 'data' ) ;
158
- console . log ( fluviewRegions )
159
-
211
+
160
212
this . indicators . forEach ( ( indicator ) => {
161
213
if ( indicator [ "_endpoint" ] === "covidcast" ) {
162
214
covidCastGeographicValues . forEach ( ( geoValue ) => {
163
215
var geographicValue = ( typeof geoValue . id === 'string' ) ? geoValue . id . toLowerCase ( ) : geoValue . id ;
164
216
var geographicType = geoValue . geoType ;
165
217
dataSets [ `${ indicator [ "signal" ] } _${ geographicValue } ` ] = {
166
- color : '#' + ( Math . random ( ) * 0xFFFFFF << 0 ) . toString ( 16 ) . padStart ( 6 , '0' ) ,
218
+ color : '#' + ( Math . random ( ) * 0xFFFFFF << 0 ) . toString ( 16 ) . padStart ( 6 , '0' ) ,
167
219
title : "value" ,
168
220
params : {
169
221
_endpoint : indicator [ "_endpoint" ] ,
@@ -179,7 +231,7 @@ class IndicatorHandler{
179
231
} else if ( indicator [ "_endpoint" ] === "fluview" ) {
180
232
fluviewRegions . forEach ( ( region ) => {
181
233
dataSets [ `${ indicator [ "signal" ] } _${ indicator [ "_endpoint" ] } _${ region . id } ` ] = {
182
- color : '#' + ( Math . random ( ) * 0xFFFFFF << 0 ) . toString ( 16 ) . padStart ( 6 , '0' ) ,
234
+ color : '#' + ( Math . random ( ) * 0xFFFFFF << 0 ) . toString ( 16 ) . padStart ( 6 , '0' ) ,
183
235
title : this . fluviewIndicatorsMapping [ indicator [ "signal" ] ] || indicator [ "signal" ] ,
184
236
params : {
185
237
_endpoint : indicator [ "_endpoint" ] ,
@@ -256,8 +308,106 @@ class IndicatorHandler{
256
308
}
257
309
258
310
var urlParamsEncoded = btoa ( `{"datasets":${ JSON . stringify ( requestParams ) } }` ) ;
259
-
311
+
260
312
var linkToEpivis = `${ epiVisUrl } #${ urlParamsEncoded } `
261
313
window . open ( linkToEpivis , '_blank' ) . focus ( ) ;
262
- }
314
+ }
315
+
316
+ exportData ( ) {
317
+ var manualDataExport = "To download data, please click on the link or copy/paste command into your terminal: \n\n"
318
+ var exportUrl ;
319
+
320
+ this . getCovidcastIndicators ( ) . forEach ( ( indicator ) => {
321
+ var startDate = document . getElementById ( 'start_date' ) . value ;
322
+ var endDate = document . getElementById ( 'end_date' ) . value ;
323
+ const [ dateFrom , dateTo ] = this . getFromToDate ( startDate , endDate , indicator [ "time_type" ] ) ;
324
+
325
+ var covidCastGeographicValues = $ ( '#geographic_value' ) . select2 ( 'data' ) ;
326
+ covidCastGeographicValues = Object . groupBy ( covidCastGeographicValues , ( { geoType } ) => [ geoType ] ) ;
327
+ var covidcastGeoTypes = Object . keys ( covidCastGeographicValues ) ;
328
+ covidcastGeoTypes . forEach ( ( geoType ) => {
329
+ var geoValues = covidCastGeographicValues [ geoType ] . map ( ( el ) => ( typeof el . id === "string" ) ? el . id . toLowerCase ( ) : el . id ) . join ( "," ) ;
330
+ exportUrl = `https://api.delphi.cmu.edu/epidata/covidcast/csv?signal=${ indicator [ "data_source" ] } :${ indicator [ "signal" ] } &start_day=${ dateFrom } &end_day=${ dateTo } &geo_type=${ geoType } &geo_values=${ geoValues } ` ;
331
+ manualDataExport += `wget --content-disposition <a href="${ exportUrl } ">${ exportUrl } </a>\n` ;
332
+ } )
333
+ } )
334
+
335
+ if ( this . getFluviewIndicators ( ) . length > 0 ) {
336
+ var startDate = document . getElementById ( 'start_date' ) . value ;
337
+ var endDate = document . getElementById ( 'end_date' ) . value ;
338
+
339
+ const [ dateFrom , dateTo ] = this . getFromToDate ( startDate , endDate , "week" ) ;
340
+
341
+ var fluviewRegions = $ ( '#fluviewRegions' ) . select2 ( 'data' ) . map ( ( region ) => region . id ) ;
342
+ fluviewRegions = fluviewRegions . join ( "," ) ;
343
+ exportUrl = `https://api.delphi.cmu.edu/epidata/fluview/?regions=${ fluviewRegions } &epiweeks=${ dateFrom } -${ dateTo } &format=csv`
344
+ manualDataExport += `wget --content-disposition <a href="${ exportUrl } ">${ exportUrl } </a>\n` ;
345
+ }
346
+
347
+ $ ( '#modeSubmitResult' ) . html ( manualDataExport ) ;
348
+ }
349
+
350
+ previewData ( ) {
351
+ $ ( '#loader' ) . show ( ) ;
352
+ var requests = [ ] ;
353
+ var previewExample = [ ] ;
354
+ var startDate = document . getElementById ( 'start_date' ) . value ;
355
+ var endDate = document . getElementById ( 'end_date' ) . value ;
356
+
357
+ if ( this . checkForCovidcastIndicators ( ) ) {
358
+ var geographicValues = $ ( '#geographic_value' ) . select2 ( 'data' ) ;
359
+ geographicValues = Object . groupBy ( geographicValues , ( { geoType } ) => [ geoType ] )
360
+ var geoTypes = Object . keys ( geographicValues ) ;
361
+ this . getCovidcastIndicators ( ) . forEach ( ( indicator ) => {
362
+ const [ dateFrom , dateTo ] = this . getFromToDate ( startDate , endDate , indicator [ "time_type" ] ) ;
363
+ var timeValues = indicator [ "time_type" ] === "week" ? `${ dateFrom } -${ dateTo } ` : `${ dateFrom } --${ dateTo } ` ;
364
+ geoTypes . forEach ( ( geoType ) => {
365
+ var geoValues = geographicValues [ geoType ] . map ( ( el ) => ( typeof el . id === "string" ) ? el . id . toLowerCase ( ) : el . id ) . join ( "," ) ;
366
+ var data = {
367
+ "time_type" : indicator [ "time_type" ] ,
368
+ "time_values" : timeValues ,
369
+ "data_source" : indicator [ "data_source" ] ,
370
+ "signal" : indicator [ "signal" ] ,
371
+ "geo_type" : geoType ,
372
+ "geo_values" : geoValues
373
+ }
374
+ requests . push ( this . sendAsyncAjaxRequest ( "epidata/covidcast/" , data ) )
375
+ } )
376
+ } )
377
+ }
378
+
379
+ if ( this . getFluviewIndicators ( ) . length > 0 ) {
380
+ const [ dateFrom , dateTo ] = this . getFromToDate ( startDate , endDate , "week" ) ;
381
+ var fluviewRegions = $ ( '#fluviewRegions' ) . select2 ( 'data' ) . map ( ( region ) => region . id ) ;
382
+ fluviewRegions = fluviewRegions . join ( "," ) ;
383
+ var data = {
384
+ "regions" : fluviewRegions ,
385
+ "epiweeks" : `${ dateFrom } -${ dateTo } ` ,
386
+ }
387
+
388
+ requests . push ( this . sendAsyncAjaxRequest ( "epidata/fluview/" , data ) )
389
+ }
390
+
391
+ $ . when . apply ( $ , requests ) . then ( ( ...responses ) => {
392
+ if ( requests . length === 1 ) {
393
+ if ( responses [ 0 ] [ "epidata" ] . length != 0 ) {
394
+ previewExample . push ( { epidata : responses [ 0 ] [ "epidata" ] [ 0 ] , result : responses [ "result" ] , message : responses [ "message" ] } )
395
+ } else {
396
+ previewExample . push ( responses [ 0 ] ) ;
397
+ }
398
+ } else {
399
+ responses . forEach ( ( response ) => {
400
+ if ( response [ 0 ] [ "epidata" ] . length != 0 ) {
401
+ previewExample . push ( { epidata : response [ 0 ] [ "epidata" ] [ 0 ] , result : response [ 0 ] [ "result" ] , message : response [ 0 ] [ "message" ] } )
402
+ } else {
403
+ previewExample . push ( response [ 0 ] [ "epidata" ] ) ;
404
+ }
405
+ } )
406
+ }
407
+ $ ( '#loader' ) . hide ( ) ;
408
+ $ ( '#modeSubmitResult' ) . html ( JSON . stringify ( previewExample , null , 2 ) ) ;
409
+
410
+ } )
411
+ }
412
+
263
413
}
0 commit comments