Skip to content

Commit

Permalink
cleaned up routes
Browse files Browse the repository at this point in the history
  • Loading branch information
shukriadams committed Mar 2, 2020
1 parent b0d7b26 commit a13621a
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 99 deletions.
3 changes: 0 additions & 3 deletions src/public/js/iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@

function update(){

if (!dashboard)
return;

inactiveFrame.contentWindow.location = `/dashboard/${dashboard}`;

// handles iframe load failure - if the frame fails to load, all active frames are
Expand Down
81 changes: 81 additions & 0 deletions src/routes/dashboard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const fs = require('fs-extra'),
path = require('path'),
settings = require('./../lib/settings').get(),
jsonfile = require('jsonfile'),
handlebars = require('./../lib/handlebars'),
arrayHelper = require('./../lib/array'),
daemon = require('./../lib/daemon');

module.exports = function(app){

/**
* Renders a dashboard. Does not autoreload, autoreload must be called via the default url /
* The autoreload frame will in turn call and autorefresh this dashboard view.
*/
app.get('/dashboard/:dashboard?', async function(req, res){
const dashboardNode = req.params.dashboard;

if (!settings.dashboards || !Object.keys(settings.dashboards).length){
let view = handlebars.getView('noDashboards');
return res.send(view());
}

let dashboard = settings.dashboards[dashboardNode];
if (!dashboard){
let view = handlebars.getView('invalidDashboard');
return res.send(view({
title : dashboardNode
}));
}

let title = dashboard.name;
let view = handlebars.getView('dashboard');

// clone array, we don't want to change source
let dashboardWatchers = arrayHelper.split(dashboard.watchers, ',');
let cronJobs = daemon.cronJobs.slice(0).filter((job)=>{
if (!job.config.enabled)
return null;
if (!dashboardWatchers.includes(job.config.__name))
return null;
return job;
});

const allJobsPassed = cronJobs.filter((job)=>{
return job.isPassing || job.config.enabled === false ? null : job;
}).length === 0;

cronJobs.sort((a,b)=>{
return a.isPassing - b.isPassing || a.config.name.localeCompare(b.config.name)
});

for (let cronJob of cronJobs){
const statusFilePath = path.join(__dirname, settings.logs, cronJob.config.__safeName, 'status.json');

cronJob.status = 'unknown'
cronJob.statusDate = null;

if (!await fs.pathExists(statusFilePath))
continue;

const status = jsonfile.readFileSync(statusFilePath);
cronJob.status = status.status;
cronJob.statusDate = new Date(status.date);

if (cronJob.nextRun){
cronJob.next = Math.floor((cronJob.nextRun.getTime() - new Date().getTime()) / 1000) + 's';
}
}

const now = new Date();

res.send(view({
title,
dashboardNode,
dashboardRefreshInterval : settings.dashboardRefreshInterval,
allJobsPassed,
renderDate: `${now.toLocaleDateString()} ${now.toLocaleTimeString()}`,
jobs : cronJobs
}));
});
}
114 changes: 18 additions & 96 deletions src/routes/default.js
Original file line number Diff line number Diff line change
@@ -1,121 +1,43 @@

const settings = require('./../lib/settings').get(),
fs = require('fs-extra'),
path = require('path'),
jsonfile = require('jsonfile'),
handlebars = require('./../lib/handlebars');
arrayHelper = require('./../lib/array'),
NO_DASHBOARDS_FLAG = '__no_dashboards_defined',
daemon = require('./../lib/daemon');

module.exports = function(app){

/**
* This is the default view of this site. To load use
*
* localhost:3000/
*
* To load with a specific dashboard use
*
* localhost:3000/[dashboard node name]
*
* Loads an autoreload frame, this frame uses JS + iframes to autoreload a dashboard view without flickering.
* If no (:dashboard) parameter is supplied, the first dashboard is automatically targetted.
*
*/
app.get('/:dashboard?', async function(req, res){
let dashboardNode = req.params.dashboard;

// fall back to first dashboard
if (!dashboardNode && settings.dashboards){
// fall back to first dashboard
let definedDashboardKeys = Object.keys(settings.dashboards);
dashboardNode = definedDashboardKeys.length ? definedDashboardKeys[0]: dashboardNode;
if (definedDashboardKeys.length)
dashboardNode = definedDashboardKeys[0];
}

if (!dashboardNode)
dashboard = NO_DASHBOARDS_FLAG;

let view = handlebars.getView('default');
const view = handlebars.getView('autoreloader');
res.send(view({
title : 'Are we down?',
dashboardNode,
dashboardRefreshInterval : settings.dashboardRefreshInterval,
}));
});


/**
* Internal url called by autorefresh default view
*/
app.get('/dashboard/:dashboard', async function(req, res){
const dashboardNode = req.params.dashboard;

if (dashboardNode === NO_DASHBOARDS_FLAG){
let view = handlebars.getView('noDashboards');
return res.send(view());
}

let dashboard = settings.dashboards[dashboardNode];
if (!dashboard){
let view = handlebars.getView('invalidDashboard');
return res.send(view({
title : dashboardNode
}));
}

let title = dashboard.name;
let view = handlebars.getView('dashboard');

// clone array, we don't want to change source
let dashboardWatchers = arrayHelper.split(dashboard.watchers, ',');
let cronJobs = daemon.cronJobs.slice(0).filter((job)=>{
if (!job.config.enabled)
return null;
if (!dashboardWatchers.includes(job.config.__name))
return null;
return job;
});

const allJobsPassed = cronJobs.filter((job)=>{
return job.isPassing || job.config.enabled === false ? null : job;
}).length === 0;

cronJobs.sort((a,b)=>{
return a.isPassing - b.isPassing || a.config.name.localeCompare(b.config.name)
});

for (let cronJob of cronJobs){
const statusFilePath = path.join(__dirname, settings.logs, cronJob.config.__safeName, 'status.json');

cronJob.status = 'unknown'
cronJob.statusDate = null;

if (!await fs.pathExists(statusFilePath))
continue;

const status = jsonfile.readFileSync(statusFilePath);
cronJob.status = status.status;
cronJob.statusDate = new Date(status.date);

if (cronJob.nextRun){
cronJob.next = Math.floor((cronJob.nextRun.getTime() - new Date().getTime()) / 1000) + 's';
}
}

const now = new Date();

res.send(view({
title,
dashboardNode,
dashboardRefreshInterval : settings.dashboardRefreshInterval,
allJobsPassed,
renderDate: `${now.toLocaleDateString()} ${now.toLocaleTimeString()}`,
jobs : cronJobs
}));
});


/**
* Returns a count of failing jobs. Returns 0 if all jobs are passing.
*/
app.get('/failing', async function(req, res){
let cronJobs = daemon.cronJobs.slice(0); // clone array, we don't want to change source

const failingJobs = cronJobs.filter((job)=>{
return job.isPassing || job.config.enabled === false ? null : job;
});

res.send(failingJobs.length.toString());
});


app.get('/isalive', function(req, res){
res.send('ARE WE DOWN? service is running');
});
}
33 changes: 33 additions & 0 deletions src/routes/status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const logger = require('./../lib/logger').instance(),
daemon = require('./../lib/daemon');

module.exports = function(app){

/**
* Returns a count of failing jobs. Returns 0 if all jobs are passing.
*/
app.get('/status/failing', async function(req, res){
try {

let cronJobs = daemon.cronJobs.slice(0); // clone array, we don't want to change source

const failingJobs = cronJobs.filter((job)=>{
return job.isPassing || job.config.enabled === false ? null : job;
});

res.send(failingJobs.length.toString());
}catch(ex){
res.status(500);
res.end('Something went wrong - check logs for details.');
logger.error.error(ex);
}
});


/**
* Simple alive check
*/
app.get('/status/isalive', function(req, res){
res.send('ARE WE DOWN? service is running');
});
}
File renamed without changes.

0 comments on commit a13621a

Please sign in to comment.