-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
85 - 86 - 87 - Fix medic-users-meta views and make compatible with da…
…ily telemetry (#91) Deprecate old `medic-users-meta` views and create new ones fixing bugs and making compatible with upcoming daily telemetry. **Issues**: #85 , #86 , #87 **Changes**: - [Bug] Change the indexes for both views (telemetry and feedback) based on the columns user_name and period_start, the PR remove the unique constraint to avoid collisions, and instead the uuid of the records are used as unique index to allow concurrent refresh of the views (#86). - [Bug] Fix bug that causes records being parsed with months 0-indexed as 1-indexed and vice versa (thanks to @kitsao ) (#87). - [Improvement] The column period_start from the telemetry view has the same DATE type, but the day component is parsed from the JSON metadata.day field if present (daily aggregation telemetry from this upcoming CHT feature), otherwise defaulted to 1 as it was before to maintain backward compatibility with monthly aggregation telemetry (#85). **Affected views**: - Feedback view: `useview_feedback` - Telemetry view: `useview_telemetry`
- Loading branch information
Showing
5 changed files
with
1,576 additions
and
6 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
59 changes: 59 additions & 0 deletions
59
libs/medic-users-meta/migrations/202105171933.do.86.usersMeta.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,59 @@ | ||
-- Drop views with wrong indexes and definitions that were | ||
-- created at 202102191153.do.54-create-couchdb-users-meta-table.sql | ||
DROP MATERIALIZED VIEW IF EXISTS useview_feedback; | ||
DROP MATERIALIZED VIEW IF EXISTS useview_telemetry; | ||
|
||
-- The views are created again | ||
CREATE MATERIALIZED VIEW useview_feedback AS | ||
SELECT | ||
doc->>'_id' AS uuid, | ||
doc#>>'{meta,source}' AS SOURCE, | ||
doc#>>'{meta,url}' AS url, | ||
doc#>>'{meta,user,name}' AS user_name, | ||
doc#>>'{meta,time}' AS period_start, | ||
COALESCE(doc#>>'{info,cause}',doc->>'info') AS cause, | ||
doc#>>'{info,message}' AS message | ||
FROM | ||
couchdb_users_meta | ||
WHERE | ||
doc->>'type'='feedback'; | ||
|
||
CREATE UNIQUE INDEX idx_useview_feedback_uuid ON useview_feedback(uuid); --> Only to allow the refresh of the view CONCURRENTLY | ||
CREATE INDEX idx_useview_feedback_period_start_user ON useview_feedback(period_start,user_name); | ||
|
||
CREATE MATERIALIZED VIEW useview_telemetry AS | ||
SELECT | ||
doc->>'_id' AS uuid, | ||
CONCAT_WS( --> Date concatenation from JSON fields, eg. 2021-5-17 | ||
'-', | ||
doc#>>'{metadata,year}', --> year | ||
CASE --> month of the year | ||
WHEN | ||
string_to_array(substring(doc#>>'{metadata,versions,app}' FROM '(\d+.\d+.\d+)'),'.')::int[] < '{3,8,0}'::int[] | ||
THEN | ||
(doc#>>'{metadata,month}')::int+1 --> Legacy, months zero-indexed (0 - 11) | ||
ELSE | ||
(doc#>>'{metadata,month}')::int --> Month is between 1 - 12 | ||
END, | ||
CASE --> day of the month, else 1 | ||
WHEN | ||
(doc#>>'{metadata,day}') IS NOT NULL | ||
THEN | ||
doc#>>'{metadata,day}' | ||
ELSE | ||
'1' | ||
END | ||
)::date AS period_start, | ||
doc#>>'{metadata,user}' AS user_name, | ||
doc#>>'{metadata,versions,app}' AS app_version, | ||
doc#>>'{metrics,boot_time,min}' AS boot_time_min, | ||
doc#>>'{metrics,boot_time,max}' AS boot_time_max, | ||
doc#>>'{metrics,boot_time,count}' AS boot_time_count, | ||
doc#>>'{dbInfo,doc_count}' AS doc_count_on_local_db | ||
FROM | ||
couchdb_users_meta | ||
WHERE | ||
doc->>'type'='telemetry'; | ||
|
||
CREATE UNIQUE INDEX idx_useview_telemetry_uuid ON useview_telemetry(uuid); --> Only to allow the refresh of the view CONCURRENTLY | ||
CREATE INDEX idx_useview_telemetry_period_start_user ON useview_telemetry(period_start,user_name); |
Oops, something went wrong.