Add support for time ranged requests

This commit is contained in:
2025-01-21 11:33:20 +01:00
parent 8fa8165fdd
commit 04008b5782
3 changed files with 24 additions and 4 deletions

View File

@@ -12,6 +12,8 @@ export const run = () => {
.version('0.0.1')
.requiredOption('-c, --config <file>', 'sets the path to the YAML config file')
.requiredOption('-d, --date <date>', 'sets the date of measurement intended to be fetched (in YYYY-MM-DD format)')
.requiredOption('-i, --interval <value>', 'sets the time interval between consecutive requests of measurement for time ranges (in ms)', "2000")
.option('-t, --to <date>', '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<CLIOptions>();
@@ -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);
}
}

View File

@@ -45,7 +45,8 @@ export class Fetcher {
}
async fetchRange(from: Dayjs, to: Dayjs, requestInterval = 1000) {
for(let date = from; date.isBefore(to); date = date.add(1, 'day')) {
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));
}

View File

@@ -1,4 +1,6 @@
export type CLIOptions = {
config: string;
date: string;
interval: string;
to?: string;
}