From 6da3187287756cb95099038cef48e568a72c6c9d Mon Sep 17 00:00:00 2001 From: Gerard Molina <47140788+gmolki@users.noreply.github.com> Date: Mon, 25 Mar 2024 14:17:29 +0100 Subject: [PATCH] feat: Always show last two weeks of metrics (#2) * Fetch two last weeks of data ordered ascending * Plot layout shows always 2 last weeks in x Axis --- src/components/MetricsPlot.tsx | 26 +++++++++++++++++++++++++- src/hooks/useFetchMetrics.ts | 14 +++++++++++++- src/pages/NodeMetricsPage.tsx | 26 +++++++++++++++++--------- 3 files changed, 55 insertions(+), 11 deletions(-) diff --git a/src/components/MetricsPlot.tsx b/src/components/MetricsPlot.tsx index 5891ffe..4ba7601 100644 --- a/src/components/MetricsPlot.tsx +++ b/src/components/MetricsPlot.tsx @@ -8,10 +8,34 @@ interface MetricsPlotProps { } const MetricsPlot: React.FC = ({ plotData, layout }) => { + const extendedLayout: Partial = { + ...layout, + xaxis: { + tickformatstops: [ + { + dtickrange: [null, 3600000], // For less than an hour + value: "%H:%M:%S %d/%m/%Y", // Show full date and time + }, + { + dtickrange: [3600000, 86400000], // For an hour to one day + value: "%H:%M %d/%m/%Y", // Show hour and date + }, + { + dtickrange: [86400000, null], // For more than a day + value: "%d/%m/%Y", // Show only date + }, + ], + type: "date", + ...layout.xaxis, + }, + }; + + console.log(extendedLayout); + return ( { setIsLoading(true); const baseUrl = "https://api2.aleph.im/api/v0"; + const endpoint = node.type === "resource" ? `${baseUrl}/compute/${node.hash}/metrics` : `${baseUrl}/core/${node.hash}/metrics`; - fetch(endpoint) + const url = new URL(endpoint, baseUrl); + + const endDate = Math.floor(Date.now() / 1000); // current time (in seconds) + const startDate = endDate - 2 * 7 * 24 * 60 * 60; // 2 weeks from now (in seconds) + + url.search = new URLSearchParams({ + start_date: startDate.toString(), + end_date: endDate.toString(), + sort: "asc", + }).toString(); + + fetch(url.toString()) .then((response) => response.json()) .then((data) => { setMetricData(data.metrics); diff --git a/src/pages/NodeMetricsPage.tsx b/src/pages/NodeMetricsPage.tsx index bfcbf7a..ae1d1ec 100644 --- a/src/pages/NodeMetricsPage.tsx +++ b/src/pages/NodeMetricsPage.tsx @@ -44,7 +44,7 @@ const NodeMetricsPage: React.FC = () => { }; const xAxisData = metricData?.measured_at.map((timestamp) => - new Date(timestamp * 1000).toLocaleString() + new Date(timestamp * 1000).toISOString() ); const plotData: Data[] = React.useMemo(() => { @@ -55,7 +55,7 @@ const NodeMetricsPage: React.FC = () => { if (metricArray && isMetricPresent(metricArray)) { acc.push({ x: xAxisData, - y: metricArray.filter((item) => item !== null) as number[], // Ensure no null values in 'y' + y: metricArray, type: "scatter", mode: "lines+markers", name: name, @@ -66,6 +66,18 @@ const NodeMetricsPage: React.FC = () => { }, []); }, [metricData, xAxisData]); + const plotLayout: Partial = React.useMemo(() => { + if (!xAxisData || !selectedNode) return {}; + + return { + autosize: true, + title: `${selectedNode?.name} Node Metrics`, + xaxis: { + range: [xAxisData[0], xAxisData[xAxisData.length - 1]], + }, + }; + }, [xAxisData, selectedNode]); + return (
{
) : metricData ? ( - + <> + + ) : (
Select a node to see its metrics.