function services(json) {
/// class helpers
// human readable class name
var class_name = {
storage: "Netzlaufwerke",
internet: "Internetzugänge",
game: "Gameserver"
};
// table header
var table = {
storage:
'
Laufwerk | '
+ 'Kapazität | '
+ 'Daten | '
+ 'beschreibbar? | ',
internet: 'Mesh IP | '
+ 'Public IP | '
+ 'Downstream | '
+ 'Upstream | ',
game: 'Server | Spieler | '
};
// table row
var row = {
storage: function(v) {
return ''
+ (v.desc != ''
? (''
+ v.desc + ' '
+ '' + v.url + ''
+ '')
: (''
+ v.url
+ ''))
+ ' | '
+ (v.capacity ? SISuffixify(v.capacity) + 'B' : '')
+ ' | '
+ (v.used ? SISuffixify(v.used) + 'B' : '')
+ ' | '
+ ((v.used && v.capacity)
? (v.used / v.capacity * 100).toPrecision(2) + '%'
: '')
+ ' | '
+ ((v.custom3 == 'true') ? '' : '')
+ ((v.custom3 == 'false') ? '' : '')
+ ' | ';
},
internet: function(v) {
return ''
+ (v.desc != ''
? v.desc
: v.url.substr(7))
+ ' | '
+ (v.public_ip ? v.public_ip : '')
+ ' | '
+ (v.downstream ? SISuffixify(v.downstream * 8) + 'b/s' : '')
+ ' | '
+ (v.upstream ? SISuffixify(v.upstream * 8) + 'b/s' : '')
+ ' | ';
},
game: function(v) {
return ''
+ (v.desc != ''
? (''
+ v.desc + ' '
+ '' + v.url + ''
+ '')
: (''
+ v.url
+ ''))
+ ' | '
+ (v.players ? v.players : '')
+ ' | ';
}
};
// mini row: emit one line per class (not one per service)
var miniRow = {
storage: function(c) {
var cap = sum(c, 'capacity');
return c.length
+ ' Netzlaufwerke'
+ ((cap) ? ' (' + SISuffixify(cap) + 'B)' : '');
},
internet: function(c) {
var up = sum(c, 'upstream');
var down = sum(c, 'downstream');
var stream = function(dir, amount) {
return amount
? (' '
+ SISuffixify(amount * 8) + 'b')
: '';
}
return c.length
+ ' Internetzugänge'
+ ((up || down)
? (' ('
+ stream('down', down)
+ ((up && down) ? ' / ' : '')
+ stream('up', up)
+ ')')
: '');
},
game: function(c) {
var pl = sum(c, 'players');
return c.length
+ ' Gameserver'
+ ((pl) ? ' (' + pl + ' )' : '');
}
};
// aliases for customX values
var parseCustom = {
storage: ['capacity', 'used', 'writable'],
internet: ['public_ip', 'upstream', 'downstream'],
game: ['players']
};
/// parse JSON
var srv = {};
$.each(json, function(k,v) {
var cn = v['class'];
for (var i=1; i<4; i++) {
var custom = v['custom' + i];
if (parseCustom[cn] && parseCustom[cn][i-1]
&& custom && custom != '')
v[parseCustom[cn][i-1]] = custom;
}
if (!srv[cn])
srv[cn] = [];
srv[cn].push(v);
});
/// helpers
function SISuffixify(x) {
suffix_char = ['', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
suffix = 0;
for (; x > 1000; x /= 1000, suffix++);
return x.toPrecision(3) + ' ' + suffix_char[suffix];
}
function sum(container, key) {
var sum = 0;
$.each(container, function(idx, obj) {
if (obj[key])
sum += +obj[key];
});
return sum > 0 ? sum : undefined;
}
/// return: data + drawing functions
return {
data: srv,
drawContainer: function(elem) {
$.each(srv, function(cn, services) {
var tbody = '';
$.each(services, function(i,v) {
tbody = tbody + '' + row[cn](v) + '
';
});
elem.append(
''
+ '
' + class_name[cn] + '
'
+ '
'
+ ''
+ table[cn]
+ '
'
+ '' + tbody + ''
+ '
'
+ '
'
);
})},
drawLine: function(elem) {
$.each(srv, function(cn, services) {
elem.append('' + miniRow[cn](services) + '');
});
}
};
}