You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.6 KiB
53 lines
1.6 KiB
2 years ago
|
import { fluxDuration, InfluxDB } from "@influxdata/influxdb-client"
|
||
|
import { Point } from "@influxdata/influxdb-client"
|
||
|
import { influxBucket, influxHost, influxOrg, influxPointName, influxToken } from "./constants"
|
||
|
import { influxQuery, processInfluxResult } from "./helpers";
|
||
|
import { InfluxField, InfluxPeriod } from "./types"
|
||
|
|
||
|
interface WriteSitesCounteParams {
|
||
|
all: number;
|
||
|
available: number;
|
||
|
}
|
||
|
|
||
|
interface QueryParams {
|
||
|
field: InfluxField;
|
||
|
period: InfluxPeriod;
|
||
|
}
|
||
|
|
||
|
class InfluxDb {
|
||
|
private client: InfluxDB
|
||
|
constructor() {
|
||
|
this.client = new InfluxDB({ url: process.env.INFLUX_URL as string, token: influxToken })
|
||
|
}
|
||
|
getWriteApi() {
|
||
|
const writeApi = this.client.getWriteApi(influxOrg, influxBucket)
|
||
|
writeApi.useDefaultTags({ host: influxHost })
|
||
|
return writeApi
|
||
|
}
|
||
|
writeSitesCount({all,available}:WriteSitesCounteParams){
|
||
|
const writeApi = this.getWriteApi()
|
||
|
const pointAll = new Point(influxPointName).intField(InfluxField.ALL_SITES,all);
|
||
|
const pointAvailable = new Point(influxPointName).intField(InfluxField.AVAILABLE_SITES,available);
|
||
|
writeApi.writePoint(pointAll)
|
||
|
writeApi.writePoint(pointAvailable)
|
||
|
writeApi.close()
|
||
|
}
|
||
|
async query({field,period}:QueryParams){
|
||
|
const queryApi = this.client.getQueryApi(influxOrg)
|
||
|
|
||
|
const query = influxQuery(field, period);
|
||
|
const result = await queryApi.collectRows(query)
|
||
|
console.log(processInfluxResult(result));
|
||
|
}
|
||
|
getAllSiteCounts(period:InfluxPeriod){
|
||
|
|
||
|
this.query({
|
||
|
field:InfluxField.ALL_SITES,
|
||
|
period
|
||
|
})
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
export default new InfluxDb()
|