-
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.
Create an annotation summary table which summaries counts by various …
…facets This greatly duplicates a number of other tables we have, and is the result of requirements becoming clearer over time. Basically we want to count everything in combination with everything else pretty much. We probably want to come back round and try and refactor all the other tables out which could be derived from this table. The main value add here is the addition of the annotation sub-type.
- Loading branch information
Showing
5 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
h/data_tasks/report/create_from_scratch/08_annotation_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,55 @@ | ||
-- This is a summary table which re-interprets the annotations data in terms of | ||
-- weekly counts split by a number of facets: | ||
-- | ||
-- * Authority | ||
-- * Group | ||
-- * User | ||
-- * Annotation sub-type | ||
-- * Annotation shared status | ||
-- | ||
-- This is intended to speed up a variety of summary queries over the table and | ||
-- make queries about the sub-type easy. | ||
|
||
DROP MATERIALIZED VIEW IF EXISTS report.annotation_counts CASCADE; | ||
|
||
-- There are various indicators of different sub-types of annotation, only | ||
-- certain combinations should happen together. This table shows all | ||
-- combinations and how we will interpret them. | ||
|
||
-- This table is in the same order as the CASE statement below. | ||
|
||
-- |----------|--------------|-----------|------------|-----------| | ||
-- | Is Root? | Is Anchored? | Has text? | Sub-Type | Expected? | | ||
-- |----------|--------------|-----------|------------|-----------| | ||
-- | False | True | True | Reply | No! | | ||
-- | False | True | False | Reply | No! | | ||
-- | False | False | True | Reply | Yes | | ||
-- | False | False | False | Reply | No! | | ||
-- | True | False | True | Page-note | Yes | | ||
-- | True | False | False | Page-note | No! | | ||
-- | True | True | False | Highlight | Yes | | ||
-- | True | True | True | Annotation | Yes | | ||
-- |----------|--------------|-----------|------------|-----------| | ||
|
||
CREATE MATERIALIZED VIEW report.annotation_counts AS ( | ||
SELECT | ||
authority_id, | ||
group_id, | ||
user_id, | ||
-- Cast to a date as it's 4 bytes instead of 8 | ||
DATE_TRUNC('week', created)::DATE AS created_week, | ||
CASE | ||
WHEN ARRAY_LENGTH(parent_uuids, 1) IS NOT NULL | ||
THEN 'reply'::report.annotation_sub_type | ||
WHEN anchored = false | ||
THEN 'page_note'::report.annotation_sub_type | ||
WHEN size = 0 | ||
THEN 'highlight'::report.annotation_sub_type | ||
ELSE 'annotation'::report.annotation_sub_type | ||
END AS sub_type, | ||
shared, | ||
COUNT(1) AS count | ||
FROM report.annotations | ||
GROUP BY created_week, authority_id, group_id, user_id, sub_type, shared | ||
ORDER BY created_week, authority_id, group_id, user_id, count DESC | ||
) WITH NO DATA; |
11 changes: 11 additions & 0 deletions
11
h/data_tasks/report/create_from_scratch/08_annotation_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,11 @@ | ||
DROP INDEX IF EXISTS report.annotation_counts_created_week_authority_id_idx; | ||
|
||
REFRESH MATERIALIZED VIEW report.annotation_counts; | ||
|
||
ANALYSE report.annotation_counts; | ||
|
||
-- A unique index is mandatory for concurrent updates used in the refresh | ||
CREATE UNIQUE INDEX annotation_counts_created_week_authority_id_idx | ||
ON report.annotation_counts ( | ||
created_week, authority_id, group_id, user_id, sub_type, shared | ||
); |
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
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 @@ | ||
../create_from_scratch/08_annotation_counts/ |
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