1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-11-01 19:07:38 +01:00
unleash.unleash/src/test/fixtures/fake-client-instance-store.ts
Gastón Fournier 2979f21631
feat: expose number of registered applications metric (#2692)
## About the changes
This metric will expose an aggregated view of how many client
applications are registered in Unleash. Since applications are ephemeral
we are exposing this metric in different time windows based on when the
application was last seen.

The caveat is that we issue a database query for each new range we want
to add. Hopefully, this should not be a problem because:
a) the amount of ranges we'd expose is small and unlikely to grow
b) this is currently updated at startup time and even if we update it on
a scheduled basis the refresh rate will be rather sparse

## Sample data
This is how metrics will look like
```
# HELP client_apps_total Number of registered client apps aggregated by range by last seen
# TYPE client_apps_total gauge
client_apps_total{range="allTime"} 3
client_apps_total{range="30d"} 3
client_apps_total{range="7d"} 2
```
2022-12-16 11:16:51 +00:00

86 lines
2.5 KiB
TypeScript

import {
IClientInstance,
IClientInstanceStore,
INewClientInstance,
} from '../../lib/types/stores/client-instance-store';
import NotFoundError from '../../lib/error/notfound-error';
export default class FakeClientInstanceStore implements IClientInstanceStore {
instances: IClientInstance[] = [];
async bulkUpsert(instances: INewClientInstance[]): Promise<void> {
instances.forEach((i) => {
this.instances.push({ createdAt: new Date(), ...i });
});
}
async delete(
key: Pick<INewClientInstance, 'appName' | 'instanceId'>,
): Promise<void> {
this.instances.splice(
this.instances.findIndex(
(i) =>
i.instanceId === key.instanceId &&
i.appName === key.appName,
),
1,
);
}
setLastSeen(): Promise<void> {
return;
}
async deleteAll(): Promise<void> {
this.instances = [];
}
async deleteForApplication(appName: string): Promise<void> {
this.instances = this.instances.filter((i) => i.appName !== appName);
}
destroy(): void {}
async exists(
key: Pick<INewClientInstance, 'appName' | 'instanceId'>,
): Promise<boolean> {
return this.instances.some(
(i) => i.appName === key.appName && i.instanceId === key.instanceId,
);
}
async get(
key: Pick<INewClientInstance, 'appName' | 'instanceId'>,
): Promise<IClientInstance> {
const instance = this.instances.find(
(i) => i.appName === key.appName && i.instanceId === key.instanceId,
);
if (instance) {
return instance;
}
throw new NotFoundError(`Could not find instance with key: ${key}`);
}
async getAll(): Promise<IClientInstance[]> {
return this.instances;
}
async getByAppName(appName: string): Promise<IClientInstance[]> {
return this.instances.filter((i) => i.appName === appName);
}
async getDistinctApplications(): Promise<string[]> {
const apps = new Set<string>();
this.instances.forEach((i) => apps.add(i.appName));
return Array.from(apps.values());
}
async getDistinctApplicationsCount(): Promise<number> {
return this.getDistinctApplications().then((apps) => apps.length);
}
async insert(details: INewClientInstance): Promise<void> {
this.instances.push({ createdAt: new Date(), ...details });
}
}