<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Analytics>
    <CronExpression>0 0 23 * * ?</CronExpression>
    <Editable>true</Editable>
    <Name>APIMAnalytics-RequestStatGenerator-RequestStatGenerator-batch1</Name>
    <Script>
        CREATE TEMPORARY TABLE REQUEST_INFO USING CarbonAnalytics OPTIONS (tableName "ORG_WSO2_APIMGT_STATISTICS_PERMINUTEREQUEST");

        CREATE TEMPORARY TABLE REQUEST_PERCENTILE_GEN USING CarbonAnalytics OPTIONS (tableName "ORG_WSO2_ANALYTICS_APIM_REQUESTPERCENTILE",
        schema "api_version string, userId string, consumerKey string, resourceTemplate string, method string, requestsPerMinUpperPercentile double, requestsPerMinLowerPercentile double ",
        primaryKeys "api_version,userId,consumerKey,resourceTemplate,method"
        );

        INSERT OVERWRITE TABLE REQUEST_PERCENTILE_GEN
        select api_version, userId, consumerKey, resourceTemplate, method,
        getpercentileValue(cast(avg(total_request_count) as double), cast(sqrt(avg(cast(total_request_count
        as double)*cast(total_request_count as double))-avg(total_request_count)*avg(total_request_count)) as double), cast(0.95 as double) ) as requestsPerMinUpperPercentile,
        getpercentileValue(cast(avg(total_request_count) as double), cast(sqrt(avg(cast(total_request_count
        as double)*cast(total_request_count as double))-avg(total_request_count)*avg(total_request_count)) as double) , cast(0.05 as double)) as requestsPerMinLowerPercentile

        from REQUEST_INFO group by api_version, userId, consumerKey, resourceTemplate, method;

    </Script>
</Analytics>
