-
-
Notifications
You must be signed in to change notification settings - Fork 664
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
1 parent
3033681
commit 5a5d909
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
src/migrations/20240408104624-fix-environment-type-trends.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
); | ||
}; |