BEGIN;

--
--  Update transfer_log model
--

DO $$
    BEGIN
        BEGIN
            ALTER TABLE transfer_log ADD COLUMN origin_app varchar(255) DEFAULT '';
        EXCEPTION
            WHEN duplicate_column THEN
            --do nothing
        END;
    END;
$$;

DO $$
    BEGIN
        BEGIN
            ALTER TABLE transfer_log ADD COLUMN destination_app varchar(255) DEFAULT '';
        EXCEPTION
            WHEN duplicate_column THEN
            --do nothing
        END;
    END;
$$;

with apps as (
  SELECT instance_id, value as app_name from instance_data where name='appName'
)
update transfer_log
set
  origin_app=(select app_name from apps where apps.instance_id=transfer_log.origin_id),
  destination_app=(select app_name from apps where apps.instance_id=transfer_log.destination_id)
;

ALTER TABLE transfer_log ALTER COLUMN flow_log_id SET DATA TYPE bigint USING flow_log_id::bigint;


--
-- indexes for history
--

DROP INDEX IF EXISTS transfer_log_updated_idx;
CREATE INDEX transfer_log_updated_idx on transfer_log(updated);

DROP INDEX IF EXISTS transfer_log_starttime_idx;
CREATE INDEX transfer_log_starttime_idx on transfer_log(starttime desc);

DROP INDEX IF EXISTS transfer_log_endtime_idx;
CREATE INDEX transfer_log_endtime_idx on transfer_log(endtime desc);

DROP INDEX IF EXISTS transfer_log_flow_log_id_idx;
CREATE INDEX transfer_log_flow_log_id_idx on transfer_log(flow_log_id);

--
-- Création des vues pour le reporting
--

-- hour reports

DROP INDEX IF EXISTS transfer_hour_report_data_hour_idx;
DROP MATERIALIZED VIEW IF EXISTS transfer_hour_report_data;
CREATE MATERIALIZED VIEW transfer_hour_report_data AS
  SELECT report.date as date,
    count(1) AS totalcount,
    sum(report.okcount) AS okcount,
    sum(report.kocount) AS kocount,
    sum(report.size) AS size,
    sum(report.duration) AS duration,
    sum(report.speed) AS speedsum,
    report.origin_hostid,
    report.destination_hostid,
    report.origin_app,
    report.destination_app
  FROM (
    SELECT
      date_trunc('hour', transfer_log.starttime at time zone 'GMT') AS date,
      CASE
        WHEN transfer_log.status::text = '6'::text THEN 1
        ELSE 0
      END AS okcount,
      CASE
        WHEN transfer_log.status::text = '4'::text THEN 1
        ELSE 0
      END AS kocount,
      transfer_log.size,
      transfer_log.duration,
      transfer_log.speed,
      transfer_log.origin_hostid,
      transfer_log.destination_hostid,
      transfer_log.origin_app,
      transfer_log.destination_app
    FROM transfer_log
  ) AS report
  GROUP BY date, report.origin_hostid, report.destination_hostid, report.origin_app, report.destination_app
  ORDER BY date DESC
WITH DATA;
CREATE INDEX transfer_hour_report_data_hour_idx ON  transfer_hour_report_data USING btree (date DESC);


-- day reports

DROP INDEX IF EXISTS transfer_day_report_data_hour_idx;
DROP MATERIALIZED VIEW IF EXISTS transfer_day_report_data;
CREATE MATERIALIZED VIEW transfer_day_report_data AS
 SELECT
    date_trunc('day', date) as date,
    sum(totalcount) AS totalcount,
    sum(okcount) AS okcount,
    sum(kocount) AS kocount,
    sum(size) AS size,
    sum(duration) AS duration,
    sum(speedsum) AS speedsum,
    origin_hostid,
    destination_hostid,
    origin_app,
    destination_app
  FROM transfer_hour_report_data
  GROUP BY date, origin_hostid, destination_hostid, origin_app, destination_app
  ORDER BY date DESC
WITH DATA;
CREATE INDEX transfer_day_report_data_hour_idx ON  transfer_day_report_data USING btree (date DESC);


-- month reports

DROP INDEX IF EXISTS transfer_month_report_data_hour_idx;
DROP MATERIALIZED VIEW IF EXISTS transfer_month_report_data;
CREATE MATERIALIZED VIEW transfer_month_report_data AS
 SELECT
    date_trunc('month', date) as date,
    sum(totalcount) AS totalcount,
    sum(okcount) AS okcount,
    sum(kocount) AS kocount,
    sum(size) AS size,
    sum(duration) AS duration,
    sum(speedsum) AS speedsum,
    origin_hostid,
    destination_hostid,
    origin_app,
    destination_app
  FROM transfer_day_report_data
  GROUP BY date, origin_hostid, destination_hostid, origin_app, destination_app
  ORDER BY date DESC
WITH DATA;
CREATE INDEX transfer_month_report_data_hour_idx ON  transfer_month_report_data USING btree (date DESC);

-- year reports
DROP INDEX IF EXISTS transfer_year_report_data_hour_idx;
DROP MATERIALIZED VIEW IF EXISTS transfer_year_report_data;
CREATE MATERIALIZED VIEW transfer_year_report_data AS
 SELECT
    date_trunc('year', date) as date,
    sum(totalcount) AS totalcount,
    sum(okcount) AS okcount,
    sum(kocount) AS kocount,
    sum(size) AS size,
    sum(duration) AS duration,
    sum(speedsum) AS speedsum,
    origin_hostid,
    destination_hostid,
    origin_app,
    destination_app
  FROM transfer_month_report_data
  GROUP BY date, origin_hostid, destination_hostid, origin_app, destination_app
  ORDER BY date DESC
WITH DATA;
CREATE INDEX transfer_year_report_data_hour_idx ON  transfer_year_report_data USING btree (date DESC);

COMMIT;
