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 api = db.getWriteApi(organization, bucket);
|
||||||
|
|
||||||
const hourlyEnergy = measurement.energyForDay.values.map((value, index) => new Point('energy')
|
measurement.energyForDay.values
|
||||||
.timestamp(date.clone().set('hour', index+1).toDate())
|
.filter(v => {
|
||||||
.tag('profile', 'hourly')
|
if (!v) {
|
||||||
.floatField('value', value)
|
console.warn(`No hourly energy value for ${date.format("YYYY-MM-DD")}`);
|
||||||
);
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
const dailyEnergy = new Point('energy')
|
return true;
|
||||||
.timestamp(date.toDate())
|
})
|
||||||
.tag('profile', 'daily')
|
.map((value, index) => new Point('energy')
|
||||||
.floatField('value', measurement.energyForDay.sum);
|
.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')
|
if(measurement.energyForMonth.sum) {
|
||||||
.timestamp(date.toDate())
|
const data = new Point('energy')
|
||||||
.tag('profile', 'monthly')
|
.timestamp(date.toDate())
|
||||||
.floatField('value', measurement.energyForMonth.sum);
|
.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')
|
if(measurement.energyForYear.sum) {
|
||||||
.timestamp(date.toDate())
|
const data = new Point('energy')
|
||||||
.tag('profile', 'annualy')
|
.timestamp(date.toDate())
|
||||||
.floatField('value', measurement.energyForYear.sum);
|
.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')
|
if(measurement.reading.C) {
|
||||||
.timestamp(date.clone().set('hour', 23).set('minute', 59).set('second', 59).toDate())
|
const data = new Point('reading')
|
||||||
.floatField('value', measurement.reading.C);
|
.timestamp(date.hour(23).minute(59).second(59).toDate())
|
||||||
|
.floatField('value', measurement.reading.C);
|
||||||
hourlyEnergy.forEach(p => api.writePoint(p));
|
api.writePoint(data);
|
||||||
api.writePoint(dailyEnergy);
|
} else console.warn(`No reading data value for ${date.format("YYYY-MM-DD")}`)
|
||||||
api.writePoint(monthlyEnergy);
|
|
||||||
api.writePoint(annualyEnergy);
|
|
||||||
api.writePoint(reading);
|
|
||||||
|
|
||||||
api.close();
|
api.close();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ import { Measurement } from "@types";
|
|||||||
import { type Tauron } from "../tauron";
|
import { type Tauron } from "../tauron";
|
||||||
import { consume } from "../consumers";
|
import { consume } from "../consumers";
|
||||||
import { Config } from "../config";
|
import { Config } from "../config";
|
||||||
|
import { gaussianRandom, sleep } from "../utils";
|
||||||
|
|
||||||
|
|
||||||
export class Fetcher {
|
export class Fetcher {
|
||||||
#config: Config;
|
#config: Config;
|
||||||
@@ -41,4 +43,11 @@ export class Fetcher {
|
|||||||
|
|
||||||
return measurement;
|
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