Skip to content

Commit 2c03997

Browse files
committed
Added export/preview data
1 parent 9734ad6 commit 2c03997

File tree

2 files changed

+176
-131
lines changed

2 files changed

+176
-131
lines changed

src/assets/js/indicatorHandler.js

+166-16
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
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
}

src/assets/js/signal_sets.js

+10-115
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ $("#showSelectedSignalsButton").click(function() {
9191
alertPlaceholder.innerHTML = "";
9292
if (!indicatorHandler.checkForCovidcastIndicators()) {
9393
$("#geographic_value").prop("disabled", true);
94+
} else {
95+
$("#geographic_value").prop("disabled", false);
9496
}
9597
$('#geographic_value').select2("data").forEach(geo => {
9698
checkGeoCoverage(geo.geoType, geo.id).then((notCoveredSignals) => {
@@ -104,9 +106,12 @@ $("#showSelectedSignalsButton").click(function() {
104106
nonCovidcastSignalSets = [...new Set(checkedSignalMembers.filter(signal => signal["_endpoint"] != "covidcast").map((signal) => signal["signal_set"]))];
105107
otherEndpointLocationsWarning += `Different location is required for following signal set(s): ${nonCovidcastSignalSets.join(", ")}`
106108
otherEndpointLocationsWarning += `</div></div>`
107-
$("#differentLocationNote").html(otherEndpointLocationsWarning)
108-
indicatorHandler.showFluviewRegions();
109-
109+
if (indicatorHandler.getFluviewIndicators().length > 0) {
110+
$("#differentLocationNote").html(otherEndpointLocationsWarning)
111+
if (document.getElementsByName("fluviewRegions").length === 0) {
112+
indicatorHandler.showFluviewRegions();
113+
}
114+
}
110115
});
111116

112117
// Add an event listener to each 'bulk-select' element
@@ -240,116 +245,6 @@ function format (signalSetId, relatedSignals, signalSetDescription) {
240245
return data;
241246
}
242247

243-
244-
function exportData() {
245-
var geographicValues = $('#geographic_value').select2('data');
246-
geographicValues = Object.groupBy(geographicValues, ({ geoType }) => [geoType])
247-
var geoTypes = Object.keys(geographicValues);
248-
249-
var startDate = document.getElementById('start_date').value;
250-
var endDate = document.getElementById('end_date').value;
251-
252-
var manualDataExport = "To download data, please click on the link or copy/paste command into your terminal: \n\n"
253-
var requests = [];
254-
255-
checkedSignalMembers.forEach((signal) => {
256-
geoTypes.forEach((geoType) => {
257-
var geoValues = geographicValues[geoType].map((el) => (typeof el.id === 'string') ? el.id.toLowerCase() : el.id).join(",");
258-
if (signal["time_type"] === "week") {
259-
var request = $.ajax({
260-
url: "get_epiweek/",
261-
type: 'POST',
262-
async: true,
263-
data: {
264-
csrfmiddlewaretoken: csrf_token,
265-
start_date: startDate,
266-
end_date: endDate,
267-
},
268-
success: function (result) {
269-
var exportUrl = `https://api.delphi.cmu.edu/epidata/covidcast/csv?signal=${signal["data_source"]}:${signal["signal"]}&start_day=${result.start_date}&end_day=${result.end_date}&geo_type=${geoType}&geo_values=${geoValues}`;
270-
manualDataExport += `wget --content-disposition <a href="${exportUrl}">${exportUrl}</a>\n`
271-
}
272-
})
273-
requests.push(request);
274-
} else {
275-
var exportUrl = `https://api.delphi.cmu.edu/epidata/covidcast/csv?signal=${signal["data_source"]}:${signal["signal"]}&start_day=${startDate}&end_day=${endDate}&geo_type=${geoType}&geo_values=${geoValues}`;
276-
manualDataExport += `wget --content-disposition <a href="${exportUrl}">${exportUrl}</a>\n`
277-
}
278-
});
279-
});
280-
$.when.apply($, requests).then(function() {
281-
$('#modeSubmitResult').html(manualDataExport);
282-
})
283-
284-
}
285-
286-
function previewData() {
287-
var geographicValues = $('#geographic_value').select2('data');
288-
geographicValues = Object.groupBy(geographicValues, ({ geoType }) => [geoType])
289-
var geoTypes = Object.keys(geographicValues);
290-
var previewExample = [];
291-
var requests = [];
292-
293-
var startDate = document.getElementById("start_date").value;
294-
var endDate = document.getElementById("end_date").value;
295-
296-
checkedSignalMembers.forEach((signal) => {
297-
var timeValues;
298-
299-
if (signal["time_type"] === "week") {
300-
$.ajax({
301-
url: "get_epiweek/",
302-
type: 'POST',
303-
async: false,
304-
data: {
305-
csrfmiddlewaretoken: csrf_token,
306-
start_date: startDate,
307-
end_date: endDate,
308-
},
309-
success: function (result) {
310-
timeValues = `${result.start_date}-${result.end_date}`;
311-
}
312-
})
313-
};
314-
315-
var requestSent = false;
316-
if (!requestSent) {
317-
geoTypes.forEach((geoType) => {
318-
var geoValues = geographicValues[geoType].map((el) => (typeof el.id === 'string') ? el.id.toLowerCase() : el.id).join(",");
319-
$('#loader').show();
320-
timeValues = signal["time_type"] === "week" ? timeValues : `${startDate}--${endDate}`;
321-
var request = $.ajax({
322-
url: "epidata/covidcast/",
323-
type: 'GET',
324-
async: true,
325-
data: {
326-
'time_type': signal["time_type"],
327-
'time_values': timeValues,
328-
'data_source': signal["data_source"],
329-
'signal': signal["signal"],
330-
'geo_type': geoType,
331-
'geo_values': geoValues
332-
},
333-
success: function (result) {
334-
if (result["epidata"].length != 0) {
335-
previewExample.push({epidata: result["epidata"][0], result: result["result"], message: result["message"]})
336-
} else {
337-
previewExample.push({epidata: result["epidata"], result: result["result"], message: result["message"]})
338-
}
339-
}
340-
})
341-
requests.push(request);
342-
})
343-
}
344-
})
345-
$.when.apply($, requests).then(function() {
346-
$('#loader').hide();
347-
$('#modeSubmitResult').html(JSON.stringify(previewExample, null, 2));
348-
requestSent = true;
349-
})
350-
}
351-
352-
353248
// Plot/Export/Preview data block
354249

355250
var currentMode = 'epivis';
@@ -447,9 +342,9 @@ function submitMode(event) {
447342
if (currentMode === 'epivis') {
448343
indicatorHandler.plotData();
449344
} else if (currentMode === 'export') {
450-
exportData();
345+
indicatorHandler.exportData();
451346
} else {
452-
previewData();
347+
indicatorHandler.previewData();
453348
}
454349
}
455350

0 commit comments

Comments
 (0)