<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Analytics>
    <Name>IsAnalytics-SparkScript-SessionData</Name>
    <Script>
        CREATE TEMPORARY TABLE isSessionAnalyticsPerMinute USING CarbonAnalytics OPTIONS (tableName "org_wso2_is_analytics_stream_SessionStatPerMinute", schema "meta_tenantId INT -i, bucketId LONG, bucketStart LONG -i, bucketEnd LONG -i, year INT, month INT, day INT, hour INT, minute INT, activeSessionCount LONG, newSessionCount LONG, terminatedSessionCount LONG, _timestamp LONG -i", primaryKeys "meta_tenantId, bucketId, bucketStart, bucketEnd", incrementalParams "isSessionAnalyticsPerHour, HOUR", mergeSchema "false");

        CREATE TEMPORARY TABLE isSessionAnalyticsPerHour USING CarbonAnalytics OPTIONS (tableName "org_wso2_is_analytics_stream_SessionStatPerHour", schema "meta_tenantId INT -i, bucketId LONG -i, bucketStart LONG -i, bucketEnd LONG -i, year INT, month INT, day INT, hour INT, activeSessionCount LONG -i, newSessionCount LONG -i, terminatedSessionCount LONG -i, _timestamp LONG -i", primaryKeys "meta_tenantId, bucketId, bucketStart, bucketEnd", incrementalParams "isSessionAnalyticsPerHour, DAY", mergeSchema "false");

        CREATE TEMPORARY TABLE isSessionAnalyticsPerDay USING CarbonAnalytics OPTIONS (tableName "org_wso2_is_analytics_stream_SessionStatPerDay", schema "meta_tenantId INT -i, bucketId LONG -i, bucketStart LONG -i, bucketEnd LONG -i, year INT, month INT, day INT, activeSessionCount LONG -i, newSessionCount LONG -i, terminatedSessionCount LONG -i, _timestamp LONG -i", primaryKeys "meta_tenantId, bucketId, bucketStart, bucketEnd", incrementalParams "isSessionAnalyticsPerDay, MONTH", mergeSchema "false");

        CREATE TEMPORARY TABLE isSessionAnalyticsPerMonth USING CarbonAnalytics OPTIONS (tableName "org_wso2_is_analytics_stream_SessionStatPerMonth", schema "meta_tenantId INT -i, bucketId LONG -i, bucketStart LONG -i, bucketEnd LONG -i, year INT, month INT, activeSessionCount LONG -i, newSessionCount LONG -i, terminatedSessionCount LONG -i, _timestamp LONG -i", primaryKeys "meta_tenantId, bucketId, bucketStart, bucketEnd", mergeSchema "false");

        INSERT INTO TABLE isSessionAnalyticsPerHour SELECT meta_tenantId, getHourStartingTime(year, month, day,hour) as bucketId, getHourStartingTime(year, month, day,hour) as bucketStart, (getHourStartingTime(year, month, day,hour) + 3600000)  as bucketEnd, year, month, day, hour, last(activeSessionCount) as activeSessionCount, sum(newSessionCount) as newSessionCount, sum(terminatedSessionCount) as terminatedSessionCount, -1 from (select * from isSessionAnalyticsPerMinute order by bucketStart) temp group by year, month, day, hour, meta_tenantId order by bucketStart;

        INCREMENTAL_TABLE_COMMIT isSessionAnalyticsPerMinute;

        INSERT INTO TABLE isSessionAnalyticsPerDay SELECT meta_tenantId, getDateStartingTime(year, month, day) as bucketId, getDateStartingTime(year, month, day) as bucketStart, (getDateStartingTime(year, month, day) + 86400000)  as bucketEnd, year, month, day, last(activeSessionCount) as activeSessionCount, sum(newSessionCount) as newSessionCount, sum(terminatedSessionCount) as terminatedSessionCount, -1 from (select * from isSessionAnalyticsPerHour order by bucketStart) temp group by year, month, day, meta_tenantId order by bucketStart;

        INCREMENTAL_TABLE_COMMIT isSessionAnalyticsPerHour;

        INSERT INTO TABLE isSessionAnalyticsPerMonth SELECT meta_tenantId, getMonthStartingTime(year, month) as bucketId, getMonthStartingTime(year, month) as bucketStart, getMonthStartingTime(year, month + 1)  as bucketEnd, year, month, last(activeSessionCount) as activeSessionCount, sum(newSessionCount) as newSessionCount, sum(terminatedSessionCount) as terminatedSessionCount, -1 from (select * from isSessionAnalyticsPerDay order by bucketStart) temp group by year, month, meta_tenantId order by bucketStart;

        INCREMENTAL_TABLE_COMMIT isSessionAnalyticsPerDay;
    </Script>
    <CronExpression>0 0/3 * * * ?</CronExpression>
</Analytics>
