Skip to content

Commit

Permalink
feat: add db metrics for environment-store (#5808)
Browse files Browse the repository at this point in the history
This will give us prometheus metrics on environment lookups in the
database
  • Loading branch information
gardleopard authored Jan 9, 2024
1 parent eb7882e commit adc47fd
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/lib/features/project-environments/environment-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,19 @@ export default class EnvironmentStore implements IEnvironmentStore {
}

async get(key: string): Promise<IEnvironment> {
const stopTimer = this.timer('get');
const row = await this.db<IEnvironmentsTable>(TABLE)
.where({ name: key })
.first();
stopTimer();
if (row) {
return mapRow(row);
}
throw new NotFoundError(`Could not find environment with name: ${key}`);
}

async getAll(query?: Object): Promise<IEnvironment[]> {
const stopTimer = this.timer('getAll');
let qB = this.db<IEnvironmentsTable>(TABLE)
.select('*')
.orderBy([
Expand All @@ -165,10 +168,12 @@ export default class EnvironmentStore implements IEnvironmentStore {
qB = qB.where(query);
}
const rows = await qB;
stopTimer();
return rows.map(mapRow);
}

async getAllWithCounts(query?: Object): Promise<IEnvironment[]> {
const stopTimer = this.timer('getAllWithCounts');
let qB = this.db<IEnvironmentsWithCountsTable>(TABLE)
.select(
'*',
Expand All @@ -190,13 +195,15 @@ export default class EnvironmentStore implements IEnvironmentStore {
qB = qB.where(query);
}
const rows = await qB;
stopTimer();
return rows.map(mapRowWithCounts);
}

async getProjectEnvironments(
projectId: string,
query?: Object,
): Promise<IProjectEnvironment[]> {
const stopTimer = this.timer('getProjectEnvironments');
let qB = this.db<IEnvironmentsWithProjectCountsTable>(TABLE)
.select(
'*',
Expand All @@ -223,23 +230,28 @@ export default class EnvironmentStore implements IEnvironmentStore {
}

const rows = await qB;
stopTimer();

return rows.map(mapRowWithProjectCounts);
}

async exists(name: string): Promise<boolean> {
const stopTimer = this.timer('exists');
const result = await this.db.raw(
`SELECT EXISTS (SELECT 1 FROM ${TABLE} WHERE name = ?) AS present`,
[name],
);
stopTimer();
const { present } = result.rows[0];
return present;
}

async getByName(name: string): Promise<IEnvironment> {
const stopTimer = this.timer('getByName');
const row = await this.db<IEnvironmentsTable>(TABLE)
.where({ name })
.first();
stopTimer();
if (!row) {
throw new NotFoundError(
`Could not find environment with name ${name}`,
Expand Down

0 comments on commit adc47fd

Please sign in to comment.