'use strict';
var config = require('./config'),
    db = require('./database');

function round(num, places) {
    if (!places) {
        places = 0;
    }
    places = Math.pow(10, places);
    return Math.round(num * places) / places;
}

function average(arr, map) {
    return sum(arr, map) / arr.length;
}

function sum(arr, map) {
    if (!map) {
        map = function (a) {
            return a;
        };
    }
    return arr.map(map).reduce(function (a, b) {
        return a + b;
    }, 0);
}

function getFlavor(value, arr) {
    var key = Object.keys(arr).filter(function (a) {
        return a <= value;
    });
    key = key[key.length - 1];
    return arr[key];
}

exports.getData = function getData(cfg, callback) {
    db.getRecentChecks(cfg.dataPeriod, function (err, data) {
        if (err || !data) {
            return callback(err || 'NO DATA');
        }
        var percentage = average(data, function (a) {
                return a.status;
            }),
            time = average(data, function (a) {
                return a.responseTime;
            }),
            score = (percentage + (time / 10)) / 2,
            result = {
                ok: function () {
                    return percentage < 250 && time < 3000;
                },
                version: config.version,
                time: new Date().toISOString(),
                up: percentage < 300,
                percentage: round(percentage, 2),
                precisionPercentage: percentage,
                average: round(time, 2),
                precisionAverage: time,
                score: round(score, 2),
                precisionScore: score,
                code: getFlavor(score, config.statusTime),
                status: getFlavor(score, config.status),
                flavor: getFlavor(score, config.flavor)
            },
            checks = {},
            keys;
        Object.keys(cfg).map(function (key) {
            result[key] = cfg[key];
        });
        result.checks = [];
        data = data.map(function (a) {
            a.checkedAt = new Date(a.checkedAt);
            return a;
        });
        data.map(function (a) {
            checks[a.key] = checks[a.key] || [];
            checks[a.key].push({
                code: a.status,
                responseTime: a.responseTime,
                polledAt: a.checkedAt
            });
        });
        keys = Object.keys(checks);
        keys.sort();
        result.summary = keys.map(function (key) {
            var avg = average(checks[key], function (a) {
                return a.code;
            });
            result.checks.push({
                name: key,
                values: checks[key]
            });
            return {
                name: key,
                response: getFlavor(avg, config.statusType),
                code: avg,
                responseTime: average(checks[key], function (a) {
                    return a.responseTime;
                }),
                polledAt: checks[key][0].polledAt
            };
        });
        callback(null, result);
    });
};

@@ -1981,36 +1981,8 @@
 %7D);%0A
-        result.checks = %5B%5D;%0A
     
@@ -2008,32 +2008,32 @@
 (function (a) %7B%0A
+
             a.ch
@@ -2225,25 +2225,33 @@
             
-c
+responseC
 ode: a.statu
@@ -2560,125 +2560,21 @@
 n a.
-code;%0A            %7D);%0A            result.checks.push(%7B%0A                name: key,%0A                values: checks%5Bkey%5D
+responseCode;
 %0A   
@@ -2707,25 +2707,33 @@
             
-c
+responseC
 ode: avg,%0A  
@@ -2851,32 +2851,32 @@
             %7D),%0A
-
                 
@@ -2904,24 +2904,61 @@
 %5B0%5D.polledAt
+,%0A                values: checks%5Bkey%5D
 %0A           

change data