Add support for time ranged requests
This commit is contained in:
@@ -20,6 +20,9 @@
|
|||||||
default = tauron-scrapper;
|
default = tauron-scrapper;
|
||||||
};
|
};
|
||||||
|
|
||||||
nixosModules.tauron-scrapper = pkgs.callPackage ./module.nix {inherit self;};
|
nixosModules = rec {
|
||||||
|
tauron-scrapper = pkgs.callPackage ./module.nix {inherit self;};
|
||||||
|
default = tauron-scrapper;
|
||||||
|
};
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
17
module.nix
17
module.nix
@@ -16,6 +16,21 @@ in {
|
|||||||
options.services.tauron-scrapper = {
|
options.services.tauron-scrapper = {
|
||||||
enable = mkEnableOption "tauron-scrapper";
|
enable = mkEnableOption "tauron-scrapper";
|
||||||
|
|
||||||
|
cliArgs = mkOption {
|
||||||
|
type = types.attrs;
|
||||||
|
description = "List of CLI arguments. Do not put '-c' argument here as it will be automatically appended basing on the 'config' option.";
|
||||||
|
|
||||||
|
example = {
|
||||||
|
date = "$(date -d '5 days ago' '+%Y-%m-%d')";
|
||||||
|
to = "$(date -d 'yesterday' '+%Y-%m-%d')";
|
||||||
|
};
|
||||||
|
|
||||||
|
default = {
|
||||||
|
date = "$(date -d '5 days ago' '+%Y-%m-%d')";
|
||||||
|
to = "$(date -d 'yesterday' '+%Y-%m-%d')";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
config = mkOption {
|
config = mkOption {
|
||||||
type = types.attr;
|
type = types.attr;
|
||||||
description = "The configuration of tauron-scrapper command";
|
description = "The configuration of tauron-scrapper command";
|
||||||
@@ -93,7 +108,7 @@ in {
|
|||||||
path = [self.packages.${system}.tauron-scrapper coreutils-full];
|
path = [self.packages.${system}.tauron-scrapper coreutils-full];
|
||||||
|
|
||||||
script = ''
|
script = ''
|
||||||
tauron-scrapper -c ${yamlConfig} -d "$(date -d "yesterday" '+%Y-%m-%d')"
|
tauron-scrapper -c ${yamlConfig} ${lib.cli.toGNUCommandLineShell {} cfg.cliArgs}
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ export const run = () => {
|
|||||||
.version('0.0.1')
|
.version('0.0.1')
|
||||||
.requiredOption('-c, --config <file>', 'sets the path to the YAML config file')
|
.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('-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()
|
.parse()
|
||||||
.opts<CLIOptions>();
|
.opts<CLIOptions>();
|
||||||
|
|
||||||
@@ -22,13 +24,28 @@ export const run = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const date = dayjs(options.date, 'YYYY-MM-DD');
|
const date = dayjs(options.date, 'YYYY-MM-DD');
|
||||||
|
const to = options.to && dayjs(options.to, 'YYYY-MM-DD');
|
||||||
|
|
||||||
if (!date.isValid) {
|
if (!date.isValid) {
|
||||||
throw new Error(`Invalid date: ${options.date}, expected date to be of 'YYYY-MM-DD' format`);
|
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 tauron = new Tauron(config.tauron);
|
||||||
const fetcher = new Fetcher(config, tauron);
|
const fetcher = new Fetcher(config, tauron);
|
||||||
|
|
||||||
fetcher.fetch(date);
|
if (!to) {
|
||||||
|
fetcher.fetch(date);
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if(to) {
|
||||||
|
fetcher.fetchRange(date, to, interval);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -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)
|
* @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
|
* @returns the measurement data
|
||||||
*/
|
*/
|
||||||
async fetch(date: Dayjs): Promise<Measurement> {
|
async fetch(date: Dayjs): Promise<Measurement> {
|
||||||
const normalizedDate = date.startOf('day');
|
const normalizedDate = date.startOf('day');
|
||||||
console.log(`Fetching measurements for: ${normalizedDate.format("YYYY-MM-DD")}`);
|
console.log(`Fetching measurements for: ${normalizedDate.format("YYYY-MM-DD")}`);
|
||||||
|
|
||||||
@@ -44,8 +44,9 @@ export class Fetcher {
|
|||||||
return measurement;
|
return measurement;
|
||||||
}
|
}
|
||||||
|
|
||||||
async fetchRange(from: Dayjs, to: Dayjs, requestInterval = 1000) {
|
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);
|
this.fetch(date);
|
||||||
await sleep(requestInterval + gaussianRandom(200, 2000));
|
await sleep(requestInterval + gaussianRandom(200, 2000));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
export type CLIOptions = {
|
export type CLIOptions = {
|
||||||
config: string;
|
config: string;
|
||||||
date: string;
|
date: string;
|
||||||
|
interval: string;
|
||||||
|
to?: string;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user