/*
 * Copyright (c) 2018 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.
 */

@App:name("APIM_FAULT_SUMMARY")
@App:description("Defines aggregations for fault stream")

@source(type = 'mgwfile', wso2.stream.id = 'org.wso2.apimgt.statistics.fault:3.0.0', @map(type = 'wso2event'))
@source(type = 'wso2event', wso2.stream.id = 'org.wso2.apimgt.statistics.fault:3.0.0',
	@map(type = 'wso2event'))
define stream FaultStream(
    meta_clientType string,
    applicationConsumerKey string,
    apiName string,
    apiVersion string,
    apiContext string,
    apiResourcePath string,
    apiMethod string,
    apiCreator string,
    username string,
    userTenantDomain string,
    apiCreatorTenantDomain string,
    hostname string,
    applicationId string,
    applicationName string,
    protocol string,
    errorCode string,
    errorMessage string,
    requestTimestamp long
);

@store(type='rdbms', datasource='APIM_ANALYTICS_DB')
@purge(enable='true', interval='60 min', @retentionPeriod(sec='1 day', min='72 hours', hours='90 days', days='1 year', months='10 years'))
define aggregation ApiFaultyInvocationAgg
from FaultStream
select apiName,apiVersion,apiCreator,apiCreatorTenantDomain,apiContext,applicationConsumerKey,hostname,applicationId,applicationName,requestTimestamp,count() as totalFaultCount,'default' as regionalID
group by apiContext,applicationId,hostname
aggregate by requestTimestamp every seconds...year;