Files
cloudron-box/src/eventlog.js

117 lines
3.7 KiB
JavaScript
Raw Normal View History

2016-04-29 23:49:56 -07:00
'use strict';
exports = module.exports = {
EventLogError: EventLogError,
add: add,
get: get,
2016-04-30 11:49:51 -07:00
getAllPaged: getAllPaged,
2016-07-25 12:36:43 -07:00
cleanup: cleanup,
2016-04-30 11:49:51 -07:00
2016-04-30 19:49:50 -07:00
// keep in sync with webadmin index.js filter
2016-04-30 13:56:03 -07:00
ACTION_ACTIVATE: 'cloudron.activate',
2016-06-17 17:12:55 -05:00
ACTION_APP_CLONE: 'app.clone',
2016-04-30 13:34:06 -07:00
ACTION_APP_CONFIGURE: 'app.configure',
ACTION_APP_INSTALL: 'app.install',
ACTION_APP_RESTORE: 'app.restore',
ACTION_APP_UNINSTALL: 'app.uninstall',
ACTION_APP_UPDATE: 'app.update',
2016-05-01 11:42:12 -07:00
ACTION_BACKUP_FINISH: 'backup.finish',
ACTION_BACKUP_START: 'backup.start',
2016-04-30 22:27:33 -07:00
ACTION_CERTIFICATE_RENEWAL: 'certificate.renew',
2016-04-30 11:49:51 -07:00
ACTION_CLI_MODE: 'settings.climode',
ACTION_START: 'cloudron.start',
2016-04-30 13:56:03 -07:00
ACTION_UPDATE: 'cloudron.update',
ACTION_USER_ADD: 'user.add',
2016-04-30 23:16:37 -07:00
ACTION_USER_LOGIN: 'user.login',
2016-04-30 13:56:03 -07:00
ACTION_USER_REMOVE: 'user.remove',
ACTION_USER_UPDATE: 'user.update'
2016-04-29 23:49:56 -07:00
};
var assert = require('assert'),
DatabaseError = require('./databaseerror.js'),
2016-04-30 11:49:51 -07:00
debug = require('debug')('box:eventlog'),
2016-04-29 23:49:56 -07:00
eventlogdb = require('./eventlogdb.js'),
util = require('util'),
uuid = require('node-uuid');
2016-04-30 11:49:51 -07:00
var NOOP_CALLBACK = function (error) { if (error) debug(error); };
2016-04-29 23:49:56 -07:00
function EventLogError(reason, errorOrMessage) {
assert.strictEqual(typeof reason, 'string');
assert(errorOrMessage instanceof Error || typeof errorOrMessage === 'string' || typeof errorOrMessage === 'undefined');
Error.call(this);
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.reason = reason;
if (typeof errorOrMessage === 'undefined') {
this.message = reason;
} else if (typeof errorOrMessage === 'string') {
this.message = errorOrMessage;
} else {
this.message = 'Internal error';
this.nestedError = errorOrMessage;
}
}
util.inherits(EventLogError, Error);
EventLogError.INTERNAL_ERROR = 'Internal error';
2016-04-30 13:00:44 -07:00
EventLogError.NOT_FOUND = 'Not Found';
2016-04-29 23:49:56 -07:00
2016-05-01 20:01:34 -07:00
function add(action, source, data, callback) {
2016-04-29 23:49:56 -07:00
assert.strictEqual(typeof action, 'string');
2016-05-01 20:01:34 -07:00
assert.strictEqual(typeof source, 'object');
2016-04-29 23:49:56 -07:00
assert.strictEqual(typeof data, 'object');
2016-04-30 11:49:51 -07:00
assert(!callback || typeof callback === 'function');
callback = callback || NOOP_CALLBACK;
2016-04-29 23:49:56 -07:00
2016-04-30 13:00:44 -07:00
var id = uuid.v4();
2016-04-30 13:56:03 -07:00
2016-04-30 13:00:44 -07:00
eventlogdb.add(id, action, source, data, function (error) {
2016-04-29 23:49:56 -07:00
if (error) return callback(new EventLogError(EventLogError.INTERNAL_ERROR, error));
2016-04-30 13:00:44 -07:00
callback(null, { id: id });
2016-04-29 23:49:56 -07:00
});
}
function get(id, callback) {
assert.strictEqual(typeof id, 'string');
assert.strictEqual(typeof callback, 'function');
eventlogdb.get(id, function (error, result) {
2016-04-30 13:00:44 -07:00
if (error && error.reason === DatabaseError.NOT_FOUND) return callback(new EventLogError(EventLogError.NOT_FOUND, 'No such event'));
2016-04-29 23:49:56 -07:00
if (error) return callback(new EventLogError(EventLogError.INTERNAL_ERROR, error));
callback(null, result);
});
}
function getAllPaged(action, search, page, perPage, callback) {
assert(typeof action === 'string' || action === null);
assert(typeof search === 'string' || search === null);
assert.strictEqual(typeof page, 'number');
assert.strictEqual(typeof perPage, 'number');
assert.strictEqual(typeof callback, 'function');
eventlogdb.getAllPaged(action, search, page, perPage, function (error, boxes) {
if (error) return callback(new EventLogError(EventLogError.INTERNAL_ERROR, error));
callback(null, boxes);
});
}
2016-07-25 12:36:43 -07:00
function cleanup(callback) {
callback = callback || NOOP_CALLBACK;
var d = new Date();
d.setDate(d.getDate() - 7); // 7 days ago
eventlogdb.delByCreationTime(d, function (error) {
if (error) return callback(new EventLogError(EventLogError.INTERNAL_ERROR, error));
callback(null);
});
}