From f025b8940f66072586ed9aa1f58a54c9c9092e23 Mon Sep 17 00:00:00 2001 From: RebeckaGothlin Date: Mon, 17 Mar 2025 12:47:13 +0100 Subject: [PATCH 1/5] add hourly option on downloadcard Co-authored-by: Helena Skagerlid --- src/client/index.tsx | 4 +-- src/components/DownloadCard/index.tsx | 41 ++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/client/index.tsx b/src/client/index.tsx index 7a0917e..80862a0 100644 --- a/src/client/index.tsx +++ b/src/client/index.tsx @@ -51,11 +51,11 @@ async function fetchSensorMeasurementsDownload( return data.results; } -async function fetchSensorMeasurements( +export async function fetchSensorMeasurements( sensorsId: number, datetimeFrom: string, datetimeTo: string, - limit: number + limit?: number ) { 'use server'; const url = new URL(import.meta.env.VITE_API_BASE_URL); diff --git a/src/components/DownloadCard/index.tsx b/src/components/DownloadCard/index.tsx index 5b9a1cb..3af988e 100644 --- a/src/components/DownloadCard/index.tsx +++ b/src/components/DownloadCard/index.tsx @@ -2,7 +2,10 @@ import { createSignal, For } from 'solid-js'; import dayjs from 'dayjs'; import tz from 'dayjs/plugin/timezone'; import utc from 'dayjs/plugin/utc'; -import { getSensorMeasurementsDownload } from '~/client'; +import { + getSensorMeasurementsDownload, + fetchSensorMeasurements, +} from '~/client'; import { useLocation, A } from '@solidjs/router'; @@ -143,6 +146,7 @@ export function DownloadCard(props: Props) { Number(o.id.replace('checkbox-sensor-', '')) ); let data = []; + for (const sensorId of sensorIds) { const measurements = await getSensorMeasurementsDownload( sensorId, @@ -157,6 +161,33 @@ export function DownloadCard(props: Props) { setDownloading(false); }; + const handleChange = async (e) => { + if (e.target.value === 'hourly') { + const startDate = dayjs().subtract(1, 'day').toISOString(); + const endDate = dayjs().toISOString(); + console.log('HEJ PROPS ID', props.id); + console.log('HEJ start date', startDate); + console.log('HEJ end date', endDate); + console.log('HEJ 24', 24); + console.log('HEJ API-anrop görs nu...'); + + try { + const testSensorId = 4275972; + console.log('HEJ testar med hårdkodat sensor-ID:', testSensorId); + const data = await fetchSensorMeasurements( + // props.id, + testSensorId, + startDate, + endDate, + 24 + ); + console.log('HEEEEEEEEJ Hourly data:', data); + } catch (error) { + console.error(' HEJ Error fetching', error); + } + } + }; + return ( <>

Download Data CSV

@@ -180,6 +211,14 @@ export function DownloadCard(props: Props) { onFormSubmit(e); }} > + + +
+
Date: Mon, 17 Mar 2025 15:22:03 +0100 Subject: [PATCH 2/5] wip add rollup hourly Co-authored-by: Helena Skagerlid --- src/client/index.tsx | 12 ++++++-- src/components/DownloadCard/index.tsx | 41 ++++++--------------------- 2 files changed, 19 insertions(+), 34 deletions(-) diff --git a/src/client/index.tsx b/src/client/index.tsx index 80862a0..bb8a354 100644 --- a/src/client/index.tsx +++ b/src/client/index.tsx @@ -25,13 +25,14 @@ export async function fetchLocation(locationsId: number) { async function fetchSensorMeasurementsDownload( sensorsId: number, + rollup: string, datetimeFrom: string, datetimeTo: string, limit: number ) { 'use server'; const url = new URL(import.meta.env.VITE_API_BASE_URL); - url.pathname = `/v3/sensors/${sensorsId}/measurements`; + url.pathname = `/v3/sensors/${sensorsId}/${rollup}`; url.search = `?datetime_from=${datetimeFrom.replace( ' ', '%2b' @@ -53,13 +54,14 @@ async function fetchSensorMeasurementsDownload( export async function fetchSensorMeasurements( sensorsId: number, + rollup: string, datetimeFrom: string, datetimeTo: string, limit?: number ) { 'use server'; const url = new URL(import.meta.env.VITE_API_BASE_URL); - url.pathname = `/v3/sensors/${sensorsId}/hours`; + url.pathname = `/v3/sensors/${sensorsId}/${rollup}`; url.search = `?datetime_from=${datetimeFrom.replace( ' ', '%2b' @@ -117,9 +119,12 @@ async function fetchProviders() { return await res.json(); } +type rollup = 'measurements' | 'hours' | 'days' | 'years'; + export const getSensorMeasurements = GET( async ( sensorsId: number, + rollup: rollup, datetimeFrom: string, datetimeTo: string, limit: number = 1000 @@ -127,6 +132,7 @@ export const getSensorMeasurements = GET( 'use server'; const data = await fetchSensorMeasurements( sensorsId, + rollup, datetimeFrom, datetimeTo, limit @@ -138,6 +144,7 @@ export const getSensorMeasurements = GET( export const getSensorMeasurementsDownload = GET( async ( sensorsId: number, + rollup: string, datetimeFrom: string, datetimeTo: string, limit: number = 1000 @@ -145,6 +152,7 @@ export const getSensorMeasurementsDownload = GET( 'use server'; const data = await fetchSensorMeasurementsDownload( sensorsId, + rollup, datetimeFrom, datetimeTo, limit diff --git a/src/components/DownloadCard/index.tsx b/src/components/DownloadCard/index.tsx index 3af988e..6d9286e 100644 --- a/src/components/DownloadCard/index.tsx +++ b/src/components/DownloadCard/index.tsx @@ -136,9 +136,10 @@ export function DownloadCard(props: Props) { e.preventDefault(); setDownloading(true); const formElements = e.target.elements; - const tz = formElements[0].value; - const dateFromValue = formElements[1].value; - const dateToValue = formElements[2].value; + const rollup = formElements[0].value; + const tz = formElements[1].value; + const dateFromValue = formElements[2].value; + const dateToValue = formElements[3].value; const sensorInputs = [...formElements] .filter((o) => o.id.includes('checkbox-sensor')) .filter((o) => o.checked); @@ -150,44 +151,20 @@ export function DownloadCard(props: Props) { for (const sensorId of sensorIds) { const measurements = await getSensorMeasurementsDownload( sensorId, + rollup, dayjs(new Date(dateFromValue), props.timezone).toISOString(), dayjs(new Date(dateToValue), props.timezone).toISOString() ); data = data.concat(measurements); + console.log('HEJ DATA', data); await sleep(300); } + const csvData = measurementsCsv(props, data); downloadFile(`openaq_location_${props.id}_measurments.csv`, csvData); setDownloading(false); }; - const handleChange = async (e) => { - if (e.target.value === 'hourly') { - const startDate = dayjs().subtract(1, 'day').toISOString(); - const endDate = dayjs().toISOString(); - console.log('HEJ PROPS ID', props.id); - console.log('HEJ start date', startDate); - console.log('HEJ end date', endDate); - console.log('HEJ 24', 24); - console.log('HEJ API-anrop görs nu...'); - - try { - const testSensorId = 4275972; - console.log('HEJ testar med hårdkodat sensor-ID:', testSensorId); - const data = await fetchSensorMeasurements( - // props.id, - testSensorId, - startDate, - endDate, - 24 - ); - console.log('HEEEEEEEEJ Hourly data:', data); - } catch (error) { - console.error(' HEJ Error fetching', error); - } - } - }; - return ( <>

Download Data CSV

@@ -212,8 +189,8 @@ export function DownloadCard(props: Props) { }} > - + From 7ad7f82a04b249f10c786c95e705988f5f53d070 Mon Sep 17 00:00:00 2001 From: RebeckaGothlin Date: Mon, 17 Mar 2025 15:46:32 +0100 Subject: [PATCH 3/5] add rollup for days and years --- src/components/DownloadCard/index.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/DownloadCard/index.tsx b/src/components/DownloadCard/index.tsx index 6d9286e..d0d5738 100644 --- a/src/components/DownloadCard/index.tsx +++ b/src/components/DownloadCard/index.tsx @@ -191,8 +191,8 @@ export function DownloadCard(props: Props) {

From 9e3cc04af2ca4a7736dac14a04ab6e9542134482 Mon Sep 17 00:00:00 2001 From: RebeckaGothlin Date: Mon, 17 Mar 2025 15:48:37 +0100 Subject: [PATCH 4/5] add measurements raw data as option --- src/components/DownloadCard/index.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/components/DownloadCard/index.tsx b/src/components/DownloadCard/index.tsx index d0d5738..afb73ad 100644 --- a/src/components/DownloadCard/index.tsx +++ b/src/components/DownloadCard/index.tsx @@ -190,6 +190,7 @@ export function DownloadCard(props: Props) { > - + -
-