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(
limit: number,
): Promise<Array<{ project: string; environment: string; size: number }>> {
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<Array<{ feature: string; environment: string; size: number }>> {
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),
}),
);
}
}