From 45ffb8bca596ef46d248cc64702ad20eda2d3cdf Mon Sep 17 00:00:00 2001 From: Haris Razis Date: Sun, 3 Jan 2021 11:46:24 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9E=95=20Add=20API=20routes=20for=20athlete?= =?UTF-8?q?=20data?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/services/socket.js | 2 +- docker-compose.yml | 2 + influxdb.conf | 157 +++++++++++++++++++++++++++++++ server/actions/influx_actions.js | 13 +-- server/routes/data.js | 24 ++++- server/services/socket.js | 7 +- 6 files changed, 189 insertions(+), 16 deletions(-) create mode 100644 influxdb.conf diff --git a/client/services/socket.js b/client/services/socket.js index 19c747d..307f3c6 100644 --- a/client/services/socket.js +++ b/client/services/socket.js @@ -30,6 +30,6 @@ socket.on('closeConn', () => { }); setInterval(() => { - socket.emit('data', {measurement: 123, pointName: 'hey-ho'}); + socket.emit('data', {measurement: 5, mac, pointName: 'leg-measurement'}); }, 3 * 1000); diff --git a/docker-compose.yml b/docker-compose.yml index a447f1d..20d51c7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,6 +10,8 @@ services: container_name: "influx" environment: - INFLUXDB_DB=db0 + volumes: + - ./influxdb.conf:/etc/influxdb/influxdb.conf networks: - backend - monitor diff --git a/influxdb.conf b/influxdb.conf new file mode 100644 index 0000000..67b7a50 --- /dev/null +++ b/influxdb.conf @@ -0,0 +1,157 @@ +reporting-disabled = false +bind-address = "127.0.0.1:8088" + +[meta] + dir = "/var/lib/influxdb/meta" + retention-autocreate = true + logging-enabled = true + +[data] + dir = "/var/lib/influxdb/data" + index-version = "inmem" + wal-dir = "/var/lib/influxdb/wal" + wal-fsync-delay = "0s" + validate-keys = false + query-log-enabled = true + cache-max-memory-size = 1073741824 + cache-snapshot-memory-size = 26214400 + cache-snapshot-write-cold-duration = "10m0s" + compact-full-write-cold-duration = "4h0m0s" + compact-throughput = 50331648 + compact-throughput-burst = 50331648 + max-series-per-database = 1000000 + max-values-per-tag = 100000 + max-concurrent-compactions = 0 + max-index-log-file-size = 1048576 + series-id-set-cache-size = 100 + series-file-max-concurrent-snapshot-compactions = 0 + trace-logging-enabled = false + tsm-use-madv-willneed = false + +[coordinator] + write-timeout = "10s" + max-concurrent-queries = 0 + query-timeout = "0s" + log-queries-after = "0s" + max-select-point = 0 + max-select-series = 0 + max-select-buckets = 0 + +[retention] + enabled = true + check-interval = "30m0s" + +[shard-precreation] + enabled = true + check-interval = "10m0s" + advance-period = "30m0s" + +[monitor] + store-enabled = true + store-database = "_internal" + store-interval = "10s" + +[subscriber] + enabled = true + http-timeout = "30s" + insecure-skip-verify = false + ca-certs = "" + write-concurrency = 40 + write-buffer-size = 1000 + +[http] + enabled = true + bind-address = ":8086" + auth-enabled = false + log-enabled = true + suppress-write-log = false + write-tracing = false + flux-enabled = true + flux-log-enabled = true + pprof-enabled = true + pprof-auth-enabled = false + debug-pprof-enabled = false + ping-auth-enabled = false + prom-read-auth-enabled = false + https-enabled = false + https-certificate = "/etc/ssl/influxdb.pem" + https-private-key = "" + max-row-limit = 0 + max-connection-limit = 0 + shared-secret = "" + realm = "InfluxDB" + unix-socket-enabled = false + unix-socket-permissions = "0777" + bind-socket = "/var/run/influxdb.sock" + max-body-size = 25000000 + access-log-path = "" + max-concurrent-write-limit = 0 + max-enqueued-write-limit = 0 + enqueued-write-timeout = 30000000000 + +[logging] + format = "auto" + level = "info" + suppress-logo = false + +[[graphite]] + enabled = false + bind-address = ":2003" + database = "graphite" + retention-policy = "" + protocol = "tcp" + batch-size = 5000 + batch-pending = 10 + batch-timeout = "1s" + consistency-level = "one" + separator = "." + udp-read-buffer = 0 + +[[collectd]] + enabled = false + bind-address = ":25826" + database = "collectd" + retention-policy = "" + batch-size = 5000 + batch-pending = 10 + batch-timeout = "10s" + read-buffer = 0 + typesdb = "/usr/share/collectd/types.db" + security-level = "none" + auth-file = "/etc/collectd/auth_file" + parse-multivalue-plugin = "split" + +[[opentsdb]] + enabled = false + bind-address = ":4242" + database = "opentsdb" + retention-policy = "" + consistency-level = "one" + tls-enabled = false + certificate = "/etc/ssl/influxdb.pem" + batch-size = 1000 + batch-pending = 5 + batch-timeout = "1s" + log-point-errors = true + +[[udp]] + enabled = false + bind-address = ":8089" + database = "udp" + retention-policy = "" + batch-size = 5000 + batch-pending = 10 + read-buffer = 0 + batch-timeout = "1s" + precision = "" + +[continuous_queries] + log-enabled = true + enabled = true + query-stats-enabled = false + run-interval = "1s" + +[tls] + min-version = "" + max-version = "" + diff --git a/server/actions/influx_actions.js b/server/actions/influx_actions.js index bd6261f..7b0e282 100644 --- a/server/actions/influx_actions.js +++ b/server/actions/influx_actions.js @@ -2,7 +2,6 @@ const {Point} = require('@influxdata/influxdb-client'); const chalk = require('chalk') const {writeApi, queryApi} = require('../connections/influx_conn') -const {bucket} = require('../config/keys') iWrite = (pointName, uuid, measurement) => { const point = new Point(pointName) @@ -25,17 +24,15 @@ closeWrite = () => { }); } -iPoint = (timeFrame, filter) => { - const query = `from(bucket: "${bucket}") |> range(start: -${timeFrame}) |> group(columns: ["client"]) - |> filter(fn: (r) => r._measurement == "${filter}")`; - +iQuery = (query) => { return queryApi .collectRows(query) - .then(async (result) => { + .then((result) => { + return result; }) - .catch(() => { + .catch((err) => { return [{Error: 'Error occurred'}]; }); } -module.exports = {iWrite, closeWrite, iPoint} +module.exports = {iWrite, closeWrite, iQuery} diff --git a/server/routes/data.js b/server/routes/data.js index 68a0541..76ffed5 100644 --- a/server/routes/data.js +++ b/server/routes/data.js @@ -1,9 +1,27 @@ const express = require('express') const router = express.Router(); +const mongoose = require('mongoose'); const {requireAuth} = require('../middlewares/middleware'); -router.get('/yoda', requireAuth, (req, res) => { - res.send('Become powerful you have, the dark side in you I sense. Yrsssss.'); -}) +const Athlete = mongoose.model('Athlete'); +const {influx_bucket} = require('../config/keys') +const {iQuery} = require('../actions/influx_actions') + +router.get('/api/data', requireAuth, async (req, res) => { + const query = `from(bucket: "${influx_bucket}") |> range(start: -1h)`; + const data = await iQuery(query); + + res.send(data); +}); + +router.get('/api/data/:id', requireAuth, async (req, res) => { + const athlete = await Athlete.findById(req.params.id); + + const query = `from(bucket: "${influx_bucket}") |> range(start: -1h) |> filter(fn: (r) => r.client == "${athlete.id}")`; + const data = await iQuery(query); + + res.send(data) +}); + module.exports = router; diff --git a/server/services/socket.js b/server/services/socket.js index f71a75c..4e2bf4e 100644 --- a/server/services/socket.js +++ b/server/services/socket.js @@ -3,9 +3,8 @@ const redisAdapter = require('socket.io-redis'); const mongoose = require('mongoose'); const chalk = require('chalk'); -const {pub, sub} = require('../connections/redis_conn') const {saveAthlete} = require('../actions/mongo_actions') -const {iWrite, closeWrite, iQuery} = require('../actions/influx_actions') +const {iWrite} = require('../actions/influx_actions') const Athlete = mongoose.model('Athlete'); module.exports = (server) => { @@ -38,10 +37,10 @@ module.exports = (server) => { }); socket.on('data', (data) => { - const {measurement, pointName} = data; + const {measurement, pointName, mac} = data; io.emit('console', {measurement}) - iWrite(pointName, socket.id, measurement) + iWrite(pointName, mac, measurement) }); });