From 727865bc00a42c0f55093eab5cb592e3b77a2d04 Mon Sep 17 00:00:00 2001 From: William Brawner Date: Mon, 31 Aug 2020 18:57:46 -0700 Subject: [PATCH] Merge event data into single representation Signed-off-by: William Brawner --- event.js | 96 +++++++++++--------------------------------------------- index.js | 63 +++++++++++++++++-------------------- 2 files changed, 47 insertions(+), 112 deletions(-) diff --git a/event.js b/event.js index d666861..33d6a9e 100644 --- a/event.js +++ b/event.js @@ -1,6 +1,13 @@ import { randomId } from './util.js'; export default class Event { + static types = [ + 'view', + 'click', + 'error', + 'crash', + ]; + id = randomId(32); appId = ''; date = new Date(); @@ -9,7 +16,7 @@ export default class Event { userAgent = ''; platform = ''; - // Unused on web + // For native only manufacturer = ''; // This doubles as the browser for web model = ''; @@ -17,18 +24,21 @@ export default class Event { locale = ''; sessionId = ''; + + /** + * This can have different meanings depending on what the event's type is: + * + * view -> page path + * click -> element identifier + * error & crash -> stacktrace + */ data; - // For interactions only - // The path for page views or some identifier for clicks - element; - // view or click, more could be added later + /** + * view,click, error, or crash + */ type; - // For errors only - stacktrace; - fatal; - constructor( appId, date, @@ -40,10 +50,7 @@ export default class Event { locale, sessionId, data, - element, type, - stacktrace, - fatal ) { this.appId = appId; this.date = date; @@ -55,71 +62,6 @@ export default class Event { this.locale = locale; this.sessionId = sessionId; this.data = data; - this.element = element; this.type = type; - this.stacktrace = stacktrace; - this.fatal = fatal; - } - - static Interaction( - appId, - date, - userAgent, - platform, - manufacturer, - model, - version, - locale, - sessionId, - data, - element, - type, - ) { - return new Event( - appId, - date, - userAgent, - platform, - manufacturer, - model, - version, - locale, - sessionId, - data, - element, - type, - ) - } - - static Error( - appId, - date, - userAgent, - platform, - manufacturer, - model, - version, - locale, - sessionId, - data, - stacktrace, - fatal - ) { - return new Event( - appId, - date, - userAgent, - platform, - manufacturer, - model, - version, - locale, - sessionId, - data, - null, - null, - stacktrace, - fatal, - ) } } diff --git a/index.js b/index.js index a6cb8f5..b429ce8 100644 --- a/index.js +++ b/index.js @@ -35,49 +35,42 @@ app.get('/events', (req, res) => { }); app.post('/events', (req, res) => { + if (typeof req.body.appId === "undefined") { + // TODO: Use some kind of authentication for this? + res.status(400).json({ message: 'Invalid appId' }); + return; + } + if (typeof req.body.sessionId === "undefined") { res.status(400).json({ message: 'Invalid sessionId' }); return; } - let event; - if (typeof req.body.element === "string" - && typeof req.body.type === "string") { - if (req.body.type !== 'view' || req.body.type !== 'click') { - res.status(400).json({ message: 'Invalid event type' }); - return; - } - event = Event.Interaction( - req.body.userAgent, - req.body.platform, - req.body.manufacturer, - req.body.model, - req.body.version, - req.body.locale, - req.body.sessionId, - req.body.data, - req.body.element, - req.body.type, - ); - } else if (typeof req.body.stacktrace === "string" - && typeof req.body.fatal === "boolean") { - event = Event.Error( - req.body.userAgent, - req.body.platform, - req.body.manufacturer, - req.body.model, - req.body.version, - req.body.locale, - req.body.sessionId, - req.body.data, - req.body.element, - req.body.type, - ); - } else { - res.status(400).json({ message: 'Invalid event data' }); + 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; + } + + const event = new Event( + req.body.appId, + req.body.date, + req.body.userAgent, + req.body.platform, + req.body.manufacturer, + req.body.model, + req.body.version, + req.body.locale, + req.body.sessionId, + req.body.data, + req.body.type, + ); + events.push(event); res.json(event); });