Commit 6be5a2d3 authored by Erik Alexandre Pucci's avatar Erik Alexandre Pucci

database: More modifications in load scripts

Signed-off-by: default avatarErik Alexandre Pucci <eap08@c3sl.ufpr.br>
parent 9ecfce82
......@@ -108,15 +108,20 @@ begin
insert into dm_avail_city_history (project, contact_date, region, state,
city, total, green, yellow, red)
with tmp_avail_machine_history as (
select
(select distinct machine_id, max(contact_date) as last_contact_month
from fact_inventory i group by machine_id, date_trunc('month',
contact_date)) s
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 max(last_contact_month), count(*) as total, sum(case when
last_contact_month <) as green
from tmp_avail_machine_history h
group by project, region, state, city;
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
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;
......
......@@ -31,7 +31,7 @@ create or replace function load_data_warehouse() returns void as $$
declare
count_total integer := 0;
tmp integer;
1 integer;
load_date_id integer;
min_date date;
/* -------------------------------------------------------------------------- */
begin
......@@ -50,10 +50,10 @@ begin
/* Update dimension tables */
raise log 'Inserting "current_date" into "dim_date"...';
select id into 1 from dim_date where load_date = current_date;
if 1 is null then
select id into load_date_id from dim_date where load_date = current_date;
if load_date_id is null then
insert into dim_date (load_date) values (current_date) returning id into
1;
load_date_id;
raise log 'Done - 1 row inserted';
count_total := 1;
else
......@@ -61,7 +61,7 @@ begin
end if;
raise log 'Updating nud_avail field inside "dim_school"...';
update dim_school s set load_date = 1, nud_avail = true
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;
......@@ -69,22 +69,22 @@ begin
raise log 'Inserting new component data into "dim_component"...';
insert into dim_component (load_date, category, description, detail)
select distinct 1, 'os'::category_enum, trim(both from
select distinct load_date_id, 'os'::category_enum, trim(both from
os_type), trim(both from os_distro) from tmp_inventory
union all select distinct 1, 'kernel'::category_enum,
union all select distinct load_date_id, 'kernel'::category_enum,
trim(both from kernel), null from tmp_inventory
union all select distinct 1, 'processor'::category_enum,
union all select distinct load_date_id, 'processor'::category_enum,
trim(both from processor), null from tmp_inventory
union all select distinct 1, 'hd'::category_enum, trim(both
union all select distinct load_date_id, 'hd'::category_enum, trim(both
from hd_model), hd_size::text from tmp_inventory
except select 1, category, description, detail from
except select load_date_id, category, description, detail from
dim_component;
get diagnostics tmp = ROW_COUNT;
count_total := count_total + tmp;
raise log 'Done - % rows inserted', tmp;
raise log 'Updating project field of old machines inside "dim_machine"...';
update dim_machine m set load_date = 1, project = i.project
update dim_machine m set load_date = load_date_id, 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;
......@@ -93,9 +93,9 @@ begin
raise log 'Inserting new machine data into "dim_machine"...';
insert into dim_machine (load_date, school_id, machine, project)
select 1, id, machine, project from tmp_inventory i,
select load_date_id, id, machine, project from tmp_inventory i,
dim_school s where i.inep = s.inep
except select 1, school_id, machine, project from
except select load_date_id, school_id, machine, project from
dim_machine;
get diagnostics tmp = ROW_COUNT;
count_total := count_total + tmp;
......@@ -167,7 +167,7 @@ begin
insert into fact_inventory (load_date, contact_date, machine_id, os_id,
kernel_id, processor_id, memory_size, hd_id, hd_used, hd2_id, hd2_used,
extra_hds, hash)
select distinct on (contact_date, m.id, hash) 1,
select distinct on (contact_date, m.id, hash) load_date_id,
contact_date, m.id, o.id, k.id, p.id, memory_size, h.id, hd_used,
i.id, hd2_used, extra_hds, md5(o.id::text || k.id::text ||
p.id::text || memory_size::text || h.id::text || hd_used::text ||
......@@ -200,11 +200,11 @@ begin
fact_inventory where contact_date >= min_date) s group by machine_id
having count(*) > 1) t where i.machine_id = t.machine_id and i.hd_id
= c.id) u
) select 1, o.id, n.id, case when n.memory_size <
) select load_date_id, o.id, n.id, case when n.memory_size <
o.memory_size * 0.9 then true else false end, case when n.hd_size <
o.hd_size * 0.9 then true else false end from tmp_alert o, tmp_alert
n where o.machine_id = n.machine_id and o.row_n = n.row_n - 1
except select 1, old_inventory, inventory, memory_alert,
except select load_date_id, old_inventory, inventory, memory_alert,
hd_alert from fact_alert;
get diagnostics tmp = ROW_COUNT;
count_total := count_total + tmp;
......@@ -216,7 +216,7 @@ begin
raise log 'Inserting new network usage data into "fact_net_usage"...';
insert into fact_net_usage (load_date, machine_id, collect_time, down_kbits,
down_packages, up_kbits, up_packages)
select 1, m.id, collect_time, down_kbits, down_packages,
select load_date_id, m.id, collect_time, down_kbits, down_packages,
up_kbits, up_packages from tmp_net_usage n, dim_machine m,
dim_school s where n.machine = m.machine and n.inep = s.inep and
m.school_id = s.id;
......@@ -226,7 +226,7 @@ begin
raise log 'Updating existing rows with new network usage data per school '
'in "fact_net_usage_school"...';
update fact_net_usage_school f set load_date = 1, down_kbits =
update fact_net_usage_school f set load_date = load_date_id, down_kbits =
sdb, down_packages = sdp, up_kbits = sub, up_packages = sup
from (select t.id, t.collect_time, sum(down_kbits) as sdb,
sum(down_packages) as sdp, sum(up_kbits) as sub, sum(up_packages) as
......@@ -245,7 +245,7 @@ begin
'"fact_net_usage_school"...';
insert into fact_net_usage_school (load_date, school_id, collect_time,
down_kbits, down_packages, up_kbits, up_packages)
select 1, t.id, t.collect_time, sum(down_kbits),
select load_date_id, t.id, t.collect_time, sum(down_kbits),
sum(down_packages), sum(up_kbits), sum(up_packages) from (select id,
collect_time from tmp_net_usage n, dim_school s where n.inep =
s.inep
......
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