Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
HOST=localhost
PORT=8080
HOSTDOMAIN
no_APP_DB_PSQL_URL=postgresql://eyevinn:[email protected]:5432/session_db_dev
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
.nyc_output/
test_vast.xml
commitlint.config.js
commitlint.config.js
.pgdata
4 changes: 0 additions & 4 deletions .pgdata/.gitignore

This file was deleted.

9 changes: 7 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
FROM node:slim

RUN apt-get update && apt-get install -y wget
# Dockerize is needed to sync containers startup
ENV DOCKERIZE_VERSION v0.6.0
RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
&& tar -C /usr/local/bin -xzvf dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
&& rm dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz

WORKDIR /app

ADD . .

RUN npm install

CMD ["npm", "start"]
8 changes: 6 additions & 2 deletions api/EventTracker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
class EventTracker {
#eventList;

constructor() {
this.#eventList = [];
constructor(list) {
if (!list) {
this.#eventList = [];
} else {
this.#eventList = list;
}
}

AddEvent(eventObj) {
Expand Down
91 changes: 66 additions & 25 deletions api/Session.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const EventTracker = require("./EventTracker.js")
const User = require("./User.js");
const { VastBuilder } = require("../utils/vast-maker");
const { v4: uuid } = require("uuid");
const DOMParser = require('xmldom').DOMParser;

class Session {
// Public Fields
Expand All @@ -17,30 +18,31 @@ class Session {
#eventTracker

constructor(params) {
// Take a time stamp.
const timeStamp = new Date().toISOString();

this.created = timeStamp;
this.sessionId = uuid();
this.host = params.host;
this.#user = new User(params.uid || null);

this.#clientRequest = new ClientRequest(params);
this.#eventTracker = new EventTracker();

// Create Vast object.
const vastObj = VastBuilder({
sessionId: this.sessionId,
desiredDuration: params.dur || "0",
adserverHostname: this.host,
maxPodDuration: params.max || null,
minPodDuration: params.min || null,
podSize: params.ps || null
});

this.#vastXml = vastObj.xml;
this.adBreakDuration = vastObj.duration;
}
if (params) {
// Take a time stamp.
this.created = (new Date()).toISOString();
this.sessionId = uuid();
this.host = params.host || null;
this.#user = new User(params.uid || "unknown");

this.#clientRequest = new ClientRequest(params);
this.#eventTracker = new EventTracker();

// Create Vast object.
const vastObj = VastBuilder({
sessionId: this.sessionId,
desiredDuration: params.dur || "0",
adserverHostname: this.host,
maxPodDuration: params.max || null,
minPodDuration: params.min || null,
podSize: params.ps || null
});


this.#vastXml = vastObj.xml;
this.adBreakDuration = vastObj.duration;
}
}

getUser() {
return this.#user.getUserId();
Expand All @@ -62,6 +64,45 @@ class Session {
this.#eventTracker.AddEvent(eventObj);
}

toJSON() {
return {
session_id: this.sessionId,
user_id: this.getUser(),
ad_break_dur: this.adBreakDuration,
created: this.created,
host: this.host,
cli_req: JSON.stringify(this.getClientRequest()),
response: this.getVastXml().toString(),
tracked_events: JSON.stringify(this.getTrackedEvents())
}
}

// Initialize new Session from its JSON form.
fromJSON(jsonObj) {
this.created = jsonObj.created;
this.sessionId = jsonObj.session_id;
this.host = jsonObj.host;
this.#user = new User(jsonObj.user_id || "unknown");
this.#clientRequest = new ClientRequest(JSON.parse(jsonObj.cli_req));
this.#eventTracker = new EventTracker(JSON.parse(jsonObj.tracked_events)['events']);
this.adBreakDuration = jsonObj.ad_break_dur;

const parser = new DOMParser();
const vastXml = parser.parseFromString(jsonObj.response, "text/xml");
this.#vastXml = vastXml;
}

toObject() {
return {
sessionId: this.sessionId,
userId: this.getUser(),
created: this.created,
adBreakDuration: this.adBreakDuration,
clientRequest: this.getClientRequest(),
response: this.getVastXml().toString(),
};
}

}

module.exports = Session;
module.exports = Session;
41 changes: 10 additions & 31 deletions api/routes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const DBAdapter = require("../controllers/memory-db-adapter");
const DBAdapter = require("../db/storage");
const logger = require("../utils/logger.js");
const { PaginateMemoryDB, Transform, CloudWatchLog } = require("../utils/utilities");
const { CloudWatchLog } = require("../utils/utilities");
const Session = require("./Session.js");

/**
Expand Down Expand Up @@ -511,8 +511,8 @@ module.exports = (fastify, opt, next) => {
limit: req.query.limit,
targetHost: req.headers['host']
};

const sessionList = await DBAdapter.getAllSessions(options);
let sessionList = await DBAdapter.getAllSessions(options);
sessionList.data = sessionList.data.map(session => session.toObject());
reply.code(200).send(sessionList);
} catch (exc) {
logger.error(exc, { label: req.headers['host'] });
Expand All @@ -532,17 +532,9 @@ module.exports = (fastify, opt, next) => {
reply.code(404).send({
message: `Session with ID: '${sessionId}' was not found`,
});
} else {
const payload = {
sessionId: session.sessionId,
userId: session.getUser(),
created: session.created,
adBreakDuration: session.adBreakDuration,
clientRequest: session.getClientRequest(),
response: session.getVastXml().toString(),
};
reply.code(200).send(payload);
}
const sessionObj = session.toObject();
reply.code(200).send(sessionObj);
} catch (exc) {
logger.error(exc, { label: req.headers['host'], sessionId: sessionId });
reply.code(500).send({ message: exc.message });
Expand Down Expand Up @@ -662,30 +654,17 @@ module.exports = (fastify, opt, next) => {
async (req, reply) => {
try {
// Get Session List via db-controller function.
let sessionList = await DBAdapter.getSessionsByUserId(
req.params.userId
);


let sessionList = await DBAdapter.getSessionsByUserId(req.params.userId);
// Check if List is null, If so assume no sessions with that user ID exists.
if (!sessionList) {
logger.info(`Sessions under User-ID: '${req.params.userId}' were not found`, { label: req.headers['host'] });
reply.code(404).send({
message: `Sessions under User-ID: '${req.params.userId}' were not found`,
});
} else {
// Send Array of: items -> containing all session information.
sessionList = sessionList.map((session) => {
return {
sessionId: session.sessionId,
userId: session.getUser(),
created: session.created,
adBreakDuration: session.adBreakDuration,
clientRequest: session.getClientRequest(),
response: session.getVastXml().toString(),
};
});
reply.code(200).send(sessionList);
}
sessionList = sessionList.map(session => session.toObject());
reply.code(200).send(sessionList);
} catch (exc) {
logger.error(exc, { label: req.headers['host'] });
reply.code(500).send({ message: exc.message });
Expand Down
4 changes: 1 addition & 3 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
// IMPORT MODULES
const Fastify = require("fastify");

function builder() {
const fastify = Fastify({ ignoreTrailingSlash: true });
// Homepage route? Replace later.

fastify.get("/", async () => {
return {
Test: "This is working fine",
};
});

// SET UP Swagger
fastify.register(require("fastify-swagger"), {
routePrefix: "/api/docs",
swagger: {
Expand Down
2 changes: 2 additions & 0 deletions controllers/db-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class DBAdapter {
async getSession(sessionId) {}

async DeleteSession(sessionId) {}

async _Paginator(opt) {}
}

module.exports = DBAdapter;
50 changes: 39 additions & 11 deletions controllers/memory-db-adapter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const DBAdapter = require("./db-adapter");
const { PaginateMemoryDB, Transform } = require("../utils/utilities");

SESSION_STORE = {};

Expand All @@ -12,32 +11,34 @@ class MemoryDBAdapter extends DBAdapter {

// Get a List of running test sessions.
async getAllSessions(opt) {

let sessionList = Object.values(SESSION_STORE);

// Filter session list on host field.
if (opt && opt.targetHost && opt.targetHost != "") {
sessionList = sessionList.filter( session => session.host.localeCompare(opt.targetHost) === 0);
}
// Sort by newest first

// Sort by newest first
sessionList.sort((a, b) => {
const dateA = new Date(a["created"]);
const dateB = new Date(b["created"]);
return dateB - dateA;
});

// Assuming We are to always respond with a pagination.
// Paginate w/ query params, deafult values used otherwise.
sessionList = PaginateMemoryDB(sessionList, opt.page, opt.limit);
sessionList.data = sessionList.data.map((session) => {
return Transform(session);
});
sessionList = await this._Paginator({ list: sessionList, pageNum: opt.page, pageLimit: opt.limit });
if (!sessionList) {
return {};
}
// Return Pagination Object
return sessionList;
}

// Get a List of running test sessions.
async getSessionsByUserId(userId) {
let sessionList = Object.values(SESSION_STORE).filter(
let sessionList = await Object.values(SESSION_STORE).filter(
(session) => userId == session.getUser()
);
// If empty, then we have no sessions.
Expand All @@ -50,15 +51,42 @@ class MemoryDBAdapter extends DBAdapter {
// Get information of a specific test session.
async getSession(sessionId) {
const session = SESSION_STORE[sessionId];
if (!session) {
return session;
}
return session;
}

async DeleteSession(sessionId) {
delete SESSION_STORE[sessionId];
return 1;
}
}

const adapter = new MemoryDBAdapter();
async _Paginator(opt) {
if (!opt) {
return false;
}
let list = opt.list || [];
const limit = parseInt(opt.pageLimit, 10) || 80;
const page = parseInt(opt.pageNum, 10) || 1;
const startAt = (page - 1) * limit;
const endAt = page * limit;
const totalCount = list.length;
const getTotalPages = (limit, totalCount) => Math.ceil(totalCount / limit);
const getNextPage = (page, limit, total) => (total / limit) > page ? page + 1 : null;
const getPreviousPage = page => page <= 1 ? null : page - 1;
let sessions = list.slice(startAt, endAt);

return {
previousPage: getPreviousPage(page),
currentPage: page,
nextPage: getNextPage(page, limit, totalCount),
totalPages: getTotalPages(limit, totalCount),
limit: limit,
totalItems: totalCount,
data: sessions,
};
}
}

module.exports = adapter;
module.exports = MemoryDBAdapter;
Loading