-
Notifications
You must be signed in to change notification settings - Fork 5
/
web.js
138 lines (122 loc) · 4.45 KB
/
web.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const Koa = require('koa');
const views = require('koa-views');
const serve = require('koa-static');
const Router = require('koa-router');
const bunyan = require('bunyan');
const bformat = require('bunyan-format');
const formatOut = bformat({ outputMode: 'short' });
const log = bunyan.createLogger({ name: 'web', stream: formatOut, level: 'debug' });
const voca = require('voca');
const moment = require('moment');
const mongoose = require('mongoose');
const path = require('path');
const models = require('./models');
const config = require('./config');
const utils = require('./utils');
const app = new Koa();
const router = new Router();
const tools = { moment, voca, utils };
mongoose.connect(config.database, { useNewUrlParser: true, useUnifiedTopology: true })
.catch((err) => {
log.warn(err.message);
});
app.use(serve(path.join(__dirname, 'public')));
app.use(views(path.join(__dirname, 'views'), { map: { html: 'ejs' } }));
router.get('/', async (ctx, next) => {
const telegrams = await models.Telegram.find({}).limit(10).sort({ timestamp: -1 });
let series1 = [];
let series2 = [];
let series3 = [];
const s1 = await models.Telegram.aggregate(
[
{
$group: {
_id: {
hour: { $hour: '$timestamp' },
},
/* "avgInstCurrent": { "$avg": "$parsedData.objects.instantaneous current L2" },
"minInstCurrent": { "$min": "$parsedData.objects.instantaneous current L2" },
"maxInstCurrent": { "$max": "$parsedData.objects.instantaneous current L2" }, */
avgActElectricitryDelivered: { $avg: '$parsedData.objects.actual electricity power delivered' },
/* "avgTotalImportEnergyQ+": { "$avg": "$parsedData.objects.total imported energy register (Q+)" },
"avgTotalExportEnergyQ-": { "$avg": "$parsedData.objects.total exported energy register (Q-)" },
"avgInstImportedReactivePower": { "$avg": "$parsedData.objects.instantaneous imported reactive power (Q+)" },
"avgInstExportedReactivePower": { "$avg": "$parsedData.objects.instantaneous exported reactive power (Q-)" },
"avgElectricityTariffReceived": { "$avg": "$parsedData.objects.electricity received tariff 0" },
"avgElectricityTariffDelivered": { "$avg": "$parsedData.objects.electricity delivered tariff 0" }, */
},
}],
).exec();
for (const [i, item] of s1.entries()) {
series1.push([
i + 1,
item.avgActElectricitryDelivered,
]);
}
series1 = JSON.stringify(series1);
const s2 = await models.Telegram.aggregate(
[
{
$group: {
_id: {
year: { $year: '$timestamp' },
month: { $month: '$timestamp' },
day: { $dayOfMonth: '$timestamp' },
},
min: { $min: '$parsedData.objects.electricity delivered tariff 0' },
max: { $max: '$parsedData.objects.electricity delivered tariff 0' },
// "avg": { "$avg": "$parsedData.objects.electricity delivered tariff 0" },
},
}],
).exec();
for (const item of s2) {
series2.push([
Date.UTC(item._id.year, item._id.month, item._id.day),
item.max - item.min,
]);
}
series2 = JSON.stringify(series2);
// const s3 = await models.Telegram.aggregate(
// [
// {
// $group: {
// _id: {
// year: { $year: '$timestamp' },
// month: { $month: '$timestamp' },
// day: { $dayOfMonth: '$timestamp' },
// hour: { $hour: '$timestamp' },
// minute: { $minute: '$timestamp' }
// },
// avgActElectricitryDelivered: { $avg: '$parsedData.objects.actual electricity power delivered' },
// },
// }]
// ).exec();
// for (const item of s3) {
// series3.push([
// Date.UTC(item._id.year, item._id.month, item._id.day, item._id.hour, item._id.minute),
// item.max - item.min,
// ]);
// }
// series2 = JSON.stringify(series2);
await ctx.render('dashboard', {
telegrams,
series1,
series2,
...tools,
});
});
router.get('/telegrams', async (ctx, next) => {
const telegrams = await models.Telegram.find({}).limit(10).sort({ timestamp: -1 });
const count = await models.Telegram.countDocuments({});
await ctx.render('telegrams', {
telegrams,
count,
...tools,
});
});
app
.use(router.routes())
.use(router.allowedMethods());
app.listen(config.port, () => {
log.info(`Web interface listening on port ${config.port}`);
});