--MARS-770

set verify off
SET serveroutput ON
--SET TIMING ON
whenever sqlerror exit sql.sqlcode rollback;

column owner format a15
column object_name format a30
column spoolname new_value spoolname 

select 'install_package_MarsGeneralRelease_MARS770_views_' || name || '_' ||to_char(sysdate,'YYYYMMDD_HH24MISS')||'.log' spoolname from v$pdbs;
spool '&spoolname'


prompt ##### started at time #####
select systimestamp from dual;
prompt ##### database name #####
select name pdb_name from v$pdbs;

--set echo on
--=============================================================================================================================
-- start install --MARS-770 views
--=============================================================================================================================

create or replace view CT_MRDS.VW_A_PROCESS_LOG as
select * from (
   select 
       l.a_process_log_key
      ,l.guid
      ,l.process_name
      ,l.procedure_name
      ,l.log_level
      ,l.log_message
      ,l.log_timestamp
      ,lead(log_message) over (partition by l.process_name, l.procedure_name, guid order by l.a_process_log_key) as next_log_message
      ,lead(log_timestamp) over (partition by l.process_name, l.procedure_name, guid order by l.a_process_log_key) as next_log_timestamp
   from CT_MRDS.a_process_log l
) where 1=1
  and log_message='Start'
order by log_timestamp
;

create or replace view CT_MRDS.VW_FILE_RECONCILIATION as 
select 
    case 
      when c.bytes is null then 'RECEIVED_MISSING_ON_CLOUD'
      when r.bytes is null then 'NOT_RECEIVED'
      when r.bytes is not null and c.bytes is not null then 'RECEIVED'
      else 'UNKNOWN'
    end as status
   ,c.object_name as cloud_file
   ,r.* 
from DBMS_CLOUD.LIST_OBJECTS('OCI$RESOURCE_PRINCIPAL', CT_MRDS.FILE_MANAGER.GET_INBOX_BUCKET_URI()) c
full join a_source_file_received r
   on  c.bytes    = r.bytes
   and c.checksum = r.checksum
   and c.created  = r.created
  ;

create or replace view CT_MRDS.VW_FILE_RECONCILIATION_SUMMARY as 
select 
	all_statuses.status
	,nvl(recon_stats.cnt, 0) as number_of_files
from (
			  select 'RECEIVED_MISSING_ON_CLOUD' as status from dual
	union all select 'NOT_RECEIVED'                        from dual
	union all select 'RECEIVED'                            from dual
	union all select 'UNKNOWN'                             from dual
	) all_statuses
left join (select status, count(*) as cnt from CT_MRDS.VW_FILE_RECONCILIATION group by status
			) recon_stats
on all_statuses.status = recon_stats.status
order by 1
;

CREATE OR REPLACE FORCE EDITIONABLE VIEW "CT_MRDS"."A_MOPDB_LOAD_HISTORY" ("A_WORKFLOW_HISTORY_KEY", "A_WORKFLOW_HISTORY_SOURCE_KEY", "A_TASK_HISTORY_KEY", "TASK_NAME", "TASK_START", "TASK_END", "WORKFLOW_SUCCESSFUL") DEFAULT COLLATION "USING_NLS_COMP"  AS 
  SELECT distinct wh.a_workflow_history_key,
                    ths.a_workflow_history_source_key,
                    th.a_task_history_key,
                    th.task_name,
                    th.task_start,
                    th.task_end,
                    wh.workflow_successful
        FROM CT_MRDS.A_WORKFLOW_HISTORY wh
        JOIN CT_MRDS.A_TASK_HISTORY th
        ON wh.a_workflow_history_key = th.a_workflow_history_key
        AND wh.service_name = th.service_name
        JOIN CT_MRDS.A_TASK_HISTORY_SOURCE ths
        ON th.a_task_history_key = ths.a_task_history_key 
        AND th.service_name = ths.service_name
        WHERE wh.service_name = 'MOPDB'
        AND wh.workflow_successful = 'Y'
        AND lower(th.task_name) not like '%_sq'
        AND ths.a_workflow_history_source_key is not null;

CREATE OR REPLACE FORCE EDITIONABLE VIEW "CT_MRDS"."A_DWH_LOAD_HISTORY" ("A_WORKFLOW_HISTORY_KEY", "A_WORKFLOW_HISTORY_SOURCE_KEY", "A_TASK_HISTORY_KEY", "TASK_NAME", "TASK_START", "TASK_END", "WORKFLOW_SUCCESSFUL") DEFAULT COLLATION "USING_NLS_COMP"  AS 
  SELECT distinct wh.a_workflow_history_key,
                    ths.a_workflow_history_source_key,
                    th.a_task_history_key,
                    th.task_name,
                    th.task_start,
                    th.task_end,
                    wh.workflow_successful
        FROM CT_MRDS.A_WORKFLOW_HISTORY wh
        JOIN CT_MRDS.A_TASK_HISTORY th
        ON wh.a_workflow_history_key = th.a_workflow_history_key
        AND wh.service_name = th.service_name
        JOIN CT_MRDS.A_TASK_HISTORY_SOURCE ths
        ON th.a_task_history_key = ths.a_task_history_key 
        AND th.service_name = ths.service_name
        WHERE wh.service_name = 'RAR'
        AND wh.workflow_successful = 'Y'
        AND lower(th.task_name) not like '%_sq'
        AND ths.a_workflow_history_source_key is not null;

