mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-20 00:08:02 +01:00
fix: environment_type_trends table data (#6796)
Creates a migration to fix the environment_type_trends table data. --------- Signed-off-by: andreas-unleash <andreas@getunleash.ai> Co-authored-by: sjaanus <sellinjaanus@gmail.com>
This commit is contained in:
parent
30336819f2
commit
5a5d909137
67
src/migrations/20240408104624-fix-environment-type-trends.js
Normal file
67
src/migrations/20240408104624-fix-environment-type-trends.js
Normal file
@ -0,0 +1,67 @@
|
||||
exports.up = function (db, cb) {
|
||||
db.runSql(
|
||||
`
|
||||
-- 1. Clear the environment_type_trends table
|
||||
TRUNCATE TABLE environment_type_trends;
|
||||
|
||||
-- 2. Prepare staging table
|
||||
with staging_environment_type_trends as (
|
||||
SELECT
|
||||
seu.day,
|
||||
e.type AS environment_type,
|
||||
SUM(seu.updates) AS total_updates
|
||||
FROM
|
||||
stat_environment_updates AS seu
|
||||
JOIN
|
||||
environments AS e ON seu.environment = e.name
|
||||
GROUP BY
|
||||
seu.day,
|
||||
e.type
|
||||
ORDER BY
|
||||
seu.day,
|
||||
e.type
|
||||
),
|
||||
-- 3. Aggregate per week
|
||||
set as (
|
||||
SELECT
|
||||
environment_type,
|
||||
SUM(total_updates) AS total_updates,
|
||||
date_trunc('week', day) AS week_start,
|
||||
CONCAT(EXTRACT(YEAR FROM date_trunc('week', day))::TEXT, '-', LPAD(EXTRACT(WEEK FROM date_trunc('week', day))::TEXT, 2, '0')) AS id
|
||||
FROM
|
||||
staging_environment_type_trends
|
||||
GROUP BY
|
||||
environment_type,
|
||||
date_trunc('week', day)
|
||||
),
|
||||
-- 4. Find correlating created dates
|
||||
created as (
|
||||
select distinct id, created_at from flag_trends
|
||||
)
|
||||
-- 5. Insert aggregated data with dates into environment_type_trends
|
||||
INSERT INTO environment_type_trends (id, environment_type, total_updates, created_at)
|
||||
SELECT
|
||||
CONCAT(EXTRACT(YEAR FROM set.week_start)::TEXT, '-', LPAD(EXTRACT(WEEK FROM set.week_start)::TEXT, 2, '0')) AS id,
|
||||
set.environment_type,
|
||||
SUM(set.total_updates) AS total_updates,
|
||||
created.created_at
|
||||
FROM set
|
||||
JOIN created ON set.id = created.id
|
||||
GROUP BY
|
||||
set.environment_type,
|
||||
set.week_start,
|
||||
created.created_at
|
||||
ORDER BY
|
||||
set.week_start;
|
||||
`,
|
||||
cb,
|
||||
);
|
||||
};
|
||||
|
||||
exports.down = function (db, cb) {
|
||||
db.runSql(
|
||||
`
|
||||
`,
|
||||
cb,
|
||||
);
|
||||
};
|
Loading…
Reference in New Issue
Block a user