Gather usage metrics to db table with sql trigger

This commit is contained in:
Teemu Patja 2017-03-17 21:35:06 +02:00
parent 6e6331f001
commit 2363c88d19
No known key found for this signature in database
GPG Key ID: F5B7035E6580FD4C
2 changed files with 23 additions and 0 deletions

View File

@ -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();