Skip to content

Commit

Permalink
feat: add influxdb
Browse files Browse the repository at this point in the history
  • Loading branch information
Trickfilm400 committed Oct 1, 2024
1 parent 0c24f3f commit 5854498
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"author": "Trickfilm400",
"license": "ISC",
"dependencies": {
"@influxdata/influxdb-client": "^1.35.0",
"convict": "^6.2.4",
"mqtt": "^5.10.1",
"mysql": "^2.18.1",
Expand Down
32 changes: 32 additions & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,38 @@ const config = convict({
env: 'VANTAGE_PORT',
},
},
influxdb: {
enabled: {
doc: 'if influxdb should be used',
format: Boolean,
default: false,
env: 'INFLUXDB_ENABLED',
},
url: {
doc: 'The URL Endpoint for InfluxDB',
format: String,
default: '',
env: 'INFLUXDB_URL',
},
api_token: {
doc: 'The API Token for InfluxDB',
format: String,
default: '',
env: 'INFLUXDB_API_TOKEN',
},
organisation: {
doc: 'The organisation for InfluxDB',
format: String,
default: '',
env: 'INFLUXDB_ORGANISATION',
},
bucket: {
doc: 'The bucket for InfluxDB',
format: String,
default: '',
env: 'INFLUXDB_BUCKET',
},
},
mysql: {
enabled: {
doc: 'The IP address Mysql should connect to',
Expand Down
38 changes: 38 additions & 0 deletions src/dataHandler/influxdb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { DataReceiver } from '../lib/dataReceiver';
import { DataPackage } from '../interfaces/IPackage';
import { InfluxDB, Point, WriteApi } from '@influxdata/influxdb-client';
import config from '../config';
import { logger } from '../core/logger';

export class Influxdb extends DataReceiver<DataPackage> {
private writerClient: WriteApi | undefined;
constructor() {
super();
if (config.get('influxdb.enabled')) {
const influxDB = new InfluxDB({
url: config.get('influxdb.url'),
token: config.get('influxdb.api_token'),
});
this.writerClient = influxDB.getWriteApi(
config.get('influxdb.organisation'),
config.get('influxdb.bucket')
);
}
}
onData(data: DataPackage): void {
logger.debug('Influxdb data', data);
if (!this.writerClient) return;

let key: keyof DataPackage;
const points: Point[] = [];
for (key in data) {
const point = new Point(key);
point.floatField('value', data[key]);
points.push(point);
}
this.writerClient.writePoints(points);
}
cleanup() {
this.writerClient?.close();
}
}
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import Mqtt from './dataHandler/mqtt';
const mqtt = new Mqtt();
import { Prometheus } from './dataHandler/prometheus';
const prometheus = new Prometheus();
import { Influxdb } from './dataHandler/influxdb';
const influxdb = new Influxdb();

process.on('SIGINT', () => {
console.log('\n');
Expand All @@ -24,6 +26,7 @@ process.on('SIGINT', () => {
.then(() => socket.cleanup())
.then(() => mqtt.cleanup())
.then(() => prometheus.cleanup())
.then(() => influxdb.cleanup())
.then(() => httpServer.cleanup())
.then(() => {
process.exit(0);
Expand Down

0 comments on commit 5854498

Please sign in to comment.