Skip to content

Commit

Permalink
Create an annotation summary table which summaries counts by various …
Browse files Browse the repository at this point in the history
…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
jon-betts committed Jun 27, 2023
1 parent 00405b1 commit dbe34f1
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
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;
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
);
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

GRANT SELECT ON report.authorities TO "{{fdw_user}}";
GRANT SELECT ON report.annotation_group_counts TO "{{fdw_user}}";
GRANT SELECT ON report.annotation_counts TO "{{fdw_user}}";
GRANT SELECT ON report.annotation_type_group_counts TO "{{fdw_user}}";
GRANT SELECT ON report.annotation_user_counts TO "{{fdw_user}}";
GRANT SELECT ON report.authority_activity TO "{{fdw_user}}";
Expand Down
1 change: 1 addition & 0 deletions h/data_tasks/report/fast_recreate/08_annotation_counts
3 changes: 3 additions & 0 deletions h/data_tasks/report/refresh/02_materialized_views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ ANALYSE report.authority_activity;

REFRESH MATERIALIZED VIEW CONCURRENTLY report.annotation_type_group_counts;
ANALYSE report.annotation_type_group_counts;

REFRESH MATERIALIZED VIEW CONCURRENTLY report.annotation_counts;
ANALYSE report.annotation_counts;

0 comments on commit dbe34f1

Please sign in to comment.