diff --git a/src/lib/db/environment-store.ts b/src/lib/db/environment-store.ts index f8250ec703..25132ea5af 100644 --- a/src/lib/db/environment-store.ts +++ b/src/lib/db/environment-store.ts @@ -81,6 +81,13 @@ export default class EnvironmentStore implements IEnvironmentStore { await this.db(TABLE).del(); } + count(): Promise { + return this.db + .from(TABLE) + .count('*') + .then((res) => Number(res[0].count)); + } + async get(key: string): Promise { const row = await this.db(TABLE) .where({ name: key }) diff --git a/src/lib/metrics.ts b/src/lib/metrics.ts index 263f5e7744..b1af637573 100644 --- a/src/lib/metrics.ts +++ b/src/lib/metrics.ts @@ -40,8 +40,13 @@ export default class MetricsMonitor { return; } - const { eventStore, featureToggleStore, userStore, projectStore } = - stores; + const { + eventStore, + featureToggleStore, + userStore, + projectStore, + environmentStore, + } = stores; client.collectDefaultMetrics(); @@ -84,6 +89,10 @@ export default class MetricsMonitor { name: 'projects_total', help: 'Number of projects', }); + const environmentsTotal = new client.Gauge({ + name: 'environments_total', + help: 'Number of environments', + }); const clientSdkVersionUsage = new client.Counter({ name: 'client_sdk_versions', @@ -95,12 +104,14 @@ export default class MetricsMonitor { let togglesCount: number = 0; let usersCount: number; let projectsCount: number; + let environmentsCount: number; try { togglesCount = await featureToggleStore.count({ archived: false, }); usersCount = await userStore.count(); projectsCount = await projectStore.count(); + environmentsCount = await environmentStore.count(); // eslint-disable-next-line no-empty } catch (e) {} @@ -114,6 +125,10 @@ export default class MetricsMonitor { projectsTotal.reset(); projectsTotal.set(projectsCount); } + if (environmentsCount) { + environmentsTotal.reset(); + environmentsTotal.set(environmentsCount); + } } collectStaticCounters(); diff --git a/src/lib/types/stores/environment-store.ts b/src/lib/types/stores/environment-store.ts index 4c1f4c48b7..e82db20cef 100644 --- a/src/lib/types/stores/environment-store.ts +++ b/src/lib/types/stores/environment-store.ts @@ -18,4 +18,5 @@ export interface IEnvironmentStore extends Store { delete(name: string): Promise; disable(environments: IEnvironment[]): Promise; enable(environments: IEnvironment[]): Promise; + count(): Promise; } diff --git a/src/test/fixtures/fake-environment-store.ts b/src/test/fixtures/fake-environment-store.ts index 0df598952a..fe896e06ac 100644 --- a/src/test/fixtures/fake-environment-store.ts +++ b/src/test/fixtures/fake-environment-store.ts @@ -26,8 +26,12 @@ export default class FakeEnvironmentStore implements IEnvironmentStore { return Promise.resolve(); } + count(): Promise { + return Promise.resolve(this.environments.length); + } + async getAll(): Promise { - return this.environments; + return Promise.resolve(this.environments); } async exists(name: string): Promise {