diff --git a/packages/cp-gql-mock-server/src/resolvers.js b/packages/cp-gql-mock-server/src/resolvers.js index 1e014555..db24547a 100644 --- a/packages/cp-gql-mock-server/src/resolvers.js +++ b/packages/cp-gql-mock-server/src/resolvers.js @@ -40,8 +40,36 @@ const find = (query = {}) => item => const cleanQuery = (q = {}) => JSON.parse(JSON.stringify(q)); +const getMetrics = query => { + const { + names, + start, + end + } = query; + + const metrics = names.reduce((metrics, name) => + metrics.concat(metricData.filter(md => + md.name === name)), []); + + return Promise.resolve(metrics); +} + const getInstances = query => { - return Promise.resolve(instances.filter(find(cleanQuery(query)))); + const metricsResolver = ({ id }) => query => + getMetrics( + Object.assign({}, query, { + instanceId: id + }) + ); + + const addNestedResolvers = instance => + Object.assign({}, instance, { + metrics: metricsResolver(instance) + }); + + return Promise.resolve( + instances.filter(find(cleanQuery(query))).map(addNestedResolvers) + ); }; const getUnfilteredServices = query => { @@ -528,10 +556,6 @@ const config = ({ return _plain ? config : Promise.resolve(config); }; -const getMetrics = () => { - return Promise.resolve(metricData); -}; - module.exports = { portal: getPortal, deploymentGroups: getDeploymentGroups, @@ -554,6 +578,5 @@ module.exports = { restartServices: (options, request, fn) => fn(null, restartServices(options)), stopServices: (options, request, fn) => fn(null, stopServices(options)), startServices: (options, request, fn) => fn(null, startServices(options)), - config, - metrics: getMetrics + config }; diff --git a/packages/cp-gql-schema/schema.gql b/packages/cp-gql-schema/schema.gql index c261eb54..28c96387 100644 --- a/packages/cp-gql-schema/schema.gql +++ b/packages/cp-gql-schema/schema.gql @@ -167,6 +167,23 @@ enum HealthyStatus { UNAVAILABLE } +type Metric { + time: String! + value: Float! +} + +enum MetricName { + AVG_MEM_BYTES + AVG_LOAD_PERCENT + AGG_NETWORK_BYTES +} + +type InstanceMetric { + instance: String! + name: MetricName! + metrics: [Metric] +} + type Instance { id: ID! name: String! @@ -175,6 +192,7 @@ type Instance { healthy: HealthyStatus watches: [String] jobs: [String] + metrics(names: [MetricName]!, start: String!, end: String!): [InstanceMetric]! } type Datacenter { @@ -184,24 +202,6 @@ type Datacenter { } -type Metric { - time: String! - value: Float! -} - -enum MetricName { - AVG_MEM_BYTES, - AVG_LOAD_PERCENT, - AGG_NETWORK_BYTES -} - -type InstanceMetric { - instance: String! - name: MetricName! - metrics: [Metric] -} - - # we probably wont use some of these queries or arguments # but this way we expose the entire db through gql type Query {