<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Analytics>
    <Name>APIM_INCREMENTAL_PROCESSING_SCRIPT</Name>
<Script>
    <!--Requests-->

    CREATE TEMPORARY TABLE APIMGT_PERMINUTE_REQUEST_DATA USING CarbonAnalytics OPTIONS(tableName "ORG_WSO2_APIMGT_STATISTICS_PERMINUTEREQUEST", schema "
    year INT, month INT, day INT, hour INT, minute INT,
    consumerKey STRING, context STRING, api_version STRING, api STRING, version STRING, requestTime LONG, userId STRING, hostName STRING,
    apiPublisher STRING, total_request_count LONG, resourceTemplate STRING, method STRING, applicationName STRING, tenantDomain STRING,
    userAgent STRING, resourcePath STRING, request INT, applicationId STRING, tier STRING, throttledOut BOOLEAN, clientIp STRING,
    applicationOwner STRING, _timestamp LONG",
    primaryKeys "year, month, day, hour, minute, consumerKey, context, api_version, userId, hostName, apiPublisher, resourceTemplate, method, userAgent, clientIp",
    incrementalParams "APIMGT_PERMINUTE_REQUEST_DATA, HOUR",
    mergeSchema "false");

    CREATE TEMPORARY TABLE APIMGT_PERHOUR_REQUEST_DATA USING CarbonAnalytics OPTIONS(tableName "ORG_WSO2_APIMGT_STATISTICS_PERHOURREQUEST", schema "
    year INT, month INT, day INT, hour INT,
    consumerKey STRING, context STRING, api_version STRING, api STRING, version STRING, maxRequestTime LONG, firstRequestTime LONG, userId STRING, hostName STRING,
    apiPublisher STRING, total_request_count_per_hour LONG, resourceTemplate STRING, method STRING, applicationName STRING, tenantDomain STRING,
    userAgent STRING, resourcePath STRING, request INT, applicationId STRING, tier STRING, throttledOut BOOLEAN, clientIp STRING,
    applicationOwner STRING, _timestamp LONG",
    primaryKeys "year, month, day, hour, consumerKey, context, api_version, userId, hostName, apiPublisher, resourceTemplate, method, userAgent, clientIp",
    incrementalParams "APIMGT_PERHOUR_REQUEST_DATA, DAY",
    mergeSchema "false");

    CREATE TEMPORARY TABLE APIMGT_PERDAY_REQUEST_DATA USING CarbonAnalytics OPTIONS(tableName "ORG_WSO2_APIMGT_STATISTICS_PERDAYREQUEST", schema "
    year INT, month INT, day INT,
    consumerKey STRING, context STRING, api_version STRING, api STRING, version STRING, maxRequestTime LONG, firstRequestTime LONG, userId STRING, hostName STRING,
    apiPublisher STRING, total_request_count_per_day LONG, resourceTemplate STRING, method STRING, applicationName STRING, tenantDomain STRING,
    userAgent STRING, resourcePath STRING, request INT, applicationId STRING, tier STRING, throttledOut BOOLEAN, clientIp STRING,
    applicationOwner STRING",
    primaryKeys "year, month, day, consumerKey, context, api_version, userId, hostName, apiPublisher, resourceTemplate, method, userAgent, clientIp",
    mergeSchema "false");

    INSERT INTO TABLE APIMGT_PERHOUR_REQUEST_DATA SELECT year, month, day, hour, consumerKey, context, api_version, first(api), first(version), max(requestTime), first(requestTime), userId, hostName,
    apiPublisher, sum(total_request_count), resourceTemplate, method, applicationName, tenantDomain,
    userAgent, first(resourcePath), request, applicationId, tier, first(throttledOut), clientIp, applicationOwner, getHourStartingTime(year, month, day, hour) as _timestamp
    FROM APIMGT_PERMINUTE_REQUEST_DATA
    GROUP BY year, month, day, hour, consumerKey, context, api_version, userId, hostName, apiPublisher, resourceTemplate, method, userAgent, clientIp,
    applicationName, tenantDomain, request, applicationId, tier, applicationOwner;

    INCREMENTAL_TABLE_COMMIT APIMGT_PERMINUTE_REQUEST_DATA;

    INSERT INTO TABLE APIMGT_PERDAY_REQUEST_DATA SELECT year, month, day, consumerKey, context, api_version, first(api), first(version), max(maxRequestTime), first(firstRequestTime), userId, hostName,
    apiPublisher, sum(total_request_count_per_hour), resourceTemplate, method, applicationName, tenantDomain,
    userAgent, first(resourcePath), request, applicationId, tier, first(throttledOut), clientIp, applicationOwner
    FROM APIMGT_PERHOUR_REQUEST_DATA
    GROUP BY year, month, day, consumerKey, context, api_version, userId, hostName, apiPublisher, resourceTemplate, method, userAgent, clientIp,
    applicationName, tenantDomain, request, applicationId, tier, applicationOwner;

    INCREMENTAL_TABLE_COMMIT APIMGT_PERHOUR_REQUEST_DATA;

    <!--Responses-->

    CREATE TEMPORARY TABLE APIMGT_PERMINUTE_RESPONSE_DATA USING CarbonAnalytics OPTIONS(tableName "ORG_WSO2_APIMGT_STATISTICS_PERMINUTERESPONSE", schema "
    year INT, month INT, day INT, hour INT, minute INT,
    context STRING, api_version STRING, api STRING, resourceTemplate STRING, version STRING, tenantDomain STRING, hostName STRING, apiPublisher STRING,
    destination STRING, consumerKey STRING, resourcePath STRING, method STRING, response INT, responseTime LONG, serviceTime LONG, backendTime LONG,
    username STRING, eventTime LONG, applicationName STRING, applicationId STRING, cacheHit BOOLEAN, responseSize LONG, protocol STRING,
    responseCode INT, total_response_count LONG, _timestamp LONG",
    primaryKeys "year, month, day, hour, minute, context, api_version, resourceTemplate, hostName, apiPublisher, destination, consumerKey, method, username",
    incrementalParams "APIMGT_PERMINUTE_RESPONSE_DATA, HOUR",
    mergeSchema "false");

    CREATE TEMPORARY TABLE APIMGT_PERHOUR_RESPONSE_DATA USING CarbonAnalytics OPTIONS(tableName "ORG_WSO2_APIMGT_STATISTICS_PERHOURRESPONSE", schema "
    year INT, month INT, day INT, hour INT,
    context STRING, api_version STRING, api STRING, resourceTemplate STRING, version STRING, tenantDomain STRING, hostName STRING, apiPublisher STRING,
    destination STRING, consumerKey STRING, resourcePath STRING, method STRING, response INT, serviceTime LONG, username STRING, maxEventTime LONG, firstEventTime LONG,
    applicationName STRING, applicationId STRING, cacheHit BOOLEAN, responseSize LONG, protocol STRING, responseCode INT,
    total_response_count_per_hour LONG, _timestamp LONG",
    primaryKeys "year, month, day, hour, context, api_version, resourceTemplate, hostName, apiPublisher, destination, consumerKey, method, username",
    incrementalParams "APIMGT_PERHOUR_RESPONSE_DATA, DAY",
    mergeSchema "false");

    CREATE TEMPORARY TABLE APIMGT_PERDAY_RESPONSE_DATA USING CarbonAnalytics OPTIONS(tableName "ORG_WSO2_APIMGT_STATISTICS_PERDAYRESPONSE", schema "
    year INT, month INT, day INT,
    context STRING, api_version STRING, api STRING, resourceTemplate STRING, version STRING, tenantDomain STRING, hostName STRING, apiPublisher STRING,
    destination STRING, consumerKey STRING, resourcePath STRING, method STRING, response INT, serviceTime LONG, username STRING, maxEventTime LONG, firstEventTime LONG,
    applicationName STRING, applicationId STRING, cacheHit BOOLEAN, responseSize LONG, protocol STRING, responseCode INT,
    total_response_count_per_day LONG",
    primaryKeys "year, month, day, context, api_version, resourceTemplate, hostName, apiPublisher, destination, consumerKey, method, username",
    mergeSchema "false");

    INSERT INTO TABLE APIMGT_PERHOUR_RESPONSE_DATA SELECT year, month, day, hour, context, api_version, first(api), resourceTemplate, first(version), tenantDomain, hostName,
    apiPublisher, destination, consumerKey, first(resourcePath), method, response, sum(serviceTime), username, max(eventTime), first(eventTime), applicationName,
    applicationId, cacheHit, responseSize, protocol, responseCode, sum(total_response_count), getHourStartingTime(year, month, day, hour) as _timestamp
    FROM APIMGT_PERMINUTE_RESPONSE_DATA
    GROUP BY year, month, day, hour, context, api_version, resourceTemplate, hostName, apiPublisher, destination, consumerKey, method, username,
    tenantDomain, response, applicationName, applicationId, cacheHit, responseSize, protocol, responseCode;

    INCREMENTAL_TABLE_COMMIT APIMGT_PERMINUTE_RESPONSE_DATA;

    INSERT INTO TABLE APIMGT_PERDAY_RESPONSE_DATA SELECT year, month, day, context, api_version, first(api), resourceTemplate, first(version), tenantDomain, hostName,
    apiPublisher, destination, consumerKey, first(resourcePath), method, response, sum(serviceTime), username, max(maxEventTime), first(firstEventTime), applicationName,
    applicationId, cacheHit, responseSize, protocol, responseCode, sum(total_response_count_per_hour)
    FROM APIMGT_PERHOUR_RESPONSE_DATA
    GROUP BY year, month, day, context, api_version, resourceTemplate, hostName, apiPublisher, destination, consumerKey, method, username,
    tenantDomain, response, applicationName, applicationId, cacheHit, responseSize, protocol, responseCode;

    INCREMENTAL_TABLE_COMMIT APIMGT_PERHOUR_RESPONSE_DATA;

    <!-- execution time-->
    CREATE TEMPORARY TABLE APIMGT_PERMINUTE_EXECUTION_TIME USING CarbonAnalytics OPTIONS(tableName "ORG_WSO2_APIMGT_STATISTICS_PERMINUTEEXECUTIONTIMES", schema "
    year INT, month INT, day INT, hour INT, minute INT, context STRING, api_version STRING, api STRING, tenantDomain STRING,
    apiPublisher STRING, apiResponseTime DOUBLE, securityLatency DOUBLE, throttlingLatency DOUBLE, requestMediationLatency DOUBLE, responseMediationLatency DOUBLE,
    backendLatency DOUBLE, otherLatency DOUBLE, eventTime LONG, _timestamp LONG",
    primaryKeys "year, month, day, hour, minute, context, api_version, tenantDomain, apiPublisher",
    incrementalParams "APIMGT_PERMINUTE_EXECUTION_TIME, HOUR",
    mergeSchema "false");

    CREATE TEMPORARY TABLE APIMGT_PERHOUR_EXECUTION_TIME USING CarbonAnalytics OPTIONS(tableName "ORG_WSO2_APIMGT_STATISTICS_PERHOUREXECUTIONTIMES", schema "
    year INT, month INT, day INT, hour INT, context STRING, api_version STRING, api STRING, tenantDomain STRING,
    apiPublisher STRING, apiResponseTime DOUBLE, securityLatency DOUBLE, throttlingLatency DOUBLE, requestMediationLatency DOUBLE, responseMediationLatency DOUBLE,
    backendLatency DOUBLE, otherLatency DOUBLE, firstEventTime LONG, _timestamp LONG",
    primaryKeys "year, month, day, hour, context, api_version, tenantDomain, apiPublisher",
    incrementalParams "APIMGT_PERHOUR_EXECUTION_TIME, DAY",
    mergeSchema "false");

    CREATE TEMPORARY TABLE APIMGT_PERDAY_EXECUTION_TIME USING CarbonAnalytics OPTIONS(tableName "ORG_WSO2_APIMGT_STATISTICS_PERDAYEXECUTIONTIMES", schema "
    year INT, month INT, day INT, context STRING, api_version STRING, api STRING, tenantDomain STRING,
    apiPublisher STRING, apiResponseTime DOUBLE, securityLatency DOUBLE, throttlingLatency DOUBLE, requestMediationLatency DOUBLE, responseMediationLatency DOUBLE,
    backendLatency DOUBLE, otherLatency DOUBLE, firstEventTime LONG",
    primaryKeys "year, month, day, context, api_version, tenantDomain, apiPublisher",
    mergeSchema "false");

    INSERT INTO TABLE APIMGT_PERHOUR_EXECUTION_TIME SELECT year, month, day, hour, context, api_version, api, tenantDomain,
    apiPublisher, avg(apiResponseTime), avg(securityLatency), avg(throttlingLatency), avg(requestMediationLatency), avg(responseMediationLatency),
    avg(backendLatency), avg(otherLatency), first(eventTime), getHourStartingTime(year, month, day, hour) as _timestamp
    FROM APIMGT_PERMINUTE_EXECUTION_TIME
    GROUP BY year, month, day, hour, context, api_version, tenantDomain, apiPublisher,
    api;

    INCREMENTAL_TABLE_COMMIT APIMGT_PERMINUTE_EXECUTION_TIME;

    INSERT INTO TABLE APIMGT_PERDAY_EXECUTION_TIME SELECT year, month, day, context, api_version, api, tenantDomain,
    apiPublisher, avg(apiResponseTime), avg(securityLatency), avg(throttlingLatency), avg(requestMediationLatency), avg(responseMediationLatency),
    avg(backendLatency), avg(otherLatency), first(firstEventTime)
    FROM APIMGT_PERHOUR_EXECUTION_TIME
    GROUP BY year, month, day, context, api_version, tenantDomain, apiPublisher,
    api;

    INCREMENTAL_TABLE_COMMIT APIMGT_PERHOUR_EXECUTION_TIME;
</Script>
    <CronExpression>0 0/2 * 1/1 * ? *</CronExpression>
</Analytics>
