<?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>HeadsetScript</Name>
    <Script>CREATE TEMPORARY TABLE AndroidHeadsetData USING CarbonAnalytics OPTIONS(tableName "IOT_PER_DEVICE_STREAM_ANDROID_HEADSET_STATS", incrementalParams "IOT_PER_DEVICE_STREAM_ANDROID_HEADSET_STATS, DAY");
    
            CREATE TEMPORARY TABLE Android_Headset_Stat_Per_Day USING CarbonAnalytics
            OPTIONS (tableName "Android_Headset_Stat_Per_Day",
            schema "owner STRING -i, deviceId STRING -i, duration LONG -i, year INT -i, month INT -i, day INT -i, timestamp STRING -i",
            primaryKeys "year, month, day, deviceId, owner",
            mergeSchema "false");

            INSERT INTO TABLE Android_Headset_Stat_Per_Day
            SELECT meta_owner as owner, meta_deviceId as deviceId, sum(duration) as duration, year, month, day, getDateStartingTime(year, month, day) as timestamp
            FROM AndroidHeadsetData
            GROUP BY year, month, day, meta_deviceId, meta_owner ORDER BY timestamp DESC;

            INCREMENTAL_TABLE_COMMIT IOT_PER_DEVICE_STREAM_ANDROID_HEADSET_STATS;
    </Script>
    <CronExpression>0 0/5 * * * ?</CronExpression>
</Analytics>
