From 5bf0b3658863b3ddab7eb405c1b4cbc25ecebdb9 Mon Sep 17 00:00:00 2001 From: sveisvei Date: Sun, 4 Dec 2016 14:09:37 +0100 Subject: [PATCH] fix lint --- lib/app.js | 6 ++-- lib/client-metrics/client-metrics.test.js | 11 ++++---- lib/client-metrics/index.js | 17 +++++------ lib/data-helper/legacy-feature-mapper.js | 6 ++-- lib/data-helper/legacy-feature-mapper.test.js | 2 +- lib/db/client-instance-store.js | 8 +++--- lib/db/client-metrics-db.js | 4 +-- lib/db/client-metrics-store.js | 11 ++++---- lib/db/client-metrics-store.test.js | 16 +++++++---- lib/events.js | 4 ++- lib/logger.js | 4 +-- lib/metrics.js | 8 ++++-- lib/metrics.test.js | 8 +++--- lib/options.js | 9 +++--- lib/options.test.js | 15 +++++----- lib/routes/backstage.js | 2 +- lib/routes/feature.js | 2 +- lib/routes/metrics-schema.js | 2 +- lib/routes/metrics.js | 26 ++++++++--------- lib/routes/route-utils.js | 6 ++-- lib/server-impl.js | 1 - package.json | 4 +++ test/unit/routes/metrics.test.js | 28 +++++++++---------- 23 files changed, 106 insertions(+), 94 deletions(-) diff --git a/lib/app.js b/lib/app.js index 5a414b6797..72fc1bf848 100644 --- a/lib/app.js +++ b/lib/app.js @@ -12,7 +12,7 @@ const routes = require('./routes'); const path = require('path'); const errorHandler = require('errorhandler'); -const {REQUEST_TIME} = require('./events'); +const { REQUEST_TIME } = require('./events'); module.exports = function (config) { const app = express(); @@ -42,12 +42,12 @@ module.exports = function (config) { app.use(bodyParser.json({ strict: false })); - if(config.enableRequestLogger) { + if (config.enableRequestLogger) { app.use(log4js.connectLogger(logger, { format: ':status :method :url :response-timems', level: 'auto', // 3XX=WARN, 4xx/5xx=ERROR })); - } + } // Setup API routes const apiRouter = express.Router(); // eslint-disable-line new-cap diff --git a/lib/client-metrics/client-metrics.test.js b/lib/client-metrics/client-metrics.test.js index 3c74189365..53da11c9dd 100644 --- a/lib/client-metrics/client-metrics.test.js +++ b/lib/client-metrics/client-metrics.test.js @@ -65,7 +65,7 @@ test.cb('data should expire', (t) => { test('should listen to metrics from store', t => { const store = new EventEmitter(); const metrics = new UnleashClientMetrics(store); - store.emit('metrics', { + store.emit('metrics', { appName, instanceId, bucket: { @@ -146,16 +146,15 @@ test('should build up list of seend toggles when new metrics arrives', t => { }); - test('should handle a lot of toggles', t => { const store = new EventEmitter(); const metrics = new UnleashClientMetrics(store); const toggleCounts = {}; - for (let i=0; i<100; i++) { - toggleCounts[`toggle${i}`] = {yes: i, no: i} + for (let i = 0; i < 100; i++) { + toggleCounts[`toggle${i}`] = { yes: i, no: i }; } - + store.emit('metrics', { appName, instanceId, @@ -170,4 +169,4 @@ test('should handle a lot of toggles', t => { t.truthy(seenToggles.length === 100); metrics.destroy(); -}); \ No newline at end of file +}); diff --git a/lib/client-metrics/index.js b/lib/client-metrics/index.js index bf2df8cca2..9b6b40bd6b 100644 --- a/lib/client-metrics/index.js +++ b/lib/client-metrics/index.js @@ -5,7 +5,6 @@ const TTLList = require('./ttl-list.js'); module.exports = class UnleashClientMetrics { constructor (clientMetricsStore) { - this.globalCount = 0; this.apps = {}; @@ -40,11 +39,11 @@ module.exports = class UnleashClientMetrics { Object.keys(this.apps).forEach(appName => { const seenToggles = Object.keys(this.apps[appName].seenToggles); const metricsCount = this.apps[appName].count; - apps.push({appName, seenToggles, metricsCount}) + apps.push({ appName, seenToggles, metricsCount }); }); return apps; } - getSeenTogglesByAppName(appName) { + getSeenTogglesByAppName (appName) { return this.apps[appName] ? Object.keys(this.apps[appName].seenToggles) : []; } @@ -57,12 +56,12 @@ module.exports = class UnleashClientMetrics { addPayload (data) { const { appName, bucket } = data; - const app = this.getApp(appName) - this.addBucket(app, data.bucket); + const app = this.getApp(appName); + this.addBucket(app, bucket); } - getApp(appName) { - this.apps[appName] = this.apps[appName] || {seenToggles: {}, count: 0}; + getApp (appName) { + this.apps[appName] = this.apps[appName] || { seenToggles: {}, count: 0 }; return this.apps[appName]; } @@ -89,7 +88,9 @@ module.exports = class UnleashClientMetrics { } addSeenToggles (app, toggleNames) { - toggleNames.forEach(t => app.seenToggles[t] = true); + toggleNames.forEach(t => { + app.seenToggles[t] = true; + }); } destroy () { diff --git a/lib/data-helper/legacy-feature-mapper.js b/lib/data-helper/legacy-feature-mapper.js index d5895b79f6..7809cf9171 100644 --- a/lib/data-helper/legacy-feature-mapper.js +++ b/lib/data-helper/legacy-feature-mapper.js @@ -36,11 +36,9 @@ function toNewFormat (feature) { description: feature.description, enabled: feature.enabled, strategies: feature.strategies, - createdAt: feature.createdAt - } - + createdAt: feature.createdAt, + }; } - } module.exports = { addOldFields, toNewFormat }; diff --git a/lib/data-helper/legacy-feature-mapper.test.js b/lib/data-helper/legacy-feature-mapper.test.js index b64efd5402..3b4842285c 100644 --- a/lib/data-helper/legacy-feature-mapper.test.js +++ b/lib/data-helper/legacy-feature-mapper.test.js @@ -27,7 +27,7 @@ test('adds old fields to feature', t => { test('adds old fields to feature handles missing strategies field', t => { const feature = { name: 'test', - enabled: 0 + enabled: 0, }; const mappedFeature = mapper.addOldFields(feature); diff --git a/lib/db/client-instance-store.js b/lib/db/client-instance-store.js index fd33b5b659..6459f0e8e4 100644 --- a/lib/db/client-instance-store.js +++ b/lib/db/client-instance-store.js @@ -13,10 +13,10 @@ const mapRow = (row) => ({ createdAt: row.created_at, }); -const mapAppsRow = (row) => ({ - appName: row.app_name, - createdAt: row.created_at, -}); +// const mapAppsRow = (row) => ({ +// appName: row.app_name, +// createdAt: row.created_at, +// }); class ClientInstanceStore { diff --git a/lib/db/client-metrics-db.js b/lib/db/client-metrics-db.js index 0439cb9884..de6a887dd8 100644 --- a/lib/db/client-metrics-db.js +++ b/lib/db/client-metrics-db.js @@ -14,8 +14,8 @@ const mapRow = (row) => ({ class ClientMetricsDb { constructor (db) { this.db = db; - - //Clear old metrics regulary + + // Clear old metrics regulary setTimeout(() => this.removeMetricsOlderThanOneHour(), 10).unref(); setInterval(() => this.removeMetricsOlderThanOneHour(), 60 * 1000).unref(); } diff --git a/lib/db/client-metrics-store.js b/lib/db/client-metrics-store.js index c2b042798b..293906a054 100644 --- a/lib/db/client-metrics-store.js +++ b/lib/db/client-metrics-store.js @@ -12,9 +12,8 @@ class ClientMetricsStore extends EventEmitter { super(); this.metricsDb = metricsDb; this.highestIdSeen = 0; - this.timer; - //Build internal state + // Build internal state metricsDb.getMetricsLastHour() .then((metrics) => this._emitMetrics(metrics)) .then(() => this._startPoller(pollInterval)) @@ -27,9 +26,9 @@ class ClientMetricsStore extends EventEmitter { this.timer.unref(); } - _fetchNewAndEmit() { + _fetchNewAndEmit () { this.metricsDb.getNewMetrics(this.highestIdSeen) - .then((metrics) => this._emitMetrics(metrics)) + .then((metrics) => this._emitMetrics(metrics)); } _emitMetrics (metrics) { @@ -41,10 +40,10 @@ class ClientMetricsStore extends EventEmitter { // Insert new client metrics insert (metrics) { - return this.metricsDb.insert(metrics) + return this.metricsDb.insert(metrics); } - destroy () { + destroy () { try { clearInterval(this.timer); } catch (e) {} diff --git a/lib/db/client-metrics-store.test.js b/lib/db/client-metrics-store.test.js index e4341ffbd0..5364b1eca5 100644 --- a/lib/db/client-metrics-store.test.js +++ b/lib/db/client-metrics-store.test.js @@ -5,15 +5,19 @@ const ClientMetricStore = require('./client-metrics-store'); const sinon = require('sinon'); function getMockDb () { - const list = [{ id: 4, metrics: {appName: 'test'} }, { id: 3, metrics: {appName: 'test'} }, { id: 2, metrics: {appName: 'test'} }]; + const list = [ + { id: 4, metrics: { appName: 'test' } }, + { id: 3, metrics: { appName: 'test' } }, + { id: 2, metrics: { appName: 'test' } }, + ]; return { getMetricsLastHour () { - return Promise.resolve([{ id: 1, metrics: {appName: 'test'} }]); + return Promise.resolve([{ id: 1, metrics: { appName: 'test' } }]); }, - getNewMetrics (v) { + getNewMetrics () { return Promise.resolve([list.pop() || { id: 0 }]); - } + }, }; } @@ -22,7 +26,7 @@ test.cb('should call database on startup', (t) => { const mock = getMockDb(); const store = new ClientMetricStore(mock); - + t.plan(2); @@ -30,7 +34,7 @@ test.cb('should call database on startup', (t) => { t.true(store.highestIdSeen === 1); t.true(metrics.appName === 'test'); store.destroy(); - + t.end(); }); }); diff --git a/lib/events.js b/lib/events.js index f133328219..bf49d8731d 100644 --- a/lib/events.js +++ b/lib/events.js @@ -1,3 +1,5 @@ +'use strict'; + module.exports = { REQUEST_TIME: 'request_time', -} \ No newline at end of file +}; diff --git a/lib/logger.js b/lib/logger.js index 47e8369aa7..fde8342a70 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -2,9 +2,9 @@ const log4js = require('log4js'); log4js.configure({ - appenders: [ + appenders: [ { type: 'console' }, - ] + ], }); const logger = log4js.getLogger('unleash'); diff --git a/lib/metrics.js b/lib/metrics.js index d72a4c7526..599facbff5 100644 --- a/lib/metrics.js +++ b/lib/metrics.js @@ -1,3 +1,5 @@ +'use strict'; + const events = require('./events'); exports.startMonitoring = (enable, eventBus) => { @@ -6,12 +8,12 @@ exports.startMonitoring = (enable, eventBus) => { } const client = require('prom-client'); - + const requestDuration = new client.Summary('http_request_duration_milliseconds', 'App response time', ['path', 'method', 'status'], { percentiles: [0.1, 0.5, 0.9, 0.99], }); - eventBus.on(events.REQUEST_TIME, ({path, method, time, statusCode}) => { + eventBus.on(events.REQUEST_TIME, ({ path, method, time, statusCode }) => { requestDuration.labels(path, method, statusCode).observe(time); }); -}; \ No newline at end of file +}; diff --git a/lib/metrics.test.js b/lib/metrics.test.js index dea82ba8bc..26161b10bb 100644 --- a/lib/metrics.test.js +++ b/lib/metrics.test.js @@ -1,3 +1,4 @@ +'use strict'; const test = require('ava'); const { EventEmitter } = require('events'); @@ -9,8 +10,7 @@ const prometheusRegister = require('prom-client/lib/register'); test('should collect metrics for requests', t => { startMonitoring(true, eventBus); eventBus.emit(REQUEST_TIME, { path: 'somePath', method: 'GET', statusCode: 200, time: 1337 }); - + const metrics = prometheusRegister.metrics(); - t.regex(metrics, /http_request_duration_milliseconds{quantile="0.99",status="200",method="GET",path="somePath"} 1337/) - -}); \ No newline at end of file + t.regex(metrics, /http_request_duration_milliseconds{quantile="0.99",status="200",method="GET",path="somePath"} 1337/); +}); diff --git a/lib/options.js b/lib/options.js index 99cc8f84db..1eafe98a3f 100644 --- a/lib/options.js +++ b/lib/options.js @@ -1,4 +1,5 @@ 'use strict'; + const { publicFolder } = require('unleash-frontend'); const isDev = () => process.env.NODE_ENV === 'development'; @@ -9,7 +10,7 @@ const DEFAULT_OPTIONS = { baseUriPath: process.env.BASE_URI_PATH || '', serverMetrics: true, publicFolder, - enableRequestLogger: isDev() ? true : false + enableRequestLogger: isDev(), }; module.exports = { @@ -17,7 +18,7 @@ module.exports = { const options = Object.assign({}, DEFAULT_OPTIONS, opts); // If we are running in development we should assume local db - if(isDev() && !options.databaseUrl) { + if (isDev() && !options.databaseUrl) { options.databaseUrl = 'postgres://unleash_user:passord@localhost:5432/unleash'; } @@ -25,5 +26,5 @@ module.exports = { throw new Error('You must either pass databaseUrl option or set environemnt variable DATABASE_URL'); } return options; - } -} \ No newline at end of file + }, +}; diff --git a/lib/options.test.js b/lib/options.test.js index 3b104277ee..c6400659e8 100644 --- a/lib/options.test.js +++ b/lib/options.test.js @@ -3,30 +3,31 @@ const test = require('ava'); delete process.env.DATABASE_URL; -const { createOptions } = require('./options'); test('should require DATABASE_URI', t => { + const { createOptions } = require('./options'); + t.throws(() => { - const options = createOptions({}); + createOptions({}); }); }); test('should set default databaseUrl for develpment', t => { - process.env.NODE_ENV = 'development'; + process.env.NODE_ENV = 'development'; const { createOptions } = require('./options'); - + const options = createOptions({}); t.true(options.databaseUrl === 'postgres://unleash_user:passord@localhost:5432/unleash'); }); test('should not override provided options', t => { - process.env.DATABASE_URL = 'test'; + process.env.DATABASE_URL = 'test'; process.env.NODE_ENV = 'production'; const { createOptions } = require('./options'); - const options = createOptions({databaseUrl: 'test', port: 1111}); + const options = createOptions({ databaseUrl: 'test', port: 1111 }); t.true(options.databaseUrl === 'test'); t.true(options.port === 1111); -}); \ No newline at end of file +}); diff --git a/lib/routes/backstage.js b/lib/routes/backstage.js index e8edabb757..b73b5f8e9e 100644 --- a/lib/routes/backstage.js +++ b/lib/routes/backstage.js @@ -3,7 +3,7 @@ const prometheusRegister = require('prom-client/lib/register'); module.exports = function (app, config) { - if(config.serverMetrics) { + if (config.serverMetrics) { app.get('/internal-backstage/prometheus', (req, res) => { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end(prometheusRegister.metrics()); diff --git a/lib/routes/feature.js b/lib/routes/feature.js index ae59d8a507..7531d78c2d 100644 --- a/lib/routes/feature.js +++ b/lib/routes/feature.js @@ -70,7 +70,7 @@ module.exports = function (app, config) { validateRequest(req) .then(validateFormat) .then(validateUniqueName) - .then((req) => legacyFeatureMapper.toNewFormat(req.body)) + .then((_req) => legacyFeatureMapper.toNewFormat(_req.body)) .then(validateStrategy) .then((featureToggle) => eventStore.store({ type: eventType.featureCreated, diff --git a/lib/routes/metrics-schema.js b/lib/routes/metrics-schema.js index b660fad914..249c79a448 100644 --- a/lib/routes/metrics-schema.js +++ b/lib/routes/metrics-schema.js @@ -23,4 +23,4 @@ const clientRegisterSchema = joi.object().keys({ interval: joi.number().required(), }); -module.exports = { clientMetricsSchema, clientRegisterSchema } +module.exports = { clientMetricsSchema, clientRegisterSchema }; diff --git a/lib/routes/metrics.js b/lib/routes/metrics.js index 2c5f71b9b0..a38dda8955 100644 --- a/lib/routes/metrics.js +++ b/lib/routes/metrics.js @@ -14,7 +14,7 @@ module.exports = function (app, config) { } = config.stores; const metrics = new ClientMetrics(clientMetricsStore); - + app.get('/client/seen-toggles', (req, res) => { const seenAppToggles = metrics.getAppsWitToggles(); res.json(seenAppToggles); @@ -41,7 +41,7 @@ module.exports = function (app, config) { clientIp, })) .catch(err => logger.error('failed to store metrics', err)); - + res.status(202).end(); }); }); @@ -72,7 +72,7 @@ module.exports = function (app, config) { app.get('/client/strategies', (req, res) => { const appName = req.query.appName; - if(appName) { + if (appName) { clientStrategyStore.getByAppName(appName) .then(data => res.json(data)) .catch(err => catchLogAndSendErrorResponse(err, res)); @@ -86,13 +86,13 @@ module.exports = function (app, config) { app.get('/client/applications/', (req, res) => { clientInstanceStore.getApplications() .then(apps => { - const applications = apps.map(({appName}) => ({ - appName: appName, + const applications = apps.map(({ appName }) => ({ + appName, links: { - appDetails: `/api/client/applications/${appName}` - } - })) - res.json({applications}) + appDetails: `/api/client/applications/${appName}`, + }, + })); + res.json({ applications }); }) .catch(err => catchLogAndSendErrorResponse(err, res)); }); @@ -101,10 +101,10 @@ module.exports = function (app, config) { const appName = req.params.appName; const seenToggles = metrics.getSeenTogglesByAppName(appName); Promise.all([ - clientInstanceStore.getByAppName(appName), - clientStrategyStore.getByAppName(appName) - ]) - .then(([instances, strategies]) => res.json({appName, instances, strategies, seenToggles})) + clientInstanceStore.getByAppName(appName), + clientStrategyStore.getByAppName(appName), + ]) + .then(([instances, strategies]) => res.json({ appName, instances, strategies, seenToggles })) .catch(err => catchLogAndSendErrorResponse(err, res)); }); }; diff --git a/lib/routes/route-utils.js b/lib/routes/route-utils.js index 8ab3aa05b5..22aac4f6e3 100644 --- a/lib/routes/route-utils.js +++ b/lib/routes/route-utils.js @@ -1,8 +1,10 @@ +'use strict'; + const logger = require('../logger'); const catchLogAndSendErrorResponse = (err, res) => { logger.error(err); res.status(500).end(); -} +}; -module.exports = { catchLogAndSendErrorResponse }; \ No newline at end of file +module.exports = { catchLogAndSendErrorResponse }; diff --git a/lib/server-impl.js b/lib/server-impl.js index e36a2257a6..2eaa09ebb5 100644 --- a/lib/server-impl.js +++ b/lib/server-impl.js @@ -5,7 +5,6 @@ const { EventEmitter } = require('events'); const logger = require('./logger'); const migrator = require('../migrator'); const getApp = require('./app'); -const events = require('./events'); const { startMonitoring } = require('./metrics'); const { createStores } = require('./db'); diff --git a/package.json b/package.json index bf46bb5891..a0e1aaf943 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,8 @@ "start:dev:pg-chain": "export DATABASE_URL=postgres://$PGUSER:$PGPASSWORD@localhost:$PGPORT/postgres ; db-migrate up && npm run start:dev", "db-migrate": "db-migrate up", "db-migrate:down": "db-migrate down", + "lint": "eslint lib", + "pretest": "npm run lint", "test": "PORT=4243 ava test lib/*.test.js lib/**/*.test.js", "test:docker": "./scripts/docker-postgres.sh", "test:watch": "npm run test -- --watch", @@ -82,6 +84,8 @@ "@types/node": "^6.0.46", "ava": "^0.17.0", "coveralls": "^2.11.15", + "eslint": "^3.11.1", + "eslint-config-finn": "^1.0.0-beta.1", "nyc": "^9.0.1", "sinon": "^1.17.5", "superagent": "^2.3.0", diff --git a/test/unit/routes/metrics.test.js b/test/unit/routes/metrics.test.js index 1ff132cedf..6600f46b6c 100644 --- a/test/unit/routes/metrics.test.js +++ b/test/unit/routes/metrics.test.js @@ -23,7 +23,7 @@ function getSetup () { return { request: supertest(app), - stores + stores, }; } @@ -66,7 +66,7 @@ test('should validate client metrics', () => { const { request } = getSetup(); return request .post('/api/client/metrics') - .send({random: 'blush'}) + .send({ random: 'blush' }) .expect(400); }); @@ -99,7 +99,7 @@ test('should return seen toggles even when there is nothing', t => { test('should return list of seen-toggles per app', t => { const { request, stores } = getSetup(); - const appName = 'asd!23' + const appName = 'asd!23'; stores.clientMetricsStore.emit('metrics', { appName, instanceId: 'instanceId', @@ -107,8 +107,8 @@ test('should return list of seen-toggles per app', t => { start: new Date(), stop: new Date(), toggles: { - toggleX: {yes: 123,no: 0}, - toggleY: {yes: 123,no: 0} + toggleX: { yes: 123, no: 0 }, + toggleY: { yes: 123, no: 0 } }, }, }); @@ -117,7 +117,7 @@ test('should return list of seen-toggles per app', t => { .get('/api/client/seen-toggles') .expect(200) .expect((res) => { - const seenAppsWithToggles = res.body; + const seenAppsWithToggles = res.body; t.true(seenAppsWithToggles.length === 1); t.true(seenAppsWithToggles[0].appName === appName); t.true(seenAppsWithToggles[0].seenToggles.length === 2); @@ -128,12 +128,12 @@ test('should return feature-toggles metrics even when there is nothing', t => { const { request } = getSetup(); return request .get('/api/client/metrics/feature-toggles') - .expect(200) + .expect(200); }); test('should return metrics for all toggles', t => { const { request, stores } = getSetup(); - const appName = 'asd!23' + const appName = 'asd!23'; stores.clientMetricsStore.emit('metrics', { appName, instanceId: 'instanceId', @@ -141,8 +141,8 @@ test('should return metrics for all toggles', t => { start: new Date(), stop: new Date(), toggles: { - toggleX: {yes: 123,no: 0}, - toggleY: {yes: 123,no: 0} + toggleX: { yes: 123, no: 0 }, + toggleY: { yes: 123, no: 0 }, }, }, }); @@ -151,8 +151,8 @@ test('should return metrics for all toggles', t => { .get('/api/client/metrics/feature-toggles') .expect(200) .expect((res) => { - - const metrics = res.body; + + const metrics = res.body; t.true(metrics.lastHour !== undefined); t.true(metrics.lastMinute !== undefined); }); @@ -160,7 +160,7 @@ test('should return metrics for all toggles', t => { test('should return list of client strategies', t => { - const { request, stores } = getSetup(); + const { request } = getSetup(); return request .get('/api/client/strategies') .expect(200) @@ -170,7 +170,7 @@ test('should return list of client strategies', t => { }); test('should return list of client applications', t => { - const { request, stores } = getSetup(); + const { request } = getSetup(); return request .get('/api/client/applications') .expect(200)