1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-10-27 11:02:16 +01:00

feat: add timers to all SQL queries in client applications store

This commit is contained in:
Jaanus 2025-10-23 21:39:32 +03:00
parent 737c8648fb
commit b95d739dc4
No known key found for this signature in database

View File

@ -141,6 +141,7 @@ export default class ClientApplicationsStore
} }
async upsert(details: Partial<IClientApplication>): Promise<void> { async upsert(details: Partial<IClientApplication>): Promise<void> {
const stopTimer = this.timer('upsert');
const row = remapRow(details); const row = remapRow(details);
await this.db(TABLE).insert(row).onConflict('app_name').merge(); await this.db(TABLE).insert(row).onConflict('app_name').merge();
const usageRows = this.remapUsageRow(details); const usageRows = this.remapUsageRow(details);
@ -148,9 +149,11 @@ export default class ClientApplicationsStore
.insert(usageRows) .insert(usageRows)
.onConflict(['app_name', 'project', 'environment']) .onConflict(['app_name', 'project', 'environment'])
.merge(); .merge();
stopTimer();
} }
async bulkUpsert(apps: Partial<IClientApplication>[]): Promise<void> { async bulkUpsert(apps: Partial<IClientApplication>[]): Promise<void> {
const stopTimer = this.timer('bulkUpsert');
const rows = apps.map(remapRow); const rows = apps.map(remapRow);
const uniqueRows = Object.values( const uniqueRows = Object.values(
rows.reduce((acc, row) => { rows.reduce((acc, row) => {
@ -176,33 +179,38 @@ export default class ClientApplicationsStore
.insert(uniqueUsageRows) .insert(uniqueUsageRows)
.onConflict(['app_name', 'project', 'environment']) .onConflict(['app_name', 'project', 'environment'])
.merge(); .merge();
stopTimer();
} }
async exists(appName: string): Promise<boolean> { async exists(appName: string): Promise<boolean> {
const stopTimer = this.timer('exists');
const result = await this.db.raw( const result = await this.db.raw(
`SELECT EXISTS(SELECT 1 FROM ${TABLE} WHERE app_name = ?) AS present`, `SELECT EXISTS(SELECT 1 FROM ${TABLE} WHERE app_name = ?) AS present`,
[appName], [appName],
); );
const { present } = result.rows[0]; const { present } = result.rows[0];
stopTimer();
return present; return present;
} }
async getAll(): Promise<IClientApplication[]> { async getAll(): Promise<IClientApplication[]> {
const stopTimer = this.timer('getAll');
const rows = await this.db const rows = await this.db
.select(COLUMNS) .select(COLUMNS)
.from(TABLE) .from(TABLE)
.orderBy('app_name', 'asc'); .orderBy('app_name', 'asc');
stopTimer();
return rows.map(mapRow); return rows.map(mapRow);
} }
async getApplication(appName: string): Promise<IClientApplication> { async getApplication(appName: string): Promise<IClientApplication> {
const stopTimer = this.timer('getApplication');
const row = await this.db const row = await this.db
.select(COLUMNS) .select(COLUMNS)
.where('app_name', appName) .where('app_name', appName)
.from(TABLE) .from(TABLE)
.first(); .first();
stopTimer();
if (!row) { if (!row) {
throw new NotFoundError(`Could not find appName=${appName}`); throw new NotFoundError(`Could not find appName=${appName}`);
} }
@ -217,6 +225,7 @@ export default class ClientApplicationsStore
async getApplications( async getApplications(
params: IClientApplicationsSearchParams, params: IClientApplicationsSearchParams,
): Promise<IClientApplications> { ): Promise<IClientApplications> {
const stopTimer = this.timer('getApplications');
const { limit, offset, sortOrder = 'asc', searchParams } = params; const { limit, offset, sortOrder = 'asc', searchParams } = params;
const validatedSortOrder = const validatedSortOrder =
sortOrder === 'asc' || sortOrder === 'desc' ? sortOrder : 'asc'; sortOrder === 'asc' || sortOrder === 'desc' ? sortOrder : 'asc';
@ -257,6 +266,7 @@ export default class ClientApplicationsStore
.whereBetween('rank', [offset + 1, offset + limit]); .whereBetween('rank', [offset + 1, offset + limit]);
const rows = await query; const rows = await query;
stopTimer();
if (rows.length !== 0) { if (rows.length !== 0) {
const applications = reduceRows(rows); const applications = reduceRows(rows);
@ -273,9 +283,11 @@ export default class ClientApplicationsStore
} }
async getUnannounced(): Promise<IClientApplication[]> { async getUnannounced(): Promise<IClientApplication[]> {
const stopTimer = this.timer('getUnannounced');
const rows = await this.db(TABLE) const rows = await this.db(TABLE)
.select(COLUMNS) .select(COLUMNS)
.where('announced', false); .where('announced', false);
stopTimer();
return rows.map(mapRow); return rows.map(mapRow);
} }
@ -284,31 +296,38 @@ export default class ClientApplicationsStore
* @return {[app]} - Apps that hadn't been announced * @return {[app]} - Apps that hadn't been announced
*/ */
async setUnannouncedToAnnounced(): Promise<IClientApplication[]> { async setUnannouncedToAnnounced(): Promise<IClientApplication[]> {
const stopTimer = this.timer('setUnannouncedToAnnounced');
const rows = await this.db(TABLE) const rows = await this.db(TABLE)
.update({ announced: true }) .update({ announced: true })
.where('announced', false) .where('announced', false)
.whereNotNull('announced') .whereNotNull('announced')
.returning(COLUMNS); .returning(COLUMNS);
stopTimer();
return rows.map(mapRow); return rows.map(mapRow);
} }
async delete(key: string): Promise<void> { async delete(key: string): Promise<void> {
const stopTimer = this.timer('delete');
await this.db(TABLE).where('app_name', key).del(); await this.db(TABLE).where('app_name', key).del();
stopTimer();
} }
async deleteAll(): Promise<void> { async deleteAll(): Promise<void> {
const stopTimer = this.timer('deleteAll');
await this.db(TABLE).del(); await this.db(TABLE).del();
stopTimer();
} }
destroy(): void {} destroy(): void {}
async get(appName: string): Promise<IClientApplication> { async get(appName: string): Promise<IClientApplication> {
const stopTimer = this.timer('get');
const row = await this.db const row = await this.db
.select(COLUMNS) .select(COLUMNS)
.where('app_name', appName) .where('app_name', appName)
.from(TABLE) .from(TABLE)
.first(); .first();
stopTimer();
if (!row) { if (!row) {
throw new NotFoundError(`Could not find appName=${appName}`); throw new NotFoundError(`Could not find appName=${appName}`);
} }
@ -481,10 +500,11 @@ export default class ClientApplicationsStore
}; };
async removeInactiveApplications(): Promise<number> { async removeInactiveApplications(): Promise<number> {
const stopTimer = this.timer('removeInactiveApplications');
const rows = await this.db(TABLE) const rows = await this.db(TABLE)
.whereRaw("seen_at < now() - interval '30 days'") .whereRaw("seen_at < now() - interval '30 days'")
.del(); .del();
stopTimer();
if (rows > 0) { if (rows > 0) {
this.logger.debug(`Deleted ${rows} applications`); this.logger.debug(`Deleted ${rows} applications`);
} }