From 2363c88d19b8fcb67208cb0a43b1f753106205c2 Mon Sep 17 00:00:00 2001 From: Teemu Patja Date: Fri, 17 Mar 2017 21:35:06 +0200 Subject: [PATCH] Gather usage metrics to db table with sql trigger --- .../20170317210604-user-stats.down.sql | 0 .../20170317210604-user-stats.up.sql | 23 +++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 resources/migrations/20170317210604-user-stats.down.sql create mode 100644 resources/migrations/20170317210604-user-stats.up.sql diff --git a/resources/migrations/20170317210604-user-stats.down.sql b/resources/migrations/20170317210604-user-stats.down.sql new file mode 100644 index 0000000..e69de29 diff --git a/resources/migrations/20170317210604-user-stats.up.sql b/resources/migrations/20170317210604-user-stats.up.sql new file mode 100644 index 0000000..8c03bc7 --- /dev/null +++ b/resources/migrations/20170317210604-user-stats.up.sql @@ -0,0 +1,23 @@ + +drop table usage_metrics; +CREATE TABLE usage_metrics ( + registered_users int, + users_with_address int, + change_timestamp timestamp without time zone + DEFAULT timezone('utc'::text, now())); + + +CREATE OR REPLACE FUNCTION store_usage_metrics() RETURNS TRIGGER AS $usage_metrics$ +BEGIN + insert into usage_metrics (registered_users, users_with_address) + values ((select count(*) from users), + (select count(*) from users + where address is not null)); + + return null; +END; +$usage_metrics$ LANGUAGE plpgsql; + +CREATE TRIGGER usage_metrics +AFTER INSERT OR UPDATE OR DELETE ON users +execute PROCEDURE store_usage_metrics();