forked from GenSpectrum/cov-spectrum-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CaseCountDataset.ts
39 lines (36 loc) · 1.3 KB
/
CaseCountDataset.ts
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
import { Dataset } from './Dataset';
import { CaseCountEntry } from './CaseCountEntry';
import { fetchCaseCounts } from './api';
import { LocationDateSelector } from './LocationDateSelector';
import { AsyncDataset } from './AsyncDataset';
export type CaseCountDataset = Dataset<LocationDateSelector, CaseCountEntry[]>;
export type CaseCountAsyncDataset = AsyncDataset<LocationDateSelector, CaseCountEntry[]>;
export class CaseCountData {
static async fromApi(selector: LocationDateSelector, signal?: AbortSignal): Promise<CaseCountDataset> {
return {
selector: selector,
payload: await fetchCaseCounts(selector, signal),
};
}
static split(
dataset: CaseCountDataset,
getKey: (entry: CaseCountEntry) => string,
getNewSelector: (oldSelector: LocationDateSelector, entry: CaseCountEntry) => LocationDateSelector
): Map<string, CaseCountDataset> {
const map = new Map<string, CaseCountDataset>();
const oldSelector = dataset.selector;
for (let entry of dataset.payload) {
const key = getKey(entry);
if (!map.has(key)) {
const newSelector = getNewSelector(oldSelector, entry);
map.set(key, {
selector: newSelector,
payload: [],
});
}
const d = map.get(key)!;
d.payload.push(entry);
}
return map;
}
}