1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00

Added cleaner script

This commit is contained in:
ivaosthu 2016-11-05 13:36:44 +01:00 committed by Ivar Conradi Østhus
parent dc5e459d3a
commit 83a443dbeb
6 changed files with 65 additions and 51 deletions

View File

@ -0,0 +1,57 @@
'use strict';
const logger = require('../logger');
const METRICS_COLUMNS = ['id', 'created_at', 'metrics'];
const TABLE = 'client_metrics';
const mapRow = (row) => ({
id: row.id,
createdAt: row.created_at,
metrics: row.metrics,
});
class ClientMetricsStore {
constructor (db) {
this.db = db;
this._removeMetricsOlderThanOneHour();
setInterval(() => this._removeMetricsOlderThanOneHour(), 60 * 60 * 1000).unref();
}
_removeMetricsOlderThanOneHour () {
this.db(TABLE)
.whereRaw('created_at < now() - interval \'1 hour\'')
.del()
.then((res) => logger.info(`Delted ${res} metrics`));
}
// Insert new client metrics
insert (metrics) {
return this.db(TABLE).insert({ metrics });
}
// Used at startup to load all metrics last week into memory!
getMetricsLastHour () {
return this.db
.select(METRICS_COLUMNS)
.from(TABLE)
.limit(2000)
.whereRaw('created_at > now() - interval \'1 hour\'')
.orderBy('created_at', 'asc')
.map(mapRow);
}
// Used to poll for new metrics
getNewMetrics (lastKnownId) {
return this.db
.select(METRICS_COLUMNS)
.from(TABLE)
.limit(1000)
.where('id', '>', lastKnownId)
.orderBy('created_at', 'asc')
.map(mapRow);
}
};
module.exports = ClientMetricsStore;

View File

@ -1,43 +0,0 @@
'use strict';
const METRICS_COLUMNS = ['id', 'created_at', 'metrics'];
const TABLE = 'client_metrics';
module.exports = function (db) {
// Insert new client metrics
function insert (metrics) {
return db(TABLE).insert({ metrics });
}
// Used at startup to load all metrics last week into memory!
function getMetricsLastHour () {
return db
.select(METRICS_COLUMNS)
.from(TABLE)
.limit(2000)
.whereRaw('created_at > now() - interval \'1 hour\'')
.orderBy('created_at', 'asc')
.map(mapRow);
}
// Used to poll for new metrics
function getNewMetrics (lastKnownId) {
return db
.select(METRICS_COLUMNS)
.from(TABLE)
.limit(1000)
.where('id', '>', lastKnownId)
.orderBy('created_at', 'asc')
.map(mapRow);
}
function mapRow (row) {
return {
id: row.id,
createdAt: row.created_at,
metrics: row.metrics,
};
}
return { insert, getMetricsLastHour, getNewMetrics };
};

View File

@ -4,7 +4,7 @@ const EventStore = require('./event-store');
const FeatureToggleStore = require('./feature-toggle-store'); const FeatureToggleStore = require('./feature-toggle-store');
const StrategyStore = require('./strategy-store'); const StrategyStore = require('./strategy-store');
const clientInstancesDbCreator = require('./client-instances'); const clientInstancesDbCreator = require('./client-instances');
const clientMetricsDbCreator = require('./client-metrics'); const ClientMetricsStore = require('./client-metrics-store');
const clientStrategiesDbCreator = require('./client-strategies'); const clientStrategiesDbCreator = require('./client-strategies');
module.exports = (db) => { module.exports = (db) => {
@ -15,7 +15,7 @@ module.exports = (db) => {
featureToggleStore: new FeatureToggleStore(db, eventStore), featureToggleStore: new FeatureToggleStore(db, eventStore),
strategyStore: new StrategyStore(db, eventStore), strategyStore: new StrategyStore(db, eventStore),
clientInstancesDb: clientInstancesDbCreator(db), clientInstancesDb: clientInstancesDbCreator(db),
clientMetricsDb: clientMetricsDbCreator(db), clientMetricsStore: new ClientMetricsStore(db),
clientStrategiesDb: clientStrategiesDbCreator(db), clientStrategiesDb: clientStrategiesDbCreator(db),
}; };
}; };

View File

@ -6,12 +6,12 @@ const ClientMetricsService = require('../client-metrics/service');
module.exports = function (app, config) { module.exports = function (app, config) {
const { const {
clientMetricsDb, clientMetricsStore,
clientStrategiesDb, clientStrategiesDb,
clientInstancesDb, clientInstancesDb,
} = config; } = config;
const metrics = new ClientMetrics(); const metrics = new ClientMetrics();
const service = new ClientMetricsService(clientMetricsDb); const service = new ClientMetricsService(clientMetricsStore);
service.on('metrics', (entries) => { service.on('metrics', (entries) => {
entries.forEach((m) => { entries.forEach((m) => {

View File

@ -18,7 +18,7 @@ function createApp (options) {
featureToggleStore, featureToggleStore,
strategyStore, strategyStore,
clientInstancesDb, clientInstancesDb,
clientMetricsDb, clientMetricsStore,
clientStrategiesDb, clientStrategiesDb,
} = require('./lib/db')(db); } = require('./lib/db')(db);
@ -30,7 +30,7 @@ function createApp (options) {
eventStore, eventStore,
featureToggleStore, featureToggleStore,
strategyStore, strategyStore,
clientMetricsDb, clientMetricsStore,
clientStrategiesDb, clientStrategiesDb,
clientInstancesDb, clientInstancesDb,
}; };

View File

@ -12,7 +12,7 @@ const {
strategyStore, strategyStore,
clientInstancesDb, clientInstancesDb,
clientStrategiesDb, clientStrategiesDb,
clientMetricsDb, clientMetricsStore,
} = require('../../lib/db')(knex); } = require('../../lib/db')(knex);
@ -24,7 +24,7 @@ const app = require('../../app')({
strategyStore, strategyStore,
clientStrategiesDb, clientStrategiesDb,
clientInstancesDb, clientInstancesDb,
clientMetricsDb, clientMetricsStore,
}); });
BPromise.promisifyAll(request); BPromise.promisifyAll(request);