<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Analytics>
    <Name>APIM_LATENCY_BREAKDOWN_STATS</Name>
<Script>
        
    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",
    incrementalParams "LATENCY_APIMGT_PERDAY_EXECUTION_TIME, DAY",
    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 "LATENCY_APIMGT_PERHOUR_EXECUTION_TIME, HOUR",
    mergeSchema "false");
    
    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 "LATENCY_APIMGT_PERMINUTE_EXECUTION_TIME, MINUTE",
    mergeSchema "false");

    CREATE TEMPORARY TABLE APIExecutionTimeDaySummaryData USING CarbonJDBC OPTIONS (dataSource "WSO2AM_STATS_DB", tableName "API_EXE_TME_DAY_SUMMARY",
    schema "api STRING ,
    version STRING ,
    tenantDomain STRING ,
    apiPublisher STRING ,
    apiResponseTime LONG ,
    context STRING ,
    securityLatency LONG,
    throttlingLatency LONG,
    requestMediationLatency LONG,
    responseMediationLatency LONG,
    backendLatency LONG,
    otherLatency LONG,
    year INTEGER ,
    month INTEGER ,
    day INTEGER ,
    time LONG ",
    primaryKeys "api,version,apiPublisher,context,year,month,day,tenantDomain"
    );

    INSERT INTO TABLE APIExecutionTimeDaySummaryData select api, api_version, tenantDomain, apiPublisher,
    avg(apiResponseTime), context, avg(securityLatency), avg(throttlingLatency), avg(requestMediationLatency),
    avg(responseMediationLatency), avg(backendLatency), avg(otherLatency),
    year, month, day,
    first(firstEventTime)
    from APIMGT_PERDAY_EXECUTION_TIME where api is not NULL group by api,api_version,tenantDomain,apiPublisher,context,
    year, month, day;

    INCREMENTAL_TABLE_COMMIT LATENCY_APIMGT_PERDAY_EXECUTION_TIME;

    CREATE TEMPORARY TABLE APIExecutionTimeHourSummaryData USING CarbonJDBC OPTIONS (dataSource "WSO2AM_STATS_DB",
                                                                                tableName "API_EXE_TIME_HOUR_SUMMARY",
    schema "api STRING ,
    version STRING ,
    tenantDomain STRING ,
    apiPublisher STRING ,
    apiResponseTime LONG,
    context STRING ,
    securityLatency LONG,
    throttlingLatency LONG,
    requestMediationLatency LONG,
    responseMediationLatency LONG,
    backendLatency LONG,
    otherLatency LONG,
    year INTEGER ,
    month INTEGER ,
    day INTEGER ,
    hour INTEGER ,
    time LONG ",
    primaryKeys "api,version,tenantDomain,apiPublisher,context,year,month,day,hour"
    );

    INSERT INTO TABLE APIExecutionTimeHourSummaryData select api, api_version,tenantDomain,apiPublisher,
    avg(apiResponseTime),context,avg(securityLatency),avg(throttlingLatency),avg(requestMediationLatency),
    avg(responseMediationLatency),avg(backendLatency),avg(otherLatency),
    year, month, day, hour,
    first(firstEventTime)
    from APIMGT_PERHOUR_EXECUTION_TIME where api is not NULL group by api,api_version,tenantDomain,apiPublisher,context,
    year, month, day, hour;

    INCREMENTAL_TABLE_COMMIT LATENCY_APIMGT_PERHOUR_EXECUTION_TIME;

    CREATE TEMPORARY TABLE APIExecutionTimeMinuteSummaryData USING CarbonJDBC OPTIONS (dataSource "WSO2AM_STATS_DB",
                                                                                tableName "API_EXE_TIME_MIN_SUMMARY",
    schema "api STRING ,
    version STRING ,
    tenantDomain STRING ,
    apiPublisher STRING ,
    apiResponseTime LONG ,
    context STRING ,
    securityLatency LONG,
    throttlingLatency LONG,
    requestMediationLatency LONG,
    responseMediationLatency LONG,
    backendLatency LONG,
    otherLatency LONG,
    year INTEGER ,
    month INTEGER ,
    day INTEGER ,
    hour INTEGER ,
    minutes INTEGER ,
    time LONG",
    primaryKeys "api,version,tenantDomain,apiPublisher,context,year,month,day,hour,minutes"
    );

    INSERT INTO TABLE APIExecutionTimeMinuteSummaryData select api, api_version,tenantDomain,apiPublisher,
    avg(apiResponseTime),context,avg(securityLatency),avg(throttlingLatency),avg(requestMediationLatency),
    avg(responseMediationLatency),avg(backendLatency),avg(otherLatency),
    year, month, day, hour, minute as minutes,
    first(eventTime)
    from APIMGT_PERMINUTE_EXECUTION_TIME where api is not NULL group by api,api_version,tenantDomain,apiPublisher,context,
    year, month, day, hour, minute;

    INCREMENTAL_TABLE_COMMIT LATENCY_APIMGT_PERMINUTE_EXECUTION_TIME;

</Script>
    <CronExpression>0 0/5 * 1/1 * ? *</CronExpression>
</Analytics>