<%
    var JSUtils = Packages.org.wso2.carbon.analytics.jsservice.Utils;
    var AnalyticsCachedJSServiceConnector = Packages.org.wso2.carbon.analytics.jsservice.AnalyticsCachedJSServiceConnector;
    var AnalyticsCache = Packages.org.wso2.carbon.analytics.jsservice.AnalyticsCachedJSServiceConnector.AnalyticsCache;
    var MultitenantConstants = Packages.org.wso2.carbon.base.MultitenantConstants;
    var configs = require('/configs/designer.json');

    var AUTHORIZATION_HEADER = "Authorization";
    var USER_TOKEN = "user";
    var USERNAME = "username";
    var TENANT_DOMAIN = "domain";
    var CONST_AT = "@";
    var superTenantId = MultitenantConstants.SUPER_TENANT_ID;

    function getLoggedInUser(){
    	var authParam = request.getHeader(AUTHORIZATION_HEADER);
    	var loggedInUser = null;
	    if (authParam != null) {
	        credentials = JSUtils.authenticate(authParam);
	        loggedInUser = credentials[0];
	    } else {
	        var token = session.get(USER_TOKEN);
	        if (token != null) {
	            loggedInUser = token[USERNAME] + CONST_AT + token[TENANT_DOMAIN];
	            return loggedInUser;
	        }
	    }
	    return loggedInUser;
    };

    function getConnector(){
    	if (configs.cacheTimeoutSeconds) {
        	cacheTimeoutSeconds = parseInt(configs.cacheTimeoutSeconds);
	    }
	    var cacheSizeBytes = 1024 * 1024 * 1024; // 1GB
	    if (configs.cacheSizeBytes) {
	        cacheSizeBytes = parseInt(configs.cacheSizeBytes);
	    }
	    var cache = application.get("AnalyticsWebServiceCache");
	    if (cache == null) {
	        cache = new AnalyticsCache(cacheTimeoutSeconds, cacheSizeBytes);
	        application.put("AnalyticsWebServiceCache", cache);
	    }
	    return new AnalyticsCachedJSServiceConnector(cache);
    };

        function getDrillDownResult(tableName, fieldName, categoryPath, queryString, scoreFunction, start, count){
    	if (scoreFunction != "") {
    		var query = stringify({
                fieldName: fieldName, //field which is indexed as a FACET
                categoryPath: categoryPath, //Path being drilled down, optional
                query: queryString,
                scoreFunction: scoreFunction,
                start: start,
                count: count
            });
    	} else {
    		var query = stringify({
                fieldName: fieldName, //field which is indexed as a FACET
                categoryPath: categoryPath, //Path being drilled down, optional
                query: queryString,
                start: start,
                count: count
            });
    	}
    	

        var resp = getConnector().drillDownCategories(superTenantId, tableName, query);

        var dataPoints = JSON.parse(resp.getMessage());

        var obj = dataPoints.categories;
        var categoryCount = dataPoints.categoryCount;

        return [obj, categoryCount];

    };

    function isStartTimestampDataAvailable(startTimestamp, firstDataPointTimestamp, timeUnit) {

        var startTimestampDataAvailable = true;
        var step;
        if(timeUnit == "MINUTE") {
            step = 60000;
        } else if(timeUnit == "HOUR") {
            step = 3600000;
        } else if(timeUnit == "DAY") {
            step = 86400000;
        } else if(timeUnit == "MONTH") {
            step = 2628000000;
        } else if(timeUnit == "YEAR") {
            step = 31540000000;
        }
        if(startTimestamp < firstDataPointTimestamp && (firstDataPointTimestamp - startTimestamp) > step) {
            startTimestampDataAvailable = false;
        }
        return startTimestampDataAvailable;
    }

    function isEndTimestampDataAvailable(endTimestamp, lastDataPointTimestamp, timeUnit) {

        var endTimestampDataAvailable = true;
        var step;
        if(timeUnit == "MINUTE") {
            step = 60000;
        } else if(timeUnit == "HOUR") {
            step = 3600000;
        } else if(timeUnit == "DAY") {
            step = 86400000;
        } else if(timeUnit == "MONTH") {
            step = 2628000000;
        } else if(timeUnit == "YEAR") {
            step = 31540000000;
        }
        if(endTimestamp > lastDataPointTimestamp && (endTimestamp - lastDataPointTimestamp) > step) {
            endTimestampDataAvailable = false;
        }
        return endTimestampDataAvailable;
    }
    
 %>