1- class IndicatorHandler {
1+ class IndicatorHandler {
22 constructor ( ) {
33 this . indicators = { } ;
44 }
55
66 fluviewIndicatorsMapping = {
77 "wili" : "%wILI" ,
8- "ili" : "%ILI" ,
8+ "ili" : "%ILI" ,
99 }
1010
1111 fluSurvRegions = [
@@ -122,6 +122,57 @@ class IndicatorHandler{
122122 } ) ;
123123 }
124124
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+
125176 showFluviewRegions ( ) {
126177 var fluviewRegionSelect = `
127178 <div class="row margin-top-1rem">
@@ -132,13 +183,15 @@ class IndicatorHandler{
132183 <select id="fluviewRegions" name="fluviewRegions" class="form-select" multiple="multiple"></select>
133184 </div>
134185 </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+ }
142195 }
143196
144197 generateEpivisCustomTitle ( indicator , geoValue ) {
@@ -151,19 +204,18 @@ class IndicatorHandler{
151204 return epivisCustomTitle ;
152205 }
153206
154- plotData ( ) {
207+ plotData ( ) {
155208 var dataSets = { } ;
156209 var covidCastGeographicValues = $ ( '#geographic_value' ) . select2 ( 'data' ) ;
157210 var fluviewRegions = $ ( '#fluviewRegions' ) . select2 ( 'data' ) ;
158- console . log ( fluviewRegions )
159-
211+
160212 this . indicators . forEach ( ( indicator ) => {
161213 if ( indicator [ "_endpoint" ] === "covidcast" ) {
162214 covidCastGeographicValues . forEach ( ( geoValue ) => {
163215 var geographicValue = ( typeof geoValue . id === 'string' ) ? geoValue . id . toLowerCase ( ) : geoValue . id ;
164216 var geographicType = geoValue . geoType ;
165217 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' ) ,
167219 title : "value" ,
168220 params : {
169221 _endpoint : indicator [ "_endpoint" ] ,
@@ -179,7 +231,7 @@ class IndicatorHandler{
179231 } else if ( indicator [ "_endpoint" ] === "fluview" ) {
180232 fluviewRegions . forEach ( ( region ) => {
181233 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' ) ,
183235 title : this . fluviewIndicatorsMapping [ indicator [ "signal" ] ] || indicator [ "signal" ] ,
184236 params : {
185237 _endpoint : indicator [ "_endpoint" ] ,
@@ -256,8 +308,106 @@ class IndicatorHandler{
256308 }
257309
258310 var urlParamsEncoded = btoa ( `{"datasets":${ JSON . stringify ( requestParams ) } }` ) ;
259-
311+
260312 var linkToEpivis = `${ epiVisUrl } #${ urlParamsEncoded } `
261313 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+
263413}
0 commit comments