-
Notifications
You must be signed in to change notification settings - Fork 425
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
56 additions
and
1 deletion.
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
45 changes: 45 additions & 0 deletions
45
h/data_tasks/report/create_from_scratch/07_annotation_type_group_counts/01_create_view.sql
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,45 @@ | ||
DROP MATERIALIZED VIEW IF EXISTS report.annotation_type_group_counts CASCADE; | ||
|
||
CREATE TYPE report.annotation_sub_type AS ENUM ( | ||
-- This is not a good name for a sub-type of an annotation, we should | ||
-- change this | ||
'annotation', | ||
-- Other primary types of annotation | ||
'reply', 'highlight', 'page_note', | ||
-- A catch-all that should not happen | ||
'other' | ||
); | ||
|
||
CREATE MATERIALIZED VIEW report.annotation_type_group_counts AS ( | ||
SELECT | ||
group_id, | ||
created_day, | ||
CASE | ||
WHEN has_text = true AND anchored = true AND is_root = true | ||
THEN 'annotation' | ||
WHEN has_text = false AND anchored = true AND is_root = true | ||
THEN 'highlight' | ||
WHEN has_text = true AND anchored = false AND is_root = true | ||
THEN 'page_note' | ||
WHEN has_text = true AND anchored = false AND is_root = false | ||
THEN 'reply' | ||
ELSE | ||
-- This shouldn't happen, but it's good to check | ||
'other'::report.annotation_sub_type | ||
END AS sub_type, | ||
shared, | ||
COUNT(1) AS count | ||
FROM ( | ||
SELECT | ||
group_id, | ||
DATE_TRUNC('day', created) AS created_day, | ||
shared, | ||
anchored, | ||
-- Prep some booleans for easier comparisons later | ||
size > 0 AS has_text, | ||
ARRAY_LENGTH(parent_uuids, 1) = 0 AS is_root | ||
FROM report.annotations | ||
) AS data | ||
GROUP BY group_id, created_day, sub_type, shared | ||
ORDER BY group_id, created_day, count DESC | ||
) WITH NO DATA; |
9 changes: 9 additions & 0 deletions
9
h/data_tasks/report/create_from_scratch/07_annotation_type_group_counts/02_initial_fill.sql
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,9 @@ | ||
DROP INDEX IF EXISTS report.annotation_type_group_counts_group_id_created_day_sub_type_idx; | ||
|
||
REFRESH MATERIALIZED VIEW report.annotation_type_group_counts; | ||
|
||
ANALYSE report.annotation_type_group_counts; | ||
|
||
-- A unique index is mandatory for concurrent updates used in the refresh | ||
CREATE UNIQUE INDEX annotation_type_group_counts_group_id_created_day_sub_type_idx | ||
ON report.annotation_type_group_counts (group_id, created_day, sub_type, shared); |