<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Analytics>
    <CronExpression>0 0 12 * * ?</CronExpression>
    <Editable>true</Editable>
    <Name>APIMAnalytics-RequestPerApi-RequestPerAPI-batch1</Name>
    <Script>
        CREATE TEMPORARY TABLE REQUEST_PER_API_INFO USING CarbonAnalytics OPTIONS (tableName "org_wso2_analytics_apim_requestPerMinPerApiStream");

        CREATE TEMPORARY TABLE REQUEST_PER_API_PERCENTILE_GEN USING CarbonAnalytics OPTIONS (tableName "ORG_WSO2_ANALYTICS_APIM_REQUESTPERAPIPERCENTILE",
        schema "requestsPerMinPerApiLowerPercentile double, api_version string -i, tenantDomain string -i",
        primaryKeys "api_version, tenantDomain", mergeSchema "false"
        );

        INSERT INTO TABLE REQUEST_PER_API_PERCENTILE_GEN
        select getpercentileValue(cast(avg(requestsPerMinPerApi) as double), cast(sqrt(avg(cast(requestsPerMinPerApi
        as double)*cast(requestsPerMinPerApi as double))-avg(requestsPerMinPerApi)*avg(requestsPerMinPerApi)) as double) , cast(0.05 as double) ) as requestsPerMinPerApiLowerPercentile,
        api_version as api_version, tenantDomain

        from REQUEST_PER_API_INFO group by api_version, tenantDomain;            

    </Script>
</Analytics>
