Skip to content

Commit

Permalink
feat(ddlview):enhance v_rpt_arc_* add new columns: tot_headloss, leng…
Browse files Browse the repository at this point in the history
…th, tot_headloss_max and tot_headloss_min

feat(ddlview): enhance ve_epa_pipe add new tot_headloss_max and tot_headloss_min
feat(tables): recreate rpt_arc_stats and archived_rpt_arc_stats with new columns, tables created on this previous commit [10cfe1e]
feat(dml): insert data on config_form_fields and recover data from old tables
feat(fct): add new logic to manage new columns on rpt functions
  • Loading branch information
danimarinBG committed Jan 20, 2025
1 parent 483513b commit b26bc1b
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 21 deletions.
75 changes: 75 additions & 0 deletions updates/40/40000/ws/ddl.sql
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,78 @@ SELECT gw_fct_admin_manage_fields($${"data":{"action":"CHANGETYPE","table":"pres
SELECT gw_fct_admin_manage_fields($${"data":{"action":"CHANGETYPE","table":"presszone", "column":"lastupdate_user", "dataType":"varchar(50)"}}$$);
SELECT gw_fct_admin_manage_fields($${"data":{"action":"CHANGETYPE","table":"sector", "column":"insert_user", "dataType":"varchar(50)"}}$$);
SELECT gw_fct_admin_manage_fields($${"data":{"action":"CHANGETYPE","table":"sector", "column":"lastupdate_user", "dataType":"varchar(50)"}}$$);

-- 20/01/2025
ALTER TABLE rpt_arc_stats RENAME TO _rpt_arc_stats;
ALTER TABLE _rpt_arc_stats RENAME CONSTRAINT rpt_arc_stats_pkey TO _rpt_arc_stats_pkey;
DROP INDEX IF EXISTS rpt_arc_stats_flow_avg;
DROP INDEX IF EXISTS rpt_arc_stats_flow_max;
DROP INDEX IF EXISTS rpt_arc_stats_flow_min;
DROP INDEX IF EXISTS rpt_arc_stats_geom;
DROP INDEX IF EXISTS rpt_arc_stats_vel_avg;
DROP INDEX IF EXISTS rpt_arc_stats_vel_max;
DROP INDEX IF EXISTS rpt_arc_stats_vel_min;

ALTER TABLE archived_rpt_arc_stats RENAME TO _archived_rpt_arc_stats;
ALTER TABLE _archived_rpt_arc_stats RENAME CONSTRAINT archived_rpt_arc_stats_pkey TO _archived_rpt_arc_stats_pkey;

CREATE TABLE rpt_arc_stats (
arc_id varchar(16) NOT NULL,
result_id varchar(30) NOT NULL,
arc_type varchar(30) NULL,
sector_id int4 NULL,
arccat_id varchar(30) NULL,
flow_max numeric NULL,
flow_min numeric NULL,
flow_avg numeric(12, 2) NULL,
vel_max numeric NULL,
vel_min numeric NULL,
vel_avg numeric(12, 2) NULL,
headloss_max numeric NULL,
headloss_min numeric NULL,
setting_max numeric NULL,
setting_min numeric NULL,
reaction_max numeric NULL,
reaction_min numeric NULL,
ffactor_max numeric NULL,
ffactor_min numeric NULL,
length numeric NULL,
tot_headloss_max numeric(12, 2) NULL,
tot_headloss_min numeric(12, 2) NULL,
the_geom public.geometry(linestring, 25831) NULL,
CONSTRAINT rpt_arc_stats_pkey PRIMARY KEY (arc_id, result_id)
);
CREATE INDEX rpt_arc_stats_flow_avg ON rpt_arc_stats USING btree (flow_avg);
CREATE INDEX rpt_arc_stats_flow_max ON rpt_arc_stats USING btree (flow_max);
CREATE INDEX rpt_arc_stats_flow_min ON rpt_arc_stats USING btree (flow_min);
CREATE INDEX rpt_arc_stats_geom ON rpt_arc_stats USING gist (the_geom);
CREATE INDEX rpt_arc_stats_vel_avg ON rpt_arc_stats USING btree (vel_avg);
CREATE INDEX rpt_arc_stats_vel_max ON rpt_arc_stats USING btree (vel_max);
CREATE INDEX rpt_arc_stats_vel_min ON rpt_arc_stats USING btree (vel_min);

CREATE TABLE archived_rpt_arc_stats (
arc_id varchar(16) NOT NULL,
result_id varchar(30) NOT NULL,
arc_type varchar(30) NULL,
sector_id int4 NULL,
arccat_id varchar(30) NULL,
flow_max numeric NULL,
flow_min numeric NULL,
flow_avg numeric(12, 2) NULL,
vel_max numeric NULL,
vel_min numeric NULL,
vel_avg numeric(12, 2) NULL,
headloss_max numeric NULL,
headloss_min numeric NULL,
setting_max numeric NULL,
setting_min numeric NULL,
reaction_max numeric NULL,
reaction_min numeric NULL,
ffactor_max numeric NULL,
ffactor_min numeric NULL,
length numeric NULL,
tot_headloss_max numeric(12, 2) NULL,
tot_headloss_min numeric(12, 2) NULL,
the_geom public.geometry(linestring, 25831) NULL,
CONSTRAINT archived_rpt_arc_stats_pkey PRIMARY KEY (arc_id, result_id)
);
21 changes: 14 additions & 7 deletions updates/40/40000/ws/ddlview.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2273,6 +2273,9 @@ AS SELECT r.arc_id,
r.reaction_min,
r.ffactor_max,
r.ffactor_min,
r.length,
r.tot_headloss_max,
r.tot_headloss_min,
r.the_geom
FROM rpt_arc_stats r,
selector_rpt_main s
Expand All @@ -2290,13 +2293,15 @@ AS SELECT rpt_arc.id,
rpt_arc.headloss,
rpt_arc.setting,
rpt_arc.ffactor,
'2001-01-01'::date + rpt_arc."time"::interval AS "time",
now()::date + rpt_arc."time"::interval AS "time",
rpt_arc.length,
rpt_arc.headloss * rpt_arc.length / 1000 AS tot_headloss,
arc.the_geom
FROM selector_rpt_main,
rpt_inp_arc arc
JOIN rpt_arc ON rpt_arc.arc_id::text = arc.arc_id::text
WHERE rpt_arc.result_id::text = selector_rpt_main.result_id::text AND selector_rpt_main.cur_user = "current_user"()::text AND arc.result_id::text = selector_rpt_main.result_id::text
ORDER BY rpt_arc.setting, arc.arc_id;
rpt_inp_arc arc
JOIN rpt_arc ON rpt_arc.arc_id::text = arc.arc_id::text
WHERE rpt_arc.result_id::text = selector_rpt_main.result_id::text AND selector_rpt_main.cur_user = "current_user"()::text AND arc.result_id::text = selector_rpt_main.result_id::text
ORDER BY rpt_arc.setting, arc.arc_id;


CREATE OR REPLACE VIEW ve_epa_junction
Expand Down Expand Up @@ -2599,7 +2604,7 @@ CREATE OR REPLACE VIEW ve_epa_pipe AS
a.builtdate,
r.roughness AS cat_roughness,
inp_pipe.custom_roughness,
a.cat_dint,
a.cat_dint,
inp_pipe.custom_dint,
inp_pipe.reactionparam,
inp_pipe.reactionvalue,
Expand All @@ -2619,7 +2624,9 @@ CREATE OR REPLACE VIEW ve_epa_pipe AS
v_rpt_arc_stats.reaction_max,
v_rpt_arc_stats.reaction_min,
v_rpt_arc_stats.ffactor_max,
v_rpt_arc_stats.ffactor_min
v_rpt_arc_stats.ffactor_min,
v_rpt_arc_stats.tot_headloss_max,
v_rpt_arc_stats.tot_headloss_min
FROM vu_arc a
JOIN inp_pipe USING (arc_id)
LEFT JOIN v_rpt_arc_stats ON split_part(v_rpt_arc_stats.arc_id::text, 'P'::text, 1) = inp_pipe.arc_id::text
Expand Down
25 changes: 25 additions & 0 deletions updates/40/40000/ws/dml.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2928,3 +2928,28 @@ DELETE FROM sys_function

UPDATE config_form_fields SET formname = 'v_rpt_node_stats' WHERE formname = 'v_rpt_node';
UPDATE config_form_fields SET formname = 'v_rpt_node' WHERE formname = 'v_rpt_node_all';

-- recover data from old tables
INSERT INTO rpt_arc_stats
SELECT arc_id, result_id, arc_type, sector_id, arccat_id, flow_max, flow_min, flow_avg, vel_max, vel_min, vel_avg,
headloss_max, headloss_min, setting_max, setting_min, reaction_max, reaction_min, ffactor_max, ffactor_min, NULL,
NULL, NULL, the_geom
FROM _rpt_arc_stats;

INSERT INTO archived_rpt_arc_stats
SELECT arc_id, result_id, arc_type, sector_id, arccat_id, flow_max, flow_min, flow_avg, vel_max, vel_min, vel_avg,
headloss_max, headloss_min, setting_max, setting_min, reaction_max, reaction_min, ffactor_max, ffactor_min, NULL,
NULL, NULL, the_geom
FROM _archived_rpt_arc_stats;

INSERT INTO config_form_fields (formname, formtype, tabname, columnname, layoutname, layoutorder, "datatype", widgettype, "label", tooltip, placeholder,
ismandatory, isparent, iseditable, isautoupdate, isfilter, dv_querytext, dv_orderby_id, dv_isnullvalue, dv_parent_id, dv_querytext_filterc, stylesheet,
widgetcontrols, widgetfunction, linkedobject, hidden, web_layoutorder)
VALUES('ve_epa_pipe', 'form_feature', 'tab_epa', 'tot_headloss_max', 'lyt_epa_data_2', 21, 'string', 'text', 'Max Tot Headloss:', 'Max Tot Headloss', NULL,
false, false, false, false, false, NULL, NULL, NULL, NULL, NULL, NULL, '{"filterSign":"ILIKE"}'::json, NULL, NULL, false, NULL);

INSERT INTO config_form_fields (formname, formtype, tabname, columnname, layoutname, layoutorder, "datatype", widgettype, "label", tooltip, placeholder,
ismandatory, isparent, iseditable, isautoupdate, isfilter, dv_querytext, dv_orderby_id, dv_isnullvalue, dv_parent_id, dv_querytext_filterc, stylesheet,
widgetcontrols, widgetfunction, linkedobject, hidden, web_layoutorder)
VALUES('ve_epa_pipe', 'form_feature', 'tab_epa', 'tot_headloss_min', 'lyt_epa_data_2', 22, 'string', 'text', 'Min Tot Headloss:', 'Min Tot Headloss', NULL,
false, false, false, false, false, NULL, NULL, NULL, NULL, NULL, NULL, '{"filterSign":"ILIKE"}'::json, NULL, NULL, false, NULL);
23 changes: 13 additions & 10 deletions ws/fct/ws_gw_fct_rpt2pg_main.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ This version of Giswater is provided by Giswater Association

DROP FUNCTION IF EXISTS "SCHEMA_NAME".gw_fct_rpt2pg(character varying );
DROP FUNCTION IF EXISTS "SCHEMA_NAME".gw_fct_rpt2pg(json);
CREATE OR REPLACE FUNCTION SCHEMA_NAME.gw_fct_rpt2pg_main(p_data json)
RETURNS json AS
CREATE OR REPLACE FUNCTION SCHEMA_NAME.gw_fct_rpt2pg_main(p_data json)
RETURNS json AS
$BODY$

/*EXAMPLE
Expand Down Expand Up @@ -38,13 +38,13 @@ BEGIN
v_file = (p_data ->>'data')::json->>'file';
v_step = (p_data->>'data')::json->>'step';

--
--
IF v_step = 1 THEN

RAISE NOTICE 'Starting rpt2pg process.';
CREATE TEMP TABLE temp_t_csv (LIKE SCHEMA_NAME.temp_csv INCLUDING ALL);
CREATE TEMP TABLE temp_audit_check_data (LIKE SCHEMA_NAME.audit_check_data INCLUDING ALL);

-- inserting file into temp table
INSERT INTO temp_t_csv (fid, cur_user, source, csv1, csv2, csv3, csv4, csv5, csv6, csv7, csv8, csv9, csv10, csv11, csv12, csv13, csv14, csv15, csv16, csv17, csv18, csv19, csv20, csv40)
SELECT 140 , current_user, replace(a::json->>'target','''',''), a::json->>'col1', a::json->>'col2',a::json->>'col3',a::json->>'col4',a::json->>'col5',a::json->>'col6',a::json->>'col7',
Expand All @@ -54,25 +54,25 @@ BEGIN

v_return = '{"status": "Accepted", "message":{"level":1, "text":"Import RPT file 1/2:Import data...... done succesfully"}}'::json;
RETURN v_return;

ELSIF v_step = 2 THEN

-- reordening data from temp table to rpt tables
p_data = concat('{"client":{"device":4, "infoType":1, "lang":"ES"}, "form":{}, "feature":{}, "data":{"resultId":"'||v_result||'"}}');
SELECT gw_fct_rpt2pg_import_rpt(p_data) INTO v_import;

-- Reverse geometries where flow is negative and updating flow values with absolute value
UPDATE rpt_inp_arc SET the_geom=st_reverse(the_geom) FROM rpt_arc WHERE rpt_arc.arc_id=rpt_inp_arc.arc_id AND flow<0 AND rpt_inp_arc.result_id=v_result;
UPDATE rpt_arc SET flow=(-1)*flow WHERE flow<0 and result_id=v_result;

-- set result on result selector
-- NOTE: In spite of there are four selectors tables () only it's setted one
DELETE FROM selector_rpt_main WHERE cur_user=current_user;
INSERT INTO selector_rpt_main (result_id, cur_user) VALUES (v_result, current_user);

-- update rpt_cat_result
UPDATE rpt_cat_result SET exec_date = now(), cur_user = current_user , status = 2 WHERE result_id = v_result;

-- delete wrong VALUES
UPDATE rpt_node SET time='0:00' where time ='null' and result_id = v_result;
UPDATE rpt_arc SET time='0:00' where time ='null' and result_id = v_result;
Expand All @@ -98,6 +98,9 @@ BEGIN
min(rpt_arc.reaction) AS reaction_min,
max(rpt_arc.ffactor) AS ffactor_max,
min(rpt_arc.ffactor) AS ffactor_min,
rpt_arc.length,
max(rpt_arc.headloss * rpt_arc.length / 1000)::numeric(12, 2) AS tot_headloss_max,
min(rpt_arc.headloss * rpt_arc.length / 1000)::numeric(12, 2) AS tot_headloss_min,
arc.the_geom
FROM selector_rpt_main,
rpt_inp_arc arc
Expand All @@ -106,7 +109,7 @@ BEGIN
GROUP BY arc.arc_id, arc.arc_type, arc.sector_id, arc.arccat_id, selector_rpt_main.result_id, arc.the_geom
ORDER BY arc.arc_id;

INSERT INTO rpt_node_stats
INSERT INTO rpt_node_stats
SELECT node.node_id,
selector_rpt_main.result_id,
node.node_type,
Expand Down
12 changes: 8 additions & 4 deletions ws/fct/ws_gw_fct_set_rpt_archived.sql
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ BEGIN
-- inserting on archived_rpt_arc_stats
EXECUTE 'INSERT INTO archived_rpt_arc_stats(
arc_id, result_id, arc_type, sector_id, arccat_id, flow_max, flow_min, flow_avg, vel_max, vel_min, vel_avg,
headloss_max, headloss_min, setting_max, setting_min, reaction_max, reaction_min, ffactor_max, ffactor_min, the_geom)
headloss_max, headloss_min, setting_max, setting_min, reaction_max, reaction_min, ffactor_max, ffactor_min, length,
tot_headloss_max, tot_headloss_min, the_geom)
SELECT
arc_id, result_id, arc_type, sector_id, arccat_id, flow_max, flow_min, flow_avg, vel_max, vel_min, vel_avg,
headloss_max, headloss_min, setting_max, setting_min, reaction_max, reaction_min, ffactor_max, ffactor_min, the_geom
headloss_max, headloss_min, setting_max, setting_min, reaction_max, reaction_min, ffactor_max, ffactor_min, length,
tot_headloss_max, tot_headloss_min, the_geom
FROM rpt_arc_stats
WHERE result_id = '||quote_literal(v_result_id)||';';

Expand Down Expand Up @@ -180,10 +182,12 @@ BEGIN
-- inserting on archived_rpt_arc_stats
EXECUTE 'INSERT INTO rpt_arc_stats(
arc_id, result_id, arc_type, sector_id, arccat_id, flow_max, flow_min, flow_avg, vel_max, vel_min, vel_avg,
headloss_max, headloss_min, setting_max, setting_min, reaction_max, reaction_min, ffactor_max, ffactor_min, the_geom)
headloss_max, headloss_min, setting_max, setting_min, reaction_max, reaction_min, ffactor_max, ffactor_min, length,
tot_headloss_max, tot_headloss_min, the_geom)
SELECT
arc_id, result_id, arc_type, sector_id, arccat_id, flow_max, flow_min, flow_avg, vel_max, vel_min, vel_avg,
headloss_max, headloss_min, setting_max, setting_min, reaction_max, reaction_min, ffactor_max, ffactor_min, the_geom
headloss_max, headloss_min, setting_max, setting_min, reaction_max, reaction_min, ffactor_max, ffactor_min, length,
tot_headloss_max, tot_headloss_min, the_geom
FROM archived_rpt_arc_stats
WHERE result_id = '||quote_literal(v_result_id)||';';

Expand Down

0 comments on commit b26bc1b

Please sign in to comment.