Commit 82423b72 authored by Erik Alexandre Pucci's avatar Erik Alexandre Pucci

database: More changes

Signed-off-by: default avatarErik Alexandre Pucci <eap08@c3sl.ufpr.br>
parent 63d66fd7
......@@ -19,6 +19,23 @@
* USA.
*/
/* Data mart table with data about the schools. Since this table is loaded apart
* and "manually" from the rest of the tables, this is the only one who would not
* need the load_date field. But, load_date field is needed to control when
* nud_avail field assumes the true value for the school */
create table dim_school (
inep text not null,
region text not null,
state text not null,
city text not null, /* Longest city name has 32 characters */
school text not null, /* MEC database limit for schools is 100 */
code text,
address text, /* Longest address has 70 characters */
fts_school tsvector not null, /* Full text search field for schools */
nud_avail boolean default false not null
/* It indicates if the school has already sent some network usage data */
);
/* -------------------------------------------------------------------------- */
/* Last load availability tables */
......
......@@ -30,8 +30,7 @@ create table dim_date (
/* Dimension table with data about the schools. Since this table is loaded apart
* and "manually" from the rest of the tables, this is the only one who would not
* need the load_date field. But, load_date field is needed to control when
* nud_avail field assumes the true value for the school */
* need the load_date field */
create table dim_school (
id serial primary key,
load_date integer references dim_date not null,
......@@ -41,10 +40,7 @@ create table dim_school (
city text not null, /* Longest city name has 32 characters */
school text not null, /* MEC database limit for schools is 100 */
code text,
address text, /* Longest address has 70 characters */
fts_school tsvector not null, /* Full text search field for schools */
nud_avail boolean default false not null
/* It indicates if the school has already sent some network usage data */
address text /* Longest address has 70 characters */
);
/* Dimension table of software and hardware components */
......
......@@ -108,16 +108,14 @@ begin
insert into dm_avail_city_history (project, contact_date, region, state,
city, total, green, yellow, red)
with tmp_avail_machine_history as (
select project, month_last_day, region, state, city, t.machine_id,
last_contact_per_month from dim_machine m, dim_school s,
(select machine_id, max(contact_date) as last_contact_per_month from
fact_inventory i group by machine_id, date_trunc('month', contact_date)) t,
(select distinct machine_id, (date_trunc('month', contact_date) +
interval '1 month - 1 day')::date as month_last_day from fact_inventory) u
where t.machine_id = u.machine_id and last_contact_per_month <= month_last_day
and t.machine_id = m.id and school_id = s.id
)
select project, month_last_day, region, state, city, count(*) as total,
select project, region, state, city, month_last_day, u.machine_id,
last_contact_per_month from dim_machine m, dim_school s,
(select month_last_day, machine_id, max(contact_date) as last_contact_per_month
from fact_inventory i, (select distinct (date_trunc('month', contact_date) + interval '1 month - 1 day')::date as month_last_day from fact_inventory) t
where contact_date <= month_last_day
group by month_last_day, machine_id) u
where u.machine_id = m.id and school_id = s.id
) select project, month_last_day, region, state, city, count(*) as total,
sum(case when last_contact_per_month >= month_last_day - 10 then 1 else 0 end) as green,
sum(case when last_contact_per_month >= month_last_day - 30 and last_contact_per_month < month_last_day - 10 then 1 else 0 end) as yellow,
sum(case when last_contact_per_month < month_last_day - 30 then 1 else 0 end) as red from tmp_avail_machine_history
......@@ -130,7 +128,10 @@ where t.machine_id = u.machine_id and last_contact_per_month <= month_last_day
'"dm_avail_state_history"...';
insert into dm_avail_state_history (project, contact_date, region, state,
total, green, yellow, red)
select
select project, month_last_day, region, state, count(*) as total,
sum(green) as green, sum(yellow) as yellow, sum(red) as red from
dm_avail_city_history group by project, region, state,
month_last_day;
get diagnostics tmp = ROW_COUNT;
count_total := count_total + tmp;
raise log 'Done - % rows inserted', tmp;
......@@ -229,7 +230,19 @@ where t.machine_id = u.machine_id and last_contact_per_month <= month_last_day
raise log 'Inserting state alert history into "dm_alert_city_history"...';
insert into dm_alert_city_history (project, contact_date, region, state,
city, memory_amount, hd_amount)
select
with tmp_alert_machine_history as (
select project, region, state, city, month_last_day, t.machine_id,
last_contact_per_month from dim_machine m, dim_school s,
(select month_last_day, machine_id, max(contact_date) as last_contact_per_month
from fact_inventory i, (select distinct (date_trunc('month', contact_date) + interval '1 month - 1 day')::date as month_last_day from fact_inventory) t
where contact_date <= month_last_day
group by month_last_day, machine_id) u
where u.machine_id = m.id and school_id = s.id
) select project, month_last_day, region, state, city, count(*) as total,
sum(case when memory_alert then 1 else 0 end) as memory_amount,
sum(case when hd_alert then 1 else 0 end) as hd_amount from
tmp_avail_machine_history group by project, region, state, city,
month_last_day;
get diagnostics tmp = ROW_COUNT;
count_total := count_total + tmp;
raise log 'Done - % rows inserted', tmp;
......@@ -237,7 +250,10 @@ where t.machine_id = u.machine_id and last_contact_per_month <= month_last_day
raise log 'Inserting state alert history into "dm_alert_state_history"...';
insert into dm_alert_state_history (project, contact_date, region, state,
memory_amount, hd_amount)
select
select project, month_last_day, region, state, count(*) as total,
sum(memory_amount) as memory_amount, sum(hd_amount) as hd_amount
from dm_alert_city_history group by project, region, state,
month_last_day;
get diagnostics tmp = ROW_COUNT;
count_total := count_total + tmp;
raise log 'Done - % rows inserted', tmp;
......
......@@ -60,13 +60,6 @@ begin
raise log 'Done - row already inserted, using it';
end if;
raise log 'Updating nud_avail field inside "dim_school"...';
update dim_school s set load_date = load_date_id, nud_avail = true
from tmp_net_usage n where s.inep = n.inep and nud_avail is false;
get diagnostics tmp = ROW_COUNT;
count_total := count_total + tmp;
raise log 'Done - % rows updated', tmp;
raise log 'Inserting new component data into "dim_component"...';
insert into dim_component (load_date, category, description, detail)
select distinct load_date_id, 'os'::category_enum, trim(both from
......@@ -84,7 +77,7 @@ begin
raise log 'Done - % rows inserted', tmp;
raise log 'Updating project field of old machines inside "dim_machine"...';
update dim_machine m set load_date = load_date_id, project = i.project
update dim_machine m set project = i.project
from tmp_inventory i, dim_school s where i.machine = m.machine and
i.inep = s.inep and s.id = m.school_id and i.project <> m.project;
get diagnostics tmp = ROW_COUNT;
......
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