From 557d9a13d53e6c9508c745db70931726dd56a837 Mon Sep 17 00:00:00 2001 From: Haris Razis Date: Tue, 8 Dec 2020 18:26:27 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9E=95=20Add=20influx=20actions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/actions/influx_actions.js | 42 ++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 server/actions/influx_actions.js diff --git a/server/actions/influx_actions.js b/server/actions/influx_actions.js new file mode 100644 index 0000000..5b6e875 --- /dev/null +++ b/server/actions/influx_actions.js @@ -0,0 +1,42 @@ +const {Point} = require('@influxdata/influxdb-client'); +const chalk = require('chalk') + +const {writeApi, queryApi} = require('../connections/db_conn') +const {bucket} = require('../config/dev') + +write = (pointName, uuid, measurement) => { + const point = new Point(pointName) + .tag('client', uuid) + .floatField('value', measurement); + writeApi.writePoint(point); + writeApi.flush().then(() => { + console.log(chalk.gray('Flushed!')) + }); +} + +closeWrite = () => { + writeApi + .close() + .then(() => { + console.log(chalk.magenta('Write finished')); + }) + .catch((e) => { + console.error(e); + console.log(chalk.red('Write ERROR')); + }); +} + +query = (timeFrame, filter) => { + const query = `from(bucket: "${bucket}") |> range(start: -${timeFrame}) |> group(columns: ["client"]) + |> filter(fn: (r) => r._measurement == "${filter}")`; + + return queryApi + .collectRows(query) + .then(async (result) => { + }) + .catch(() => { + return [{Error: 'Error occured'}]; + }); +} + +module.exports = {write, closeWrite, query}