Skip to content
This repository has been archived by the owner on Aug 12, 2021. It is now read-only.

Commit

Permalink
Fix retrieving metrics for a subject (#1)
Browse files Browse the repository at this point in the history
* Fix retrieving metrics for a subject
  • Loading branch information
RobertHerhold authored and MarkHerhold committed Jan 14, 2017
1 parent d3714f5 commit c39ff2d
Showing 1 changed file with 42 additions and 6 deletions.
48 changes: 42 additions & 6 deletions src/metrics/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,58 @@

const check = require('check-types');
const common = require('./../common');
const pageToGenerator = require('../utils/pageToGenerator');
const ENDPT = 'metrics';

// Metrics module
// @param context: The context to make requests in. Basically, `this`
module.exports = function metrics(context) {
const obj = common(context, ENDPT);

// retrives metrics for a subject
// retrives metrics for a subject, returned as an array
// @param subject: subject to retrieve the metrics for
// @param userOpts: option overrides for this request
// @returns Returns a promise that resolves to a list of metrics
function getSubjectMetrics(subject, userOpts) {
function getSubjectMetricsList(subject, userOpts) {
check.string(subject, 'subject must be a string');

return context.http.makeRequest({
url: `/v1/apps/${context.applicationId}/${ENDPT}/${subject}`
}, userOpts).then(function(body) { return body.data; });
let array = [];
let url = `/v1/apps/${context.applicationId}/${ENDPT}/${subject}`;

function pageFn() {
return context.http.makeRequest({ url }, userOpts).then(function(body) {
array = array.concat(body.data || []); // concatinate the new data

url = body.pages.next;
if (url) {
return pageFn();
} else {
return array;
}
});
}

return pageFn();
}

// retrives metrics for a subject, returned as an iterator
// @param subject: subject to retrieve the metrics for
// @param userOpts: option overrides for this request
// @return An iterator that returns promises that resolve with the next object
function* getAllSubjectMetrics(subject, userOpts) {
check.string(subject, 'subject must be a string');

function pageFn() {
let url = `/v1/apps/${context.applicationId}/${ENDPT}/${subject}`;
return function() {
return context.http.makeRequest({ url }, userOpts).then(function(body) {
url = body.pages.next;
return body;
});
};
}

yield* pageToGenerator(pageFn());
}

// retrieves a single metric for a subject by key
Expand Down Expand Up @@ -54,7 +89,8 @@ module.exports = function metrics(context) {
getAll: obj.getAll,
getList: obj.getList,
create: obj.create,
getSubjectMetrics,
getSubjectMetricsList,
getAllSubjectMetrics,
getIndividualSubjectMetric, // TODO: consider aliasing to "get"
removeIndividualSubjectMetric // TODO: consider aliasing to "remove"
};
Expand Down

0 comments on commit c39ff2d

Please sign in to comment.