Skip to content

Commit 8db8233

Browse files
Connected InfluxDB and Grafana
1 parent 91de9ea commit 8db8233

2 files changed

Lines changed: 209 additions & 0 deletions

File tree

services/lumberjack/src/router.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import koaProtobuf from 'koa-protobuf';
2+
import Router from 'koa-router';
3+
4+
const router = new Router();
5+
6+
// Encode outbound protobuf messages.
7+
router.use(koaProtobuf.protobufSender());
8+
9+
router.get('/api/alive', (ctx) => {
10+
ctx.body = 'Yeah, this is kinda meta tho.\n';
11+
});
12+
13+
export default router;

services/lumberjack/src/service.js

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
import Koa from 'koa';
2+
import request from 'superagent';
3+
const Influx = require('influx');
4+
import addProtobuf from 'superagent-protobuf';
5+
6+
import { stats } from './messages';
7+
import { createTimeoutTask } from './common/task';
8+
import koaLogger from './common/koa-logger';
9+
import logger from './common/logger';
10+
import router from './router';
11+
12+
addProtobuf(request);
13+
14+
export default class Service {
15+
/**
16+
* Create a new service.
17+
* @param {Object} options
18+
* @param {string} pinghost
19+
* @param {number} pingport
20+
* @param {string} telemhost
21+
* @param {number} telemport
22+
* @param {string} influxhost
23+
* @param {number} influxport
24+
* @param {???} influxDB
25+
*/
26+
27+
constructor(options) {
28+
this._pingHost = '10.148.67.123';
29+
this._pingPort = 7000;
30+
this._telemHost = '10.148.67.123';
31+
this._telemPort = 5000;
32+
this._influxHost = options.influxHost;
33+
this._influxPort = options.influxPort;
34+
this._influx = null;
35+
}
36+
37+
/** Start the service. */
38+
async start() {
39+
logger.debug('Starting service.');
40+
41+
this._influx = new Influx.InfluxDB({
42+
host: '10.148.67.123', //env variable localhost
43+
port: 8086, //env variable 8086
44+
database: 'lumberjack',
45+
schema: [
46+
{
47+
measurement: 'ping',
48+
fields: {
49+
ping: Influx.FieldType.FLOAT
50+
},
51+
tags: [
52+
'host',
53+
'port'
54+
]
55+
},
56+
{
57+
measurement: 'telemetry',
58+
fields: {
59+
t1: Influx.FieldType.INTEGER,
60+
t5: Influx.FieldType.INTEGER,
61+
f1: Influx.FieldType.INTEGER,
62+
f5: Influx.FieldType.INTEGER
63+
},
64+
tags: [
65+
'host',
66+
'port'
67+
]
68+
}
69+
]
70+
})
71+
72+
/*this._influx.getDatabaseNames()
73+
.then(names => {
74+
if (!names.includes('lumberjack')) {
75+
return this._influx.createDatabase('lumberjack');
76+
}
77+
})
78+
.catch(err => {
79+
logger.debug('Failed to create database');
80+
})*/
81+
this._influx.createDatabase('lumberjack');
82+
83+
this._startTasks();
84+
this.server = await this._createApi();
85+
logger.debug('Service started');
86+
}
87+
88+
/** Stop the service. */
89+
async stop() {
90+
logger.debug('Stopping service.');
91+
92+
await Promise.all([
93+
this._server.closeAsync(),
94+
Promise.all(this._forwardTasks.map(t => t.stop()))
95+
]);
96+
97+
logger.debug('Service stopped.');
98+
}
99+
100+
// Create the koa api and return the http server.
101+
async _createApi() {
102+
const app = new Koa();
103+
104+
app.use(koaLogger());
105+
106+
// Set up the router middleware.
107+
app.use(router.routes());
108+
app.use(router.allowedMethods());
109+
110+
// Start and wait until the server is up and then return it.
111+
return await new Promise((resolve, reject) => {
112+
const server = app.listen(6000, (err) => {
113+
if (err) {
114+
reject(err);
115+
console.log(err);
116+
}
117+
else {
118+
console.log('lmaoooo');
119+
resolve(server);
120+
}
121+
});
122+
123+
server.closeAsync = () => new Promise((resolve) => {
124+
server.close(() => resolve());
125+
});
126+
});
127+
}
128+
129+
130+
131+
_startTasks() {
132+
this._forwardTask =
133+
createTimeoutTask(this._logging.bind(this), 500)
134+
.on('error', () => {
135+
console.log('muppet');
136+
})
137+
.start();
138+
}
139+
140+
// Get telemetry and ping data and send to database
141+
async _logging() {
142+
logger.debug('');
143+
let ping = Math.random()*100+1;
144+
145+
try {
146+
let { body: ping } =
147+
await request.get('http://' + this._pingHost + ':' + this._pingPort + '/api/ping')
148+
.proto(stats.PingTimes)
149+
.timeout(1000);
150+
} catch (err) {
151+
console.log(err);
152+
}
153+
//console.log(ping);
154+
155+
try {
156+
this._influx.writeMeasurement('ping', [
157+
{
158+
fields: { ping: ping },
159+
tags: { host: this._pingHost, port: this._pingPort }
160+
}], {
161+
database: 'lumberjack'
162+
});
163+
} catch (err) {
164+
console.log(err);
165+
}
166+
167+
try {
168+
let { body: total_1, fresh_1, total_5, fresh_5 } =
169+
await request.get('http://' + this._telemHost + ':' + this._telemPort + '/api/upload-rate')
170+
.proto(stats.InteropUploadRate)
171+
.timeout(1000);
172+
} catch (err) {
173+
console.log(err);
174+
}
175+
let total_1 = Math.random()*100+1;
176+
let total_5 = Math.random()*100+1;
177+
let fresh_1 = Math.random()*100+1;
178+
let fresh_5 = Math.random()*100+1;
179+
180+
//console.log(total_1);
181+
//console.log(total_5);
182+
//console.log(fresh_1);
183+
//console.log(fresh_5);
184+
185+
try {
186+
this._influx.writePoints([
187+
{
188+
measurement: 'telemetry',
189+
fields: { t1: total_1, t5: total_5, f1: fresh_1, f5: fresh_5 },
190+
tags: { host: this._telemHost, port: this._telemPort }
191+
}])
192+
} catch (err) {
193+
console.error(err);
194+
}
195+
}
196+
}

0 commit comments

Comments
 (0)