UNPKG

rutilus-analytics-node-js

Version:

Provides a GUI web app that allows users to examine their data in detail. Includes CSV export functionality.

100 lines (79 loc) 2.74 kB
/** * Rutilus * * @homepage https://gmrutilus.github.io * @license Apache-2.0 */ const fs = require('fs'); /** @module */ module.exports = /** @param {KoaCtx} ctx */ async (ctx) => { // Expecting these as query parameters: // Skip (optional) let skip = false; let { start, end } = ctx.query; if (start !== undefined && end !== undefined) { start = +start; end = +end; if (!isNaN(start) && !isNaN(end)) { skip = [start, end]; } } // can be "client", "pushapi", or "getapi". Tells API which type of error it's getting, whether it's coming from // the db or from the file system. const categoryNameParam = ctx.query.categoryName; // Must be one of the errorGroups names for errors that have been grouped (for client errors, errorGroups names are the // "details.details" field. For file system errors, errorGroups names are the "message" field. const groupNameParam = ctx.query.groupName; const errorHandler = ctx.res.errorHandler; ctx.body = {}; // Begin empty error array, will fill up with errors after deciding what type of error we're getting let errors = []; // Use the errorGroups to decide what errors to return in the response switch(categoryNameParam.toLowerCase()) { case 'observer': // Contact the database to get the errors await getClientErrors(); break; default: // Invalid break; } // Set response body ctx.body = errors; /** * Gets the errors that happened in the client-side in the db * Mutates the state of the errors array in the enclosing scope. */ function getClientErrors() { // No grouping, just get all the errors that match the groupName from the query string const aggPipe = [ { $match: { 'details.details': groupNameParam } }, { $sort: { date: -1 } } ]; if (skip) { aggPipe.push({ $skip: skip[0] }, { $limit: skip[1] }); } return new Promise((resolve) => { /** @type {{ aggregate: Function }} */ const collection = ctx.res.db.collection('errors'); collection.aggregate(aggPipe, (err, docs) => { if (errorHandler.dbErrorCatcher(err)) { return; } // Now we have the errors (grouped), add them to errors array docs.forEach(doc => { errors.push(doc); }); resolve(); }); }); } };