/* vim: set expandtab sw=4 ts=4: */
/**
 * Common functions and variables to enable a dashboard.
 *
 * Copyright (C) 2014-2015 Dieter Adriaenssens <ruleant@users.sourceforge.net>
 *
 * This file is part of buildtimetrend/dashboard
 * <https://github.com/buildtimetrend/dashboard/>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

var TIMEFRAME_LAST_WEEK = "this_7_days";
var TIMEFRAME_LAST_MONTH = "this_30_days";
var TIMEFRAME_LAST_YEAR = "this_52_weeks";

var CAPTION_LAST_WEEK = "Last week";
var CAPTION_LAST_MONTH = "Last month";
var CAPTION_LAST_YEAR = "Last year";

var TIMEZONE_SECS = "UTC"; // named timezone or offset in seconds, fe. GMT+1 = 3600

// Timeframe button constants
var BUTTON_TIMEFRAME_PREFIX = "timeframe_";
var BUTTON_TIMEFRAME_DEFAULT = "week";
var BUTTONS_TIMEFRAME = {
    "day": {
        "caption": "Day",
        "keenTimeframe": "today",
        "keenInterval": "hourly",
        "keenMaxAge": 300 // 5 min
    },
    "week": {
        "caption": "Week",
        "keenTimeframe": TIMEFRAME_LAST_WEEK,
        "keenInterval": "daily",
        "keenMaxAge": 600 // 10 min
    },
    "month": {
        "caption": "Month",
        "keenTimeframe": TIMEFRAME_LAST_MONTH,
        "keenInterval": "daily",
        "keenMaxAge": 600 // 10 min
    },
    "year": {
        "caption": "Year",
        "keenTimeframe": TIMEFRAME_LAST_YEAR,
        "keenInterval": "weekly",
        "keenMaxAge": 1800 // 30 min
   }
};

var timeframeButtons = new ButtonClass(
    BUTTONS_TIMEFRAME,
    BUTTON_TIMEFRAME_DEFAULT,
    BUTTON_TIMEFRAME_PREFIX
);
timeframeButtons.onClick = function() { updateCharts(); };

// arrays with queries and query request to update
var queriesInterval = [];
var queriesTimeframe = [];
var queryRequests = [];

function getUpdatePeriod() {
    return timeframeButtons.getCurrentButton();
}

/**
 * Refresh charts with interval and timeframe selected by timeframeButtons.
 */
function updateCharts() {
    // get Update Period settings
    var updatePeriod = getUpdatePeriod();

    var i;

    // update all interval based queries
    for (i = 0; i < queriesInterval.length; i++) {
        queriesInterval[i].set({interval: updatePeriod.keenInterval});
    }

    // update all timeframe based queries
    for (i = 0; i < queriesTimeframe.length; i++) {
        queriesTimeframe[i].set({
            timeframe: updatePeriod.keenTimeframe,
            maxAge: updatePeriod.keenMaxAge});
    }

    // refresh all updated query requests
    for (i = 0; i < queryRequests.length; i++) {
        queryRequests[i].refresh();
    }
}

/**
 * Enable auto refresh (if defined by url parameter).
 *
 * if url parameter `refresh` is defined, auto refreshing the charts is enabled.
 * refresh is defined in minutes
 */
function setAutoRefresh() {
    var refreshParam = getUrlParameter('refresh');
    if (isEmpty(refreshParam) || isNaN(refreshParam)) {
        return;
    }

    refreshSecs = 60 * parseInt(refreshParam, 10);

    setInterval(function(){updateCharts();}, 1000 * refreshSecs);
}

@@ -3236,17 +3236,17 @@
 .%0A *%0A * 
-i
+I
 f url pa
@@ -3317,17 +3317,17 @@
 led.%0A * 
-r
+R
 efresh i
@@ -3346,16 +3346,161 @@
  minutes
+.%0A * Refresh rate should at least be equal to maximum age of the Query cache,%0A * if not the cache max age value will be used (typically, 10 min).
 %0A */%0Afun
@@ -3702,24 +3702,373 @@
 aram, 10);%0A%0A
+    // disable auto refresh if value is zero or less%0A    if (refreshSecs %3C= 0) %7B%0A        return;%0A    %7D%0A%0A    // get Update Period settings%0A    var updatePeriod = getUpdatePeriod();%0A%0A    // refresh rate should not be smaller than the cache max age%0A    if (refreshSecs %3C updatePeriod.keenMaxAge) %7B%0A        refreshSecs = updatePeriod.keenMaxAge;%0A    %7D%0A%0A
     setInter

Refresh rate should at least be query cache max age.