2016-11-04 09:03:13 +01:00
|
|
|
/* eslint camelcase: "off" */
|
|
|
|
'use strict';
|
|
|
|
|
2016-11-28 17:11:11 +01:00
|
|
|
const logger = require('../logger');
|
2017-06-28 10:17:14 +02:00
|
|
|
const COLUMNS = [
|
|
|
|
'app_name',
|
|
|
|
'instance_id',
|
|
|
|
'client_ip',
|
|
|
|
'last_seen',
|
|
|
|
'created_at',
|
|
|
|
];
|
2016-11-04 09:03:13 +01:00
|
|
|
const TABLE = 'client_instances';
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
const mapRow = row => ({
|
2016-11-05 14:08:47 +01:00
|
|
|
appName: row.app_name,
|
|
|
|
instanceId: row.instance_id,
|
|
|
|
clientIp: row.client_ip,
|
|
|
|
lastSeen: row.last_seen,
|
|
|
|
createdAt: row.created_at,
|
|
|
|
});
|
|
|
|
|
2016-12-04 14:09:37 +01:00
|
|
|
// const mapAppsRow = (row) => ({
|
|
|
|
// appName: row.app_name,
|
|
|
|
// createdAt: row.created_at,
|
|
|
|
// });
|
2016-11-28 17:11:11 +01:00
|
|
|
|
2016-11-05 14:08:47 +01:00
|
|
|
class ClientInstanceStore {
|
2017-06-28 10:17:14 +02:00
|
|
|
constructor(db) {
|
2016-11-05 14:08:47 +01:00
|
|
|
this.db = db;
|
2016-11-28 17:11:11 +01:00
|
|
|
setTimeout(() => this._removeInstancesOlderThanTwoDays(), 10).unref();
|
2017-06-28 10:17:14 +02:00
|
|
|
setInterval(
|
|
|
|
() => this._removeInstancesOlderThanTwoDays(),
|
|
|
|
24 * 61 * 60 * 1000
|
|
|
|
).unref();
|
2016-11-28 17:11:11 +01:00
|
|
|
}
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
_removeInstancesOlderThanTwoDays() {
|
2016-11-28 17:11:11 +01:00
|
|
|
this.db(TABLE)
|
2017-06-28 10:17:14 +02:00
|
|
|
.whereRaw("created_at < now() - interval '2 days'")
|
2016-11-28 17:11:11 +01:00
|
|
|
.del()
|
2017-06-28 10:17:14 +02:00
|
|
|
.then(res => res > 0 && logger.info(`Deleted ${res} instances`));
|
2016-11-05 14:08:47 +01:00
|
|
|
}
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
updateRow(details) {
|
2016-11-05 14:08:47 +01:00
|
|
|
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,
|
2016-11-04 09:03:13 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
insertNewRow(details) {
|
2016-11-05 14:08:47 +01:00
|
|
|
return this.db(TABLE).insert({
|
2016-11-04 09:03:13 +01:00
|
|
|
app_name: details.appName,
|
|
|
|
instance_id: details.instanceId,
|
|
|
|
client_ip: details.clientIp,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
insert(details) {
|
2016-11-05 14:08:47 +01:00
|
|
|
return this.db(TABLE)
|
2016-11-04 09:03:13 +01:00
|
|
|
.count('*')
|
|
|
|
.where('app_name', details.appName)
|
|
|
|
.where('instance_id', details.instanceId)
|
|
|
|
.map(row => ({ count: row.count }))
|
|
|
|
.then(rows => {
|
|
|
|
if (rows[0].count > 0) {
|
2016-11-05 14:08:47 +01:00
|
|
|
return this.updateRow(details);
|
2016-11-04 09:03:13 +01:00
|
|
|
} else {
|
2016-11-05 14:08:47 +01:00
|
|
|
return this.insertNewRow(details);
|
2016-11-04 09:03:13 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
getAll() {
|
2016-11-05 14:08:47 +01:00
|
|
|
return this.db
|
2016-11-04 09:03:13 +01:00
|
|
|
.select(COLUMNS)
|
|
|
|
.from(TABLE)
|
2016-11-04 23:02:55 +01:00
|
|
|
.orderBy('last_seen', 'desc')
|
2016-11-04 09:03:13 +01:00
|
|
|
.map(mapRow);
|
|
|
|
}
|
2016-11-28 17:11:11 +01:00
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
getByAppName(appName) {
|
2016-11-28 17:11:11 +01:00
|
|
|
return this.db
|
2016-12-09 17:30:12 +01:00
|
|
|
.select()
|
2016-11-28 17:11:11 +01:00
|
|
|
.from(TABLE)
|
|
|
|
.where('app_name', appName)
|
|
|
|
.orderBy('last_seen', 'desc')
|
|
|
|
.map(mapRow);
|
|
|
|
}
|
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
getApplications() {
|
2016-11-28 17:11:11 +01:00
|
|
|
return this.db
|
|
|
|
.distinct('app_name')
|
|
|
|
.select(['app_name'])
|
|
|
|
.from(TABLE)
|
|
|
|
.orderBy('app_name', 'desc')
|
|
|
|
.map(mapRow);
|
|
|
|
}
|
2017-06-28 10:17:14 +02:00
|
|
|
}
|
2016-11-05 14:08:47 +01:00
|
|
|
|
|
|
|
module.exports = ClientInstanceStore;
|