From 58e5ac0b2465acf233206e122205648416acd262 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Pluta?= Date: Thu, 14 Nov 2024 18:27:43 +0100 Subject: [PATCH] Add support for fetching range of dates --- src/consumers/influxdb.ts | 68 ++++++++++++++++++++++++--------------- src/fetcher/index.ts | 11 ++++++- src/utils/index.ts | 8 +++++ 3 files changed, 60 insertions(+), 27 deletions(-) create mode 100644 src/utils/index.ts diff --git a/src/consumers/influxdb.ts b/src/consumers/influxdb.ts index 7a0a75a..104d1ad 100644 --- a/src/consumers/influxdb.ts +++ b/src/consumers/influxdb.ts @@ -22,36 +22,52 @@ export class InfluxDBConsumer extends Consumer { const api = db.getWriteApi(organization, bucket); - const hourlyEnergy = measurement.energyForDay.values.map((value, index) => new Point('energy') - .timestamp(date.clone().set('hour', index+1).toDate()) - .tag('profile', 'hourly') - .floatField('value', value) - ); + measurement.energyForDay.values + .filter(v => { + if (!v) { + console.warn(`No hourly energy value for ${date.format("YYYY-MM-DD")}`); + return false; + } - const dailyEnergy = new Point('energy') - .timestamp(date.toDate()) - .tag('profile', 'daily') - .floatField('value', measurement.energyForDay.sum); + return true; + }) + .map((value, index) => new Point('energy') + .timestamp(date.startOf('day').hour(index + 1).minute(0).toDate()) + .tag('profile', 'hourly') + .floatField('value', value) + ) + .forEach(p => api.writePoint(p)); + + if(measurement.energyForDay.sum) { + const data = new Point('energy') + .timestamp(date.toDate()) + .tag('profile', 'daily') + .floatField('value', measurement.energyForDay.sum); + api.writePoint(data); + } else console.warn(`No daily energy value for ${date.format("YYYY-MM-DD")}`) - const monthlyEnergy = new Point('energy') - .timestamp(date.toDate()) - .tag('profile', 'monthly') - .floatField('value', measurement.energyForMonth.sum); + if(measurement.energyForMonth.sum) { + const data = new Point('energy') + .timestamp(date.toDate()) + .tag('profile', 'monthly') + .floatField('value', measurement.energyForMonth.sum); + api.writePoint(data); + } else console.warn(`No monthly energy value for ${date.format("YYYY-MM-DD")}`) - const annualyEnergy = new Point('energy') - .timestamp(date.toDate()) - .tag('profile', 'annualy') - .floatField('value', measurement.energyForYear.sum); + if(measurement.energyForYear.sum) { + const data = new Point('energy') + .timestamp(date.toDate()) + .tag('profile', 'annualy') + .floatField('value', measurement.energyForYear.sum); + api.writePoint(data); + } else console.warn(`No anually energy value for ${date.format("YYYY-MM-DD")}`) - const reading = new Point('reading') - .timestamp(date.clone().set('hour', 23).set('minute', 59).set('second', 59).toDate()) - .floatField('value', measurement.reading.C); - - hourlyEnergy.forEach(p => api.writePoint(p)); - api.writePoint(dailyEnergy); - api.writePoint(monthlyEnergy); - api.writePoint(annualyEnergy); - api.writePoint(reading); + if(measurement.reading.C) { + const data = new Point('reading') + .timestamp(date.hour(23).minute(59).second(59).toDate()) + .floatField('value', measurement.reading.C); + api.writePoint(data); + } else console.warn(`No reading data value for ${date.format("YYYY-MM-DD")}`) api.close(); } diff --git a/src/fetcher/index.ts b/src/fetcher/index.ts index 11d8081..e07005d 100644 --- a/src/fetcher/index.ts +++ b/src/fetcher/index.ts @@ -3,6 +3,8 @@ import { Measurement } from "@types"; import { type Tauron } from "../tauron"; import { consume } from "../consumers"; import { Config } from "../config"; +import { gaussianRandom, sleep } from "../utils"; + export class Fetcher { #config: Config; @@ -41,4 +43,11 @@ export class Fetcher { return measurement; } -}; \ No newline at end of file + + async fetchRange(from: Dayjs, to: Dayjs, requestInterval = 1000) { + for(let date = from; date.isBefore(to); date = date.add(1, 'day')) { + this.fetch(date); + await sleep(requestInterval + gaussianRandom(200, 2000)); + } + } +}; diff --git a/src/utils/index.ts b/src/utils/index.ts new file mode 100644 index 0000000..ec012d0 --- /dev/null +++ b/src/utils/index.ts @@ -0,0 +1,8 @@ +export const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); + +export const gaussianRandom = (mean = 0, stddev = 1) => { + let u1 = Math.random(); + let u2 = Math.random(); + let z0 = Math.sqrt(-2.0 * Math.log(u1)) * Math.cos(2.0 * Math.PI * u2); + return z0 * stddev + mean; +} \ No newline at end of file