if (select top 1 0 from sys.databases where name = 'NucelarMonitor') is null create database NucelarMonitor collate Latin1_General_100_CI_AS_SC_UTF8
go
use NucelarMonitor

if object_id(N'dbo.set_exception', N'P') is not null drop procedure dbo.set_exception
go
create procedure dbo.set_exception 
    @database varchar(64), 
    @procedure varchar(64),
    @message varchar(128), 
    @parameters varchar(max) = null 
as begin 

    declare @procedure_id integer
    declare @message_id integer
    declare @exception_message varchar(max) = error_message()
    declare @exception_message_id integer
    declare @parameters_id integer

    set nocount on

    execute dbo.get_procedure @database, @procedure, @procedure_id output
    execute dbo.get_big_data @exception_message, @exception_message_id output
    execute dbo.get_big_data @parameters, @parameters_id output
    execute dbo.get_message @message, @message_id output

    insert into dbo.Exceptions([procedure], [message], [parameters], exception, [status], code) values
        (@procedure_id, @message_id, @parameters_id, @exception_message_id, error_severity(), error_number())

end
go

if object_id(N'dbo.set_machine_data', N'P') is not null drop procedure dbo.set_machine_data
go
create procedure dbo.set_machine_data 
    @key varchar(32),
    @candle_start datetime, 
    @candle_end datetime,
    @candle_interations integer, 
    @ram_total bigint,
    @ram_in bigint,
    @ram_out bigint,
    @ram_minimum bigint,
    @ram_maximum bigint,
    @ram_average bigint,
    @cpu_in float,
    @cpu_out float,
    @cpu_minimum float,
    @cpu_maximum float,
    @cpu_average float
as begin set nocount on

    declare @machine_id integer
    declare @candle_time_id integer
    declare @machine_ram_id integer
    declare @machine_cpu_id integer

    execute dbo.get_basic_data_ids 
        @key, 
        @candle_start, 
        @candle_end, 
        @candle_interations, 
        @machine_id output, 
        @candle_time_id output

    insert into dbo.MachineRAM(machine, candle_time, total, [in], [out], minimum, maximum, average) values
        (@machine_id, @candle_time_id, @ram_total, @ram_in, @ram_out, @ram_minimum, @ram_maximum, @ram_average)
    set @machine_ram_id = scope_identity()

    insert into dbo.MachineCPU(machine, candle_time, [in], [out], minimum, maximum, average) values
        (@machine_id, @candle_time_id, @cpu_in, @cpu_out, @cpu_minimum, @cpu_maximum, @cpu_average)
    set @machine_cpu_id = scope_identity()

    if (select top 1 id from dbo.MachinePlain where 
        date_out is null and 
        machine = @machine_id and 
        candle_time = @candle_time_id 
    ) is null 
        insert into dbo.MachinePlain([key], machine, candle_time, machine_ram, machine_cpu, ram_total, ram_in, ram_out, ram_minimum, ram_maximum, ram_average, cpu_in, cpu_out, cpu_minimum, cpu_maximum, cpu_average) values
            (@key, @machine_id, @candle_time_id, @machine_ram_id, @machine_cpu_id, @ram_total, @ram_in, @ram_out, @ram_minimum, @ram_maximum, @ram_average, @cpu_in, @cpu_out, @cpu_minimum, @cpu_maximum, @cpu_average)
    else 
        update dbo.MachinePlain set 
            machine_ram = @machine_ram_id, 
            machine_cpu = @machine_cpu_id, 
            ram_total = @ram_total,
            ram_in = @ram_in,
            ram_out = @ram_out,
            ram_minimum = @ram_minimum,
            ram_maximum = @ram_maximum,
            ram_average = @ram_average,
            cpu_in = @cpu_in,
            cpu_out = @cpu_out,
            cpu_minimum = @cpu_minimum,
            cpu_maximum = @cpu_maximum,
            cpu_average = @cpu_average
        where 
            date_out is null and 
            [key] = @key and 
            candle_time = @candle_time_id

end
go

if object_id(N'dbo.set_machine_interface_data', N'P') is not null drop procedure dbo.set_machine_interface_data
go
create procedure dbo.set_machine_interface_data 
    @key varchar(32),
    @name varchar(32),
    @bytes bigint,
    @packages integer,
    @errors integer,
    @is_ipv6 bit,
    @address varchar(45),
    @mask tinyint
as begin set nocount on 

    declare @machine_id integer
    declare @interface_id integer
    declare @machine_interface_id integer
    declare @machine_interface_data_id integer

    execute dbo.get_interface @name, @interface_id output
    execute dbo.get_machine @key, @machine_id output

    set @machine_interface_id = (
        select top 1 id from dbo.MachineInterfaces where 
            date_out is null and 
            machine = @machine_id and 
            [interface] = @interface_id
    )

    if @machine_interface_id is null begin
        insert into dbo.MachineInterfaces(machine, [interface]) values (@machine_id, @interface_id)
        set @machine_interface_id = scope_identity()
    end

    set @machine_interface_data_id = (
        select top 1 id from dbo.MachineInterfacesData where 
            date_out is null and 
            machine_interface = @machine_interface_id and 
            is_ipv6 = @is_ipv6 and 
            [address] = @address and 
            mask = @mask
    )

    if @machine_interface_data_id is null begin 

        insert into dbo.MachineInterfacesData(machine_interface, is_ipv6, [address], mask) values
            (@machine_interface_id, @is_ipv6, @address, @mask)
        set @machine_interface_data_id = scope_identity()

        if (select top 1 0 from dbo.MachineInterfacesPlain where 
            date_out is null and 
            machine = @machine_id and 
            [interface] = @interface_id 
        ) is null 
            insert into dbo.MachineInterfacesPlain(plain, machine, [interface], machine_interface, [name], is_ipv6, [address], mask) values
                (null, @machine_id, @interface_id, @machine_interface_id, @name, @is_ipv6, @address, @mask)
        else 
            update dbo.MachineInterfacesPlain set 
                machine_interface = @machine_interface_id,
                is_ipv6 = @is_ipv6,
                [address] = @address,
                mask = @mask
            where 
                date_out is null and 
                machine = @machine_id and 
                [interface] = @interface_id 

    end

end
go

if object_id(N'dbo.set_machine_interface_traffic', N'P') is not null drop procedure dbo.set_machine_interface_traffic
go
create procedure dbo.set_machine_interface_traffic 
    @key varchar(32),
    @name varchar(32),
    @type varchar(32), 
    @candle_start datetime, 
    @candle_end datetime,
    @candle_interations integer,
    @bytes bigint,
    @packages integer,
    @errors integer,
as begin set nocount on 

    declare @machine_id integer
    declare @type_id integer
    declare @candle_time_id integer
    declare @interface_id integer
    declare @machine_interface_id integer
    declare @plain_id integer

    execute dbo.get_basic_data_ids 
        @key, 
        @candle_start, 
        @candle_end, 
        @candle_interations, 
        @machine_id output, 
        @candle_time_id output
    execute dbo.get_type @type, @type_id output
    execute dbo.get_interface @name, @interface_id output

    set @machine_interface_id = (
        select top 1 id from dbo.MachineInterfaces where 
            date_out is null and 
            machine = @machine_id and 
            [interface] = @interface_id
    )

    insert into dbo.MachineInterfacesTraffic(machine_interface, [type], candle_time, bytes, packages, errors) values
        (@machine_interface_id, @type_id, @candle_time_id, @bytes, @packages, @errors)

    set @plain_id = (select top 1 id from dbo.MachineInterfacesPlain where 
        date_out is null and 
        machine = @machine_id and 
        [interface] = @interface_id
    )

    if (select top 1 0 from dbo.MachineInterfacesTrafficPlain where 
        date_out is null and 
        machine_interface = @machine_interface_id and 
        [type] = @type_id
    ) is null 
        insert into dbo.MachineInterfacesTrafficPlain(plain, machine_interface, candle_time, [type], bytes, packages, errors) values
            (@plain_id, @machine_interface_id, @candle_time_id, @type_id, @bytes, @packages, @errors)
    else 
        update dbo.MachineInterfacesTrafficPlain set 
            bytes = @bytes, 
            candle_time = @candle_time_id
        where 
            date_out is null and 
            machine_interface = @machine_interface_id and 
            [type] = @type_id

end
go