From 17def8e304e2574158f8b94674aa178ad1140d1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=CC=81rgio=20Ramos?= Date: Mon, 28 Aug 2017 21:31:38 +0100 Subject: [PATCH] style: static methods --- packages/portal-api/lib/data/index.js | 18 +++++++------ .../portal-api/lib/watch/container-pilot.js | 16 ++++++------ packages/portal-api/lib/watch/machines.js | 8 +++--- packages/portal-api/server.js | 12 ++++----- packages/portal-api/test/_mocks/data.js | 26 +++++++++---------- packages/portal-api/test/index.js | 2 +- 6 files changed, 42 insertions(+), 40 deletions(-) diff --git a/packages/portal-api/lib/data/index.js b/packages/portal-api/lib/data/index.js index b817c58d..aa0a1ec8 100644 --- a/packages/portal-api/lib/data/index.js +++ b/packages/portal-api/lib/data/index.js @@ -675,7 +675,7 @@ class Data extends EventEmitter { }); } - _calcCurrentScale ({ config, currentVersion }, cb) { + static _calcCurrentScale ({ config, currentVersion }, cb) { return config.map(({ name }) => { const currentScale = Find(ForceArray(currentVersion ? currentVersion.scale : []), [ 'serviceName', @@ -696,7 +696,7 @@ class Data extends EventEmitter { console.error(err); } - this._calcCurrentScale({ config, currentVersion }, cb); + Data._calcCurrentScale({ config, currentVersion }, cb); }; if (!this._triton) { @@ -1534,7 +1534,7 @@ class Data extends EventEmitter { return Object.assign({}, branch, { instances: this._instancesFilter(branch.instances) }); - }).filter(({ name }) => name); + }).filter(({ name }) => { return name; }); return cb(null, Transform.fromService({ service, @@ -1602,7 +1602,7 @@ class Data extends EventEmitter { return Object.assign({}, branch, { instances: this._instancesFilter(branch.instances) }); - }).filter(({ name }) => name); + }).filter(({ name }) => { return name; }); return Transform.fromService({ service, @@ -2564,9 +2564,11 @@ class Data extends EventEmitter { metric.name = metricNameEnum[i]; } - metric.metrics = metric.metrics.map((entry) => Object.assign(entry, { - time: entry.time.toISOString() - })); + metric.metrics = metric.metrics.map((entry) => { + return Object.assign(entry, { + time: entry.time.toISOString() + }); + }); return Object.assign(metric, { start: metric.metrics[0].time, @@ -2587,7 +2589,7 @@ class Data extends EventEmitter { prometheus.getMetrics({ names: formattedNames, - instances: ctx.machines.map(({ name }) => name), + instances: ctx.machines.map(({ name }) => { return name; }), start, end }, (err, metrics) => { diff --git a/packages/portal-api/lib/watch/container-pilot.js b/packages/portal-api/lib/watch/container-pilot.js index 22734521..ee4538af 100644 --- a/packages/portal-api/lib/watch/container-pilot.js +++ b/packages/portal-api/lib/watch/container-pilot.js @@ -311,7 +311,7 @@ module.exports = class ContainerPilotWatcher extends Events { }, cb); } - _resolveServiceConnections ({ services, service }) { + static _resolveServiceConnections ({ services, service }) { const watches = Uniq( Flatten( ForceArray(service.instances).map(({ watches }) => { @@ -348,7 +348,7 @@ module.exports = class ContainerPilotWatcher extends Events { }); } - _resolveInstanceHealth ({ name }, instance) { + static _resolveInstanceHealth ({ name }, instance) { if (!instance) { return 'UNAVAILABLE'; } @@ -379,7 +379,7 @@ module.exports = class ContainerPilotWatcher extends Events { return 'UNKNOWN'; } - _resolveServiceBranches ({ name, slug, instances }) { + static _resolveServiceBranches ({ name, slug, instances }) { if (instances.length <= 1) { return []; } @@ -437,7 +437,7 @@ module.exports = class ContainerPilotWatcher extends Events { return Object.values(branches).concat(defaultBranch); } - _resolveDeploymentGroups (dgs) { + static _resolveDeploymentGroups (dgs) { return dgs .filter(Boolean) .map((dg) => { @@ -451,7 +451,7 @@ module.exports = class ContainerPilotWatcher extends Events { }); return Object.assign({}, instance, { - healthy: this._resolveInstanceHealth(service, instance), + healthy: ContainerPilotWatcher._resolveInstanceHealth(service, instance), jobs, watches }); @@ -464,8 +464,8 @@ module.exports = class ContainerPilotWatcher extends Events { return Object.assign({}, dg, { services: ForceArray(dg.services).map((service) => { return Object.assign({}, service, { - branches: this._resolveServiceBranches(service), - connections: this._resolveServiceConnections({ + branches: ContainerPilotWatcher._resolveServiceBranches(service), + connections: ContainerPilotWatcher._resolveServiceConnections({ services: dg.services, service }) @@ -485,7 +485,7 @@ module.exports = class ContainerPilotWatcher extends Events { this.emit('error', err); } - const dgs = this._resolveDeploymentGroups( + const dgs = ContainerPilotWatcher._resolveDeploymentGroups( ForceArray((results || {}).successes) ); diff --git a/packages/portal-api/lib/watch/machines.js b/packages/portal-api/lib/watch/machines.js index 44a0b644..bf6910b6 100644 --- a/packages/portal-api/lib/watch/machines.js +++ b/packages/portal-api/lib/watch/machines.js @@ -141,7 +141,7 @@ module.exports = class MachineWatcher { }); } - getInstances (service, cb) { + static getInstances (service, cb) { service.instances() .then((instances) => { return cb(null, instances); @@ -151,7 +151,7 @@ module.exports = class MachineWatcher { }); } - getVersion (deploymentGroup, cb) { + static getVersion (deploymentGroup, cb) { if (typeof deploymentGroup.version !== 'function') { return cb(new Error('version must be a function')); } @@ -578,12 +578,12 @@ module.exports = class MachineWatcher { service, deploymentGroup }, cb) => { - this.getInstances(service, (err, instances = []) => { + MachineWatcher.getInstances(service, (err, instances = []) => { if (err && !isNotFound(err)) { return cb(err); } - this.getVersion(deploymentGroup, (err, version) => { + MachineWatcher.getVersion(deploymentGroup, (err, version) => { if (err) { return cb(err); } diff --git a/packages/portal-api/server.js b/packages/portal-api/server.js index 3c5666f5..0b1861da 100644 --- a/packages/portal-api/server.js +++ b/packages/portal-api/server.js @@ -42,13 +42,13 @@ server.register([ options: { namespace: 'portal', subsystem: 'api' } } ], - (err) => { +(err) => { + handlerError(err); + server.start((err) => { handlerError(err); - server.start((err) => { - handlerError(err); - console.log(`server started at http://localhost:${server.info.port}`); - }); - } + console.log(`server started at http://localhost:${server.info.port}`); + }); +} ); function handlerError (error) { diff --git a/packages/portal-api/test/_mocks/data.js b/packages/portal-api/test/_mocks/data.js index 1b92efdb..8bcc8af8 100644 --- a/packages/portal-api/test/_mocks/data.js +++ b/packages/portal-api/test/_mocks/data.js @@ -6,25 +6,25 @@ module.exports = ({ deploymentGroups = [], services = [], instances = [] }) => { const getInstances = (serviceId) => { return (opts, cb) => { cb(null, instances - .filter((instance) => { - return instance.serviceId === serviceId; - }) - ); + .filter((instance) => { + return instance.serviceId === serviceId; + }) + ); }; }; const getServices = (deploymentGroupId) => { return (opts, cb) => { cb(null, services - .filter((service) => { - return service.deploymentGroupId === deploymentGroupId; - }) - .map((service) => { - return Object.assign({}, service, { - instances: getInstances(service.id) - }); - }) - ); + .filter((service) => { + return service.deploymentGroupId === deploymentGroupId; + }) + .map((service) => { + return Object.assign({}, service, { + instances: getInstances(service.id) + }); + }) + ); }; }; diff --git a/packages/portal-api/test/index.js b/packages/portal-api/test/index.js index 4384df55..09a97f8d 100644 --- a/packages/portal-api/test/index.js +++ b/packages/portal-api/test/index.js @@ -97,7 +97,7 @@ describe('graphql', () => { server.inject({ method: 'POST', url: '/graphql', payload }, (res) => { expect(res.statusCode).to.equal(200); - console.log(res.result.data) + console.log(res.result.data); const deploymentGroup = res.result.data.portal.deploymentGroups[0]; expect(deploymentGroup.name).to.equal('test'); expect(deploymentGroup.services[0].name).to.equal('service');