1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/lib/db/client-instance-store.js

139 lines
3.3 KiB
JavaScript
Raw Normal View History

2016-11-04 09:03:13 +01:00
/* eslint camelcase: "off" */
2016-11-04 09:03:13 +01:00
'use strict';
2019-08-04 11:15:40 +02:00
const metricsHelper = require('../metrics-helper');
const { DB_TIME } = require('../events');
2017-06-28 10:17:14 +02:00
const COLUMNS = [
'app_name',
'instance_id',
'sdk_version',
2017-06-28 10:17:14 +02:00
'client_ip',
'last_seen',
'created_at',
];
2016-11-04 09:03:13 +01:00
const TABLE = 'client_instances';
const ONE_DAY = 24 * 61 * 60 * 1000;
2017-06-28 10:17:14 +02:00
const mapRow = row => ({
appName: row.app_name,
instanceId: row.instance_id,
sdkVersion: row.sdk_version,
clientIp: row.client_ip,
lastSeen: row.last_seen,
createdAt: row.created_at,
});
class ClientInstanceStore {
constructor(db, eventBus, getLogger) {
this.db = db;
this.eventBus = eventBus;
this.logger = getLogger('client-instance-store.js');
2020-09-18 09:05:09 +02:00
this.timer = action =>
metricsHelper.wrapTimer(eventBus, DB_TIME, {
store: 'instance',
action,
});
const clearer = () => this._removeInstancesOlderThanTwoDays();
setTimeout(clearer, 10).unref();
setInterval(clearer, ONE_DAY).unref();
}
2020-09-18 09:05:09 +02:00
async _removeInstancesOlderThanTwoDays() {
const rows = await this.db(TABLE)
2017-06-28 10:17:14 +02:00
.whereRaw("created_at < now() - interval '2 days'")
2020-09-18 09:05:09 +02:00
.del();
if (rows > 0) {
this.logger.debug(`Deleted ${rows} instances`);
}
}
2020-09-18 09:05:09 +02:00
async updateRow(details) {
return this.db(TABLE)
2016-11-04 09:03:13 +01:00
.where('app_name', details.appName)
.where('instance_id', details.instanceId)
.update({
last_seen: 'now()',
2016-11-05 12:42:58 +01:00
client_ip: details.clientIp,
sdk_version: details.sdkVersion,
2016-11-04 09:03:13 +01:00
});
}
2020-09-18 09:05:09 +02:00
async insertNewRow(details) {
return this.db(TABLE).insert({
2016-11-04 09:03:13 +01:00
app_name: details.appName,
instance_id: details.instanceId,
sdk_version: details.sdkVersion,
2016-11-04 09:03:13 +01:00
client_ip: details.clientIp,
});
}
2020-09-18 09:05:09 +02:00
async insert(details) {
const stopTimer = this.timer('insert');
const result = await this.db(TABLE)
2016-11-04 09:03:13 +01:00
.count('*')
.where('app_name', details.appName)
.where('instance_id', details.instanceId)
2020-09-18 09:05:09 +02:00
.first();
let item;
if (Number(result.count) > 0) {
item = await this.updateRow(details);
} else {
item = await this.insertNewRow(details);
}
stopTimer();
return item;
2016-11-04 09:03:13 +01:00
}
2020-09-18 09:05:09 +02:00
async getAll() {
const stopTimer = this.timer('getAll');
const rows = await this.db
2016-11-04 09:03:13 +01:00
.select(COLUMNS)
.from(TABLE)
2020-09-18 09:05:09 +02:00
.orderBy('last_seen', 'desc');
const toggles = rows.map(mapRow);
stopTimer();
return toggles;
2016-11-04 09:03:13 +01:00
}
2020-09-18 09:05:09 +02:00
async getByAppName(appName) {
const rows = await this.db
.select()
.from(TABLE)
.where('app_name', appName)
2020-09-18 09:05:09 +02:00
.orderBy('last_seen', 'desc');
return rows.map(mapRow);
}
2020-09-18 09:05:09 +02:00
async getApplications() {
const rows = await this.db
.distinct('app_name')
.select(['app_name'])
.from(TABLE)
2020-09-18 09:05:09 +02:00
.orderBy('app_name', 'desc');
return rows.map(mapRow);
}
2020-09-25 09:39:12 +02:00
async deleteForApplication(appName) {
return this.db(TABLE)
.where('app_name', appName)
.del();
}
2017-06-28 10:17:14 +02:00
}
module.exports = ClientInstanceStore;