diff --git a/src/lib/features/metrics/sizes/largest-resources-read-model.ts b/src/lib/features/metrics/sizes/largest-resources-read-model.ts index bf4623fd4b..91bc61d5a1 100644 --- a/src/lib/features/metrics/sizes/largest-resources-read-model.ts +++ b/src/lib/features/metrics/sizes/largest-resources-read-model.ts @@ -11,66 +11,54 @@ export class LargestResourcesReadModel implements ILargestResourcesReadModel { async getLargestProjectEnvironments( limit: number, ): Promise> { - const { rows } = await this.db.raw(` - WITH ProjectSizes AS ( - SELECT - project_name, - environment, - SUM(pg_column_size(constraints) + pg_column_size(variants) + pg_column_size(parameters)) AS total_size - FROM - feature_strategies - GROUP BY - project_name, - environment - ) - SELECT - project_name, - environment, - total_size - FROM - ProjectSizes - ORDER BY - total_size DESC - LIMIT ${limit} - `); + const result = await this.db('feature_strategies') + .select('project_name', 'environment') + .sum({ + total_size: this.db.raw( + 'pg_column_size(constraints) + pg_column_size(variants) + pg_column_size(parameters)', + ), + }) + .groupBy('project_name', 'environment') + .orderBy('total_size', 'desc') + .limit(limit); - return rows.map((row) => ({ - project: row.project_name, - environment: row.environment, - size: Number(row.total_size), - })); + return result.map( + (row: { + project_name: string; + environment: string; + total_size: string; + }) => ({ + project: row.project_name, + environment: row.environment, + size: Number(row.total_size), + }), + ); } async getLargestFeatureEnvironments( limit: number, ): Promise> { - const { rows } = await this.db.raw(` - WITH FeatureSizes AS ( - SELECT - feature_name, - environment, - SUM(pg_column_size(constraints) + pg_column_size(variants) + pg_column_size(parameters)) AS total_size - FROM - feature_strategies - GROUP BY - feature_name, - environment - ) - SELECT - feature_name, - environment, - total_size - FROM - FeatureSizes - ORDER BY - total_size DESC - LIMIT ${limit} - `); + const result = await this.db('feature_strategies') + .select('feature_name', 'environment') + .sum({ + total_size: this.db.raw( + 'pg_column_size(constraints) + pg_column_size(variants) + pg_column_size(parameters)', + ), + }) + .groupBy('feature_name', 'environment') + .orderBy('total_size', 'desc') + .limit(limit); - return rows.map((row) => ({ - feature: row.feature_name, - environment: row.environment, - size: Number(row.total_size), - })); + return result.map( + (row: { + feature_name: string; + environment: string; + total_size: string; + }) => ({ + feature: row.feature_name, + environment: row.environment, + size: Number(row.total_size), + }), + ); } }