1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-05-31 01:16:01 +02:00

feat: updating last seen now will create instance if does not exist (#6328)

This commit is contained in:
Jaanus Sellin 2024-02-26 10:08:28 +02:00 committed by GitHub
parent 68abe28257
commit 1633722877
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 5 deletions

View File

@ -78,10 +78,18 @@ export default class ClientInstanceStore implements IClientInstanceStore {
clientIp,
}: INewClientInstance): Promise<void> {
await this.db(TABLE)
.update({ last_seen: new Date(), client_ip: clientIp })
.where({ app_name: appName, instance_id: instanceId, environment })
.insert({
app_name: appName,
instance_id: instanceId,
environment,
last_seen: new Date(),
client_ip: clientIp,
})
.onConflict(['app_name', 'instance_id', 'environment'])
.ignore();
.merge({
last_seen: new Date(),
client_ip: clientIp,
});
}
async bulkUpsert(instances: INewClientInstance[]): Promise<void> {
@ -97,7 +105,10 @@ export default class ClientInstanceStore implements IClientInstanceStore {
instanceId,
}: Pick<INewClientInstance, 'appName' | 'instanceId'>): Promise<void> {
await this.db(TABLE)
.where({ app_name: appName, instance_id: instanceId })
.where({
app_name: appName,
instance_id: instanceId,
})
.del();
}
@ -113,7 +124,10 @@ export default class ClientInstanceStore implements IClientInstanceStore {
'appName' | 'instanceId'
>): Promise<IClientInstance> {
const row = await this.db(TABLE)
.where({ app_name: appName, instance_id: instanceId })
.where({
app_name: appName,
instance_id: instanceId,
})
.first();
return mapRow(row);
}

View File

@ -11,6 +11,13 @@ beforeAll(async () => {
app = await setupApp(db.stores);
});
afterEach(async () => {
await Promise.all([
db.stores.clientMetricsStoreV2.deleteAll(),
db.stores.clientInstanceStore.deleteAll(),
]);
});
afterAll(async () => {
await app.destroy();
await db.destroy();
@ -46,3 +53,14 @@ test('should accept empty client metrics', async () => {
})
.expect(202);
});
test('should create instance if does not exist', async () => {
const instances = await db.stores.clientInstanceStore.getAll();
expect(instances.length).toBe(0);
await app.request
.post('/api/client/metrics')
.send(metricsExample)
.expect(202);
const finalInstances = await db.stores.clientInstanceStore.getAll();
expect(finalInstances.length).toBe(1);
});