<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<!--
  ~ Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
  ~
  ~ WSO2 Inc. licenses this file to you under the Apache License,
  ~ Version 2.0 (the "License"); you may not use this file except
  ~ in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~ http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing,
  ~ software distributed under the License is distributed on an
  ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  ~ KIND, either express or implied. See the License for the
  ~ specific language governing permissions and limitations
  ~ under the License.
  -->
<Analytics>
    <Editable>true</Editable>
    <Name>AndroidCallScript</Name>
    <Script>        CREATE TEMPORARY TABLE AndroidCallData USING CarbonAnalytics OPTIONS(tableName "IOT_PER_DEVICE_STREAM_ANDROID_CALL_STATS", incrementalParams "IOT_PER_DEVICE_STREAM_ANDROID_CALL_STATS, DAY");
    
        CREATE TEMPORARY TABLE Android_Call_Stat_Per_Day USING CarbonAnalytics
        OPTIONS (tableName "Android_Call_Stat_Per_Day",
        schema "owner STRING, deviceId STRING, type STRING, duration LONG -i, year INT -i, month INT -i, day INT -i, timestamp STRING",
        primaryKeys "year, month, day, deviceId, owner, type",
        mergeSchema "false");

        INSERT INTO TABLE Android_Call_Stat_Per_Day
        SELECT meta_owner as owner, meta_deviceId as deviceId, type, sum(duration) as duration, year, month, day, getDateStartingTime(year, month, day) as timestamp
        FROM AndroidCallData
        GROUP BY year, month, day, meta_deviceId, meta_owner, type ORDER BY timestamp DESC;
        
        INCREMENTAL_TABLE_COMMIT IOT_PER_DEVICE_STREAM_ANDROID_CALL_STATS;
    </Script>
    <CronExpression>0 0/5 * * * ?</CronExpression>
</Analytics>
