-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b0d7b26
commit a13621a
Showing
5 changed files
with
132 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
})); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.