2020-09-01 23:19:30 +00:00
|
|
|
const utils = require('./util.js');
|
|
|
|
const randomId = utils.randomId;
|
|
|
|
const firstOfMonth = utils.firstOfMonth;
|
|
|
|
const lastOfMonth = utils.lastOfMonth;
|
|
|
|
const pool = require('./db.js');
|
|
|
|
const express = require('express');
|
|
|
|
const basicAuth = require('express-basic-auth');
|
|
|
|
const basicAuthConfig = require('./config.js')
|
2020-09-15 14:31:58 +00:00
|
|
|
const cors = require('cors');
|
2020-09-01 23:19:30 +00:00
|
|
|
|
|
|
|
class Event {
|
2020-09-01 01:57:46 +00:00
|
|
|
static types = [
|
2020-09-15 14:31:58 +00:00
|
|
|
'VIEW',
|
|
|
|
'CLICK',
|
|
|
|
'ERROR',
|
|
|
|
'CRASH',
|
2020-09-01 01:57:46 +00:00
|
|
|
];
|
|
|
|
|
2020-08-30 20:42:50 +00:00
|
|
|
id = randomId(32);
|
|
|
|
appId = '';
|
|
|
|
date = new Date();
|
|
|
|
|
|
|
|
// For web only
|
|
|
|
userAgent = '';
|
|
|
|
|
|
|
|
platform = '';
|
2020-09-01 01:57:46 +00:00
|
|
|
// For native only
|
2020-08-30 20:42:50 +00:00
|
|
|
manufacturer = '';
|
|
|
|
// This doubles as the browser for web
|
|
|
|
model = '';
|
|
|
|
version = '';
|
|
|
|
|
|
|
|
locale = '';
|
|
|
|
sessionId = '';
|
2020-09-01 01:57:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This can have different meanings depending on what the event's type is:
|
|
|
|
*
|
|
|
|
* view -> page path
|
|
|
|
* click -> element identifier
|
|
|
|
* error & crash -> stacktrace
|
|
|
|
*/
|
2020-08-30 20:42:50 +00:00
|
|
|
data;
|
|
|
|
|
2020-09-01 01:57:46 +00:00
|
|
|
/**
|
|
|
|
* view,click, error, or crash
|
|
|
|
*/
|
2020-08-30 20:42:50 +00:00
|
|
|
type;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
appId,
|
|
|
|
date,
|
|
|
|
userAgent,
|
|
|
|
platform,
|
|
|
|
manufacturer,
|
|
|
|
model,
|
|
|
|
version,
|
|
|
|
locale,
|
|
|
|
sessionId,
|
|
|
|
data,
|
|
|
|
type,
|
|
|
|
) {
|
|
|
|
this.appId = appId;
|
|
|
|
this.date = date;
|
|
|
|
this.userAgent = userAgent;
|
|
|
|
this.platform = platform;
|
|
|
|
this.manufacturer = manufacturer;
|
|
|
|
this.model = model;
|
|
|
|
this.version = version;
|
|
|
|
this.locale = locale;
|
|
|
|
this.sessionId = sessionId;
|
|
|
|
this.data = data;
|
|
|
|
this.type = type;
|
|
|
|
}
|
|
|
|
}
|
2020-09-01 16:13:40 +00:00
|
|
|
|
|
|
|
pool.query(`CREATE TABLE IF NOT EXISTS events (
|
|
|
|
id VARCHAR(32) PRIMARY KEY,
|
|
|
|
appId VARCHAR(32) NOT NULL,
|
|
|
|
date DATETIME NOT NULL,
|
|
|
|
userAgent VARCHAR(256),
|
|
|
|
platform VARCHAR(32),
|
|
|
|
manufacturer VARCHAR(256),
|
|
|
|
model VARCHAR(256),
|
|
|
|
version VARCHAR(32),
|
|
|
|
locale VARCHAR(8),
|
|
|
|
sessionId VARCHAR(32),
|
|
|
|
data TEXT DEFAULT NULL,
|
|
|
|
type VARCHAR(256) DEFAULT NULL,
|
|
|
|
FOREIGN KEY (appId)
|
|
|
|
REFERENCES apps(id)
|
|
|
|
ON DELETE CASCADE
|
|
|
|
)`);
|
|
|
|
|
2020-09-01 23:19:30 +00:00
|
|
|
class EventRepository {
|
2020-09-01 16:13:40 +00:00
|
|
|
static getEvents(
|
|
|
|
appId,
|
|
|
|
from,
|
|
|
|
to,
|
|
|
|
count,
|
|
|
|
page,
|
|
|
|
) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let query = 'SELECT * FROM events WHERE appId = ?';
|
|
|
|
let queryParams = [appId]
|
|
|
|
if (from) {
|
|
|
|
query += ' AND date >= ?'
|
|
|
|
queryParams.push(from)
|
|
|
|
}
|
|
|
|
if (to) {
|
|
|
|
query += ' AND date <= ?'
|
|
|
|
queryParams.push(to)
|
|
|
|
}
|
|
|
|
if (count) {
|
|
|
|
let limit = count;
|
|
|
|
let offset = 0;
|
|
|
|
if (page) {
|
|
|
|
offset = count * (page - 1);
|
|
|
|
limit = count * page;
|
|
|
|
}
|
|
|
|
query += ' LIMIT ?,?';
|
|
|
|
queryParams.push(offset, limit);
|
|
|
|
}
|
|
|
|
pool.query(query, queryParams, (err, res) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(res);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2020-09-01 16:18:17 +00:00
|
|
|
|
|
|
|
static createEvent(event) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
pool.query('INSERT INTO events SET ?', event, (err, res, fields) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(event);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
2020-09-01 16:13:40 +00:00
|
|
|
}
|
|
|
|
|
2020-09-01 23:19:30 +00:00
|
|
|
const router = express.Router()
|
|
|
|
router.get('/', basicAuth(basicAuthConfig), (req, res) => {
|
2020-09-01 16:13:40 +00:00
|
|
|
const appId = req.query.appId;
|
|
|
|
if (!appId) {
|
|
|
|
res.status(400).send({ message: 'Invalid appId' });
|
|
|
|
}
|
|
|
|
const from = req.query.from || firstOfMonth()
|
|
|
|
const to = req.query.to || lastOfMonth()
|
|
|
|
const count = req.query.count || 1000;
|
|
|
|
const page = req.query.count || 1;
|
|
|
|
EventRepository.getEvents(appId, from, to, count, page)
|
|
|
|
.then((events) => {
|
|
|
|
res.json(events);
|
|
|
|
}).catch((err) => {
|
|
|
|
res.status(500).send();
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
// This is one of the few routes that don't require authentication. Since
|
|
|
|
// events will be coming from all over the place, I don't think it makes
|
|
|
|
// sense to try to put auth in front of this. Even some kind of client
|
|
|
|
// "secret" would be trivial to deduce by examining the requests.
|
2020-09-15 14:31:58 +00:00
|
|
|
router.post('/', cors({origin: true, methods: ['POST']}), (req, res) => {
|
|
|
|
console.log(req.body);
|
2020-09-01 16:13:40 +00:00
|
|
|
if (typeof req.body.appId === "undefined") {
|
|
|
|
res.status(400).json({ message: 'Invalid appId' });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-15 14:31:58 +00:00
|
|
|
// Without Cookies, websites can't consistently send the same sessionId
|
|
|
|
// if (typeof req.body.sessionId === "undefined") {
|
|
|
|
// res.status(400).json({ message: 'Invalid sessionId' });
|
|
|
|
// return;
|
|
|
|
// }
|
2020-09-01 16:13:40 +00:00
|
|
|
|
|
|
|
if (Event.types.indexOf(req.body.type) === -1) {
|
|
|
|
res.status(400).json({ message: 'Invalid event type' });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof req.body.data === "undefined") {
|
|
|
|
// TODO: Handle data validation better than this
|
|
|
|
res.status(400).json({ message: 'Invalid data' });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-01 16:18:17 +00:00
|
|
|
EventRepository.createEvent(new Event(
|
2020-09-01 16:13:40 +00:00
|
|
|
req.body.appId,
|
2020-09-01 16:24:07 +00:00
|
|
|
new Date(req.body.date),
|
2020-09-15 14:31:58 +00:00
|
|
|
req.headers['User-Agent'],
|
2020-09-01 16:13:40 +00:00
|
|
|
req.body.platform,
|
|
|
|
req.body.manufacturer,
|
|
|
|
req.body.model,
|
|
|
|
req.body.version,
|
|
|
|
req.body.locale,
|
|
|
|
req.body.sessionId,
|
|
|
|
req.body.data,
|
|
|
|
req.body.type,
|
2020-09-01 16:18:17 +00:00
|
|
|
))
|
|
|
|
.then((event) => {
|
|
|
|
res.json(event);
|
|
|
|
}).catch((err) => {
|
|
|
|
console.error(err);
|
|
|
|
res.sendStatus(500);
|
|
|
|
});
|
2020-09-01 16:13:40 +00:00
|
|
|
});
|
2020-09-01 23:19:30 +00:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
Event,
|
|
|
|
EventRepository,
|
|
|
|
router
|
|
|
|
}
|