Skip to content

Commit 4b7a05a

Browse files
authored
Merge pull request #87 from cmu-delphi/release/v2.1.6
Release v2.1.6
2 parents 5c8526f + 866c146 commit 4b7a05a

20 files changed

+534
-186
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "www-epivis",
3-
"version": "2.1.5",
3+
"version": "2.1.6",
44
"private": true,
55
"license": "MIT",
66
"description": "",

src/api/EpiData.ts

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable @typescript-eslint/restrict-template-expressions */
12
import UIkit from 'uikit';
23
import { appendIssueToTitle } from '../components/dialogs/utils';
34
import {
@@ -43,11 +44,23 @@ const ENDPOINT = process.env.EPIDATA_ENDPOINT_URL;
4344

4445
export const fetchOptions: RequestInit = process.env.NODE_ENV === 'development' ? { cache: 'force-cache' } : {};
4546

47+
function processResponse<T>(response: Response): Promise<T> {
48+
if (response.ok) {
49+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
50+
return response.json();
51+
}
52+
return response.text().then((text) => {
53+
throw new Error(`[${response.status}] ${text}`);
54+
});
55+
}
56+
4657
export function fetchImpl<T>(url: URL): Promise<T> {
4758
const urlGetS = url.toString();
4859
if (urlGetS.length < 4096) {
4960
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
50-
return fetch(url.toString(), fetchOptions).then((d) => d.json());
61+
return fetch(url.toString(), fetchOptions).then((d) => {
62+
return processResponse(d);
63+
});
5164
}
5265
const params = new URLSearchParams(url.searchParams);
5366
url.searchParams.forEach((d) => url.searchParams.delete(d));
@@ -56,7 +69,9 @@ export function fetchImpl<T>(url: URL): Promise<T> {
5669
...fetchOptions,
5770
method: 'POST',
5871
body: params,
59-
}).then((d) => d.json());
72+
}).then((d) => {
73+
return processResponse(d);
74+
});
6075
}
6176

6277
// generic epidata loader
@@ -147,26 +162,50 @@ export function loadDataSet(
147162
url.searchParams.set('format', 'json');
148163
return fetchImpl<Record<string, unknown>[]>(url)
149164
.then((res) => {
150-
const data = loadEpidata(title, res, columns, columnRenamings, { _endpoint: endpoint, ...params });
151-
if (data.datasets.length == 0) {
165+
try {
166+
const data = loadEpidata(title, res, columns, columnRenamings, { _endpoint: endpoint, ...params });
167+
if (data.datasets.length == 0) {
168+
return UIkit.modal
169+
.alert(
170+
`
171+
<div class="uk-alert uk-alert-error">
172+
<a href="${url.href}">API Link</a> returned no data, which suggests that the API has no available information for the selected location.
173+
</div>`,
174+
)
175+
.then(() => null);
176+
}
177+
return data;
178+
} catch (error) {
179+
console.warn('failed loading data', error);
180+
// EpiData API error - JSON with "message" property
181+
if ('message' in res) {
182+
return UIkit.modal
183+
.alert(
184+
`
185+
<div class="uk-alert uk-alert-error">
186+
[f01] Failed to fetch API data from <a href="${url.href}">API Link</a>:<br/><i>${res['message']}</i>
187+
</div>`,
188+
)
189+
.then(() => null);
190+
}
191+
// Fallback for generic error
152192
return UIkit.modal
153193
.alert(
154194
`
155195
<div class="uk-alert uk-alert-error">
156-
<a href="${url.href}">API Link</a> returned no data.
196+
[f02] Failed to fetch API data from <a href="${url.href}">API Link</a>:<br/><i>${error}</i>
157197
</div>`,
158198
)
159199
.then(() => null);
160200
}
161-
return data;
162201
})
163202
.catch((error) => {
164203
console.warn('failed fetching data', error);
165204
return UIkit.modal
166205
.alert(
167206
`
168207
<div class="uk-alert uk-alert-error">
169-
Failed to fetch API data from <a href="${url.href}">API Link</a>.
208+
[f03] Failed to fetch API data from <a href="${url.href}">API Link</a>:<br/><i>${error}</i>
170209
</div>`,
171210
)
172211
.then(() => null);

src/components/dialogs/ImportAPIDialog.svelte

Lines changed: 130 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,14 @@
1919
import NowCast from './dataSources/Nowcast.svelte';
2020
import CovidHosp from './dataSources/COVIDHosp.svelte';
2121
import CoviDcast from './dataSources/COVIDcast.svelte';
22-
import { navMode } from '../../store';
22+
import { navMode, storeApiKeys } from '../../store';
2323
import { NavMode } from '../chartUtils';
24+
import { formSelections } from '../../store';
2425
2526
const dispatch = createEventDispatcher();
2627
2728
const id = randomId();
2829
29-
let dataSource:
30-
| 'fluview'
31-
| 'flusurv'
32-
| 'gft'
33-
| 'ght'
34-
| 'twitter'
35-
| 'wiki'
36-
| 'cdc'
37-
| 'quidel'
38-
| 'nidss_flu'
39-
| 'nidss_dengue'
40-
| 'sensors'
41-
| 'nowcast'
42-
| 'covidcast'
43-
| 'covid_hosp' = 'fluview';
44-
4530
let loading = false;
4631
let handler: unknown = null;
4732
@@ -71,68 +56,134 @@
7156
<div class="uk-form-label">Data Source</div>
7257
<div class="uk-form-controls uk-form-controls-text">
7358
<label
74-
><input class="uk-radio" type="radio" name="dataSource" bind:group={dataSource} value="fluview" />
59+
><input
60+
class="uk-radio"
61+
type="radio"
62+
name="dataSource"
63+
bind:group={$formSelections.dataSource}
64+
value="fluview"
65+
/>
7566
ILINet (aka FluView) (source:
7667
<a target="_blank" href="https://gis.cdc.gov/grasp/fluview/fluportaldashboard.html">cdc.gov</a>)
7768
</label>
7869
<label
79-
><input class="uk-radio" type="radio" name="dataSource" bind:group={dataSource} value="flusurv" /> FluSurv
80-
(source: <a target="_blank" href="https://gis.cdc.gov/GRASP/Fluview/FluHospRates.html">cdc.gov</a>)</label
70+
><input
71+
class="uk-radio"
72+
type="radio"
73+
name="dataSource"
74+
bind:group={$formSelections.dataSource}
75+
value="flusurv"
76+
/>
77+
FluSurv (source:
78+
<a target="_blank" href="https://gis.cdc.gov/GRASP/Fluview/FluHospRates.html">cdc.gov</a>)</label
8179
>
8280
<label
83-
><input class="uk-radio" type="radio" name="dataSource" bind:group={dataSource} value="gft" /> Google Flu
84-
Trends (source: <a target="_blank" href="https://www.google.org/flutrends/">google.com</a>)</label
81+
><input class="uk-radio" type="radio" name="dataSource" bind:group={$formSelections.dataSource} value="gft" />
82+
Google Flu Trends (source: <a target="_blank" href="https://www.google.org/flutrends/">google.com</a>)</label
8583
>
8684
<label
87-
><input class="uk-radio" type="radio" name="dataSource" bind:group={dataSource} value="ght" /> Google Health Trends
88-
(source: private Google API)</label
85+
><input class="uk-radio" type="radio" name="dataSource" bind:group={$formSelections.dataSource} value="ght" />
86+
Google Health Trends (source: private Google API)</label
8987
>
9088
<label
91-
><input class="uk-radio" type="radio" name="dataSource" bind:group={dataSource} value="twitter" />
89+
><input
90+
class="uk-radio"
91+
type="radio"
92+
name="dataSource"
93+
bind:group={$formSelections.dataSource}
94+
value="twitter"
95+
/>
9296
HealthTweets (source: <a target="_blank" href="http://www.healthtweets.org/">healthtweets.org</a>)</label
9397
>
9498
<label
95-
><input class="uk-radio" type="radio" name="dataSource" bind:group={dataSource} value="wiki" /> Wikipedia
96-
Access (source:
99+
><input
100+
class="uk-radio"
101+
type="radio"
102+
name="dataSource"
103+
bind:group={$formSelections.dataSource}
104+
value="wiki"
105+
/>
106+
Wikipedia Access (source:
97107
<a target="_blank" href="https://dumps.wikimedia.org/other/pagecounts-raw/">dumps.wikimedia.org</a>)</label
98108
>
99109
<label
100-
><input class="uk-radio" type="radio" name="dataSource" bind:group={dataSource} value="cdc" /> CDC Page Hits (source:
101-
private CDC dataset)</label
110+
><input class="uk-radio" type="radio" name="dataSource" bind:group={$formSelections.dataSource} value="cdc" />
111+
CDC Page Hits (source: private CDC dataset)</label
102112
>
103113
<label
104-
><input class="uk-radio" type="radio" name="dataSource" bind:group={dataSource} value="quidel" /> Quidel Data (source:
105-
private Quidel dataset)</label
114+
><input
115+
class="uk-radio"
116+
type="radio"
117+
name="dataSource"
118+
bind:group={$formSelections.dataSource}
119+
value="quidel"
120+
/> Quidel Data (source: private Quidel dataset)</label
106121
>
107122
<label
108-
><input class="uk-radio" type="radio" name="dataSource" bind:group={dataSource} value="nidss_flu" /> NIDSS -
109-
Influenza (source:
123+
><input
124+
class="uk-radio"
125+
type="radio"
126+
name="dataSource"
127+
bind:group={$formSelections.dataSource}
128+
value="nidss_flu"
129+
/>
130+
NIDSS - Influenza (source:
110131
<a target="_blank" href="http://nidss.cdc.gov.tw/en/CDCWNH01.aspx?dc=wnh">nidss.cdc.gov.tw</a>)</label
111132
>
112133
<label
113-
><input class="uk-radio" type="radio" name="dataSource" bind:group={dataSource} value="nidss_dengue" /> NIDSS
114-
- Dengue (source:
134+
><input
135+
class="uk-radio"
136+
type="radio"
137+
name="dataSource"
138+
bind:group={$formSelections.dataSource}
139+
value="nidss_dengue"
140+
/>
141+
NIDSS - Dengue (source:
115142
<a
116143
target="_blank"
117144
href="http://nidss.cdc.gov.tw/en/SingleDisease.aspx?dc=1&amp;dt=4&amp;disease=061&amp;position=1"
118145
>nidss.cdc.gov.tw</a
119146
>)</label
120147
>
121148
<label
122-
><input class="uk-radio" type="radio" name="dataSource" bind:group={dataSource} value="sensors" /> Delphi
123-
Sensors (source: <a target="_blank" href="https://delphi.cmu.edu/">delphi.cmu.edu</a>)</label
149+
><input
150+
class="uk-radio"
151+
type="radio"
152+
name="dataSource"
153+
bind:group={$formSelections.dataSource}
154+
value="sensors"
155+
/>
156+
Delphi Sensors (source: <a target="_blank" href="https://delphi.cmu.edu/">delphi.cmu.edu</a>)</label
124157
>
125158
<label
126-
><input class="uk-radio" type="radio" name="dataSource" bind:group={dataSource} value="nowcast" /> Delphi
127-
Nowcast (source: <a target="_blank" href="https://delphi.cmu.edu/">delphi.cmu.edu</a>)</label
159+
><input
160+
class="uk-radio"
161+
type="radio"
162+
name="dataSource"
163+
bind:group={$formSelections.dataSource}
164+
value="nowcast"
165+
/>
166+
Delphi Nowcast (source: <a target="_blank" href="https://delphi.cmu.edu/">delphi.cmu.edu</a>)</label
128167
>
129168
<label
130-
><input class="uk-radio" type="radio" name="dataSource" bind:group={dataSource} value="covidcast" /> Delphi
131-
COVIDcast (source: <a target="_blank" href="https://delphi.cmu.edu/">delphi.cmu.edu</a>)</label
169+
><input
170+
class="uk-radio"
171+
type="radio"
172+
name="dataSource"
173+
bind:group={$formSelections.dataSource}
174+
value="covidcast"
175+
/>
176+
Delphi COVIDcast (source: <a target="_blank" href="https://delphi.cmu.edu/">delphi.cmu.edu</a>)</label
132177
>
133178
<label
134-
><input class="uk-radio" type="radio" name="dataSource" bind:group={dataSource} value="covid_hosp" /> COVID
135-
Hospitalization (source:
179+
><input
180+
class="uk-radio"
181+
type="radio"
182+
name="dataSource"
183+
bind:group={$formSelections.dataSource}
184+
value="covid_hosp"
185+
/>
186+
COVID Hospitalization (source:
136187
<a
137188
target="_blank"
138189
href="https://healthdata.gov/dataset/covid-19-reported-patient-impact-and-hospital-capacity-state-timeseries"
@@ -142,41 +193,57 @@
142193
</div>
143194
</div>
144195

145-
{#if dataSource === 'fluview'}
196+
{#if $formSelections.dataSource === 'fluview'}
146197
<FluView {id} bind:this={handler} />
147-
{:else if dataSource === 'flusurv'}
198+
{:else if $formSelections.dataSource === 'flusurv'}
148199
<FluSurv {id} bind:this={handler} />
149-
{:else if dataSource === 'gft'}
200+
{:else if $formSelections.dataSource === 'gft'}
150201
<Gft {id} bind:this={handler} />
151-
{:else if dataSource === 'ght'}
202+
{:else if $formSelections.dataSource === 'ght'}
152203
<GHT {id} bind:this={handler} />
153-
{:else if dataSource === 'twitter'}
204+
{:else if $formSelections.dataSource === 'twitter'}
154205
<Twitter {id} bind:this={handler} />
155-
{:else if dataSource === 'wiki'}
206+
{:else if $formSelections.dataSource === 'wiki'}
156207
<Wiki {id} bind:this={handler} />
157-
{:else if dataSource === 'quidel'}
208+
{:else if $formSelections.dataSource === 'quidel'}
158209
<Quidel {id} bind:this={handler} />
159-
{:else if dataSource === 'nidss_dengue'}
210+
{:else if $formSelections.dataSource === 'nidss_dengue'}
160211
<NidssDengue {id} bind:this={handler} />
161-
{:else if dataSource === 'nidss_flu'}
212+
{:else if $formSelections.dataSource === 'nidss_flu'}
162213
<NidssFlu {id} bind:this={handler} />
163-
{:else if dataSource === 'cdc'}
214+
{:else if $formSelections.dataSource === 'cdc'}
164215
<Cdc {id} bind:this={handler} />
165-
{:else if dataSource === 'sensors'}
216+
{:else if $formSelections.dataSource === 'sensors'}
166217
<Sensors {id} bind:this={handler} />
167-
{:else if dataSource === 'nowcast'}
218+
{:else if $formSelections.dataSource === 'nowcast'}
168219
<NowCast {id} bind:this={handler} />
169-
{:else if dataSource === 'covid_hosp'}
220+
{:else if $formSelections.dataSource === 'covid_hosp'}
170221
<CovidHosp {id} bind:this={handler} />
171-
{:else if dataSource === 'covidcast'}
222+
{:else if $formSelections.dataSource === 'covidcast'}
172223
<CoviDcast {id} bind:this={handler} />
173224
{/if}
174225
</form>
175226

176-
<button slot="footer" class="uk-button uk-button-primary" type="submit" form={id} disabled={loading}>
177-
Fetch Data
178-
{#if loading}
179-
<div uk-spinner />
180-
{/if}
181-
</button>
227+
<div slot="footer">
228+
<div class="uk-form-controls uk-form-controls-text container">
229+
<button class="uk-button uk-button-primary" type="submit" form={id} disabled={loading}>
230+
Fetch Data
231+
{#if loading}
232+
<div uk-spinner />
233+
{/if}
234+
</button>
235+
<label
236+
><input class="uk-checkbox" type="checkbox" bind:checked={$storeApiKeys} />
237+
Save API key (auth token) between visits</label
238+
>
239+
</div>
240+
</div>
182241
</Dialog>
242+
243+
<style>
244+
.container {
245+
display: flex;
246+
align-items: center;
247+
column-gap: 2em;
248+
}
249+
</style>

0 commit comments

Comments
 (0)