58 lines
2.1 KiB
JavaScript
58 lines
2.1 KiB
JavaScript
|
|
'use strict';
|
||
|
|
|
||
|
|
exports = module.exports = {
|
||
|
|
getByAppId
|
||
|
|
};
|
||
|
|
|
||
|
|
const assert = require('assert'),
|
||
|
|
BoxError = require('./boxerror.js'),
|
||
|
|
safe = require('safetydance'),
|
||
|
|
superagent = require('superagent'),
|
||
|
|
debug = require('debug')('box:graphs');
|
||
|
|
|
||
|
|
const GRAPHITE_RENDER_URL = 'http://127.0.0.1:8417/graphite-web/render';
|
||
|
|
|
||
|
|
async function getByAppId(appId, fromMinutes, noNullPoints) {
|
||
|
|
assert.strictEqual(typeof appId, 'string');
|
||
|
|
assert.strictEqual(typeof fromMinutes, 'number');
|
||
|
|
assert.strictEqual(typeof noNullPoints, 'boolean');
|
||
|
|
|
||
|
|
const timeBucketSize = fromMinutes > (24 * 60) ? (6*60) : 5;
|
||
|
|
|
||
|
|
debug(`getByAppId: ${appId} from ${fromMinutes}`);
|
||
|
|
|
||
|
|
const memoryQuery = {
|
||
|
|
target: `summarize(sum(collectd.localhost.table-${appId}-memory.gauge-rss, collectd.localhost.table-${appId}-memory.gauge-swap), "${timeBucketSize}min", "avg")`,
|
||
|
|
format: 'json',
|
||
|
|
from: `-${fromMinutes}min`,
|
||
|
|
until: 'now'
|
||
|
|
};
|
||
|
|
if (noNullPoints) memoryQuery.noNullPoints = true;
|
||
|
|
|
||
|
|
const [memoryError, memoryResponse] = await safe(superagent.get(GRAPHITE_RENDER_URL)
|
||
|
|
.query(memoryQuery)
|
||
|
|
.timeout(30 * 1000)
|
||
|
|
.ok(() => true));
|
||
|
|
|
||
|
|
if (memoryError) throw new BoxError(BoxError.NETWORK_ERROR, memoryError.message);
|
||
|
|
if (memoryResponse.status !== 200) throw new BoxError(BoxError.EXTERNAL_ERROR, `Unknown error: ${memoryError.message}`);
|
||
|
|
|
||
|
|
const diskQuery = {
|
||
|
|
target: `summarize(collectd.localhost.du-${appId}.capacity-usage, "${timeBucketSize}min", "avg")`,
|
||
|
|
format: 'json',
|
||
|
|
from: `-${fromMinutes}min`,
|
||
|
|
until: 'now'
|
||
|
|
};
|
||
|
|
if (noNullPoints) diskQuery.noNullPoints = true;
|
||
|
|
|
||
|
|
const [diskError, diskResponse] = await safe(superagent.get(GRAPHITE_RENDER_URL)
|
||
|
|
.query(diskQuery)
|
||
|
|
.timeout(30 * 1000)
|
||
|
|
.ok(() => true));
|
||
|
|
|
||
|
|
if (diskError) throw new BoxError(BoxError.NETWORK_ERROR, diskError.message);
|
||
|
|
if (diskResponse.status !== 200) throw new BoxError(BoxError.EXTERNAL_ERROR, `Unknown error: ${diskError.message}`);
|
||
|
|
|
||
|
|
return { memory: memoryResponse.body[0], disk: diskResponse.body[0] };
|
||
|
|
}
|