-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sql scripts uses for creating views in db
- Loading branch information
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
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,40 @@ | ||
/* | ||
View: latest_version_status | ||
This view selects the latest event for each product version | ||
from the event_logging table, excluding 'db-template'. | ||
It ranks events by priority ('publish', 'promote_to_draft', 'build') | ||
and timestamp, returning the top-ranked event. | ||
*/ | ||
|
||
CREATE VIEW product_data.latest_version_status AS ( | ||
WITH exclude_template AS ( | ||
SELECT * | ||
FROM product_data.event_logging | ||
WHERE product <> 'db-template' | ||
), | ||
ranked_events AS ( | ||
SELECT *, | ||
ROW_NUMBER() OVER ( | ||
PARTITION BY product, version | ||
ORDER BY | ||
CASE | ||
WHEN event IN ('publish', 'promote_to_draft') THEN 1 | ||
WHEN event = 'build' THEN 2 | ||
END, | ||
timestamp DESC | ||
) AS rank | ||
FROM exclude_template | ||
) | ||
SELECT | ||
product, | ||
version, | ||
event, | ||
path, | ||
old_path, | ||
runner_type, | ||
runner, | ||
timestamp | ||
FROM ranked_events | ||
WHERE rank = 1 | ||
); |
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,51 @@ | ||
/* | ||
View: product_version_lifecycle | ||
This query aggregates information from the event logging table per product + version. | ||
It returns only product versions that have both publish and draft events, | ||
including the count of published and draft records, the earliest draft | ||
timestamp, the latest publish timestamp, and the difference in days | ||
between the two timestamps. | ||
*/ | ||
|
||
CREATE VIEW product_data.product_version_lifecycle AS ( | ||
WITH latest_publish AS ( | ||
SELECT | ||
product, | ||
version, | ||
COUNT(*) AS publish_count, | ||
MAX(timestamp) AS latest_publish_timestamp | ||
FROM product_data.event_logging | ||
WHERE product <> 'db-template' | ||
AND event = 'publish' | ||
GROUP BY | ||
product, | ||
version | ||
), | ||
earliest_draft AS ( | ||
SELECT | ||
product, | ||
version, | ||
COUNT(*) AS draft_count, | ||
MIN(timestamp) AS earliest_draft_timestamp | ||
FROM product_data.event_logging | ||
WHERE product <> 'db-template' | ||
AND event = 'promote_to_draft' | ||
GROUP BY | ||
product, | ||
version | ||
) | ||
SELECT | ||
published.product, | ||
published.version, | ||
published.publish_count, | ||
draft.draft_count, | ||
draft.earliest_draft_timestamp, | ||
published.latest_publish_timestamp, | ||
DATE_PART('day', published.latest_publish_timestamp - draft.earliest_draft_timestamp) AS total_days | ||
FROM latest_publish AS published | ||
INNER JOIN earliest_draft AS draft | ||
ON published.product = draft.product | ||
AND published.version = draft.version | ||
) | ||
); |