/*
    Copyright (c) 2018, WSO2 Inc. (http://wso2.com) All Rights Reserved.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

@App:name("APIM_ALERT_CONFIGURATION")
@App:description("Store the alert configurations of each API into two different tables from the two streams published corresponding to alert configurations done by the API creator and the subscriber.")

-- Stream and table definition for the alerts configured by the API creator
@source(type = 'wso2event', wso2.stream.id = 'org.wso2.apimgt.creator.alert.configuration:1.0.0', @map(type = 'wso2event'))
define stream ApiCreatorAlertConfigurationStream (
    apiName string,
    apiVersion string,
    apiCreator string,
    apiCreatorTenantDomain string,
    thresholdResponseTime long,
    thresholdBackendTime long);

@PrimaryKey('apiName', 'apiVersion','apiCreator', 'apiCreatorTenantDomain')
@store(type = 'rdbms', datasource = 'APIM_ANALYTICS_DB')
define table ApiCreatorAlertConfiguration (apiName string, apiVersion string, apiCreator string, apiCreatorTenantDomain string, thresholdResponseTime long, thresholdBackendTime long);

-- Stream and table definition for the alerts configured by the API subscriber
@source(type = 'wso2event', wso2.stream.id = 'org.wso2.apimgt.subscriber.alert.configuration:1.0.0', @map(type = 'wso2event'))
define stream ApiSubscriberAlertConfigurationStream (
    applicationId string,
    subscriber string,
    apiName string,
    apiVersion string,
    thresholdRequestCountPerMin int);

@PrimaryKey('applicationId','apiName', 'apiVersion')
@store(type = 'rdbms', datasource = 'APIM_ANALYTICS_DB')
define table ApiSubAlertConf (applicationId string, subscriber string, apiName string, apiVersion string, thresholdRequestCountPerMin int);

-- Data storing logic
from ApiCreatorAlertConfigurationStream
select *
update or insert into ApiCreatorAlertConfiguration
set ApiCreatorAlertConfiguration.thresholdResponseTime = thresholdResponseTime, ApiCreatorAlertConfiguration.thresholdBackendTime = thresholdBackendTime
on ApiCreatorAlertConfiguration.apiName == apiName and ApiCreatorAlertConfiguration.apiVersion == apiVersion and ApiCreatorAlertConfiguration.apiCreator == apiCreator and ApiCreatorAlertConfiguration.apiCreatorTenantDomain == apiCreatorTenantDomain;

from ApiSubscriberAlertConfigurationStream
select *
update or insert into ApiSubAlertConf
set ApiSubAlertConf.thresholdRequestCountPerMin = thresholdRequestCountPerMin
on ApiSubAlertConf.applicationId == applicationId and ApiSubAlertConf.apiName == apiName and ApiSubAlertConf.apiVersion == apiVersion;