mirror of
https://github.com/Unleash/unleash.git
synced 2025-06-18 01:18:23 +02:00
feat: updating last seen now will create instance if does not exist (#6328)
This commit is contained in:
parent
68abe28257
commit
1633722877
@ -78,10 +78,18 @@ export default class ClientInstanceStore implements IClientInstanceStore {
|
|||||||
clientIp,
|
clientIp,
|
||||||
}: INewClientInstance): Promise<void> {
|
}: INewClientInstance): Promise<void> {
|
||||||
await this.db(TABLE)
|
await this.db(TABLE)
|
||||||
.update({ last_seen: new Date(), client_ip: clientIp })
|
.insert({
|
||||||
.where({ app_name: appName, instance_id: instanceId, environment })
|
app_name: appName,
|
||||||
|
instance_id: instanceId,
|
||||||
|
environment,
|
||||||
|
last_seen: new Date(),
|
||||||
|
client_ip: clientIp,
|
||||||
|
})
|
||||||
.onConflict(['app_name', 'instance_id', 'environment'])
|
.onConflict(['app_name', 'instance_id', 'environment'])
|
||||||
.ignore();
|
.merge({
|
||||||
|
last_seen: new Date(),
|
||||||
|
client_ip: clientIp,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async bulkUpsert(instances: INewClientInstance[]): Promise<void> {
|
async bulkUpsert(instances: INewClientInstance[]): Promise<void> {
|
||||||
@ -97,7 +105,10 @@ export default class ClientInstanceStore implements IClientInstanceStore {
|
|||||||
instanceId,
|
instanceId,
|
||||||
}: Pick<INewClientInstance, 'appName' | 'instanceId'>): Promise<void> {
|
}: Pick<INewClientInstance, 'appName' | 'instanceId'>): Promise<void> {
|
||||||
await this.db(TABLE)
|
await this.db(TABLE)
|
||||||
.where({ app_name: appName, instance_id: instanceId })
|
.where({
|
||||||
|
app_name: appName,
|
||||||
|
instance_id: instanceId,
|
||||||
|
})
|
||||||
.del();
|
.del();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,7 +124,10 @@ export default class ClientInstanceStore implements IClientInstanceStore {
|
|||||||
'appName' | 'instanceId'
|
'appName' | 'instanceId'
|
||||||
>): Promise<IClientInstance> {
|
>): Promise<IClientInstance> {
|
||||||
const row = await this.db(TABLE)
|
const row = await this.db(TABLE)
|
||||||
.where({ app_name: appName, instance_id: instanceId })
|
.where({
|
||||||
|
app_name: appName,
|
||||||
|
instance_id: instanceId,
|
||||||
|
})
|
||||||
.first();
|
.first();
|
||||||
return mapRow(row);
|
return mapRow(row);
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,13 @@ beforeAll(async () => {
|
|||||||
app = await setupApp(db.stores);
|
app = await setupApp(db.stores);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
await Promise.all([
|
||||||
|
db.stores.clientMetricsStoreV2.deleteAll(),
|
||||||
|
db.stores.clientInstanceStore.deleteAll(),
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
await app.destroy();
|
await app.destroy();
|
||||||
await db.destroy();
|
await db.destroy();
|
||||||
@ -46,3 +53,14 @@ test('should accept empty client metrics', async () => {
|
|||||||
})
|
})
|
||||||
.expect(202);
|
.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);
|
||||||
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user