JBAS-1378 Purge user activity ; apply transience
authorBill Erickson <berickxx@gmail.com>
Tue, 19 Apr 2016 17:56:33 +0000 (13:56 -0400)
committerBill Erickson <berickxx@gmail.com>
Thu, 21 Mar 2019 19:46:23 +0000 (15:46 -0400)
Remove all but the most recent actor.usr_activity row per etype and per
user, except keep all of the lynda and hoopla SIP login events.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
KCLS/sql/schema/deploy/purge-user-activity.sql [new file with mode: 0644]
KCLS/sql/schema/revert/purge-user-activity.sql [new file with mode: 0644]
KCLS/sql/schema/sqitch.plan
KCLS/sql/schema/verify/purge-user-activity.sql [new file with mode: 0644]

diff --git a/KCLS/sql/schema/deploy/purge-user-activity.sql b/KCLS/sql/schema/deploy/purge-user-activity.sql
new file mode 100644 (file)
index 0000000..5bb71d2
--- /dev/null
@@ -0,0 +1,36 @@
+-- purge-user-activity
+
+BEGIN;
+
+SET STATEMENT_TIMEOUT = 0;
+
+CREATE TEMPORARY TABLE tmp_usr_activity ON COMMIT DROP AS
+    -- Most recent usr_activity row per usr per etype
+    WITH ranked_entry_per_type_and_user AS (
+        SELECT 
+            entry.*, 
+            RANK() OVER (
+                PARTITION BY entry.etype, entry.usr
+                ORDER BY event_time DESC
+            ) AS position
+        FROM actor.usr_activity entry
+    ) 
+    SELECT ranked.id, ranked.usr, ranked.etype, ranked.event_time
+    FROM ranked_entry_per_type_and_user ranked
+    WHERE ranked.position = 1 
+        -- keep all events for lyndasip and hooplasip2 authz
+        OR ranked.etype IN (1005, 1006);
+
+TRUNCATE actor.usr_activity;
+
+INSERT INTO actor.usr_activity SELECT * FROM tmp_usr_activity;
+
+-- keep only the most recent even going forward with 2 
+-- exceptions for lyndasip and hooplasip2.
+UPDATE config.usr_activity_type 
+    SET transient = TRUE WHERE id NOT IN (1005, 1006);
+
+COMMIT;
+
+ANALYZE actor.usr_activity;
+
diff --git a/KCLS/sql/schema/revert/purge-user-activity.sql b/KCLS/sql/schema/revert/purge-user-activity.sql
new file mode 100644 (file)
index 0000000..80d17a3
--- /dev/null
@@ -0,0 +1,7 @@
+-- Revert kcls-evergreen:purge-user-activity from pg
+
+BEGIN;
+
+-- XXX Add DDLs here.
+
+COMMIT;
index 3b45bcd..cf3e77d 100644 (file)
@@ -25,3 +25,4 @@ ingram-edi-mods [sip-activity-types] 2016-05-24T16:31:46Z Bill Erickson <berickx
 org-unit-addrs-copy [sip-activity-types] 2016-05-03T14:17:59Z Bill Erickson <berickxx@gmail.com> # Give each org unit its own mailing address
 drop-cc-cols [sip-activity-types] 2016-05-03T15:26:50Z Bill Erickson <berickxx@gmail.com> # Drop unneeded CC payment columns
 connexion-auth-imports [sip-activity-types] 2016-05-11T15:10:49Z Bill Erickson,,, <berick@teapot> # OCLC Connexion Authority Record Imports Data
+purge-user-activity [sip-activity-types] 2016-04-29T17:07:46Z Bill Erickson <berickxx@gmail.com> # Clean up actor.usr_activity
diff --git a/KCLS/sql/schema/verify/purge-user-activity.sql b/KCLS/sql/schema/verify/purge-user-activity.sql
new file mode 100644 (file)
index 0000000..4587b5b
--- /dev/null
@@ -0,0 +1,7 @@
+-- Verify kcls-evergreen:purge-user-activity on pg
+
+BEGIN;
+
+-- XXX Add verifications here.
+
+ROLLBACK;