Merge event data into single representation
Signed-off-by: William Brawner <me@wbrawner.com>
This commit is contained in:
parent
a16ff6be7a
commit
727865bc00
2 changed files with 47 additions and 112 deletions
96
event.js
96
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,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
63
index.js
63
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);
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue