*
 * @internal revisions
 * @since 1.9.10
 *
**/
require_once '../../config.inc.php';
require_once 'common.php';
define('PCHART_PATH', '../../third_party/pchart');
include PCHART_PATH . "/pChart/pData.class";
include PCHART_PATH . "/pChart/pChart.class";
$resultsCfg = config_get('results');
$chart_cfg = $resultsCfg['charts']['dimensions']['overallPieChart'];
$args = init_args($db);
$tplan_mgr = new testplan($db);
$metricsMgr = new tlTestPlanMetrics($db);
$totals = $metricsMgr->getExecCountersByExecStatus($args->tplan_id);
unset($totals['total']);
$values = array();
$labels = array();
foreach ($totals as $key => $value) {
    $values[] = $value;
    $labels[] = lang_get($resultsCfg['status_label'][$key]) . " ({$value})";
    if (isset($resultsCfg['charts']['status_colour'][$key])) {
        $series_color[] = $resultsCfg['charts']['status_colour'][$key];
    }
}
// Dataset definition
$DataSet = new pData();
$DataSet->AddPoint($values, "Serie1");
$DataSet->AddPoint($labels, "Serie8");
$DataSet->AddAllSeries();
/**
 *  only active builds has to be used
 *
 *  @internal revisions
 *
 *  
 */
function getMetrics(&$db, $userObj, $args, $result_cfg, $labels)
{
    $user_id = $args->currentUserID;
    $tproject_id = $args->tproject_id;
    $linked_tcversions = array();
    $metrics = array();
    $tplan_mgr = new testplan($db);
    $show_platforms = false;
    $platforms = array();
    // get all tesplans accessibles  for user, for $tproject_id
    $options = array('output' => 'map');
    $options['active'] = $args->show_only_active ? ACTIVE : TP_ALL_STATUS;
    $test_plans = $userObj->getAccessibleTestPlans($db, $tproject_id, null, $options);
    // Get count of testcases linked to every testplan
    // Hmm Count active and inactive ?
    $linkedItemsQty = $tplan_mgr->count_testcases(array_keys($test_plans), null, array('output' => 'groupByTestPlan'));
    $metricsMgr = new tlTestPlanMetrics($db);
    $show_platforms = false;
    $metrics = array('testplans' => null, 'total' => null);
    $mm =& $metrics['testplans'];
    $metrics['total'] = array('active' => 0, 'total' => 0, 'executed' => 0);
    foreach ($result_cfg['status_label_for_exec_ui'] as $status_code => &$dummy) {
        $metrics['total'][$status_code] = 0;
    }
    $codeStatusVerbose = array_flip($result_cfg['status_code']);
    foreach ($test_plans as $key => &$dummy) {
        // We need to know if test plan has builds, if not we can not call any method
        // that try to get exec info, because you can only execute if you have builds.
        //
        // 20130909 - added active filter
        $buildSet = $tplan_mgr->get_builds($key, testplan::ACTIVE_BUILDS);
        if (is_null($buildSet)) {
            continue;
        }
        $platformSet = $tplan_mgr->getPlatforms($key);
        if (isset($platformSet)) {
            $platforms = array_merge($platforms, $platformSet);
        }
        $show_platforms_for_tplan = !is_null($platformSet);
        $show_platforms = $show_platforms || $show_platforms_for_tplan;
        if (!is_null($platformSet)) {
            $neurus = $metricsMgr->getExecCountersByPlatformExecStatus($key, null, array('getPlatformSet' => true, 'getOnlyActiveTCVersions' => true));
            $mm[$key]['overall']['active'] = $mm[$key]['overall']['executed'] = 0;
            foreach ($neurus['with_tester'] as $platform_id => &$pinfo) {
                $xd =& $mm[$key]['platforms'][$platform_id];
                $xd['tplan_name'] = $dummy['name'];
                $xd['platform_name'] = $neurus['platforms'][$platform_id];
                $xd['total'] = $xd['active'] = $neurus['total'][$platform_id]['qty'];
                $xd['executed'] = 0;
                foreach ($pinfo as $code => &$elem) {
                    $xd[$codeStatusVerbose[$code]] = $elem['exec_qty'];
                    if ($codeStatusVerbose[$code] != 'not_run') {
                        $xd['executed'] += $elem['exec_qty'];
                    }
                    if (!isset($mm[$key]['overall'][$codeStatusVerbose[$code]])) {
                        $mm[$key]['overall'][$codeStatusVerbose[$code]] = 0;
                    }
                    $mm[$key]['overall'][$codeStatusVerbose[$code]] += $elem['exec_qty'];
                    $metrics['total'][$codeStatusVerbose[$code]] += $elem['exec_qty'];
                }
                $mm[$key]['overall']['executed'] += $xd['executed'];
                $mm[$key]['overall']['active'] += $xd['active'];
            }
            unset($neurus);
            $mm[$key]['overall']['total'] = $mm[$key]['overall']['active'];
            $metrics['total']['executed'] += $mm[$key]['overall']['executed'];
            $metrics['total']['active'] += $mm[$key]['overall']['active'];
        } else {
            $mm[$key]['overall'] = $metricsMgr->getExecCountersByExecStatus($key, null, array('getOnlyActiveTCVersions' => true));
            $mm[$key]['overall']['active'] = $mm[$key]['overall']['total'];
            // compute executed
            $mm[$key]['overall']['executed'] = 0;
            foreach ($mm[$key]['overall'] as $status_code => $qty) {
                if ($status_code != 'not_run' && $status_code != 'total' && $status_code != 'active') {
                    $mm[$key]['overall']['executed'] += $qty;
                }
                if ($status_code != 'total' && $status_code != 'active') {
                    if (!isset($metrics['total'][$status_code])) {
                        $metrics['total'][$status_code] = 0;
                    }
                    $metrics['total'][$status_code] += $qty;
                }
            }
            $metrics['total']['executed'] += $mm[$key]['overall']['executed'];
            $metrics['total']['active'] += $mm[$key]['overall']['active'];
            $mm[$key]['platforms'][0] = $mm[$key]['overall'];
            $mm[$key]['platforms'][0]['tplan_name'] = $dummy['name'];
            $mm[$key]['platforms'][0]['platform_name'] = $labels['not_aplicable'];
        }
    }
    // remove duplicate platform names
    $platformsUnique = array();
    foreach ($platforms as $platform) {
        if (!in_array($platform['name'], $platformsUnique)) {
            $platformsUnique[] = $platform['name'];
        }
    }
    return array($metrics, $show_platforms, $platformsUnique);
}