From 5cd4bdbb64139105d9f7948e095c2001f6be8733 Mon Sep 17 00:00:00 2001 From: Haris Razis Date: Mon, 23 Nov 2020 10:04:17 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9E=95=20Add=20measurements=20API=20route?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/src/class/Database.ts | 40 ++++++++++++++++++++---------------- server/src/class/Service.ts | 2 +- server/src/routes/Routes.ts | 19 +++++++++++++++++ 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/server/src/class/Database.ts b/server/src/class/Database.ts index ceee4b0..0165138 100644 --- a/server/src/class/Database.ts +++ b/server/src/class/Database.ts @@ -48,25 +48,29 @@ export class Database { }); } - query(filter: string): void { - const query = `from(bucket: "${process.env.DB_BUCKET}") |> range(start: -1h) |> filter(fn: (r) => r._measurement == "${filter}")`; + query(filter: string, timeFrame: string) { + const query = `from(bucket: "${process.env.DB_BUCKET}") |> range(start: -${timeFrame}) |> filter(fn: (r) => r._measurement == "${filter}")`; - this.queryApi.queryRows(query, { - next(row, tableMeta) { - const o = tableMeta.toObject(row); - console.log( - chalk.cyan( - `On ${o._time} took ${o._measurement} of ${o._field}=${o._value}` - ) - ); - }, - error(error) { - console.error(error); - console.log(chalk.red('Query ERROR')); - }, - complete() { - console.log(chalk.magenta('Query finished')); - }, + const data = this.queryApi + .collectRows(query) + .then(async (result) => { + const formatedData = await this.formatData(result); + return formatedData; + }) + .catch(() => { + return [{ Error: 'Error occured' }]; + }); + + return data; + } + + formatData(rawData: any): Array<{}> { + let formatedData: Array<{}> = []; + + rawData.forEach((element: any) => { + formatedData.push({ time: element._time, value: element._value }); }); + + return formatedData; } } diff --git a/server/src/class/Service.ts b/server/src/class/Service.ts index acfefcc..86eb80b 100644 --- a/server/src/class/Service.ts +++ b/server/src/class/Service.ts @@ -3,7 +3,7 @@ import { Database } from './Database'; import { Socket } from './Socket'; export class Service { - private database!: Database; + database!: Database; socket!: Socket; constructor(private http: Server) { diff --git a/server/src/routes/Routes.ts b/server/src/routes/Routes.ts index 5c050e0..5621bb4 100644 --- a/server/src/routes/Routes.ts +++ b/server/src/routes/Routes.ts @@ -1,4 +1,5 @@ import * as express from 'express'; +import { Database } from '../class/Database'; import { Service } from '../class/Service'; export class Routes { @@ -12,6 +13,7 @@ export class Routes { private devices(): void { this.app.get('/devices', async (req, res) => { const data = await this.service.socket.rooms; + res.header('Content-Type', 'application/json'); if (data) { res.send(JSON.stringify(data)); @@ -35,6 +37,22 @@ export class Routes { }); } + private measurements(): void { + this.app.get('/measurements', async (req, res) => { + req.on('data', async (data) => { + const { filter, timeFrame } = JSON.parse(data); + const points = await this.service.database.query('temperature', '5m'); + + res.header('Content-Type', 'application/json'); + if (points) { + res.send(JSON.stringify(points)); + } else { + res.send(JSON.stringify({})); + } + }); + }); + } + private socket(): void { this.app.post('/socket', (req, res) => { req.on('data', async (data) => { @@ -54,6 +72,7 @@ export class Routes { public routes(): void { this.devices(); + this.measurements(); this.socket(); } }