Add support for fetching range of dates
This commit is contained in:
@@ -22,36 +22,52 @@ export class InfluxDBConsumer extends Consumer<InfluxDBConfig> {
|
||||
|
||||
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));
|
||||
|
||||
const monthlyEnergy = new Point('energy')
|
||||
.timestamp(date.toDate())
|
||||
.tag('profile', 'monthly')
|
||||
.floatField('value', measurement.energyForMonth.sum);
|
||||
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 annualyEnergy = new Point('energy')
|
||||
.timestamp(date.toDate())
|
||||
.tag('profile', 'annualy')
|
||||
.floatField('value', measurement.energyForYear.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 reading = new Point('reading')
|
||||
.timestamp(date.clone().set('hour', 23).set('minute', 59).set('second', 59).toDate())
|
||||
.floatField('value', measurement.reading.C);
|
||||
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")}`)
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
};
|
||||
8
src/utils/index.ts
Normal file
8
src/utils/index.ts
Normal file
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user