Skip to content

Commit

Permalink
Refactor statistics endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
robflop committed Sep 12, 2018
1 parent 12b38f9 commit 1bb1168
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ server.get('/api/sounds', (req, res) => { // eslint-disable-line complexity
let requestedSounds = sounds;

if (['source', 'over', 'under', 'equals'].some(parameter => Object.keys(req.query).includes(parameter))) {
const equals = req.query.equals ? parseInt(req.query.equals) : null; // TODO look into these assignments
const equals = req.query.equals ? parseInt(req.query.equals) : null;
const over = req.query.over ? parseInt(req.query.over) : null;
const under = req.query.under ? parseInt(req.query.under) : null;

if ((equals && isNaN(equals)) || (over && isNaN(over)) || (under && isNaN(under))) { // TODO look into this one
if ((equals && isNaN(equals)) || (over && isNaN(over)) || (under && isNaN(under))) {
return res.status(400).json({ code: 400, name: 'Invalid range', message: 'The "over", "under" and "equals" parameters must be numbers.' });
}

Expand Down Expand Up @@ -181,7 +181,7 @@ server.get('/api/counter', (req, res) => {
});

server.get('/api/statistics', (req, res) => { // eslint-disable-line complexity
let requestedStats = {}, countFiltered;
let requestedStats = statistics;
const dateRegex = new RegExp(/^(\d{4})-(\d{2})-(\d{2})$/);
const firstStatDate = Object.keys(statistics)[0];
const latestStatDate = Object.keys(statistics)[Object.keys(statistics).length - 1];
Expand All @@ -194,7 +194,7 @@ server.get('/api/statistics', (req, res) => { // eslint-disable-line complexity

const to = req.query.to;
const from = req.query.from;
const equals = req.query.equals ? parseInt(req.query.equals) : null; // TODO look into these assignments
const equals = req.query.equals ? parseInt(req.query.equals) : null;
const over = req.query.over ? parseInt(req.query.over) : null;
const under = req.query.under ? parseInt(req.query.under) : null;

Expand All @@ -206,62 +206,59 @@ server.get('/api/statistics', (req, res) => { // eslint-disable-line complexity
return res.status(400).json({ code: 400, name: 'Invalid timespan', message: 'The start date must be before the end date.' });
}

if ((equals && isNaN(equals)) || (over && isNaN(over)) || (under && isNaN(under))) { // TODO look into this one
if ((equals && isNaN(equals)) || (over && isNaN(over)) || (under && isNaN(under))) {
return res.status(400).json({ code: 400, name: 'Invalid range', message: 'The "over", "under" and "equals" parameters must be numbers.' });
}

if ((over && under) && over > under) {
return res.status(400).json({ code: 400, name: 'Invalid range', message: 'The "under" parameter must be bigger than the "over" parameter.' });
}

// Date filtering
if (from && !to) {
requestedStats[from] = requestedStats[from] || 0;
}
else if (!from && to) {
requestedStats = filterStats(requestedStats, firstStatDate, to, (iterator, startDate, endDate) => {
return dateFns.isSameDay(iterator, endDate) || dateFns.isBefore(iterator, endDate);
});
}
else if (from && to) {
requestedStats = filterStats(requestedStats, from, to, (iterator, startDate, endDate) => {
return dateFns.isWithinRange(iterator, startDate, endDate);
});
}

// Count filtering
if (equals || over || under) {
if (equals) {
countFiltered = filterStats(statistics, firstStatDate, latestStatDate, (iterator, startDate, endDate) => {
requestedStats = filterStats(statistics, firstStatDate, latestStatDate, (iterator, startDate, endDate) => {
return statistics[iterator] === equals;
});
}
else if (over && !under) {
countFiltered = filterStats(statistics, firstStatDate, latestStatDate, (iterator, startDate, endDate) => {
requestedStats = filterStats(statistics, firstStatDate, latestStatDate, (iterator, startDate, endDate) => {
return statistics[iterator] > over;
});
}
else if (!over && under) {
countFiltered = filterStats(statistics, firstStatDate, latestStatDate, (iterator, startDate, endDate) => {
requestedStats = filterStats(statistics, firstStatDate, latestStatDate, (iterator, startDate, endDate) => {
return statistics[iterator] < under;
});
}
else if (over && under) {
countFiltered = filterStats(statistics, firstStatDate, latestStatDate, (iterator, startDate, endDate) => {
requestedStats = filterStats(statistics, firstStatDate, latestStatDate, (iterator, startDate, endDate) => {
return statistics[iterator] > over && statistics[iterator] < under;
});
}
}

// Date filtering
if (from && !to) {
requestedStats[from] = countFiltered ? countFiltered[from] || 0 : statistics[from] || 0;
}
else if (!from && to) {
requestedStats = filterStats(countFiltered ? countFiltered : statistics, firstStatDate, to, (iterator, startDate, endDate) => {
return dateFns.isSameDay(iterator, endDate) || dateFns.isBefore(iterator, endDate);
});
}
else if (from && to) {
requestedStats = filterStats(countFiltered ? countFiltered : statistics, from, to, (iterator, startDate, endDate) => {
return dateFns.isWithinRange(iterator, startDate, endDate);
});
}
else requestedStats = countFiltered;

if (countFiltered) {
for (const entryKey in requestedStats) {
if (requestedStats[entryKey] === 0) delete requestedStats[entryKey];
} // Filter padded entries if a count filter is used
}
}
else {
requestedStats = filterStats(statistics, firstStatDate, latestStatDate);
requestedStats = filterStats(requestedStats, firstStatDate, latestStatDate);
}

return res.json(requestedStats);
Expand Down

0 comments on commit 1bb1168

Please sign in to comment.