1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00

refactor: largest resources queries (#7466)

This commit is contained in:
Mateusz Kwasniewski 2024-06-27 09:26:40 +02:00 committed by GitHub
parent a9a87bc84d
commit db525a6617
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -11,66 +11,54 @@ export class LargestResourcesReadModel implements ILargestResourcesReadModel {
async getLargestProjectEnvironments( async getLargestProjectEnvironments(
limit: number, limit: number,
): Promise<Array<{ project: string; environment: string; size: number }>> { ): Promise<Array<{ project: string; environment: string; size: number }>> {
const { rows } = await this.db.raw(` const result = await this.db('feature_strategies')
WITH ProjectSizes AS ( .select('project_name', 'environment')
SELECT .sum({
project_name, total_size: this.db.raw(
environment, 'pg_column_size(constraints) + pg_column_size(variants) + pg_column_size(parameters)',
SUM(pg_column_size(constraints) + pg_column_size(variants) + pg_column_size(parameters)) AS total_size ),
FROM })
feature_strategies .groupBy('project_name', 'environment')
GROUP BY .orderBy('total_size', 'desc')
project_name, .limit(limit);
environment
)
SELECT
project_name,
environment,
total_size
FROM
ProjectSizes
ORDER BY
total_size DESC
LIMIT ${limit}
`);
return rows.map((row) => ({ return result.map(
project: row.project_name, (row: {
environment: row.environment, project_name: string;
size: Number(row.total_size), environment: string;
})); total_size: string;
}) => ({
project: row.project_name,
environment: row.environment,
size: Number(row.total_size),
}),
);
} }
async getLargestFeatureEnvironments( async getLargestFeatureEnvironments(
limit: number, limit: number,
): Promise<Array<{ feature: string; environment: string; size: number }>> { ): Promise<Array<{ feature: string; environment: string; size: number }>> {
const { rows } = await this.db.raw(` const result = await this.db('feature_strategies')
WITH FeatureSizes AS ( .select('feature_name', 'environment')
SELECT .sum({
feature_name, total_size: this.db.raw(
environment, 'pg_column_size(constraints) + pg_column_size(variants) + pg_column_size(parameters)',
SUM(pg_column_size(constraints) + pg_column_size(variants) + pg_column_size(parameters)) AS total_size ),
FROM })
feature_strategies .groupBy('feature_name', 'environment')
GROUP BY .orderBy('total_size', 'desc')
feature_name, .limit(limit);
environment
)
SELECT
feature_name,
environment,
total_size
FROM
FeatureSizes
ORDER BY
total_size DESC
LIMIT ${limit}
`);
return rows.map((row) => ({ return result.map(
feature: row.feature_name, (row: {
environment: row.environment, feature_name: string;
size: Number(row.total_size), environment: string;
})); total_size: string;
}) => ({
feature: row.feature_name,
environment: row.environment,
size: Number(row.total_size),
}),
);
} }
} }