Commit 2be0c887 authored by Eduardo L. Buratti's avatar Eduardo L. Buratti

databse: Modify aggr_availability table

Now aggregating the total count of machines in each state (red, yellow
and green) by school and by month (only the last 6 months). This is a
more general and usable version of the previous aggr_availability.
Signed-off-by: Eduardo L. Buratti's avatarEduardo L. Buratti <elb09@c3sl.ufpr.br>
parent d132c1aa
CREATE TABLE aggr_availability (
base_date DATE NOT NULL,
macaddr MACADDR NOT NULL,
project project_enum DEFAULT 'proinfo' NOT NULL,
sch_inep CHARACTER VARYING(12) NOT NULL,
sch_name CHARACTER VARYING(150) NOT NULL,
city CHARACTER VARYING(100) NOT NULL,
state CHARACTER VARYING(2) NOT NULL,
region CHARACTER VARYING(12) NOT NULL,
is_green SMALLINT NOT NULL DEFAULT 0,
is_yellow SMALLINT NOT NULL DEFAULT 0,
is_red SMALLINT NOT NULL DEFAULT 0
sch_id INTEGER NOT NULL REFERENCES dim_school,
cit_id INTEGER NOT NULL REFERENCES dim_city,
dat_id DATE NOT NULL REFERENCES dim_date,
mac_count BIGINT NOT NULL DEFAULT 0,
mac_count_red BIGINT NOT NULL DEFAULT 0,
mac_count_yellow BIGINT NOT NULL DEFAULT 0,
mac_count_green BIGINT NOT NULL DEFAULT 0,
project project_enum DEFAULT 'proinfo' NOT NULL,
UNIQUE (sch_id, cit_id, dat_id, project)
);
CREATE OR REPLACE FUNCTION load_aggr_availability() returns void as $$
DECLARE
date_it date;
i INTEGER;
start_ts TIMESTAMP;
total_rows INTEGER;
cur_rows INTEGER;
......@@ -11,15 +10,14 @@ BEGIN
start_ts = CLOCK_TIMESTAMP();
total_rows := 0;
-- remove the index - later we create it again
DROP INDEX IF EXISTS aggr_availability_base_date_idx;
-- remove old data
TRUNCATE TABLE aggr_availability;
-- iterate over past months
FOR i IN 0..5 LOOP
date_it := CURRENT_DATE - (i * interval '1 month');
FOR date_it IN SELECT (date_trunc('month', generate_series(
current_date - interval '4 months',
current_date,
interval '1 month')) - interval '1 day')::date UNION select current_date LOOP
INSERT INTO aggr_availability
SELECT
......@@ -62,9 +60,6 @@ BEGIN
END LOOP;
-- creating index to speed-up per-date queries, and it speeds up a lot..
CREATE INDEX aggr_availability_base_date_idx ON aggr_availability(base_date);
-- logging
INSERT INTO control (name, start_time, end_time, total)
VALUES ('loading aggr_availability', start_ts, CLOCK_TIMESTAMP(), total_rows);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment