From 04008b578206a260c01854786967f82dd1551ea4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Pluta?= Date: Tue, 21 Jan 2025 11:33:20 +0100 Subject: [PATCH] Add support for time ranged requests --- src/cli/index.ts | 19 ++++++++++++++++++- src/fetcher/index.ts | 7 ++++--- src/types/cli.ts | 2 ++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/cli/index.ts b/src/cli/index.ts index cd76880..a45105d 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -12,6 +12,8 @@ export const run = () => { .version('0.0.1') .requiredOption('-c, --config ', 'sets the path to the YAML config file') .requiredOption('-d, --date ', 'sets the date of measurement intended to be fetched (in YYYY-MM-DD format)') + .requiredOption('-i, --interval ', 'sets the time interval between consecutive requests of measurement for time ranges (in ms)', "2000") + .option('-t, --to ', 'sets the end date of measurement intended to be fetched (in YYYY-MM-DD format). If provided, the -d option acts as a start date.') .parse() .opts(); @@ -22,13 +24,28 @@ export const run = () => { } const date = dayjs(options.date, 'YYYY-MM-DD'); + const to = options.to && dayjs(options.to, 'YYYY-MM-DD'); if (!date.isValid) { throw new Error(`Invalid date: ${options.date}, expected date to be of 'YYYY-MM-DD' format`); } + if (to && !to.isValid) { + throw new Error(`Invalid 'to' date: ${options.to}, expected date to be of 'YYYY-MM-DD' format`); + } + + const interval = parseInt(options.interval); + + const tauron = new Tauron(config.tauron); const fetcher = new Fetcher(config, tauron); - fetcher.fetch(date); + if (!to) { + fetcher.fetch(date); + return + } + + if(to) { + fetcher.fetchRange(date, to, interval); + } } \ No newline at end of file diff --git a/src/fetcher/index.ts b/src/fetcher/index.ts index e07005d..910c6f8 100644 --- a/src/fetcher/index.ts +++ b/src/fetcher/index.ts @@ -20,7 +20,7 @@ export class Fetcher { * @param date - the measurement date (note, that 'todays' date may not be available at the time being when request is made) * @returns the measurement data */ - async fetch(date: Dayjs): Promise { + async fetch(date: Dayjs): Promise { const normalizedDate = date.startOf('day'); console.log(`Fetching measurements for: ${normalizedDate.format("YYYY-MM-DD")}`); @@ -44,8 +44,9 @@ export class Fetcher { return measurement; } - async fetchRange(from: Dayjs, to: Dayjs, requestInterval = 1000) { - for(let date = from; date.isBefore(to); date = date.add(1, 'day')) { + async fetchRange(from: Dayjs, to: Dayjs, requestInterval = 1000) { + console.log(`Fetching data from time range: ${from.format("MM.DD.YYYY")} - ${to.format("MM.DD.YYYY")}`) + for(let date = from; date.isBefore(to.add(1, 'day')); date = date.add(1, 'day')) { this.fetch(date); await sleep(requestInterval + gaussianRandom(200, 2000)); } diff --git a/src/types/cli.ts b/src/types/cli.ts index d8a4b7c..cef100b 100644 --- a/src/types/cli.ts +++ b/src/types/cli.ts @@ -1,4 +1,6 @@ export type CLIOptions = { config: string; date: string; + interval: string; + to?: string; } \ No newline at end of file