-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathuse-stac-metadata-datasets.ts
202 lines (181 loc) · 6.05 KB
/
use-stac-metadata-datasets.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import {
useQueries,
UseQueryOptions,
UseQueryResult
} from '@tanstack/react-query';
import axios from 'axios';
import {
StacDatasetData,
TimeDensity,
TimelineDataset,
DatasetStatus,
VizDataset
} from '../types.d.ts';
import { resolveLayerTemporalExtent } from '../data-utils';
import { useEffectPrevious } from '$utils/use-effect-previous';
import { SetState } from '$types/aliases';
function didDataChange(curr: UseQueryResult, prev?: UseQueryResult) {
const currKey = `${curr.errorUpdatedAt}-${curr.dataUpdatedAt}-${curr.failureCount}`;
const prevKey = `${prev?.errorUpdatedAt}-${prev?.dataUpdatedAt}-${prev?.failureCount}`;
return prevKey !== currKey;
}
/**
* Merges STAC metadata with local dataset, computing the domain.
*
* @param queryData react-query response with data from STAC request
* @param dataset Local dataset data.
*
* @returns Reconciled dataset with STAC data.
*/
function reconcileQueryDataWithDataset(
queryData: UseQueryResult<StacDatasetData>,
dataset: TimelineDataset | VizDataset
): TimelineDataset | VizDataset {
try {
let base = {
...dataset,
status: queryData.status as DatasetStatus,
error: queryData.error
};
if (queryData.status === DatasetStatus.SUCCESS) {
const domain = resolveLayerTemporalExtent(base.data.id, queryData.data);
base = {
...base,
data: {
...base.data,
...queryData.data,
domain
}
};
}
return base as TimelineDataset | VizDataset;
} catch (error) {
const e = new Error('Error reconciling query data with dataset');
// @ts-expect-error detail is not a property of Error
e.detail = error;
return {
...dataset,
status: DatasetStatus.ERROR,
error: e
} as TimelineDataset | VizDataset;
}
}
async function fetchStacDatasetById(
dataset: TimelineDataset | VizDataset
): Promise<StacDatasetData> {
const { type, stacCol, stacApiEndpoint, time_density } = dataset.data;
const stacApiEndpointToUse = stacApiEndpoint ?? process.env.API_STAC_ENDPOINT;
const { data } = await axios.get(
`${stacApiEndpointToUse}/collections/${stacCol}`
);
const commonTimeseriesParams = {
isPeriodic: !!data['dashboard:is_periodic'],
// priority is given to time density in dataset configuration, then metadata in STAC, and falling back to DAY if neither is present
timeDensity: time_density || data['dashboard:time_density'] || TimeDensity.DAY
};
if (type === 'vector') {
const featuresApiEndpoint = data.links.find(
(l) => l.rel === 'external'
).href;
const { data: featuresApiData } = await axios.get(featuresApiEndpoint);
return {
...commonTimeseriesParams,
domain: featuresApiData.extent.temporal.interval[0]
};
} else {
const domain = data.summaries?.datetime?.[0]
? data.summaries.datetime
: data.extent.temporal.interval[0];
// STAC may return datetimes with `null` as the last value to indicate ongoing data.
const domain_length = domain.length;
if (domain[domain_length - 1] == null) {
domain[domain_length - 1] = new Date().toISOString();
}
if (!domain?.length || domain.some((d) => !d)) {
throw new Error('Invalid datetime domain');
}
return {
...commonTimeseriesParams,
domain
};
}
}
// Create a query object for react query.
function makeQueryObject(
dataset: TimelineDataset | VizDataset
): UseQueryOptions<unknown, unknown, StacDatasetData> {
return {
queryKey: ['dataset', dataset.data.id],
queryFn: () => fetchStacDatasetById(dataset),
// This data will not be updated in the context of a browser session, so it is
// safe to set the staleTime to Infinity. As specified by react-query's
// "Important Defaults", cached data is considered stale which means that
// there would be a constant refetching.
staleTime: Infinity,
// Errors are always considered stale. If any layer errors, any refocus would
// cause a refetch. This is normally a good thing but since we have a refetch
// button, this is not needed.
refetchOnMount: false,
refetchOnReconnect: false,
refetchOnWindowFocus: false
};
}
/**
* Extends local dataset state with STAC metadata.
* Whenever a dataset is added to the timeline, this hook will fetch the STAC
* metadata for that dataset and add it to the dataset state atom.
*/
export function useReconcileWithStacMetadata(
datasets: TimelineDataset[] | VizDataset[],
handleUpdate: SetState<TimelineDataset[] | VizDataset[] | undefined>
) {
const noDatasetsToQuery: boolean =
!datasets || (datasets.length === 1 && datasets[0] === undefined);
const datasetsQueryData = useQueries({
queries: noDatasetsToQuery
? []
: datasets
.filter((d) => !(d as any)?.mocked)
.map((dataset) => makeQueryObject(dataset))
});
useEffectPrevious<
[typeof datasetsQueryData, TimelineDataset[] | VizDataset[]]
>(
(prev) => {
if (noDatasetsToQuery) return;
const prevQueryData = prev[0];
const hasPrev = !!prevQueryData;
const { updated, data: updatedDatasets } = datasets
.filter((d) => !(d as any)?.mocked)
.reduce<{
updated: boolean;
data: TimelineDataset[] | VizDataset[];
}>(
(acc, dataset, idx) => {
const curr = datasetsQueryData[idx];
// We want to reconcile the data event if it is the first time.
// In practice data will have changes, since prev is undefined.
if (!hasPrev || didDataChange(curr, prevQueryData[idx])) {
return {
updated: true,
data: [
...acc.data,
reconcileQueryDataWithDataset(curr, dataset)
]
};
} else {
return {
...acc,
data: [...acc.data, dataset]
};
}
},
{ updated: false, data: [] }
);
if (updated) {
handleUpdate(updatedDatasets);
}
},
[datasetsQueryData, datasets]
);
}