feat: can query metrics on instance

This commit is contained in:
geek 2017-08-22 11:55:37 -05:00 committed by Sérgio Ramos
parent ce31f505a0
commit 77acb731ac
5 changed files with 98 additions and 7 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "joyent-cp-gql-schema", "name": "joyent-cp-gql-schema",
"version": "1.5.2", "version": "1.6.0",
"license": "MPL-2.0", "license": "MPL-2.0",
"repository": "github:yldio/joyent-portal", "repository": "github:yldio/joyent-portal",
"main": "index.js", "main": "index.js",

View File

@ -1999,6 +1999,24 @@ class Data extends EventEmitter {
}); });
} }
getMetricsForInstance (instance) {
return ({ names, start, end }) => {
return new Promise((resolve, reject) => {
const options = {
deploymentGroupId: instance.deployment_group_id,
instances: [instance.name],
names,
start,
end
};
this.getMetrics(options, (err, metrics) => {
return err ? reject(err) : resolve(metrics);
});
});
};
}
getInstance ({ id }, cb) { getInstance ({ id }, cb) {
this._db.instances.single({ id }, (err, instance) => { this._db.instances.single({ id }, (err, instance) => {
if (err) { if (err) {
@ -2009,7 +2027,7 @@ class Data extends EventEmitter {
return cb(Boom.notFound()); return cb(Boom.notFound());
} }
cb(null, Transform.fromInstance(instance)); cb(null, Transform.fromInstance({ instance, metrics: this.getMetricsForInstance(instance) }));
}); });
} }
@ -2045,7 +2063,9 @@ class Data extends EventEmitter {
return cb(null, []); return cb(null, []);
} }
cb(null, instances.map(Transform.fromInstance)); cb(null, instances.map((instance) => {
return Transform.fromInstance({ instance, metrics: this.getMetricsForInstance(instance) });
}));
}); });
} }

View File

@ -165,7 +165,7 @@ exports.toPackage = function (packages) {
}; };
exports.fromInstance = function (instance) { exports.fromInstance = function ({ instance, metrics }) {
return { return {
id: instance.id, id: instance.id,
name: instance.name, name: instance.name,
@ -175,7 +175,8 @@ exports.fromInstance = function (instance) {
status: instance.status, status: instance.status,
healthy: instance.healthy, healthy: instance.healthy,
watches: instance.watches, watches: instance.watches,
jobs: instance.jobs jobs: instance.jobs,
metrics
}; };
}; };

View File

@ -39,7 +39,7 @@
"force-array": "^3.1.0", "force-array": "^3.1.0",
"graphi": "^2.2.1", "graphi": "^2.2.1",
"hoek": "^4.1.1", "hoek": "^4.1.1",
"joyent-cp-gql-schema": "^1.5.1", "joyent-cp-gql-schema": "^1.6.0",
"lodash.find": "^4.6.0", "lodash.find": "^4.6.0",
"lodash.flatten": "^4.4.0", "lodash.flatten": "^4.4.0",
"lodash.get": "^4.4.2", "lodash.get": "^4.4.2",

View File

@ -52,7 +52,77 @@ describe('getMetrics()', () => {
expect(err).to.not.be.an.error(); expect(err).to.not.be.an.error();
expect(metrics.length).to.equal(1); expect(metrics.length).to.equal(1);
expect(metrics[0].name).to.equal('AVG_MEM_BYTES'); expect(metrics[0].name).to.equal('AVG_MEM_BYTES');
done(); server.close(() => done());
});
});
});
});
describe('getInstance()', () => {
it('retrieves an instance that can also be used to query for metrics', (done) => {
const server = Http.createServer((req, res) => {
const payload = JSON.stringify({ 'status': 'success', 'data': { 'resultType': 'matrix', 'result': [{ 'metric': { '__name__': 'mem_agg_usage', 'instance': 'joyentportal_compose-api_1', 'job': 'triton' }, 'values': [[1502896217.371, '60518400'], [1502899817.371, '60641280'], [1502903417.371, '60575744']] }] } });
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(payload);
});
server.listen(9090, (err) => {
expect(err).to.not.exist();
const FakePortalData = function () {
this._db = {
instances: {
single: (options, next) => {
expect(options.id).to.exist();
const instance = {
id: options.id,
deployment_group_id: 'deploymentGroup',
service_id: 'service',
machine_id: 'joyentportal_compose-api_1'
};
next(null, instance);
}
}
};
this._triton = {
getInstance: (id, next) => {
const tritonInstance = {
primaryIp: 'localhost'
};
next(null, tritonInstance);
}
};
};
FakePortalData.prototype = PortalData.prototype;
FakePortalData.prototype.getServices = ({ deploymentGroupId, name }, next) => {
const instance = {
machine_id: 'machineId'
};
const service = {
instances: () => { return Promise.resolve([instance]); }
};
next(null, [service]);
};
const fakePortalData = new FakePortalData();
fakePortalData.getInstance({ id: 'joyentportal_compose-api_1' }, (err, instance) => {
expect(err).to.not.be.an.error();
expect(instance).to.exist();
instance.metrics({ names: ['AVG_MEM_BYTES'], start: new Date(Date.now() - 10000).toISOString(), end: new Date(Date.now()).toISOString() }).catch((err) => {
expect(err).to.not.exist();
}).then((metrics) => {
expect(metrics.length).to.equal(1);
expect(metrics[0].name).to.equal('AVG_MEM_BYTES');
server.close(() => done());
});
}); });
}); });
}); });