Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 4x | import { useQuery } from "react-query";
import { ISnap } from "../types";
type Series = {
name: string;
values: Array<number>;
};
type SnapMetrics = {
series: Array<Series>;
buckets: Array<string>;
daysWithoutData: Array<string>;
};
function useFetchPublishedSnapMetrics(snaps: ISnap[]) {
return useQuery({
queryKey: ["publishedSnapMetrics", snaps],
queryFn: async () => {
const snapList = snaps.reduce(
(acc, item) => {
acc[item.snapName] = item["snap-id"];
return acc;
},
{} as { [name: string]: string },
);
const response = await fetch("/snaps/metrics/json", {
method: "POST",
body: JSON.stringify(snapList),
headers: {
"Content-Type": "application/json",
"X-CSRFToken": window.CSRF_TOKEN,
},
});
if (!response.ok) {
throw new Error("Something went wrong. Please try again later.");
}
const data = await response.json();
const metrics: SnapMetrics = {
series: [],
buckets: data.buckets,
daysWithoutData: data.days_without_data,
};
data.snaps.forEach((snap: { series: Array<Series>; name: string }) => {
const continuedDevices = snap.series.filter(
(singleSeries) => singleSeries.name === "continued",
)[0];
const newDevices = snap.series.filter(
(singleSeries) => singleSeries.name === "new",
)[0];
let totalSeries: Array<number> = [];
if (continuedDevices && newDevices) {
totalSeries = continuedDevices.values.map((continuedValue, index) => {
return continuedValue + newDevices.values[index];
});
}
metrics.series.push({
name: snap.name,
values: totalSeries,
});
});
return metrics;
},
retry: 0,
refetchOnWindowFocus: false,
});
}
export default useFetchPublishedSnapMetrics;
|