diff --git a/src/lib/db/client-instance-store.ts b/src/lib/db/client-instance-store.ts index fc3bcc5a1f..c1983a94d3 100644 --- a/src/lib/db/client-instance-store.ts +++ b/src/lib/db/client-instance-store.ts @@ -77,6 +77,19 @@ export default class ClientInstanceStore implements IClientInstanceStore { } } + async setLastSeen({ + appName, + instanceId, + environment, + clientIp, + }: INewClientInstance): Promise { + await this.db(TABLE) + .update({ last_seen: new Date(), client_ip: clientIp }) + .where({ app_name: appName, instance_id: instanceId, environment }) + .onConflict(['app_name', 'instance_id', 'environment']) + .ignore(); + } + async bulkUpsert(instances: INewClientInstance[]): Promise { const rows = instances.map(mapToDb); await this.db(TABLE) diff --git a/src/lib/services/client-metrics/instance-service.ts b/src/lib/services/client-metrics/instance-service.ts index 7017ecd5c4..ab66a70be4 100644 --- a/src/lib/services/client-metrics/instance-service.ts +++ b/src/lib/services/client-metrics/instance-service.ts @@ -94,10 +94,11 @@ export default class ClientInstanceService { clientIp: string, ): Promise { const value = await clientMetricsSchema.validateAsync(data); - await this.clientInstanceStore.insert({ + await this.clientInstanceStore.setLastSeen({ appName: value.appName, instanceId: value.instanceId, - clientIp, + environment: value.environment, + clientIp: clientIp, }); } diff --git a/src/lib/types/stores/client-instance-store.ts b/src/lib/types/stores/client-instance-store.ts index 8bc0f74d52..ce225f7341 100644 --- a/src/lib/types/stores/client-instance-store.ts +++ b/src/lib/types/stores/client-instance-store.ts @@ -18,6 +18,7 @@ export interface IClientInstanceStore Pick > { bulkUpsert(instances: INewClientInstance[]): Promise; + setLastSeen(INewClientInstance): Promise; insert(details: INewClientInstance): Promise; getByAppName(appName: string): Promise; getDistinctApplications(): Promise; diff --git a/src/test/fixtures/fake-client-instance-store.ts b/src/test/fixtures/fake-client-instance-store.ts index f938d684d2..78ddf1e715 100644 --- a/src/test/fixtures/fake-client-instance-store.ts +++ b/src/test/fixtures/fake-client-instance-store.ts @@ -27,6 +27,10 @@ export default class FakeClientInstanceStore implements IClientInstanceStore { ); } + setLastSeen(): Promise { + return; + } + async deleteAll(): Promise { this.instances = []; }