$smarty->display('report/downloadData.tpl');
    exit;
}
if ($_REQUEST['fromMonth']) {
    $fromDate = strtotime($_REQUEST['fromYear'] . "/" . $_REQUEST['fromMonth'] . "/" . $_REQUEST['fromDay'] . " " . $_REQUEST['fromHour'] . ":" . $_REQUEST['fromMinute']);
} else {
    $fromDate = current_seconds() - 86400;
}
if ($_REQUEST['toMonth']) {
    $toDate = strtotime($_REQUEST['toYear'] . "/" . $_REQUEST['toMonth'] . "/" . $_REQUEST['toDay'] . " " . $_REQUEST['toHour'] . ":" . $_REQUEST['toMinute']);
} else {
    $toDate = current_seconds();
}
try {
    header("Content-Type: text/csv");
    header("Content-Disposition: attachment; filename=\"data-" . $_REQUEST['fromYear'] . "-" . $_REQUEST['fromMonth'] . "-" . $_REQUEST['fromDay'] . "_" . $_REQUEST['toYear'] . "-" . $_REQUEST['toMonth'] . "-" . $_REQUEST['toDay'] . ".csv\"");
    print "label,id,status,valid,runlabel,date,wptjobid,wpthost,ttfb,dom,doc,fully,repeat ttfb,repeat dom,repeat doc,repeat fully\n";
    foreach ($labels as $label) {
        $data = getReportDataForLabel($label, $fromDate, $toDate, null, $runLabel);
        printResultData($data, $label);
    }
} catch (Exception $e) {
    print $e;
}
exit;
function printResultData($data, $label)
{
    foreach ($data as $item) {
        print $label . "," . $item['Id'] . "," . $item['Status'] . "," . $item['ValidationState'] . "," . $item['RunLabel'] . "," . date("Y/m/d", $item['Date']) . "," . $item['WPTJobId'] . "," . $item['WPTHost'] . "," . $item['AvgFirstViewFirstByte'] . "," . $item['AvgFirstViewDomTime'] . "," . $item['AvgFirstViewDocCompleteTime'] . "," . $item['AvgFirstViewFullyLoadedTime'] . "," . $item['AvgRepeatViewRepeatByte'] . "," . $item['AvgRepeatViewDomTime'] . "," . $item['AvgRepeatViewDocCompleteTime'] . "," . $item['AvgRepeatViewFullyLoadedTime'] . "\n";
    }
}
$smarty->assign('ninetieth', $ninetieth);
$smarty->assign('location', $location);
$smarty->assign('fromDate', $fromDate);
$smarty->assign('toDate', $toDate);
if ($download) {
    header("Content-Type: text/csv");
    header("Content-Disposition: attachment; filename=\"data-" . $_REQUEST['fromYear'] . "-" . $_REQUEST['fromMonth'] . "-" . $_REQUEST['fromDay'] . "_" . $_REQUEST['toYear'] . "-" . $_REQUEST['toMonth'] . "-" . $_REQUEST['toDay'] . ".csv\"");
    print "label,id,status,valid,date,wptjobid,wpthost,ttfb,dom,doc\n";
    if ($location == "-AU") {
        $labels = array(0 => "HomePageDirect", 1 => "HomePageOptimized", 2 => "FLPOptimized", 3 => "FLPDirect", 4 => "HLPOptimized", 5 => "HLPDirect", 6 => "FSROptimized", 7 => "FSRDirect", 8 => "HSROptimized", 9 => "HSRDirect", 10 => "PSROptimized", 11 => "PSRDirect");
    } else {
        $labels = array(0 => "HomePageUnOptimized", 1 => "HomePageOptimized", 2 => "FLPOptimized", 3 => "FLPUnOptimized", 4 => "HLPOptimized", 5 => "HLPUnOptimized", 6 => "FSROptimized", 7 => "FSRUnOptimized", 8 => "HSROptimized", 9 => "HSRUnOptimized", 10 => "PSROptimized", 11 => "PSRUnOptimized");
    }
    //    $labels[]="HomePageUnOptimized";
    foreach ($labels as $label) {
        $data = getReportDataForLabel($label . $location, $fromDate, $toDate, $stddev);
        printResultData($data, $label);
    }
    exit;
} else {
    if ($versusDirect) {
        $smarty->assign('unOptOrDirect', "Direct");
        $homePageUnOptimized = getReportResultsForLabel("HomePageDirect" . $location, $fromDate, $toDate, $stddev, $runLabel, $ninetieth);
        $homePageOptimized = getReportResultsForLabel("HomePageOptimized" . $location, $fromDate, $toDate, $stddev, $runLabel, $ninetieth);
        $flpOptimized = getReportResultsForLabel("FLPOptimized" . $location, $fromDate, $toDate, $stddev, $runLabel, $ninetieth);
        $flpUnOptimized = getReportResultsForLabel("FLPDirect" . $location, $fromDate, $toDate, $stddev, $runLabel, $ninetieth);
        $hlpOptimized = getReportResultsForLabel("HLPOptimized" . $location, $fromDate, $toDate, $stddev, $runLabel, $ninetieth);
        $hlpUnOptimized = getReportResultsForLabel("HLPDirect" . $location, $fromDate, $toDate, $stddev, $runLabel, $ninetieth);
        if ($completePath) {
            $location = " ( Complete Path )";
        }