DROP DATABASE IF EXISTS `monitor`;
CREATE DATABASE `monitor`;
use `monitor`;

DROP TABLE IF EXISTS `event_record`;
CREATE TABLE `event_record`
(
    `id`          BIGINT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    `system_id`   TINYINT(4) UNSIGNED NOT NULL,
    `category`    VARCHAR(255)         NOT NULL,
    `name`        VARCHAR(255)         NOT NULL,
    `create_time` BIGINT(13) UNSIGNED NOT NULL,
    `status`      TINYINT(4) UNSIGNED NOT NULL DEFAULT 1,
    INDEX `idx_event_ct` (`create_time`)
) ENGINE = innoDB
  DEFAULT CHARSET = utf8mb4;

DROP TABLE IF EXISTS `error_record`;
CREATE TABLE `error_record`
(
    `id`             BIGINT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    `system_id`      TINYINT(4) UNSIGNED NOT NULL,
    `category`       VARCHAR(255)         NOT NULL,
    `name`           VARCHAR(255)         NOT NULL,
    `create_time`    BIGINT(13) UNSIGNED NOT NULL,
    `message`        VARCHAR(255)        NOT NULL,
    `exception_info` VARCHAR(255)        NOT NULL,
    `stack_trace`    TEXT                NOT NULL,
    INDEX `idx_error_ct` (`create_time`)
) ENGINE = innoDB
  DEFAULT CHARSET = utf8mb4;

DROP TABLE IF EXISTS `metric_record`;
CREATE TABLE `metric_record`
(
    `id`         BIGINT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    `system_id`  TINYINT(4) UNSIGNED NOT NULL,
    `category`   VARCHAR(255)         NOT NULL,
    `name`       VARCHAR(255)         NOT NULL,
    `start_time` BIGINT(13) UNSIGNED NOT NULL,
    `total`      INT(10) UNSIGNED    NOT NULL DEFAULT 0,
    `value`      INT(10)             NOT NULL,
    `type`       TINYINT(4)          NOT NULL,
    UNIQUE INDEX `uniq_idx_metric_sid_c_s_st_t` (`system_id`, `category`, `name`, `start_time`, `type`),
    INDEX `idx_metric_st` (`start_time`)
) ENGINE = innoDB
  DEFAULT CHARSET = utf8mb4;

DROP TABLE IF EXISTS `call_record`;
CREATE TABLE `call_record`
(
    `id`          BIGINT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    `system_id`   TINYINT(4) UNSIGNED NOT NULL,
    `category`    VARCHAR(255)         NOT NULL,
    `name`        VARCHAR(255)         NOT NULL,
    `create_time` BIGINT(13) UNSIGNED NOT NULL,
    `duration`    BIGINT(13) UNSIGNED NOT NULL,
    `status`      TINYINT(4) UNSIGNED NOT NULL,
    INDEX `idx_call_ct` (`create_time`)
) ENGINE = innoDB
  DEFAULT CHARSET = utf8mb4;

DROP TABLE IF EXISTS `alert_config`;
CREATE TABLE `alert_config`
(
    `id`             BIGINT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    `type`           TINYINT(4) UNSIGNED NOT NULL,
    `system_id`      TINYINT(4) UNSIGNED NOT NULL,
    `category`       VARCHAR(255)         NOT NULL,
    `name`           VARCHAR(255)         NOT NULL,
    `alert_item`     TINYINT(4)          NOT NULL,
    `alert_level`    TINYINT(4) UNSIGNED NOT NULL,
    `duration`       INT(4) UNSIGNED     NOT NULL,
    `start_time`     VARCHAR(5)          NOT NULL,
    `end_time`       VARCHAR(5)          NOT NULL,
    `receiver_email` VARCHAR(255)        NOT NULL,
    INDEX `idx_alert_call_sid_c_n` (`type`, `system_id`, `category`, `name`)
) ENGINE = innoDB
  DEFAULT CHARSET = utf8mb4;

DROP TABLE IF EXISTS `sub_condition`;
CREATE TABLE `sub_condition`
(
    `id`        BIGINT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    `cid`       BIGINT(10) UNSIGNED NOT NULL,
    `type`      TINYINT(4) UNSIGNED NOT NULL,
    `value`     DECIMAL(10, 4)      NOT NULL,
    `frequency` INT(4)              NOT NULL DEFAULT 0,
    INDEX `idx_sc_cid` (`cid`)
) ENGINE = innoDB
  DEFAULT CHARSET = utf8mb4;

DROP TABLE IF EXISTS `alert_receiver`;
CREATE TABLE `alert_receiver`
(
    `id`          BIGINT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    `type`        TINYINT(4) UNSIGNED NOT NULL,
    `system_id`   TINYINT(4) UNSIGNED NOT NULL,
    `category`    VARCHAR(255)         NOT NULL,
    `name`        VARCHAR(255)         NOT NULL,
    `alert_level` TINYINT(4) UNSIGNED NOT NULL,
    `email`       VARCHAR(255)        NOT NULL
) ENGINE = innoDB
  DEFAULT CHARSET = utf8mb4;

DROP TABLE IF EXISTS `alert_receiver_group`;
CREATE TABLE `alert_receiver_group`
(
    `id`         BIGINT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    `group_name` VARCHAR(255) NOT NULL,
    `emails`     TEXT        NOT NULL,
    UNIQUE INDEX `uniq_idx_gn` (`group_name`)
) ENGINE = innoDB
  DEFAULT CHARSET = utf8mb4;

DROP TABLE IF EXISTS `alert_mail`;
CREATE TABLE `alert_mail`
(
    `id`          BIGINT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    `create_time` BIGINT(13) UNSIGNED NOT NULL,
    `subject`     TEXT                NOT NULL,
    `text`        TEXT                NOT NULL,
    INDEX `idx_am_ct` (`create_time`)
) ENGINE = innoDB
  DEFAULT CHARSET = utf8mb4;