-- lawe.v_bi_alm_all_alarm_year source

CREATE OR REPLACE
ALGORITHM = UNDEFINED VIEW `v_bi_alm_all_alarm_year` AS
select
    `m`.`alarm_object_ip` AS `alarm_object_ip`,
    `m`.`alarm_object_id` AS `alarm_object_id`,
    `m`.`alarm_object_type` AS `alarm_object_type`,
    `m`.`alarm_object_name` AS `alarm_object_name`,
    `m`.`severity_id` AS `severity_id`,
    `m`.`title` AS `title`,
    `m`.`alarm_text` AS `alarm_text`,
    `m`.`clr_status` AS `clr_status`,
    `m`.`alarm_count` AS `alarm_count`,
    `m`.`bg_time` AS `bg_time`,
    `m`.`ed_time` AS `ed_time`,
    `m`.`years` AS `years`,
    `m`.`dtype` AS `dtype`,
    `m`.`thedvalue` AS `thedvalue`,
    `m`.`is_active_alarm` AS `is_active_alarm`,
    floor((`m`.`alarm_count` / `m`.`years`)) AS `avg_alarm_count`,
    if((`m`.`alarm_source` = '性能告警'),
    (((60 / 5) * 24) * 365),
    (((60 * 2) * 24) * 365)) AS `collection_frequency`,
    abs((case when ((`m`.`occur_time` <= `m`.`thedvalue_b`) and (`m`.`update_time` >= `m`.`thedvalue_e`)) then timestampdiff(MINUTE, str_to_date(`m`.`thedvalue_b`, '%Y%m%d%H%m'), str_to_date(`m`.`thedvalue_e`, '%Y%m%d%H%m')) when ((`m`.`occur_time` > `m`.`thedvalue_b`) and (`m`.`update_time` >= `m`.`thedvalue_e`)) then timestampdiff(MINUTE, str_to_date(`m`.`occur_time`, '%Y%m%d%H%m'), str_to_date(`m`.`thedvalue_e`, '%Y%m%d%H%m')) when ((`m`.`occur_time` <= `m`.`thedvalue_b`) and (`m`.`update_time` < `m`.`thedvalue_e`)) then timestampdiff(MINUTE, str_to_date(`m`.`thedvalue_b`, '%Y%m%d%H%m'), str_to_date(`m`.`update_time`, '%Y%m%d%H%m')) when ((`m`.`occur_time` > `m`.`thedvalue_b`) and (`m`.`update_time` < `m`.`thedvalue_e`)) then timestampdiff(MINUTE, str_to_date(`m`.`occur_time`, '%Y%m%d%H%m'), str_to_date(`m`.`update_time`, '%Y%m%d%H%m')) end)) AS `alarm_duration`
from
    (
    select
        `aa`.`alarm_object_ip` AS `alarm_object_ip`,
        `aa`.`alarm_object_id` AS `alarm_object_id`,
        `aa`.`alarm_object_type` AS `alarm_object_type`,
        `aa`.`alarm_object_name` AS `alarm_object_name`,
        `aa`.`severity_id` AS `severity_id`,
        `aa`.`title` AS `title`,
        `aa`.`alarm_text` AS `alarm_text`,
        `aa`.`clr_status` AS `clr_status`,
        `aa`.`alarm_count` AS `alarm_count`,
        cast(date_format(`aa`.`occur_time`, '%Y') as unsigned) AS `bg_time`,
        cast(date_format(`aa`.`update_time`, '%Y') as unsigned) AS `ed_time`,
        (timestampdiff(YEAR,
        `aa`.`occur_time`,
        `aa`.`update_time`) + 1) AS `years`,
        'YEAR' AS `dtype`,
        `dates`.`thedvalue` AS `thedvalue`,
        1 AS `is_active_alarm`,
        `aa`.`alarm_source` AS `alarm_source`,
        date_format(str_to_date(concat(`dates`.`thedvalue`, '0101'), '%Y%m%d'), '%Y%m%d0000') AS `thedvalue_b`,
        date_format((str_to_date(concat(`dates`.`thedvalue`, '0101'), '%Y%m%d') + interval 1 year), '%Y%m%d%H00') AS `thedvalue_e`,
        date_format(`aa`.`occur_time`, '%Y%m%d%H%m') AS `occur_time`,
        date_format(`aa`.`update_time`, '%Y%m%d%H%m') AS `update_time`
    from
        (`t_alm_active_alarm` `aa`
    join (
        select
            distinct floor((`dd`.`YYYYMMDD` / 10000)) AS `thedvalue`
        from
            `dim_date` `dd`
        where
            (`dd`.`YYYYMMDD` between 20230101 and 20261231)) `dates`)
union all
    select
        `ha`.`alarm_object_ip` AS `alarm_object_ip`,
        `ha`.`alarm_object_id` AS `alarm_object_id`,
        `ha`.`alarm_object_type` AS `alarm_object_type`,
        `ha`.`alarm_object_name` AS `alarm_object_name`,
        `ha`.`severity_id` AS `severity_id`,
        `ha`.`title` AS `title`,
        `ha`.`alarm_text` AS `alarm_text`,
        `ha`.`clr_status` AS `clr_status`,
        `ha`.`alarm_count` AS `alarm_count`,
        cast(date_format(`ha`.`occur_time`, '%Y') as unsigned) AS `bg_time`,
        cast(date_format(`ha`.`update_time`, '%Y') as unsigned) AS `ed_time`,
        (timestampdiff(YEAR,
        `ha`.`occur_time`,
        `ha`.`update_time`) + 1) AS `years`,
        'YEAR' AS `dtype`,
        `dates`.`thedvalue` AS `thedvalue`,
        0 AS `is_active_alarm`,
        `ha`.`alarm_source` AS `alarm_source`,
        date_format(str_to_date(concat(`dates`.`thedvalue`, '0101'), '%Y%m%d'), '%Y%m%d0000') AS `thedvalue_b`,
        date_format((str_to_date(concat(`dates`.`thedvalue`, '0101'), '%Y%m%d%H%m') + interval 1 year), '%Y%m%d%H00') AS `thedvalue_e`,
        date_format(`ha`.`occur_time`, '%Y%m%d%H%m') AS `occur_time`,
        date_format(`ha`.`update_time`, '%Y%m%d%H%m') AS `update_time`
    from
        (`t_alm_history_alarm` `ha`
    join (
        select
            distinct floor((`dd`.`YYYYMMDD` / 10000)) AS `thedvalue`
        from
            `dim_date` `dd`
        where
            (`dd`.`YYYYMMDD` between 20230101 and 20261231)) `dates`)) `m`
where
    (`m`.`thedvalue` between `m`.`bg_time` and `m`.`ed_time`);