Skip to content

Commit

Permalink
updates for human time and services
Browse files Browse the repository at this point in the history
  • Loading branch information
vipworld committed Sep 11, 2012
1 parent 024fddc commit e67b18a
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 51 deletions.
4 changes: 3 additions & 1 deletion www/app.ms
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ module.exports = #(server, config) {
, routes = require('./routes')
, http = require('http')
, jade = require('jade')
, path = require('path');
, path = require('path')
, HumanTime = require('./htime');

var app = express();

Expand Down Expand Up @@ -70,6 +71,7 @@ module.exports = #(server, config) {
locals.path = req.path;
locals.query = req.query;
locals.body = req.body;
locals.htime = new HumanTime();
next();
});

Expand Down
47 changes: 2 additions & 45 deletions www/helpers.ms
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = #(app) {

var SUFFIXES = {
min: 'seconds ago',
hour: 'minutes ago',
Expand Down Expand Up @@ -36,50 +37,6 @@ module.exports = #(app) {
'<span style="color: red">' + (down || 'down') + '</span>';
};

app.locals.humanTime = #(time) {
var now = (new Date()).getTime();
var sec = 0, min = 0, hr = 0;
sec = secondsAgo(time);

function secondsAgo(t) {
return Math.floor((now - t)/1000);
}

if (sec >= 60) {
min = Math.floor(sec/60);
sec = sec % 60;
if (min >= 60) {
hr = Math.floor(min/60);
min = min % 60
}
}
var hrMinSec = { hour: hr, minute: min, second: sec };
var timeTxt = '';

for (var division in hrMinSec) {
var t = hrMinSec[division];
if (t > 0 || division == 'second') {
timeTxt += (t.toString() + ' ' + division + (t != 1 ? 's ago ' : ' ago '));
}
}
return timeTxt;
};

app.locals.dateTime = #(t) {
if (typeof t != 'number') return '';
var d = new Date(t);
var date = [d.getMonth(), d.getDay(), d.getFullYear()].join('-');
var pad = #(t) {
if (t < 10) => '0' + t.toString();
else return t.toString();
}
var minute = pad(d.getMinutes());
var second = pad(d.getSeconds());
var time = [d.getHours(), minute, second].join(':');
return date + ' ' + time;
};

app.locals.suffixes = SUFFIXES;

app.locals.suffixes = SUFFIXES;

};
59 changes: 59 additions & 0 deletions www/htime.ms
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
export class HumanTime {
function now() {
return this._now || (this._now = (new Date()).getTime());
}

function secondsAgo(t) {
return Math.floor((self.now() - t)/1000);
}

function timeDistance(time) {
var now = this.now();
var sec = 0, min = 0, hr = 0, day = 0;
sec = this.secondsAgo(time);
if (isNaN(sec)) => '---';

if (sec >= 60) {
min = Math.floor(sec/60);
sec = sec % 60;
if (min >= 60) {
hr = Math.floor(min/60);
min = min % 60;
if (hr >= 24) {
day = Math.floor(hr/24);
hr = hr % 24;
}
}
}
var hrMinSec = [
['d', day],
['h', hr],
['m', min],
['s', sec]
];
var timeTxt = '';
var nStr;

foreach (var division in hrMinSec) {
var t = division[1];
if (t === 0 && division[0] != 's') continue;
nStr = t.toString();
timeTxt += (nStr + division[0] + ' ');
}
return timeTxt;
};

function dateTime(t) {
if (typeof t != 'number') return '';
var d = new Date(t);
var date = [d.getMonth(), d.getDay(), d.getFullYear()].join('-');
var pad = #(t) {
if (t < 10) => '0' + t.toString();
else return t.toString();
}
var minute = pad(d.getMinutes());
var second = pad(d.getSeconds());
var time = [d.getHours(), minute, second].join(':');
return date + ' ' + time;
};
}
10 changes: 5 additions & 5 deletions www/views/_services.jade
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ table.table.table-condensed.table-bordered
th Pass/Total
th Last Pass
th Last Failure
th Avg. Request Time
- var i = 0;
each service, name in server.services
- var sname = name;
Expand All @@ -21,11 +20,12 @@ table.table.table-condensed.table-bordered
- first = false

td(style="padding-left: 20px"): a(href="/services/#{sname}/sensors/#{name}?time=min")= name
td!= health(sensor.isHealthy)
td
!= health(sensor.isHealthy)
span.inlinesparkline= sensor.getAvgResponse('hour').join(',');
td #{sensor.passCount}/#{sensor.totalCount}
td= humanTime(sensor.lastPass);
td= humanTime(sensor.lastFail);
td: span.inlinesparkline= sensor.getAvgResponse('hour').join(',');
td= htime.timeDistance(sensor.lastPass) + ' ago';
td= htime.timeDistance(sensor.lastFail) + ' ago';

:mochi
$(function() {
Expand Down

0 comments on commit e67b18a

Please sign in to comment.