Permalink
Browse files

Log Events are now in sqlite3 database

  • Loading branch information...
andrewshell committed May 30, 2017
1 parent f954458 commit 062ec8fdea7c8f19f12967e8465c4a1257f0b53e
Showing with 448 additions and 38 deletions.
  1. +1 −1 app.js
  2. +1 −1 client.js
  3. +21 −3 controllers/view-log.js
  4. +7 −0 database.json
  5. +34 −0 migrations/20170530180056-add-log-events.js
  6. +3 −0 package.json
  7. +30 −14 services/log-event.js
  8. +351 −19 yarn.lock
2 app.js
@@ -50,7 +50,7 @@
});
morgan.format('mydate', function() {
var df = require('console-stamp/node_modules/dateformat');
var df = require('dateformat');
return df(new Date(), 'HH:MM:ss.l');
});
@@ -31,7 +31,7 @@ nconf
console.log(nconf.get('APP_NAME') + ' ' + nconf.get('APP_VERSION'));
morgan.format('mydate', function() {
var df = require('console-stamp/node_modules/dateformat');
var df = require('dateformat');
return df(new Date(), 'HH:MM:ss.l');
});
@@ -2,10 +2,25 @@
"use strict";
var async = require('async'),
data = require('../services/data'),
errorResult = require('../services/error-result'),
express = require('express'),
router = express.Router(),
syncStruct = require('../services/sync-struct');
router = express.Router();
function fetchData(db, callback) {
var data = {
'eventlog': []
};
db.serialize(function() {
db.each("SELECT * FROM log_events ORDER BY time DESC LIMIT 100", function(err, row) {
row.headers = JSON.parse(row.headers);
data.eventlog.push(row);
}, function () {
callback(null, data);
});
});
}
function processResponse(req, res, data) {
switch (req.accepts('html', 'json')) {
@@ -28,7 +43,10 @@
router.get('/', function (req, res) {
async.waterfall([
function (callback) {
syncStruct.watchStruct('data', callback);
data.getDb(callback);
},
function (db, callback) {
fetchData(db, callback);
},
function (data) {
processResponse(req, res, data);
@@ -0,0 +1,7 @@
{
"defaultEnv": "dev",
"dev": {
"driver": "sqlite3",
"filename": "data/data.db"
}
}
@@ -0,0 +1,34 @@
'use strict';
var dbm;
var type;
var seed;
/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
exports.setup = function(options, seedLink) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
};
exports.up = function(db, callback) {
db.createTable('log_events', {
id: { type: 'int', primaryKey: true },
eventtype: 'text',
htmltext: 'text',
secs: 'int',
time: 'text',
headers: 'text'
}, callback);
};
exports.down = function(db, callback) {
db.dropTable('log_events', callback);
};
exports._meta = {
"version": 1
};
@@ -21,13 +21,16 @@
"body-parser": "^1.17.2",
"console-stamp": "^0.2.5",
"cors": "^2.8.3",
"db-migrate": "^0.10.0-beta.20",
"db-migrate-sqlite3": "^0.2.1",
"express": "^4.15.3",
"express-handlebars": "^3.0.0",
"moment": "^2.18.1",
"morgan": "^1.8.2",
"nconf": "^0.8.4",
"request": "^2.81.0",
"sprintf-js": "^1.1.1",
"sqlite3": "^3.1.8",
"xmlbuilder": "^9.0.0"
},
"repository": {
@@ -1,9 +1,10 @@
(function () {
"use strict";
var moment = require('moment');
var data = require('./data'),
moment = require('moment');
function logEvent(data, eventtype, htmltext, startticks, req) {
function logEvent(deprecated, eventtype, htmltext, startticks, req) {
var secs, time;
time = moment();
@@ -13,19 +14,34 @@
req = {headers: false};
}
data.eventlog.unshift({
'eventtype': eventtype,
'htmltext': htmltext,
'secs': secs,
'time': time,
'headers': req.headers
data.getDb(function (err, db) {
if (err) {
console.error(err);
return;
}
db.serialize(function() {
var stmt = db.prepare(`
INSERT INTO log_events (
eventtype,
htmltext,
secs,
time,
headers
) VALUES (
?,
?,
?,
?,
?
)
`);
stmt.run(eventtype, htmltext, secs, time.toISOString(), JSON.stringify(req.headers));
});
});
while (data.prefs.maxEvents < data.eventlog.length) {
data.eventlog.pop();
}
data.dirty = true;
}
module.exports = logEvent;
Oops, something went wrong.

0 comments on commit 062ec8f

Please sign in to comment.