-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_logs.js
59 lines (52 loc) · 1.36 KB
/
get_logs.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
var K = require('./constants')
function fetchLogs(request, db) {
let p = request.path;
switch (p) {
case `${K.Constants.FETCH_LOGS_IN_RANGE()}`:
return new Promise( (resolve,reject) => fetchLogsInRange(request, db).then( data => {
resolve(data);
}).catch( data => {
reject(data);
}));
break;
case `${K.Constants.FETCH_LOGS_RECENT()}`:
console.log("Will be implemented in future");
break;
default:
console.log("Will be implemented in future");
break;
}
}
function fetchLogsInRange(request, db) {
let q = request.query;
let from = q.from;
let to = q.to;
let matchingUrl = q.matching || '';
var data = [];
let matchString = `%${matchingUrl}%`;
let fromOK = Date.parse(from);
let toOK = Date.parse(to);
if (isNaN(fromOK) || isNaN(toOK)) {
return new Promise( (resolve,reject) => {
reject("One or both of input dates are invalid");
});
}
return new Promise( (resolve,reject) =>
db.each("SELECT verb, headers, body, createdat, query, url,contenttype FROM requestlogs WHERE \
createdat BETWEEN ? AND ? AND url LIKE ? ORDER BY createdat DESC",
[from, to, matchString], (error,row) => {
if (error) {
reject(error);
} else {
data.push(row);
}
}, (error,rows) => {
if (error) {
reject(error);
} else {
console.log(`${rows} rows found.`)
resolve(data);
}
}));
}
exports.fetchLogs = fetchLogs