CREATE TABLE public.tenant_info (
    id character varying(255) NOT NULL,
    name character varying(255) NOT NULL,
    in_used boolean DEFAULT true,
    created_date timestamp without time zone NOT NULL,
    modified_date timestamp without time zone NOT NULL,
    delete_scheduler_date timestamp without time zone NULL,
    contract_id bigint NOT NULL,
    tenant_init_clusters character varying(255) NULL
);
ALTER TABLE ONLY public.tenant_info ADD CONSTRAINT tenant_info_pkey PRIMARY KEY (id);

CREATE TABLE public.alert_group (
    id bigint NOT NULL,
    created_date timestamp without time zone NOT NULL,
    modified_date timestamp without time zone NOT NULL,
    cluster_id character varying(255) NOT NULL,
    description character varying(255),
    name character varying(255) NOT NULL,
    type character varying(255) NOT NULL,
    namespace character varying(255) DEFAULT 'default'::character varying
);

ALTER TABLE public.alert_group OWNER TO admin;

ALTER TABLE ONLY public.alert_group
    ADD CONSTRAINT alert_group_pkey PRIMARY KEY (id);

CREATE UNIQUE INDEX alert_group_name_uindex ON public.alert_group USING btree (name);

CREATE TABLE public.alert_target (
    id bigint NOT NULL,
    created_date timestamp without time zone,
    modified_date timestamp without time zone,
    cluster_id character varying(255) NOT NULL,
    entity_id character varying(255) NOT NULL,
    entity_type character varying(255) NOT NULL,
    alert_group_id bigint,
    namespace character varying(255)
);

ALTER TABLE public.alert_target OWNER TO admin;

ALTER TABLE ONLY public.alert_target
    ADD CONSTRAINT alert_target_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.alert_target
    ADD CONSTRAINT fkjrvj775641ky7s0f82kx3sile FOREIGN KEY (alert_group_id) REFERENCES public.alert_group(id);

CREATE TABLE public.report_group (
    id bigint NOT NULL,
    created_by character varying(255),
    created_date timestamp without time zone,
    modified_by character varying(255),
    modified_date timestamp without time zone,
    users text,
    name character varying(255)
);
ALTER TABLE public.report_group OWNER TO admin;

ALTER TABLE ONLY public.report_group ADD CONSTRAINT report_group_pkey PRIMARY KEY (id);

CREATE TABLE public.report_template (
    id bigint NOT NULL,
    created_by character varying(255),
    created_date timestamp without time zone NOT NULL,
    modified_by character varying(255),
    modified_date timestamp without time zone NOT NULL,
    cron_exp character varying(255),
    enable boolean NOT NULL,
    metric_data text,
    template_data text,
    title character varying(255),
    report_group_id int8 NULL
);

ALTER TABLE public.report_template OWNER TO admin;

ALTER TABLE ONLY public.report_template
    ADD CONSTRAINT report_template_pkey PRIMARY KEY (id);


CREATE TABLE public.report_group_registry (
    id bigint NOT NULL,
    report_template_id bigint NOT NULL,
    report_group_id bigint NOT NULL
);

ALTER TABLE public.report_group_registry OWNER TO admin;

ALTER TABLE ONLY public.report_group_registry ADD CONSTRAINT report_group_registry_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.report_group_registry ADD CONSTRAINT report_group_registry_template_id_fk FOREIGN KEY (report_template_id) REFERENCES public.report_template(id);
ALTER TABLE ONLY public.report_group_registry ADD CONSTRAINT report_group_registry_group_id_fk FOREIGN KEY (report_group_id) REFERENCES public.report_group(id);

CREATE TABLE public.alert_event (
    id bigint NOT NULL,
    created_date timestamp without time zone NOT NULL,
    modified_date timestamp without time zone NOT NULL,
    alert_name character varying(255) NOT NULL,
    cluster_id character varying(255) NOT NULL,
    data text NOT NULL,
    entity_id character varying(255) NOT NULL,
    entity_type character varying(255) NOT NULL,
    level character varying(255) NOT NULL,
    meta_id character varying(255) NOT NULL,
    namespace character varying(255),
    starts_at bigint NOT NULL,
    threshold character varying(255) NOT NULL,
    value character varying(255) NOT NULL,
    message character varying(255),
    ends_at bigint,
    status character varying(20) NOT NULL,
    hook_collect_at bigint
);

ALTER TABLE public.alert_event OWNER TO admin;

CREATE TABLE public.metric_meta2 (
    id character varying(255) NOT NULL,
    meta_name character varying(255) NOT NULL,
    description character varying(255) NOT NULL,
    expr text NOT NULL,
    resource_type character varying(255),
    entity_type character varying(255) NOT NULL,
    groupby_keys character varying(255),
    in_use boolean DEFAULT false NOT NULL,
    anomaly_score boolean DEFAULT false NOT NULL,
    message character varying(255) NOT NULL,
    created_date timestamp without time zone DEFAULT now() NOT NULL,
    modified_date timestamp without time zone DEFAULT now() NOT NULL
);

ALTER TABLE public.metric_meta2 OWNER to admin;

ALTER TABLE ONLY public.metric_meta2
    ADD CONSTRAINT metric_meta2_pk PRIMARY KEY (id);

CREATE TABLE public.alert_rule (
    id bigint NOT NULL,
    created_date timestamp without time zone NOT NULL,
    modified_date timestamp without time zone NOT NULL,
    critical float,
    name character varying(255),
    warning float,
    alert_group_id bigint,
    alert_rule_meta_id character varying(255) NOT NULL,
    alert_target_id bigint,
    duration character varying(255) NOT NULL,
    pause boolean DEFAULT false NOT NULL,
    warning_sign character varying(255),
    critical_sign character varying(255)
);

ALTER TABLE public.alert_rule OWNER TO admin;

ALTER TABLE ONLY public.alert_rule
    ADD CONSTRAINT alert_rule_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.alert_rule
    ADD CONSTRAINT fk6b09d1xfyago6wiiqhdiv03s3 FOREIGN KEY (alert_rule_meta_id) REFERENCES public.metric_meta2(id);

ALTER TABLE ONLY public.alert_rule
    ADD CONSTRAINT fk8wkucwkgr48hkfg8cvuptww0f FOREIGN KEY (alert_group_id) REFERENCES public.alert_group(id);

ALTER TABLE ONLY public.alert_rule
    ADD CONSTRAINT fkiqaskea7ts0f872u3nx9ne25u FOREIGN KEY (alert_target_id) REFERENCES public.alert_target(id);

CREATE TABLE public.alert_rule_meta (
    id bigint NOT NULL,
    created_date timestamp without time zone NOT NULL,
    modified_date timestamp without time zone NOT NULL,
    description text NOT NULL,
    expr character varying(255) NOT NULL,
    meta_name character varying(255) NOT NULL,
    target character varying(255) NOT NULL,
    message character varying(255)
);

ALTER TABLE public.alert_rule_meta OWNER TO admin;

ALTER TABLE ONLY public.alert_rule_meta
    ADD CONSTRAINT alert_rule_meta_pkey PRIMARY KEY (id);

CREATE SEQUENCE hibernate_sequence;

CREATE TABLE public.cloud_group (
    id bigint NOT NULL,
    created_date timestamp without time zone NOT NULL,
    modified_date timestamp without time zone NOT NULL,
    name character varying(255) NOT NULL,
    description character varying(255),
    created_by character varying(255),
    auth_resource_id bigint
);

ALTER TABLE public.cloud_group OWNER TO admin;

ALTER TABLE ONLY public.cloud_group
    ADD CONSTRAINT cloud_group_pkey PRIMARY KEY (id);

CREATE UNIQUE INDEX cloud_group_name_uindex ON public.cloud_group USING btree (name);

CREATE TABLE public.cloud_user (
    user_id character varying(255) NOT NULL,
    email character varying(255),
    is_admin boolean NOT NULL,
    phone character varying(255),
    user_nm character varying(255) NOT NULL,
    user_pw character varying(255) NOT NULL,
    created_date timestamp without time zone NOT NULL,
    modified_date timestamp without time zone NOT NULL,
    dormancy_date timestamp without time zone NULL,
    company character varying(255),
    department character varying(255),
    last_log_in_date timestamp without time zone,
    "position" character varying(255),
    use_ldap boolean NOT NULL,
    auth_method character varying(255) NOT NULL,
    log_in_count integer default 0 NOT NULL,
    user_lock boolean default false NOT NULL,
    user_lock_date timestamp without time zone,
    tenant_id character varying(120),
    is_tenant_owner boolean default false,
    auth_resource_id bigint,
    status character varying(255) default 'use' NOT NULL
);

ALTER TABLE public.cloud_user OWNER TO admin;

ALTER TABLE ONLY public.cloud_user ADD CONSTRAINT cloud_user_pkey PRIMARY KEY (user_id);

ALTER TABLE ONLY public.cloud_user
    ADD CONSTRAINT cloud_user_tenant_id_fk FOREIGN KEY (tenant_id) REFERENCES public.tenant_info(id);

CREATE TABLE public.menu_meta (
    id bigint NOT NULL,
    description character varying(255),
    icon character varying(255),
    "position" integer NOT NULL,
    url character varying(255),
    auth_resource3_id bigint NOT NULL,
    scope_level int default 0
);

ALTER TABLE public.menu_meta OWNER TO admin;

ALTER TABLE ONLY public.menu_meta
    ADD CONSTRAINT menu_meta_pkey PRIMARY KEY (id);



CREATE TABLE public.metric_base (
    meta_name character varying(255) NOT NULL,
    provider character varying(255) NOT NULL,
    description character varying(255) NOT NULL,
    resource_type character varying(255),
    diag_type character varying(255),
    entity_type character varying(255) NOT NULL,
    metric_type character varying(255) NOT NULL,
    keys character varying(255),
    created_date timestamp without time zone DEFAULT now() NOT NULL,
    modified_date timestamp without time zone DEFAULT now() NOT NULL
);


ALTER TABLE public.metric_base OWNER TO admin;

ALTER TABLE ONLY public.metric_base
    ADD CONSTRAINT metric_base_pk PRIMARY KEY (meta_name);

CREATE TABLE public.report_static (
    id bigint NOT NULL,
    created_by character varying(255),
    created_date timestamp without time zone NOT NULL,
    modified_by character varying(255),
    modified_date timestamp without time zone NOT NULL,
    cron_exp character varying(255),
    metric_data text,
    template_data text,
    title character varying(255),
    type character varying(255),
    report_template_id bigint
);

ALTER TABLE public.report_static OWNER TO admin;

ALTER TABLE ONLY public.report_static
    ADD CONSTRAINT report_static_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.report_static
    ADD CONSTRAINT fk7o821ym9a57lrcfipf928cfpe FOREIGN KEY (report_template_id) REFERENCES public.report_template(id);

CREATE TABLE public.user_group (
    user_group_id bigint NOT NULL,
    user_id character varying(255) NOT NULL
);

ALTER TABLE public.user_group OWNER TO admin;

ALTER TABLE ONLY public.user_group
    ADD CONSTRAINT user_group_pkey PRIMARY KEY (user_group_id, user_id);

ALTER TABLE ONLY public.user_group
    ADD CONSTRAINT fkooy6rip2craw6jy3geb5wnix6 FOREIGN KEY (user_id) REFERENCES public.cloud_user(user_id);

ALTER TABLE ONLY public.user_group
    ADD CONSTRAINT fkowo8h9te5nwashab3u30docg FOREIGN KEY (user_group_id) REFERENCES public.cloud_group(id);

CREATE TABLE public.cloud_user_profile (
    user_id character varying(255) NOT NULL,
    created_date timestamp without time zone,
    modified_date timestamp without time zone,
    profile_image oid
);

ALTER TABLE public.cloud_user_profile OWNER TO admin;

ALTER TABLE ONLY public.cloud_user_profile
    ADD CONSTRAINT cloud_user_profile_pkey PRIMARY KEY (user_id);


CREATE TABLE public.common_setting (
    code_id character varying(255) NOT NULL,
    code_value character varying(255),
    code_desc character varying(255),
    code_auth character varying(255),
    code_group character varying(255),
    created_date timestamp without time zone,
    modified_date timestamp without time zone
);


ALTER TABLE public.common_setting OWNER TO admin;

ALTER TABLE ONLY public.common_setting
    ADD CONSTRAINT common_setting_pkey PRIMARY KEY (code_id);



CREATE TABLE public.dashboard_thumbnail (
    id bigint NOT NULL,
    thumbnail_image oid,
    created_date timestamp without time zone,
    modified_date timestamp without time zone
);


ALTER TABLE public.dashboard_thumbnail OWNER TO admin;

ALTER TABLE ONLY public.dashboard_thumbnail
    ADD CONSTRAINT dashboard_thumbnail_pkey PRIMARY KEY (id);



CREATE TABLE public.notification_channel (
    id bigint NOT NULL,
    created_by character varying(255),
    created_date timestamp without time zone,
    modified_by character varying(255),
    modified_date timestamp without time zone,
    cluster_id character varying(255),
    config text,
    name character varying(255),
    type character varying(255)
);

ALTER TABLE public.notification_channel OWNER TO admin;

ALTER TABLE ONLY public.notification_channel
    ADD CONSTRAINT notification_channel_pkey PRIMARY KEY (id);


CREATE TABLE public.notification_registry (
    id bigint NOT NULL,
    alert_rule_id bigint NOT NULL,
    notification_channel_id bigint
);

ALTER TABLE public.notification_registry OWNER TO admin;

ALTER TABLE ONLY public.notification_registry
    ADD CONSTRAINT notification_registry_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.notification_registry
    ADD CONSTRAINT fk28xo8snm6fd19i3uap0oba0d1 FOREIGN KEY (notification_channel_id) REFERENCES public.notification_channel(id);


CREATE TABLE public.license_check_2 (
    id bigint NOT NULL,
    site_name character varying(255) NOT NULL,
    license_type integer NOT NULL,
    expire_date character varying(255) NOT NULL,
    imxc_host_id integer NOT NULL,
    real_host_id integer NOT NULL,
    imxc_cpu_count integer NOT NULL,
    real_cpu_count integer NOT NULL,
    target_clusters_count integer NOT NULL,
    real_clusters_count integer NOT NULL,
    target_nodes_count integer NOT NULL,
    real_nodes_count integer NOT NULL,
    target_pods_count integer NOT NULL,
    real_pods_count integer NOT NULL,
    target_svcs_count integer NOT NULL,
    real_svcs_count integer NOT NULL,
    target_core_count integer NOT NULL,
    real_core_count integer NOT NULL,
    features_bitmap integer NOT NULL,
    allowable_range integer NOT NULL,
    check_time timestamp without time zone NOT NULL,
    check_result integer NOT NULL
);

ALTER TABLE public.license_check_2
    ADD CONSTRAINT license_check_pkey PRIMARY KEY (id);

CREATE INDEX license_check_check_time_idx ON license_check_2(check_time);


CREATE TABLE public.license_violation (
    id bigint not null,
    check_id bigint not null,
    check_time timestamp without time zone not null,
    violation_item varchar not null,
    allow_time timestamp without time zone not null,
    resolved_id bigint,
    resolved_time timestamp without time zone
);

ALTER TABLE public.license_violation
    ADD CONSTRAINT license_violation_pkey PRIMARY KEY (id);

ALTER TABLE public.license_violation
    ADD CONSTRAINT license_violation_check_id_fk FOREIGN KEY (check_id) REFERENCES public.license_check_2(id);

ALTER TABLE public.license_violation
    ADD CONSTRAINT license_violation_resolved_id_fk FOREIGN KEY (resolved_id) REFERENCES public.license_check_2(id);

CREATE INDEX license_violation_check_time_idx ON license_violation(check_time);
CREATE INDEX license_violation_resolved_time_idx ON license_violation(resolved_time);


CREATE TABLE public.license_key (
	id bigint NOT NULL,
	license_key text NOT NULL,
	set_time timestamp NOT NULL,
	in_used bool NULL,
	tenant_id varchar NULL,
	cluster_id bigint NULL,
	CONSTRAINT license_key_pkey PRIMARY KEY (id)
);

ALTER TABLE public.license_key ADD CONSTRAINT license_key_tenant_id_fk FOREIGN KEY (tenant_id) REFERENCES public.tenant_info(id);

CREATE TABLE public.license_check2 (
    id bigint NOT NULL,
    site_name character varying(255) NOT NULL,
    license_type integer NOT NULL,
    expire_date character varying(255) NOT NULL,
    imxc_host_ids character varying(255),
    real_host_ids character varying(255),
    target_nodes_count integer NOT NULL,
    real_nodes_count integer NOT NULL,
    target_pods_count integer NOT NULL,
    real_pods_count integer NOT NULL,
    target_svcs_count integer NOT NULL,
    real_svcs_count integer NOT NULL,
    target_core_count integer NOT NULL,
    real_core_count integer NOT NULL,
    allowable_range integer NOT NULL,
    license_cluster_id character varying(255),
    check_time timestamp without time zone NOT NULL,
    check_result integer NOT null
);

ALTER TABLE public.license_check2
    ADD CONSTRAINT license_check2_pkey PRIMARY KEY (id);

CREATE INDEX license_check2_time_idx ON license_check2(check_time);

CREATE TABLE public.license_violation2 (
    id bigint not null,
    check_id bigint not null,
    check_time timestamp without time zone not null,
    violation_item varchar not null,
    allow_time timestamp without time zone not null,
    resolved_id bigint,
    resolved_time timestamp without time zone,
    cluster_id varchar not null
);

ALTER TABLE public.license_violation2
    ADD CONSTRAINT license_violation2_pkey PRIMARY KEY (id);

ALTER TABLE public.license_violation2
    ADD CONSTRAINT license_violation2_check_id_fk FOREIGN KEY (check_id) REFERENCES public.license_check2(id);

ALTER TABLE public.license_violation2
    ADD CONSTRAINT license_violation2_resolved_id_fk FOREIGN KEY (resolved_id) REFERENCES public.license_check2(id);

CREATE INDEX license_violation2_check_time_idx ON license_violation2(check_time);
CREATE INDEX license_violation2_resolved_time_idx ON license_violation2(resolved_time);

CREATE TABLE public.license_key2 (
    id bigint not null,
    license_key text not null,
    set_time timestamp without time zone not null,
    cluster_id varchar,
    license_used bool not null
);

ALTER TABLE public.license_key2
    ADD CONSTRAINT license_key2_pkey PRIMARY KEY (id);

create table public.license_policy (
	policy_id character varying(255) NOT NULL,
	policy_desc character varying(255),
	term_year integer NOT NULL,
	term_month integer NOT NULL,
	term_day integer NOT NULL,
	license_type character varying(255) NOT NULL,
	allowable_range character varying(255) NOT NULL,
	storage_capacity character varying(255) NOT NULL,
	cluster_count character varying(255) NOT NULL,
	node_count character varying(255) NOT NULL,
	pod_count character varying(255) NOT NULL,
	service_count character varying(255) NOT NULL,
	core_count character varying(255) NOT NULL,
	host_ids character varying(255) NOT NULL,
	user_division character varying(255) NOT NULL,
	created_date timestamp without time zone,
    modified_date timestamp without time zone
);

ALTER TABLE ONLY public.license_policy
    ADD CONSTRAINT license_policy_pkey PRIMARY KEY (policy_id);


CREATE TABLE public.auth_resource2 (
    id bigint NOT NULL default nextval('hibernate_sequence'),
    access_type integer NOT NULL,
    name character varying(255) NOT NULL,
    parent_id bigint,
    type character varying(255) NOT NULL
);

ALTER TABLE public.auth_resource2 OWNER TO admin;

ALTER TABLE ONLY public.auth_resource2
    ADD CONSTRAINT auth_resource2_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.auth_resource2
    ADD CONSTRAINT resource_name_uniq UNIQUE (name, type, parent_id);

--ALTER TABLE ONLY public.auth_resource2
--    ADD CONSTRAINT auth_resource2_auth_resource_id_fk FOREIGN KEY (parent_id) REFERENCES public.auth_resource2(id);
--
--ALTER TABLE ONLY public.menu_meta
--    ADD CONSTRAINT  fk2tqq4ybf6w130fsaejhrsnw5s FOREIGN KEY (auth_resource_id) REFERENCES public.auth_resource2(id);

CREATE TABLE public.user_permission2 (
    id bigint NOT NULL,
    created_date timestamp without time zone NOT NULL,
    modified_date timestamp without time zone NOT NULL,
    all_child boolean NOT NULL,
    permission integer NOT NULL,
    auth_resource_id bigint,
    user_id character varying(255)
);

ALTER TABLE public.user_permission2 OWNER TO admin;

ALTER TABLE ONLY public.user_permission2
    ADD CONSTRAINT user_permission2_pkey PRIMARY KEY (id);

-- ALTER TABLE ONLY public.user_permission2
--     ADD CONSTRAINT user_permission2_auth_resource2_fk FOREIGN KEY (auth_resource_id) REFERENCES public.auth_resource2(id);

ALTER TABLE ONLY public.user_permission2
    ADD CONSTRAINT user_permission2_user_id_fk FOREIGN KEY (user_id) REFERENCES public.cloud_user(user_id);


CREATE TABLE public.group_permission2 (
    id bigint NOT NULL,
    created_date timestamp without time zone NOT NULL,
    modified_date timestamp without time zone NOT NULL,
    all_child boolean NOT NULL,
    permission integer NOT NULL,
    auth_resource_id bigint,
    user_group_id bigint
);

ALTER TABLE public.group_permission2 OWNER TO admin;

ALTER TABLE ONLY public.group_permission2
    ADD CONSTRAINT group_permission2_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.group_permission2
    ADD CONSTRAINT group_permission2_user_group_id_fk FOREIGN KEY (user_group_id) REFERENCES public.cloud_group(id);

-- ALTER TABLE ONLY public.group_permission2
--     ADD CONSTRAINT group_permission2_auth_resource2_fk FOREIGN KEY (auth_resource_id) REFERENCES public.auth_resource2(id);

CREATE TABLE public.resource_group2 (
	id int8 NOT NULL,
	created_date timestamp NOT NULL,
	modified_date timestamp NOT NULL,
	"name" varchar(255) NOT NULL,
	description varchar(255) NULL,
	CONSTRAINT resource_group2_pkey PRIMARY KEY (id)
--	CONSTRAINT resource_group2_fk1 FOREIGN KEY (id) REFERENCES auth_resource2(id)
);

ALTER TABLE public.resource_group2 OWNER TO "admin";
GRANT ALL ON TABLE public.resource_group2 TO "admin";

CREATE TABLE public.resource_member2 (
	resource_group_id int8 NOT NULL,
	auth_resource_id int8 NOT NULL,
	CONSTRAINT resource_member2_pkey PRIMARY KEY (resource_group_id, auth_resource_id),
	CONSTRAINT resource_member2_fkey1 FOREIGN KEY (resource_group_id) REFERENCES resource_group2(id)
--  CONSTRAINT resource_member2_fkey2 FOREIGN KEY (auth_resource_id) REFERENCES auth_resource2(id)
);

ALTER TABLE public.resource_member2 OWNER TO "admin";
GRANT ALL ON TABLE public.resource_member2 TO "admin";

CREATE TABLE public.dashboard2 (
    id bigint NOT NULL,
    created_date timestamp without time zone NOT NULL,
    modified_date timestamp without time zone NOT NULL,
    layout text NOT NULL,
    title character varying(255) NOT NULL,
    auth_resource_id bigint NOT NULL,
    created_by character varying(255) NOT NULL,
    modified_by character varying(255) NOT NULL,
    description character varying(255),
    share boolean DEFAULT false
);

ALTER TABLE public.dashboard2 OWNER TO admin;

ALTER TABLE ONLY public.dashboard2
    ADD CONSTRAINT dashboard2_pkey PRIMARY KEY (id);

-- ALTER TABLE ONLY public.dashboard2
--     ADD CONSTRAINT dashboard_resource_fk FOREIGN KEY (auth_resource_id) REFERENCES public.auth_resource2(id);

CREATE TABLE public.log_management (
    cluster_id varchar NOT NULL,
    node_id varchar NOT NULL,
    log_rotate_dir varchar,
    log_rotate_count integer,
    log_rotate_size integer,
    log_rotate_management boolean NOT NULL,
    back_up_dir varchar,
    back_up_period integer,
    back_up_dir_size integer,
    back_up_management boolean NOT NULL,
    created_date timestamp without time zone NOT NULL,
    modified_date timestamp without time zone
);

alter table public.log_management add constraint log_management_pkey primary key (cluster_id, node_id);

CREATE TABLE public.sampling_setting (
    service_id bigint NOT NULL,
    service_name character varying(255),
    sampling_type character varying(255),
    sampling_param character varying(255),
    cluster varchar,
    namespace varchar,
    cluster_id bigint
);
ALTER TABLE public.sampling_setting OWNER TO admin;

ALTER TABLE ONLY public.sampling_setting
    ADD CONSTRAINT sampling_setting_pkey PRIMARY KEY (service_id);

CREATE TABLE public.operation_setting (
    id bigint NOT NULL,
    service_id bigint NOT NULL,
    sampling_type character varying(255),
    sampling_param character varying(255),
	operation_name character varying(255)
);

ALTER TABLE public.operation_setting OWNER TO admin;

ALTER TABLE ONLY public.operation_setting
    ADD CONSTRAINT operation_setting_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.operation_setting
    ADD CONSTRAINT operation_setting_fkey FOREIGN KEY (service_id) REFERENCES public.sampling_setting(service_id);

CREATE TABLE public.cluster_setting (
    cluster_id bigint NOT NULL,
    param_type character varying(255),
    param_value character varying(255),
    cluster_name varchar,
	name character varying(255)
);

ALTER TABLE ONLY public.cluster_setting
    ADD CONSTRAINT cluster_setting_pkey PRIMARY KEY (cluster_id);

CREATE TABLE public.alias_code (
	user_id varchar NOT NULL,
	id varchar NOT NULL,
	name varchar,
	type varchar,
	use_yn varchar,
	created_date timestamp without time zone NOT NULL,
    modified_date timestamp without time zone
);

ALTER TABLE ONLY public.alias_code add constraint alias_code_pkey primary key (user_id, id);

CREATE TABLE public.sparse_log_info (
    id varchar NOT NULL,
    cluster_id varchar,
    namespace varchar,
    target_type varchar,
    target_id varchar,
    log_path varchar,
    created_date timestamp,
    modified_date timestamp,
    threshold float4,
    PRIMARY KEY ("id")
);

CREATE TABLE public.view_code (
    user_id varchar NOT NULL,
    view_id varchar NOT NULL,
    json_data text,
    created_date timestamp without time zone NOT NULL,
    modified_date timestamp without time zone
);

ALTER TABLE ONLY public.view_code add constraint view_code_pkey primary key (user_id, view_id);

CREATE TABLE public.entity_black_list (
    entity_type varchar not null,
    entity_name varchar not null,
    cluster_id varchar not null,
    namespace varchar,
    black_list bool not null,
    workload varchar(255) not null
);

ALTER TABLE public.entity_black_list
    ADD CONSTRAINT entity_black_list_pkey PRIMARY KEY (entity_type, entity_name, cluster_id, namespace);

CREATE TABLE public.script_setting (
    id bigint NOT NULL,
    name character varying(255),
    agent_list character varying(255),
    file_path character varying(255),
    args character varying(255),
    valid_cmd character varying(255),
    valid_val character varying(255),
    cron_exp character varying(255),
	create_user character varying(255),
	mtime BIGINT,
	created_date timestamp without time zone NOT NULL,
    modified_date timestamp without time zone
);

ALTER TABLE ONLY public.script_setting
    ADD CONSTRAINT script_setting_pkey PRIMARY KEY (id);

CREATE TABLE public.agent_install_file_info (
    id bigint NOT NULL,
    name character varying(255) NOT NULL,
    type character varying(255) NOT NULL,
    description text,
    version character varying(255),
    yaml text,
    use_yn boolean NOT NULL,
    created_date timestamp without time zone NOT NULL,
    modified_date timestamp without time zone NOT NULL
);

ALTER TABLE ONLY public.agent_install_file_info ADD CONSTRAINT agent_install_file_info_pkey PRIMARY KEY (id);

create table auth_resource3(
	id bigint NOT NULL default nextval('hibernate_sequence'),
	name character varying(255) NOT NULL,
	is_deleted boolean not null,
	tenant_id character varying(255)
);

ALTER TABLE public.auth_resource3 owner to admin;

ALTER TABLE ONLY public.auth_resource3
    ADD CONSTRAINT auth_resource3_pkey PRIMARY KEY (id);

ALTER TABLE ONLY public.auth_resource3
    ADD CONSTRAINT auth_resource3_name_uniq UNIQUE (name);

create table resource_member3(
	resource_group_id bigint not null,
	auth_resource3_id bigint not null
);

ALTER TABLE resource_member3 owner to admin;

ALTER TABLE ONLY public.resource_member3
    ADD CONSTRAINT resource_member3_pkey primary key (resource_group_id, auth_resource3_id);

ALTER TABLE ONLY public.auth_resource3 ADD CONSTRAINT auth_resource3_tenant_id_fk FOREIGN KEY (tenant_id) REFERENCES public.tenant_info(id);

ALTER TABLE public.menu_meta ADD CONSTRAINT menu_meta_auth_resource3_fk FOREIGN KEY (auth_resource3_id) REFERENCES auth_resource3(id);
ALTER TABLE public.user_permission2 ADD CONSTRAINT user_permission2_auth_resource3_fk FOREIGN KEY (auth_resource_id) REFERENCES auth_resource3(id);
ALTER TABLE public.resource_group2 ADD CONSTRAINT resource_group2_auth_resource3_fk1 FOREIGN KEY (id) REFERENCES auth_resource3(id);
ALTER TABLE public.resource_member3 ADD CONSTRAINT resource_member3_auth_resource3_fkey1 FOREIGN KEY (resource_group_id) REFERENCES public.resource_group2(id);
ALTER TABLE public.resource_member3 ADD CONSTRAINT resource_member3_auth_resource3_fkey2 FOREIGN KEY (auth_resource3_id) REFERENCES auth_resource3(id);
ALTER TABLE public.group_permission2 ADD CONSTRAINT group_permission2_auth_resource3_fk FOREIGN KEY (auth_resource_id) REFERENCES auth_resource3(id);
ALTER TABLE public.dashboard2 ADD CONSTRAINT dashboard2_auth_resource3_fk FOREIGN KEY (auth_resource_id) REFERENCES auth_resource3(id);
ALTER TABLE public.cloud_user ADD CONSTRAINT cloud_user_auth_resource3_fk FOREIGN KEY (auth_resource_id) REFERENCES auth_resource3(id);
ALTER TABLE public.cloud_group ADD CONSTRAINT cloud_group_auth_resource3_fk FOREIGN KEY (auth_resource_id) REFERENCES auth_resource3(id);

CREATE DATABASE CONFIGS;
CREATE DATABASE keycloak;

-- JSPD 옵션 값 테이블
CREATE TABLE public.jspd_prop (
    code_id character varying(255) NOT NULL,
    default_value character varying(255) NOT NULL,
    description text,
    code_type character varying(255),
    input_type character varying(255),
    input_props character varying(255),
    use_yn boolean NOT NULL,
    created_date timestamp without time zone NOT NULL,
    modified_date timestamp without time zone NOT NULL
);

ALTER TABLE ONLY public.jspd_prop ADD CONSTRAINT jspd_prop_pkey PRIMARY KEY (code_id);

-- JSPD 옵션 값 설정 LIST table
CREATE TABLE public.jspd_config (
    cluster_id character varying(255) NOT NULL,
    namespace character varying(255) NOT NULL,
    service character varying(255) NOT NULL,
    code_id character varying(255),
    code_value character varying(255),
    code_type character varying(255),
    created_date timestamp without time zone NOT NULL,
    modified_date timestamp without time zone NOT NULL
);
-- ALTER TABLE public.jspd_prop
--     ADD input_type character varying(255);

-- ALTER TABLE public.jspd_prop
--     ADD input_props character varying(255);


ALTER TABLE public.jspd_config
    ADD CONSTRAINT jspd_config_pkey PRIMARY KEY (cluster_id, namespace, service, code_id);

ALTER TABLE ONLY public.jspd_config
    ADD CONSTRAINT jspd_config_code_id_fk FOREIGN KEY (code_id) REFERENCES public.jspd_prop(code_id);

-- noti server table
CREATE TABLE public.alert_group_v2 (
    id            bigint       NOT NULL,
    created_date  timestamp    NOT NULL,
    modified_date timestamp    NOT NULL,
    cluster_id    varchar(255) NOT NULL,
    description   varchar(255),
    name          varchar(255) NOT NULL,
    type          varchar(255) NOT NULL,
    namespace     varchar(255) default 'default'::character varying,
    destination   varchar(255) NOT NULL,
    created_by    varchar(255) NOT NULL
);

CREATE TABLE public.alert_target_v2 (
    id             bigint       NOT NULL,
    created_date   timestamp,
    modified_date  timestamp,
    cluster_id     varchar(255) NOT NULL,
    entity_id      varchar(255) NOT NULL,
    entity_type    varchar(255) NOT NULL,
    alert_group_id bigint,
    namespace      varchar(255)
);

CREATE TABLE public.alert_rule_v2 (
    id                 bigint                NOT NULL,
    created_date       timestamp             NOT NULL,
    modified_date      timestamp             NOT NULL,
    critical           double precision,
    name               varchar(255),
    warning            double precision,
    alert_group_id     bigint,
    alert_rule_meta_id varchar(255)          NOT NULL,
    alert_target_id    bigint,
    duration           varchar(255)          NOT NULL,
    pause              boolean DEFAULT false NOT NULL,
    critical_sign      varchar(255),
    warning_sign       varchar(255),
    destination        varchar(255),
    created_by         varchar(255)
);

ALTER TABLE public.alert_group_v2 ADD CONSTRAINT alert_group_v2_id_pk PRIMARY KEY (id);
ALTER TABLE public.alert_target_v2 ADD CONSTRAINT alert_target_v2_id_pk PRIMARY KEY (id);
ALTER TABLE public.alert_rule_v2 ADD CONSTRAINT alert_rule_v2_id_pk PRIMARY KEY (id);

ALTER TABLE public.alert_target_v2 ADD CONSTRAINT alert_target_v2_alert_group_id_fk FOREIGN KEY (alert_group_id) REFERENCES public.alert_group_v2(id);
ALTER TABLE public.alert_rule_v2 ADD CONSTRAINT alert_rule_v2_alert_group_id_fk FOREIGN KEY (alert_group_id) REFERENCES public.alert_group_v2(id);
ALTER TABLE public.alert_rule_v2 ADD CONSTRAINT alert_rule_v2_alert_rule_meta_id_fk FOREIGN KEY (alert_rule_meta_id) REFERENCES public.metric_meta2(id);
ALTER TABLE public.alert_rule_v2 ADD CONSTRAINT alert_rule_v2_alert_target_id_fk FOREIGN KEY (alert_target_id) REFERENCES public.alert_target_v2(id);
ALTER TABLE ONLY public.notification_registry
    ADD CONSTRAINT fk4lljw4fnija73tm3lthjg90rx FOREIGN KEY (alert_rule_id) REFERENCES public.alert_rule_v2(id);


-- cortex alert
create table public.alert_rule_config_info (
    config_id     varchar              not null,
    config_data   text                 not null,
    in_use        boolean default true not null,
    created_date  timestamp,
    modified_date timestamp
);

create table alert_config_info
(
    config_id      varchar              not null,
    config_data    text                 not null,
    config_default text                 not null,
    in_use         boolean default true not null,
    created_date   timestamp,
    modified_date  timestamp
);

create table alert_config
(
    id              varchar               not null,
    cluster_id      varchar,
    resolve_timeout varchar,
    receiver        varchar,
    group_by        varchar,
    group_wait      varchar,
    group_interval  varchar,
    repeat_interval varchar,
    routes_level    varchar,
    routes_continue varchar,
    receiver_name   varchar,
    webhook_url     varchar,
    send_resolved   varchar,
    inner_route     boolean,
    inner_webhook   boolean,
    in_use          boolean default true not null,
    created_date    timestamp,
    modified_date   timestamp
);

ALTER TABLE public.alert_rule_config_info ADD CONSTRAINT alert_rule_config_info_config_id_pk PRIMARY KEY (config_id);
ALTER TABLE public.alert_config_info ADD CONSTRAINT alert_config_info_config_id_pk PRIMARY KEY (config_id);
ALTER TABLE public.alert_config ADD CONSTRAINT alert_config_id_pk PRIMARY KEY (id);

CREATE TABLE public.cloud_user_setting (
    user_id character varying(255) NOT NULL,
    lang character varying(20) DEFAULT 'en',
    theme character varying(20) DEFAULT 'dark',
    access_token integer DEFAULT 30,
    refresh_token integer DEFAULT 10080,
    error_msg boolean DEFAULT false,
    alert_sound boolean DEFAULT false,
    session_persistence boolean DEFAULT true,
    gpu_acc_topology boolean DEFAULT true,
    created_date timestamp without time zone,
    modified_date timestamp without time zone
);

ALTER TABLE public.cloud_user_setting OWNER TO admin;

ALTER TABLE ONLY public.cloud_user_setting ADD CONSTRAINT cloud_user_setting_pkey PRIMARY KEY (user_id);

-------- 2022-05-31 KubeInfo flatting table --------
CREATE TABLE cmoa_configmap_base(
    kube_flatting_time    bigint,
    cluster_id    varchar(255),
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    metadata_name    text,
    kind_status    varchar(50),
    metadata_resourceVersion    text,
    metadata_annotations    text,
    metadata_creationTimestamp    varchar(25),
    metadata_labels    text,
    metadata_namespace    text,
    binaryData    text,
    data    text,
    immutable    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, cluster_id, kind, metadata_uid, row_index)
);
-----------------------
CREATE TABLE cmoa_cronjob_active(
    kube_flatting_time    bigint,
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    metadata_name    text,
    status_active_apiVersion    text,
    status_active_fieldPath    text,
    status_active_kind    text,
    status_active_name    text,
    status_active_namespace    text,
    status_active_resourceVersion    text,
    status_active_uid    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, kind, metadata_uid, row_index)
);
----------------------------
CREATE TABLE cmoa_cronjob_base(
    kube_flatting_time    bigint,
    cluster_id    varchar(255),
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    kind_status    varchar(50),
    metadata_annotations    text,
    metadata_creationTimestamp    varchar(25),
    metadata_labels    text,
    metadata_name    text,
    metadata_namespace    text,
    metadata_resourceVersion    text,
    spec_failedJobsHistoryLimit    text,
    spec_schedule    text,
    spec_successfulJobsHistoryLimit    text,
    spec_suspend    text,
    status_lastScheduleTime    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, cluster_id, kind, metadata_uid, row_index)
);
----------------------------
CREATE TABLE cmoa_daemonset_base(
    kube_flatting_time    bigint,
    cluster_id    varchar(255),
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    kind_status    varchar(50),
    metadata_annotations    text,
    metadata_creationTimestamp    varchar(25),
    metadata_labels    text,
    metadata_name    text,
    metadata_namespace    text,
    metadata_resourceVersion    text,
    status_currentNumberScheduled    text,
    status_desiredNumberScheduled    text,
    status_numberAvailable    text,
    status_numberMisscheduled    text,
    status_numberReady    text,
    status_numberUnavailable    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, cluster_id, kind, metadata_uid, row_index)
);
----------------------------
CREATE TABLE cmoa_deployment_base(
    kube_flatting_time    bigint,
    cluster_id    varchar(255),
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    kind_status    varchar(50),
    metadata_annotations    text,
    metadata_creationTimestamp    varchar(25),
    metadata_labels    text,
    metadata_name    text,
    metadata_namespace    text,
    metadata_resourceVersion    text,
    spec_replicas    text,
    spec_template_spec_containers_image    text,
    status_availableReplicas    text,
    status_readyReplicas    text,
    status_replicas    text,
    status_unavailableReplicas    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, cluster_id, kind, metadata_uid, row_index)
);
----------------------------
CREATE TABLE cmoa_deploymentconfig_base(
    kube_flatting_time    bigint,
    cluster_id    varchar(255),
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    kind_status    varchar(50),
    metadata_creationTimestamp    varchar(25),
    metadata_name    text,
    metadata_namespace    text,
    metadata_resourceVersion    text,
    spec_replicas    text,
    spec_template_spec_containers_image    text,
    spec_template_metadata_labels    text,
    status_availableReplicas    text,
    status_updatedReplicas    text,
    status_replicas    text,
    status_unavailableReplicas    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, cluster_id, kind, metadata_uid, row_index)
);
----------------------------
CREATE TABLE cmoa_endpoint_addresses(
    kube_flatting_time    bigint,
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    metadata_name    text,
    subset_addresses_ip    text,
    subset_addresses_hostname    text,
    subset_addresses_nodeName    text,
    subset_addresses_targetRef    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, kind, metadata_uid, row_index)
);
----------------------------
CREATE TABLE cmoa_endpoint_base(
    kube_flatting_time    bigint,
    cluster_id    varchar(255),
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    kind_status    varchar(50),
    metadata_name    text,
    metadata_resourceVersion    text,
    metadata_annotations    text,
    metadata_creationTimestamp    varchar(25),
    metadata_labels    text,
    metadata_namespace    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, cluster_id, kind, metadata_uid, row_index)
);
----------------------------
CREATE TABLE cmoa_endpoint_notreadyaddresses(
    kube_flatting_time    bigint,
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    metadata_name    text,
    subset_notreadyaddresses_ip    text,
    subset_notreadyaddresses_hostname    text,
    subset_notreadyaddresses_nodename    text,
    subset_notreadyaddresses_targetref    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, kind, metadata_uid, row_index)
);
----------------------------
CREATE TABLE cmoa_endpoint_ports(
    kube_flatting_time    bigint,
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    metadata_name    text,
    subset_ports_port    text,
    subset_ports_appprotocol    text,
    subset_ports_name    text,
    subset_ports_protocol    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, kind, metadata_uid, row_index)
);
----------------------------
CREATE TABLE cmoa_event_base    (
    kube_flatting_time    bigint,
    cluster_id    varchar(255),
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    kind_status    varchar(50),
    action    text,
    count    text,
    eventtime    text,
    firsttimestamp    text,
    involvedobject_apiversion    text,
    involvedobject_fieldpath    text,
    involvedobject_kind    text,
    involvedobject_name    text,
    involvedobject_namespace    text,
    involvedobject_resourceversion    text,
    involvedobject_uid    text,
    lasttimestamp    text,
    message    text,
    metadata_annotations    text,
    metadata_creationtimestamp    varchar(25),
    metadata_labels    text,
    metadata_name    text,
    metadata_namespace    text,
    metadata_resourceversion    text,
    reason    text,
    related_apiversion    text,
    related_fieldpath    text,
    related_kind    text,
    related_name    text,
    related_namespace    text,
    related_resourceversion    text,
    related_uid    text,
    series_count    text,
    series_lastobservedtime    text,
    series_state    text,
    source_component    text,
    source_host    text,
    type    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, cluster_id, kind, metadata_uid, row_index)
);
----------------------------
CREATE TABLE cmoa_job_base    (
    kube_flatting_time    bigint,
    cluster_id    varchar(255),
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    kind_status    varchar(50),
    metadata_annotations    text,
    metadata_creationtimestamp    varchar(25),
    metadata_labels    text,
    metadata_name    text,
    metadata_namespace    text,
    metadata_ownerreferences    text,
    metadata_ownerReferences_kind    varchar(30),
    metadata_ownerReferences_uid    varchar(40),
    metadata_resourceversion    text,
    spec_backofflimit    text,
    spec_completions    text,
    spec_parallelism    text,
    status_active    text,
    status_completiontime    text,
    status_failed    text,
    status_starttime    text,
    status_succeeded    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, cluster_id, kind, metadata_uid, row_index)
);
CREATE INDEX cmoa_job_base_index_1
    on cmoa_job_base (metadata_uid);
CREATE INDEX cmoa_job_base_index_2
    on cmoa_job_base (kind);
----------------------------
CREATE TABLE cmoa_job_template    (
    kube_flatting_time    bigint,
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    metadata_name    text,
    spec_template_spec_containers_args    text,
    spec_template_spec_containers_command    text,
    spec_template_spec_containers_image    text,
    spec_template_spec_containers_name    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, kind, metadata_uid, row_index)
);
----------------------------
CREATE TABLE cmoa_namespace_base    (
    kube_flatting_time    bigint,
    cluster_id    varchar(255),
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    kind_status    varchar(50),
    metadata_name    text,
    metadata_resourceversion    text,
    metadata_annotations    text,
    metadata_creationtimestamp    varchar(25),
    metadata_labels    text,
    metadata_namespace    text,
    spec_finalizers    text,
    status_phase    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, cluster_id, kind, metadata_uid, row_index)
);
----------------------------
CREATE TABLE cmoa_node_base    (
    kube_flatting_time    bigint,
    cluster_id    varchar(255),
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    kind_status    varchar(50),
    metadata_name    text,
    metadata_selflink    text,
    metadata_resourceversion    text,
    metadata_creationtimestamp    varchar(25),
    metadata_labels    text,
    metadata_annotations    text,
    spec_podcidr    text,
    spec_taints    text,
    status_capacity_cpu    text,
    status_capacity_ephemeral_storage    text,
    status_capacity_hugepages_1gi    text,
    status_capacity_hugepages_2mi    text,
    status_capacity_memory    text,
    status_capacity_pods    text,
    status_allocatable_cpu    text,
    status_allocatable_ephemeral_storage    text,
    status_allocatable_hugepages_1gi    text,
    status_allocatable_hugepages_2mi    text,
    status_allocatable_memory    text,
    status_allocatable_pods    text,
    status_addresses    text,
    status_daemonendpoints_kubeletendpoint_port    text,
    status_nodeinfo_machineid    text,
    status_nodeinfo_systemuuid    text,
    status_nodeinfo_bootid    text,
    status_nodeinfo_kernelversion    text,
    status_nodeinfo_osimage    text,
    status_nodeinfo_containerruntimeversion    text,
    status_nodeinfo_kubeletversion    text,
    status_nodeinfo_kubeproxyversion    text,
    status_nodeinfo_operatingsystem    text,
    status_nodeinfo_architecture    text,
    status_volumesinuse    text,
    status_volumesattached    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, cluster_id, kind, metadata_uid, row_index)
);
----------------------------
CREATE TABLE cmoa_node_condition    (
    kube_flatting_time    bigint,
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    metadata_name    text,
    status_conditions_type    text,
    status_conditions_status    text,
    status_conditions_lastheartbeattime    text,
    status_conditions_lasttransitiontime    text,
    status_conditions_reason    text,
    status_conditions_message    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, kind, metadata_uid, row_index)
);
----------------------------
CREATE TABLE cmoa_node_image    (
    kube_flatting_time    bigint,
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    metadata_name    text,
    status_images_names    text,
    status_images_sizebytes    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, kind, metadata_uid, row_index)
);
----------------------------
CREATE TABLE cmoa_persistentvolume_base    (
    kube_flatting_time    bigint,
    cluster_id    varchar(255),
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    kind_status    varchar(50),
    metadata_annotations    text,
    metadata_creationtimestamp    varchar(25),
    metadata_labels    text,
    metadata_name    text,
    metadata_namespace    text,
    metadata_resourceversion    text,
    spec_accessmodes    text,
    spec_awselasticblockstore    text,
    spec_azuredisk    text,
    spec_azurefile    text,
    spec_capacity    text,
    spec_claimref_apiversion    text,
    spec_claimref_fieldpath    text,
    spec_claimref_kind    text,
    spec_claimref_name    text,
    spec_claimref_namespace    text,
    spec_claimref_resourceversion    text,
    spec_claimref_uid    text,
    spec_csi    text,
    spec_fc    text,
    spec_flexvolume    text,
    spec_flocker    text,
    spec_gcepersistentdisk    text,
    spec_glusterfs    text,
    spec_hostpath    text,
    spec_iscsi    text,
    spec_local    text,
    spec_nfs    text,
    spec_persistentvolumereclaimpolicy    text,
    spec_photonpersistentdisk    text,
    spec_portworxvolume    text,
    spec_quobyte    text,
    spec_rbd    text,
    spec_scaleio    text,
    spec_storageclassname    text,
    spec_storageos    text,
    spec_volumemode    text,
    spec_vspherevolume    text,
    status_message    text,
    status_phase    text,
    status_reason    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, cluster_id, kind, metadata_uid, row_index)
);
----------------------------
CREATE TABLE cmoa_persistentvolumeclaim_base    (
    kube_flatting_time    bigint,
    cluster_id    varchar(255),
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    kind_status    varchar(50),
    metadata_annotations    text,
    metadata_creationtimestamp    varchar(25),
    metadata_labels    text,
    metadata_name    text,
    metadata_namespace    text,
    metadata_resourceversion    text,
    spec_accessmodes    text,
    spec_storageclassname    text,
    spec_volumemode    text,
    spec_volumename    text,
    status_accessmodes    text,
    status_capacity    text,
    status_phase    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, cluster_id, kind, metadata_uid, row_index)
);
----------------------------
CREATE TABLE cmoa_pod_base    (
    kube_flatting_time    bigint,
    cluster_id    varchar(255),
    kind    varchar(30),
    kind_status    varchar(50),
    metadata_uid    varchar(40),
    row_index    int,
    metadata_name    text,
    metadata_selflink    text,
    metadata_resourceversion    text,
    metadata_creationtimestamp    varchar(25),
    metadata_generatename    text,
    metadata_namespace    text,
    metadata_deletiontimestamp    text,
    metadata_deletiongraceperiodseconds    text,
    metadata_labels    text,
    metadata_ownerreferences    text,
    metadata_ownerReferences_kind    varchar(30),
    metadata_ownerReferences_uid    varchar(40),
    metadata_annotations    text,
    spec_hostnetwork    text,
    spec_priorityclassname    text,
    spec_enableservicelinks    text,
    spec_priority    text,
    spec_schedulername    text,
    spec_hostpid    text,
    spec_nodename    text,
    spec_serviceaccount    text,
    spec_serviceaccountname    text,
    spec_dnspolicy    text,
    spec_terminationgraceperiodseconds    text,
    spec_restartpolicy    text,
    spec_securitycontext    text,
    spec_nodeselector_kubernetes_io_hostname    text,
    spec_tolerations    text,
    status_phase    text,
    status_hostip    text,
    status_podip    text,
    status_starttime    text,
    status_qosclass    text,
    status_reason    text,
    status_message    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, cluster_id, kind, metadata_uid, row_index)
);
CREATE INDEX cmoa_pod_base_index_1
    on cmoa_pod_base (metadata_ownerReferences_uid);
CREATE INDEX cmoa_pod_base_index_2
    on cmoa_pod_base (metadata_ownerreferences_kind);
----------------------------
CREATE TABLE cmoa_pod_conditions    (
    kube_flatting_time    bigint,
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    metadata_name    text,
    status_conditions_type    text,
    status_conditions_status    text,
    status_conditions_lasttransitiontime    text,
    status_conditions_reason    text,
    status_conditions_message    text,
    status_conditions_lastprobetime    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, kind, metadata_uid, row_index)
);
----------------------------
CREATE TABLE cmoa_pod_containerstatuses    (
    kube_flatting_time    bigint,
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    metadata_name    text,
    status_containerstatuses_name    text,
    status_containerstatuses_ready    text,
    status_containerstatuses_restartcount    text,
    status_containerstatuses_image    text,
    status_containerstatuses_imageid    text,
    status_containerstatuses_containerid    text,
    status_containerstatuses_state_terminated_exitcode    text,
    status_containerstatuses_state_terminated_reason    text,
    status_containerstatuses_state_terminated_startedat    text,
    status_containerstatuses_state_terminated_finishedat    text,
    status_containerstatuses_state_terminated_containerid    text,
    status_containerstatuses_state_waiting_reason    text,
    status_containerstatuses_state_waiting_message    text,
    status_containerstatuses_state_running_startedat    text,
    status_containerstatuses_laststate_terminated_exitcode    text,
    status_containerstatuses_laststate_terminated_reason    text,
    status_containerstatuses_laststate_terminated_startedat    text,
    status_containerstatuses_laststate_terminated_finishedat    text,
    status_containerstatuses_laststate_terminated_containerid    text,
    status_containerstatuses_laststate_waiting_reason    text,
    status_containerstatuses_laststate_waiting_message    text,
    status_containerstatuses_laststate_running_startedat    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, kind, metadata_uid, row_index)
);
----------------------------
CREATE TABLE cmoa_pod_containers    (
    kube_flatting_time    bigint,
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    metadata_name    text,
    spec_containers_name    text,
    spec_containers_image    text,
    spec_containers_env    text,
    spec_containers_resources_limits_cpu    text,
    spec_containers_resources_limits_memory    text,
    spec_containers_resources_requests_cpu    text,
    spec_containers_resources_requests_memory    text,
    spec_containers_volumemounts    text,
    spec_containers_securitycontext_privileged    text,
    spec_containers_command    text,
    spec_containers_ports    text,
    spec_containers_args    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, kind, metadata_uid, row_index)
);
----------------------------
CREATE TABLE cmoa_pod_volume    (
    kube_flatting_time    bigint,
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    metadata_name    text,
    spec_volumes_name    text,
    spec_volumes_hostpath    text,
    spec_volumes_secret    text,
    spec_volumes_configmap    text,
    spec_volumes_persistentvolumeclaim text NULL DEFAULT NULL,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, kind, metadata_uid, row_index)
);
----------------------------
CREATE TABLE cmoa_replicaset_base    (
    kube_flatting_time    bigint,
    cluster_id    varchar(255),
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    kind_status    varchar(50),
    metadata_annotations    text,
    metadata_creationtimestamp    varchar(25),
    metadata_labels    text,
    metadata_name    text,
    metadata_namespace    text,
    metadata_resourceversion    text,
    spec_replicas    text,
    status_availablereplicas    text,
    status_readyreplicas    text,
    status_replicas    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, cluster_id, kind, metadata_uid, row_index)
);
----------------------------
CREATE TABLE cmoa_replicationcontroller_base    (
    kube_flatting_time    bigint,
    cluster_id    varchar(255),
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    kind_status    varchar(50),
    metadata_annotations    text,
    metadata_creationtimestamp    varchar(25),
    metadata_labels    text,
    metadata_name    text,
    metadata_namespace    text,
    metadata_resourceversion    text,
    spec_replicas    text,
    status_availablereplicas    text,
    status_readyreplicas    text,
    status_replicas    text,
    status_observedgeneration    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, cluster_id, kind, metadata_uid, row_index)
);
----------------------------
CREATE TABLE cmoa_resourcequota_base    (
    kube_flatting_time    bigint,
    cluster_id    varchar(255),
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    kind_status    varchar(50),
    metadata_annotations    text,
    metadata_creationtimestamp    varchar(25),
    metadata_labels    text,
    metadata_name    text,
    metadata_namespace    text,
    metadata_resourceversion    text,
    spec_hard    text,
    spec_scopes    text,
    status_hard    text,
    status_used    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, cluster_id, kind, metadata_uid, row_index)
);
----------------------------
CREATE TABLE cmoa_resourcequota_scopeselector    (
    kube_flatting_time    bigint,
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    metadata_name    text,
    spec_scopeselector_matchexpressions_operator    text,
    spec_scopeselector_matchexpressions_scopename    text,
    spec_scopeselector_matchexpressions_values    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, kind, metadata_uid, row_index)
);
----------------------------
CREATE TABLE cmoa_service_base    (
    kube_flatting_time    bigint,
    cluster_id    varchar(255),
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    kind_status    varchar(50),
    metadata_name    text,
    metadata_resourceversion    text,
    metadata_ownerreferences    text,
    metadata_ownerReferences_kind    varchar(30),
    metadata_ownerReferences_uid    varchar(40),
    metadata_annotations    text,
    metadata_creationtimestamp    varchar(25),
    metadata_deletiongraceperiodseconds    text,
    metadata_deletiontimestamp    text,
    metadata_labels    text,
    metadata_namespace    text,
    spec_clusterip    text,
    spec_externalips    text,
    spec_selector    text,
    spec_type    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, cluster_id, kind, metadata_uid, row_index)
);
----------------------------
CREATE TABLE cmoa_service_ports    (
    kube_flatting_time    bigint,
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    metadata_name    text,
    spec_ports_appprotocol    text,
    spec_ports_name    text,
    spec_ports_nodeport    text,
    spec_ports_port    text,
    spec_ports_protocol    text,
    spec_ports_targetport    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, kind, metadata_uid, row_index)
);
----------------------------
CREATE TABLE cmoa_statefulset_base    (
    kube_flatting_time    bigint,
    cluster_id    varchar(255),
    kind    varchar(30),
    metadata_uid    varchar(40),
    row_index    int,
    kind_status    varchar(50),
    metadata_annotations    text,
    metadata_creationtimestamp    varchar(25),
    metadata_labels    text,
    metadata_name    text,
    metadata_namespace    text,
    metadata_resourceversion    text,
    spec_replicas    text,
    status_readyreplicas    text,
    status_replicas    text,
    create_time    timestamp    default now(),
    PRIMARY KEY (kube_flatting_time, cluster_id, kind, metadata_uid, row_index)
);
----------------------------
CREATE TABLE cmoa_secret_base    (
    kube_flatting_time         bigint       not null,
    cluster_id                 varchar(255) not null,
    kind                       varchar(30)  not null,
    metadata_uid               varchar(40)  not null,
    row_index                  integer      not null,
    metadata_name              text,
    kind_status                varchar(50),
    metadata_resourceversion   text,
    metadata_annotations       text,
    metadata_creationtimestamp varchar(25),
    metadata_labels            text,
    metadata_namespace         text,
    data                       text,
    type                       text,
    create_time                timestamp default now(),
    PRIMARY KEY (kube_flatting_time, cluster_id, kind, metadata_uid, row_index)
);
----------------------------
CREATE VIEW v_cmoa_job AS
    select Q.kube_flatting_time, Q.cluster_id, Q.metadata_ownerreferences_kind, Q.metadata_uid, K.status_phase
        from (select A.kube_flatting_time as job_flatting_time, A.metadata_uid as job_metadata_uid, B.kube_flatting_time as pod_flatting_time
                from (select metadata_uid, max(kube_flatting_time) as kube_flatting_time
                        from cmoa_job_base
                        where kind='Job' and kube_flatting_time > extract(epoch from (CURRENT_TIMESTAMP - INTERVAL '1 hours'))*1000
                        group by metadata_uid) A,
                     (select metadata_ownerreferences_uid, max(kube_flatting_time) as kube_flatting_time
                        from cmoa_pod_base
                        where metadata_ownerreferences_kind='Job' and kube_flatting_time > extract(epoch from (CURRENT_TIMESTAMP - INTERVAL '1 hours'))*1000
                        group by metadata_ownerreferences_uid) B
        where A.metadata_uid = B.metadata_ownerreferences_uid) J
    inner join cmoa_job_base Q on Q.metadata_uid = J.job_metadata_uid and Q.kube_flatting_time = J.job_flatting_time
    inner join cmoa_pod_base K on K.metadata_ownerreferences_uid = J.job_metadata_uid and K.kube_flatting_time = J.pod_flatting_time
    group by Q.kube_flatting_time, Q.cluster_id, Q.metadata_ownerreferences_kind, Q.metadata_uid, K.status_phase;

CREATE TABLE public.api_error_history (
	id int8 NOT NULL,
	api_msg varchar(255) NULL,
	code varchar(255) NULL,
	"exception" varchar(255) NULL,
	http_error varchar(255) NULL,
	http_status int4 NULL,
	occureence_time varchar(255) NULL,
	params varchar(255) NULL,
	"path" varchar(255) NULL,
	"type" varchar(255) NULL,
	CONSTRAINT api_error_history_pkey PRIMARY KEY (id)
);

CREATE TABLE public.metric_score (
	clst_id varchar(255) NOT NULL,
	entity_id varchar(255) NOT NULL,
	entity_type varchar(255) NOT NULL,
	metric_id varchar(255) NOT NULL,
	sub_key varchar(255) NOT NULL,
	unixtime int4 NOT NULL,
	anomaly bool NOT NULL,
	cont_name varchar(255) NULL,
	"instance" varchar(255) NULL,
	"namespace" varchar(255) NULL,
	node_id varchar(255) NULL,
	pod_id varchar(255) NULL,
	score int4 NOT NULL,
	yhat_lower_upper json NULL,
	CONSTRAINT metric_score_pkey PRIMARY KEY (clst_id, entity_id, entity_type, metric_id, sub_key, unixtime)
);


CREATE TABLE public.tenant_info_auth_resources (
	tenant_info_id varchar(255) NOT NULL,
	auth_resources_id int8 NOT NULL,
	CONSTRAINT tenant_info_auth_resources_pkey PRIMARY KEY (tenant_info_id, auth_resources_id),
	CONSTRAINT uk_7s6l8e2c8gli4js43c4xoifcl UNIQUE (auth_resources_id)
);


-- public.tenant_info_auth_resources foreign keys

ALTER TABLE public.tenant_info_auth_resources ADD CONSTRAINT fkkecsc13ydhwg8u05aumkqbnx1 FOREIGN KEY (tenant_info_id) REFERENCES public.tenant_info(id);
ALTER TABLE public.tenant_info_auth_resources ADD CONSTRAINT fkpvvec4ju3hsma6s1rtgvr4mf6 FOREIGN KEY (auth_resources_id) REFERENCES public.auth_resource3(id);

create table public.audit_log
(
    id           bigserial
        constraint audit_log_pk
            primary key,
    created_date timestamp not null,
    user_id      varchar   not null,
    type         varchar   not null,
    menu_path    varchar   not null,
    result       boolean   not null,
    target       jsonb
);

