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';
|
import { randomId } from './util.js';
|
||||||
|
|
||||||
export default class Event {
|
export default class Event {
|
||||||
|
static types = [
|
||||||
|
'view',
|
||||||
|
'click',
|
||||||
|
'error',
|
||||||
|
'crash',
|
||||||
|
];
|
||||||
|
|
||||||
id = randomId(32);
|
id = randomId(32);
|
||||||
appId = '';
|
appId = '';
|
||||||
date = new Date();
|
date = new Date();
|
||||||
|
@ -9,7 +16,7 @@ export default class Event {
|
||||||
userAgent = '';
|
userAgent = '';
|
||||||
|
|
||||||
platform = '';
|
platform = '';
|
||||||
// Unused on web
|
// For native only
|
||||||
manufacturer = '';
|
manufacturer = '';
|
||||||
// This doubles as the browser for web
|
// This doubles as the browser for web
|
||||||
model = '';
|
model = '';
|
||||||
|
@ -17,18 +24,21 @@ export default class Event {
|
||||||
|
|
||||||
locale = '';
|
locale = '';
|
||||||
sessionId = '';
|
sessionId = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This can have different meanings depending on what the event's type is:
|
||||||
|
*
|
||||||
|
* view -> page path
|
||||||
|
* click -> element identifier
|
||||||
|
* error & crash -> stacktrace
|
||||||
|
*/
|
||||||
data;
|
data;
|
||||||
|
|
||||||
// For interactions only
|
/**
|
||||||
// The path for page views or some identifier for clicks
|
* view,click, error, or crash
|
||||||
element;
|
*/
|
||||||
// view or click, more could be added later
|
|
||||||
type;
|
type;
|
||||||
|
|
||||||
// For errors only
|
|
||||||
stacktrace;
|
|
||||||
fatal;
|
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
appId,
|
appId,
|
||||||
date,
|
date,
|
||||||
|
@ -40,10 +50,7 @@ export default class Event {
|
||||||
locale,
|
locale,
|
||||||
sessionId,
|
sessionId,
|
||||||
data,
|
data,
|
||||||
element,
|
|
||||||
type,
|
type,
|
||||||
stacktrace,
|
|
||||||
fatal
|
|
||||||
) {
|
) {
|
||||||
this.appId = appId;
|
this.appId = appId;
|
||||||
this.date = date;
|
this.date = date;
|
||||||
|
@ -55,71 +62,6 @@ export default class Event {
|
||||||
this.locale = locale;
|
this.locale = locale;
|
||||||
this.sessionId = sessionId;
|
this.sessionId = sessionId;
|
||||||
this.data = data;
|
this.data = data;
|
||||||
this.element = element;
|
|
||||||
this.type = type;
|
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,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
57
index.js
57
index.js
|
@ -35,49 +35,42 @@ app.get('/events', (req, res) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post('/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") {
|
if (typeof req.body.sessionId === "undefined") {
|
||||||
res.status(400).json({ message: 'Invalid sessionId' });
|
res.status(400).json({ message: 'Invalid sessionId' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let event;
|
if (Event.types.indexOf(req.body.type) === -1) {
|
||||||
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' });
|
res.status(400).json({ message: 'Invalid event type' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
event = Event.Interaction(
|
|
||||||
req.body.userAgent,
|
if (typeof req.body.data === "undefined") {
|
||||||
req.body.platform,
|
// TODO: Handle data validation better than this
|
||||||
req.body.manufacturer,
|
res.status(400).json({ message: 'Invalid data' });
|
||||||
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' });
|
|
||||||
return;
|
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);
|
events.push(event);
|
||||||
res.json(event);
|
res.json(event);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue