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:
parent
dc5e459d3a
commit
83a443dbeb
57
packages/unleash-api/lib/db/client-metrics-store.js
Normal file
57
packages/unleash-api/lib/db/client-metrics-store.js
Normal 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;
|
@ -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 };
|
||||
};
|
@ -4,7 +4,7 @@ const EventStore = require('./event-store');
|
||||
const FeatureToggleStore = require('./feature-toggle-store');
|
||||
const StrategyStore = require('./strategy-store');
|
||||
const clientInstancesDbCreator = require('./client-instances');
|
||||
const clientMetricsDbCreator = require('./client-metrics');
|
||||
const ClientMetricsStore = require('./client-metrics-store');
|
||||
const clientStrategiesDbCreator = require('./client-strategies');
|
||||
|
||||
module.exports = (db) => {
|
||||
@ -15,7 +15,7 @@ module.exports = (db) => {
|
||||
featureToggleStore: new FeatureToggleStore(db, eventStore),
|
||||
strategyStore: new StrategyStore(db, eventStore),
|
||||
clientInstancesDb: clientInstancesDbCreator(db),
|
||||
clientMetricsDb: clientMetricsDbCreator(db),
|
||||
clientMetricsStore: new ClientMetricsStore(db),
|
||||
clientStrategiesDb: clientStrategiesDbCreator(db),
|
||||
};
|
||||
};
|
||||
|
@ -6,12 +6,12 @@ const ClientMetricsService = require('../client-metrics/service');
|
||||
|
||||
module.exports = function (app, config) {
|
||||
const {
|
||||
clientMetricsDb,
|
||||
clientMetricsStore,
|
||||
clientStrategiesDb,
|
||||
clientInstancesDb,
|
||||
} = config;
|
||||
const metrics = new ClientMetrics();
|
||||
const service = new ClientMetricsService(clientMetricsDb);
|
||||
const service = new ClientMetricsService(clientMetricsStore);
|
||||
|
||||
service.on('metrics', (entries) => {
|
||||
entries.forEach((m) => {
|
||||
|
@ -18,7 +18,7 @@ function createApp (options) {
|
||||
featureToggleStore,
|
||||
strategyStore,
|
||||
clientInstancesDb,
|
||||
clientMetricsDb,
|
||||
clientMetricsStore,
|
||||
clientStrategiesDb,
|
||||
} = require('./lib/db')(db);
|
||||
|
||||
@ -30,7 +30,7 @@ function createApp (options) {
|
||||
eventStore,
|
||||
featureToggleStore,
|
||||
strategyStore,
|
||||
clientMetricsDb,
|
||||
clientMetricsStore,
|
||||
clientStrategiesDb,
|
||||
clientInstancesDb,
|
||||
};
|
||||
|
@ -12,7 +12,7 @@ const {
|
||||
strategyStore,
|
||||
clientInstancesDb,
|
||||
clientStrategiesDb,
|
||||
clientMetricsDb,
|
||||
clientMetricsStore,
|
||||
} = require('../../lib/db')(knex);
|
||||
|
||||
|
||||
@ -24,7 +24,7 @@ const app = require('../../app')({
|
||||
strategyStore,
|
||||
clientStrategiesDb,
|
||||
clientInstancesDb,
|
||||
clientMetricsDb,
|
||||
clientMetricsStore,
|
||||
});
|
||||
|
||||
BPromise.promisifyAll(request);
|
||||
|
Loading…
Reference in New Issue
Block a user