-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·288 lines (222 loc) · 7.8 KB
/
app.js
File metadata and controls
executable file
·288 lines (222 loc) · 7.8 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
node app.js
*/
// Requirements
var pug = require("pug")
var express = require("express");
var app = express();
var router = express.Router();
var bodyParser = require('body-parser');
var Slack = require("slack-node");
var sqlite3 = require('sqlite3').verbose();
var socketio = require('socket.io');
var maxID = 0;
var currentTime=new Date();
// Slack variables
var slack = new Slack();
var webhookUri = "https://hooks.slack.com/services/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";
slack.setWebhook(webhookUri);
var slackChannel = "#general"
// SQLite3 database variables
var db = new sqlite3.Database('privat/testDB.db');
// View templating engine
app.set('views', __dirname + '/views');
app.set('view engine', 'pug');
// Console logiranje requesta
router.use(function (req,res,next) {
console.log("[>>] " + currentTime + "[>>] Request /" + req.method);
next();
});
// Open listener
var io = socketio.listen('3020', function (err, msg) {
if (err) {
console.error(err);
}
});
// logging connection
io.on('connection', function (socket) {
console.log('[>>] Client connected');
socket.on('disconnect', function () {
console.log('[>>] Client disconnected');
});
});
// load index.html document
router.get("/",function(req,res){
res.sendFile(__dirname + "/public/index.html");
console.log("[>>] " + currentTime + "Requested -- index.html");
});
// Request counter API
// GET /requestCount to recieve number of total requests in log
router.get("/requestCount", function(req, res){
db.serialize(function () {
db.all("select count(*) from log", function(err, row){
if(err) throw err;
console.log(err);
//res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(row));
})
})
})
// TOP 5 in log API
router.get("/latest", function(req, res){
db.serialize(function () {
db.get("SELECT *" +
+"FROM log " +
+"WHERE id IS NOT null" +
+"ORDER BY id DESC LIMIT 3",
function(err, row){
if(err) throw err;
console.log(err);
//res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(row));
io.emit('latest', JSON.stringify(row));
})
})
})
// > GET /ClientList
// < returns unique client list alphabetically
router.get("/clientList", function(req, res){
db.serialize(function () {
db.all("SELECT DISTINCT client FROM log ORDER BY client", function(err, row){
if(err) throw err;
console.log(err);
//res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(row));
})
})
})
// last client to get in touch
router.get("/clientLastSync", function(req, res){
db.serialize(function () {
db.all("select * from log where service not like \"test\" order by id desc limit 100", function(err, row){
if(err) throw err;
console.log(err);
//res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify(row));
})
})
})
router.get("/details/:client",function(req,res){
res.sendFile(__dirname + "/public/clientDetails.html");
//var user = "davor";
//res.render( '/public/clientDetails.html', { user: user } );
console.log("[>>] " + currentTime + "Requested -- clientDetails.html");
var db_query = "SELECT * FROM LOG WHERE CLIENT ='" + req.params.client +"' ORDER BY ID DESC LIMIT 25"
// // get from database?
// db.serialize(function () {
// })
});
router.get("/view/details/:client",function(req,res){
console.log(dateNow() + " -- clientDetails.html + db query");
var db_query = "SELECT * FROM LOG WHERE CLIENT =\'" + req.params.client +"\' ORDER BY ID DESC LIMIT 25"
console.log(db_query)
db.serialize(function () {
db.all(db_query, function (err, row) {
if(err) throw err;
//console.log(row);
console.log("[>>] " + currentTime + " Requested Client Details ")
var x = JSON.stringify(row);
res.send(JSON.stringify(row));
//io.emit('clientDetails', x);
});
})
});
// About link
router.get("/about",function(req,res){
res.sendFile(__dirname + "/public/about.html");
});
// Registriraj CLIENT, SERVICE i STATUS
//
//
var vrijednost = 0;
router.get("/api/:client/:service/:status",function(req,res)
{
// res.json({"message" : "Hello "+req.params.client + req.params.service + req.params.status});
res.send('OK');
db.serialize(function () {
// Upisati novu poruku remote clienta u bazu
db.get("select count(*) from log", function(err, row) {
if (err) throw err;
for (key in row){
vrijednost = row[key];
console.log("[>>] maxID: " + vrijednost)
}
});
//console.log(JSON.stringify(value));
db.run("INSERT INTO log VALUES (?, ?, ?, ?, (select datetime(\"now\")))", [ vrijednost, req.params.client, req.params.service, req.params.status ]);
console.log("[>>] " + currentTime + " << INSERT INTO status servisa " + vrijednost);
query = "SELECT * FROM LOG WHERE ID IS NOT NULL ORDER BY ID desc limit 1"
db.each(query, function (err, row) {
if(err) throw err;
console.log("[>>] SQL: " + query)
// log input SQL values
// console.log(row);
var x = JSON.stringify(row);
io.emit('logline', JSON.stringify(row));
});
db.all("SELECT id FROM LOG WHERE ID IS NOT NULL order by id desc limit 1 ", function (err, row) {
if (err) throw err;
var jstfy = JSON.stringify(row);
//JSON.stringify(row, '"'replace?:'' any, space?: any)
console.log("[>>] Average counter: " + jstfy);
io.emit("dashAvg", row);
})
// API za provjeru backupa u posljednja 24 sata
// izdvojiti u posebnu funkciju koja listena server kada ima novosti
// a.k.a. subscription // sada se vrti pri svakom insertu i osvježi stats
db.all("SELECT id as t FROM LOG WHERE ID IS NOT NULL limit 1", function(err, row) {
if (err) throw err;
console.log("[>>] postotak backupa u zadnja 24 sata: " + JSON.stringify(row));
var postotak = [];
postotak = row;
console.log(postotak)
io.emit('percentStatus', JSON.stringify(postotak));
})
});
//db.close();
// Server console logiranje
console.log("[>>] " + currentTime + " -- Client: " + req.params.client + ' Service: ' + req.params.service + ' Message: ' + req.params.status);
// Slack notifikacija po API update-u
slack.webhook({
channel: slackChannel,
username: "Messaging Service",
text: dateNow() + " Client: [" + req.params.client + "] Servis: [" + req.params.service + "] Status: [" + req.params.status + "]"}, function(err, response) {
// console.log(response);
});
})
app.use(express.static('public'));
app.use("/",router);
//app.set('view engine', 'pug');
app.use("/error",function(req,res){
res.sendFile(__dirname + "/public/404.html");
});
app.use(bodyParser.json());
router.post('/post', function(req, res, next){
//console.log(req.bodyparser);
// console.log(JSON.stringify(req.computer));
next();
// your JSON
// echo the result back
});
app.listen(3000,function(){
console.log("[>>] " + currentTime + " -- Live at Port 3000 ");
});
// datumi
function dateToString(date) {
var month = date.getMonth() + 1;
var day = date.getDate();
var dateOfString = (("" + day).length < 2 ? "0" : "") + day + "/";
dateOfString += (("" + month).length < 2 ? "0" : "") + month + "/";
dateOfString += date.getFullYear();
return dateOfString;
}
function dateNow() {
var currentdate = new Date();
var datetime = dateToString(currentdate);
datetime += ' ';
datetime += + currentdate.getHours() + ":"
+ currentdate.getMinutes() + ":"
+ currentdate.getSeconds();
// console.log(datetime);
return datetime;
}