Exemplo n.º 1
0
function metric_as_graph($result, $xaxis, $metric, $system, $start_date, $end_date)
{
    set_time_limit(900);
    $myresult = $result;
    $nrows = 0;
    $xmax = 0;
    while ($myresult->fetchInto($row)) {
        $keys = array_keys($row);
        foreach ($keys as $key) {
            $rawdata[$nrows][$key] = $row[$key];
        }
        $nrows++;
    }
    if ($xaxis == 'nproc') {
        for ($i = 0; $i <= nprocs($system); $i++) {
            $x[$i] = "";
            $y[$i] = "";
            $min[$i] = "";
            $max[$i] = "";
            $stddev[$i] = "";
            $max[$i] = "";
            $ysigma[2 * $i] = "";
            $ysigma[2 * $i + 1] = "";
        }
    }
    if ($metric == 'jobcount') {
        for ($i = 0; $i < $nrows; $i++) {
            $x[$i] = $rawdata[$i][0];
            if ($xaxis == 'nproc') {
                if ($x[$i] > $xmax) {
                    $xmax = $x[$i];
                }
                $y[$x[$i]] = $rawdata[$i][1];
            } else {
                $y[$i] = $rawdata[$i][1];
            }
        }
    } elseif ($metric == 'cpuhours' || $metric == 'xfactor' || $metric == 'users' || $metric == 'groups') {
        for ($i = 0; $i < $nrows; $i++) {
            $x[$i] = $rawdata[$i][0];
            if ($xaxis == 'nproc') {
                if ($x[$i] > $xmax) {
                    $xmax = $x[$i];
                }
                $y[$x[$i]] = $rawdata[$i][2];
            } else {
                $y[$i] = $rawdata[$i][2];
            }
        }
    } elseif ($metric == 'backlog') {
        for ($i = 0; $i < $nrows; $i++) {
            $x[$i] = $rawdata[$i][0];
            if ($xaxis == 'nproc') {
                if ($x[$i] > $xmax) {
                    $xmax = $x[$i];
                }
                $y[$x[$i]] = time_to_hrs($rawdata[$i][3]);
                $max[$x[$i]] = time_to_hrs($rawdata[$i][2]);
            } else {
                $y[$i] = time_to_hrs($rawdata[$i][3]);
                $max[$i] = time_to_hrs($rawdata[$i][2]);
            }
        }
    } else {
        for ($i = 0; $i < $nrows; $i++) {
            $x[$i] = $rawdata[$i][0];
            if ($xaxis == 'nproc') {
                if ($x[$i] > $xmax) {
                    $xmax = $x[$i];
                }
                $y[$x[$i]] = $rawdata[$i][4];
                if (preg_match('/^[0-9]+:[0-5][0-9]:[0-5][0-9]$/', $y[$x[$i]])) {
                    $y[$x[$i]] = time_to_hrs($y[$x[$i]]);
                }
                $min[$x[$i]] = $rawdata[$i][2];
                if (preg_match('/^[0-9]+:[0-5][0-9]:[0-5][0-9]$/', $min[$x[$i]])) {
                    $min[$x[$i]] = time_to_hrs($min[$x[$i]]);
                }
                $max[$x[$i]] = $rawdata[$i][3];
                if (preg_match('/^[0-9]+:[0-5][0-9]:[0-5][0-9]$/', $max[$x[$i]])) {
                    $max[$x[$i]] = time_to_hrs($max[$x[$i]]);
                }
                $stddev[$x[$i]] = $rawdata[$i][5];
                if (preg_match('/^[0-9]+:[0-5][0-9]:[0-5][0-9]$/', $stddev[$x[$i]])) {
                    $stddev[$x[$i]] = time_to_hrs($stddev[$x[$i]]);
                }
                $ysigma[2 * $x[$i]] = $y[$x[$i]] - $stddev[$x[$i]];
                if ($ysigma[2 * $x[$i]] < 0.0) {
                    $ysigma[2 * $x[$i]] = 0.0;
                }
                $ysigma[2 * $x[$i] + 1] = $y[$x[$i]] + $stddev[$x[$i]];
            } else {
                $y[$i] = $rawdata[$i][4];
                if (preg_match('/^[0-9]+:[0-5][0-9]:[0-5][0-9]$/', $y[$i])) {
                    $y[$i] = time_to_hrs($y[$i]);
                }
                $min[$i] = $rawdata[$i][2];
                if (preg_match('/^[0-9]+:[0-5][0-9]:[0-5][0-9]$/', $min[$i])) {
                    $min[$i] = time_to_hrs($min[$i]);
                }
                $max[$i] = $rawdata[$i][3];
                if (preg_match('/^[0-9]+:[0-5][0-9]:[0-5][0-9]$/', $max[$i])) {
                    $max[$i] = time_to_hrs($max[$i]);
                }
                $stddev[$i] = $rawdata[$i][5];
                if (preg_match('/^[0-9]+:[0-5][0-9]:[0-5][0-9]$/', $stddev[$i])) {
                    $stddev[$i] = time_to_hrs($stddev[$i]);
                }
                $ysigma[2 * $i] = $y[$i] - $stddev[$i];
                if ($ysigma[2 * $i] < 0.0) {
                    $ysigma[2 * $i] = 0.0;
                }
                $ysigma[2 * $i + 1] = $y[$i] + $stddev[$i];
            }
        }
    }
    $cache = APACHE_CACHE_DIR;
    if (!file_exists("/tmp/" . $cache)) {
        mkdir("/tmp/" . $cache, 0750);
    }
    $plot = $system . "-" . $metric . "_vs_" . $xaxis . "-" . $start_date . "-" . $end_date . ".png";
    //  $graph = new graph(640,480,$plot,2,0);
    $graph = new graph(800, 600, $plot, 2, 0);
    $graph->img->SetMargin(75, 30, 30, 75);
    if ($xaxis == 'nproc') {
        $graph->SetScale("linlin");
        //$graph->xaxis->SetAutoMax(nprocs($system));
    } else {
        $graph->SetScale("textlin");
        $graph->xaxis->SetLabelAngle(90);
        $graph->xaxis->SetTickLabels($x);
    }
    $graph->xaxis->title->Set($xaxis);
    $graph->yaxis->title->Set($metric . units($metric));
    if ($metric == "walltime_acc" || $metric == "cpu_eff") {
        $graph->yscale->SetAutoMax(1.1);
    } elseif ($metric == "xfactor") {
        $graph->yscale->SetAutoMin(1.0);
    }
    if ($metric != "jobcount" && $metric != "cpuhours" && $metric != "backlog" && $metric != "xfactor" && $metric != "users" && $metric != "groups") {
        $maxbar = new BarPlot($max);
        $maxbar->SetWidth(1.0);
        $maxbar->SetFillColor("gray");
        $maxbar->SetLegend("Maximum");
        $graph->Add($maxbar);
    } else {
        if ($metric == "backlog") {
            $maxbar = new BarPlot($max);
            $maxbar->SetWidth(1.0);
            $maxbar->SetFillColor("gray");
            $maxbar->SetLegend("CPU Hours");
            $graph->Add($maxbar);
        }
    }
    $ybar = new BarPlot($y);
    $ybar->SetWidth(1.0);
    if ($metric != "jobcount" && $metric != "cpuhours" && $metric != "backlog" && $metric != "xfactor" && $metric != "users" && $metric != "groups") {
        $ybar->SetLegend("Mean");
    } else {
        if ($metric == "backlog") {
            $ybar->SetLegend("Queue Hours");
        }
    }
    $graph->Add($ybar);
    if ($metric != "jobcount" && $metric != "cpuhours" && $metric != "backlog" && $metric != "xfactor" && $metric != "users" && $metric != "groups") {
        $minbar = new BarPlot($min);
        $minbar->SetWidth(1.0);
        $minbar->SetFillColor("white");
        $minbar->SetLegend("Minimum");
        $graph->Add($minbar);
        $errbars = new ErrorPlot($ysigma);
        $errbars->SetColor("red");
        //$errbars->SetCenter();
        $errbars->SetWeight(2);
        $errbars->SetLegend("Std.Dev.");
        $graph->Add($errbars);
    }
    $graph->Stroke();
    $imgurl = $cache . rawurlencode($plot);
    echo "<img src=\"" . $imgurl . "\">\n";
}
Exemplo n.º 2
0
 /**
  * $past = '2009-12-24 16:49:00';
  * echo stamp($past);   
  *
  * @param unknown_type $past
  * @return unknown
  */
 function stamp($past)
 {
     date_default_timezone_set("America/New_York");
     $year = (int) substr($past, 0, 4);
     $month = (int) substr($past, 5, 2);
     $day = (int) substr($past, 8, 2);
     $hour = (int) substr($past, 11, 2);
     $minutes = (int) substr($past, 14, 2);
     $second = (int) substr($past, 17, 2);
     $past = mktime($hour, $minutes, $second, $month, $day, $year);
     $now = time();
     $diff = $now - $past;
     return '发表于' . units($diff) . '前';
 }