<%
/*
 * Copyright (c) 2015, 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.
 */

// define significant number of records to be displayed in charts
var BROWSER_SIGNIFICANT = 4;
var OS_SIGNIFICANT = 5;
var DEVICE_TYPE_SIGNIFICANT = 3;

// define sorting column id in tabular visualization
var BROWSER_SORT = 2;
var OS_SORT = 2;
var DEVICE_TYPE_SORT = 2;
var HTTP_RESPONSE_SORT = 2;
var LANGUAGE_SORT = 2;
var COUNTRY_SORT = 2;
var CONTEXT_SORT = 2;
var REFERRAL_SORT = 2;

var util = require('modules/as-data-util.js');
var action = request.getParameter('action');
var conditions = buildConditions();

var log = new Log();

/*
 build the ammendment to the prepared statement along with the parameter values based on
 start_time, end_time, node and webapp
 */
function buildConditions() {
    // default data range is the past 24 hours
    var startTime = request.getParameter('start_time') || util.formatDate(new Date(new Date() - 86400000));
    var endTime = request.getParameter('end_time') || util.formatDate(new Date());
    var node = request.getParameter('node');
    var webapp = request.getParameter('appname');
    var where = [];
    var params = [];

    if (startTime != null) {
        where.push('time >= ?');
        params.push(startTime);
    }
    if (endTime != null) {
        where.push('time <= ?');
        params.push(endTime);
    }
    if (node != null && node != 'All') {
        where.push('serverName = ?');
        params.push(String(node));
    }
    if (webapp != null && webapp != 'null') {
        where.push('webappName= ?');
        params.push(String(webapp));
    }
    return {'sql': ' WHERE ' + where.join(' AND '), 'params': params};
}

function getNodeList() {
    var nodeList = require('modules/node-list.js');
    if (log.isDebugEnabled()) {
        log.debug('Retrieving node list');
    }
    nodeList.getNodeList();
}

function getAppsStat() {
    var appList = require('modules/app-list.js');
    if (log.isDebugEnabled()) {
        log.debug('Retrieving applicaiton list');
    }
    appList.getAppsStat(conditions, request.getParameter('end_time') || util.formatDate(new Date()));
}

function getBrowserStat() {
    var technologyStat = require('modules/technology-stat.js');
    if (log.isDebugEnabled()) {
        log.debug('Retrieving browser stats');
    }
    technologyStat.getTechnologyStat(conditions, 'browser', BROWSER_SIGNIFICANT, 'Other Browsers');
}

function getBrowserTabularStat() {
    var technologyStat = require('modules/technology-stat.js');
    if (log.isDebugEnabled()) {
        log.debug('Retrieving browser stat - tabular');
    }
    technologyStat.getTechnologyTubularStat(conditions, 'browser',
            ['Browser', 'Request Count', 'Percentage'], BROWSER_SORT);
}

function getOperatingSystemStat() {
    var technologyStat = require('modules/technology-stat.js');
    if (log.isDebugEnabled()) {
        log.debug('Retrieving operating system stat');
    }
    technologyStat.getTechnologyStat(conditions, 'os', OS_SIGNIFICANT, 'Other Operating Systems');
}

function getOperatingSystemTabularStat() {
    var technologyStat = require('modules/technology-stat.js');
    if (log.isDebugEnabled()) {
        log.debug('Retrieving operating system stat - tabular');
    }
    technologyStat.getTechnologyTubularStat(conditions, 'os',
            ['Operating System', 'Request Count', 'Percentage'], OS_SORT);
}

function getDeviceTypeStat() {
    var technologyStat = require('modules/technology-stat.js');
    if (log.isDebugEnabled()) {
        log.debug('Retrieving device type stat');
    }
    technologyStat.getTechnologyStat(conditions, 'device-type', DEVICE_TYPE_SIGNIFICANT,
            'Other devices');
}

function getDeviceTypeTabularStat() {
    var technologyStat = require('modules/technology-stat.js');
    if (log.isDebugEnabled()) {
        log.debug('Retrieving device type stat - tabular');
    }
    technologyStat.getTechnologyTubularStat(conditions, 'device-type',
            ['Device type', 'Request Count', 'Percentage'], DEVICE_TYPE_SORT);
}

function getHttpStatusStat() {
    var technologyStat = require('modules/technology-stat.js');
    if (log.isDebugEnabled()) {
        log.debug('Retrieving HTTP response code stat');
    }
    technologyStat.getHttpStatusStat(conditions);
}

function getHttpStatusTabularStat() {
    var technologyStat = require('modules/technology-stat.js');
    if (log.isDebugEnabled()) {
        log.debug('Retrieving HTTP response code stat - tabular');
    }
    technologyStat.getHttpStatusTabularStat(conditions, ['HTTP Response Code', 'Request Count',
                                                         'Percentage'], HTTP_RESPONSE_SORT);
}

function getLanguageStat() {
    var geoStat = require('modules/geo-stat.js');
    if (log.isDebugEnabled()) {
        log.debug('Retrieving language stat');
    }
    geoStat.getLanguageStat(conditions);
}

function getLanguageTabularStat() {
    var geoStat = require('modules/geo-stat.js');
    if (log.isDebugEnabled()) {
        log.debug('Retrieving language stat - tabular');
    }
    geoStat.getLanguageTabularStat(conditions, ['Language', 'Request Count', 'Percentage'],
            LANGUAGE_SORT);
}

function getCountryStat() {
    var geoStat = require('modules/geo-stat.js');
    if (log.isDebugEnabled()) {
        log.debug('Retrieving country stat');
    }
    geoStat.getCountryStat(conditions);
}

function getCountryTabularStat() {
    var geoStat = require('modules/geo-stat.js');
    if (log.isDebugEnabled()) {
        log.debug('Retrieving country stat - tabular');
    }
    geoStat.getCountryTabularStat(conditions, ['Country', 'Request Count', 'Percentage'],
            COUNTRY_SORT);
}

function getContextStat() {
    var trafficStat = require('modules/traffic-stat.js');
    if (log.isDebugEnabled()) {
        log.debug('Retrieving context stat');
    }
    trafficStat.getTrafficStat(conditions, 'context', ['Context', 'Request Count', 'Percentage'],
            CONTEXT_SORT);
}

function getReferralStat() {
    var trafficStat = require('modules/traffic-stat.js');
    if (log.isDebugEnabled()) {
        log.debug('Retrieving referral stat');
    }
    trafficStat.getTrafficStat(conditions, 'referral', ['Referrer', 'Request Count',
                                                        'Percentage'], REFERRAL_SORT)
}

function getRequestStat() {
    var timeVaryingStat = require('modules/time-varying-stat.js');
    if (log.isDebugEnabled()) {
        log.debug('Retrieving request count stat');
    }
    timeVaryingStat.getTimeVaryingStat(conditions, 'request');
}

function getResponseStat() {
    var timeVaryingStat = require('modules/time-varying-stat.js');
    if (log.isDebugEnabled()) {
        log.debug('Retrieving response time stat');
    }
    timeVaryingStat.getTimeVaryingStat(conditions, 'response', '#bacf0b');
}

function getErrorStat() {
    var timeVaryingStat = require('modules/time-varying-stat.js');
    if (log.isDebugEnabled()) {
        log.debug('Retrieving error count stat');
    }
    timeVaryingStat.getTimeVaryingStat(conditions, 'error', '#e7912a');
}

function getInfoBoxRequestStat() {
    var infoBoxStat = require('modules/info-box-stat.js');
    if (log.isDebugEnabled()) {
        log.debug('Retrieving infobox stat - request count');
    }
    return infoBoxStat.getInfoBoxRequestStat(conditions);
}

function getInfoBoxResponseStat() {
    var infoBoxStat = require('modules/info-box-stat.js');
    if (log.isDebugEnabled()) {
        log.debug('Retrieving infobox stat - response time');
    }
    return infoBoxStat.getInfoBoxResponseStat(conditions);
}

function getInfoBoxSessionStat() {
    var infoBoxStat = require('modules/info-box-stat.js');
    if (log.isDebugEnabled()) {
        log.debug('Retrieving infobox stat - session count');
    }
    return infoBoxStat.getInfoBoxSessionStat(conditions);
}

function getInfoBoxErrorStat() {
    var infoBoxStat = require('modules/info-box-stat.js');
    if (log.isDebugEnabled()) {
        log.debug('Retrieving infobox stat - error count');
    }
    return infoBoxStat.getInfoBoxErrorStat(conditions);
}

var actionMapping = {
    'node-list': getNodeList,
    'apps': getAppsStat,
    'browser': getBrowserStat,
    'browser-tabular': getBrowserTabularStat,
    'operating-system': getOperatingSystemStat,
    'operating-system-tabular': getOperatingSystemTabularStat,
    'device-type': getDeviceTypeStat,
    'device-type-tabular': getDeviceTypeTabularStat,
    'top-http-status': getHttpStatusStat,
    'top-http-status-tabular': getHttpStatusTabularStat,
    'language': getLanguageStat,
    'language-tabular': getLanguageTabularStat,
    'country': getCountryStat,
    'country-tabular': getCountryTabularStat,
    'context': getContextStat,
    'referral': getReferralStat,
    'request': getRequestStat,
    'response': getResponseStat,
    'error': getErrorStat,
    'request-count': getInfoBoxRequestStat,
    'response-count': getInfoBoxResponseStat,
    'session-count': getInfoBoxSessionStat,
    'error-count': getInfoBoxErrorStat
};

actionMapping[action]();

%>