Exemplo n.º 1
0
    public function getDashboardHTML($width, $height, $add_integral = true)
    {
        $id = 'graphite-pie-' . str_replace('.', '', microtime(true));
        $legend_width = $width - 20;
        $graph_height = $height + 20;
        $show_labels = $this->show_labels ? 'true' : 'false';
        $show_legend = $this->show_legend ? 'true' : 'false';
        $sort_legend = $this->sort_legend ? 'true' : 'false';
        if ($this->is_ajax) {
            $time = GraphiteHelper::getTimeParam($this->time);
            $metrics = json_encode($this->metrics);
            $legend_keys = json_encode($this->legend_keys);
            $threshold = isset($this->threshold) ? $this->threshold : 'null';
            $formatted_colors = array();
            foreach ((array) $this->colors as $color_description) {
                $color = GraphFactory::getHexColorValue($color_description);
                $formatted_colors[] = "#{$color}";
            }
            $formatted_use_average = $this->use_average ? 'true' : 'false';
            $colors = json_encode($formatted_colors);
            return <<<EOF
            <div class='flotGraph graphite-pie'
                 data-time='{$time}'
                 data-metrics='{$metrics}'
                 data-legend-keys='{$legend_keys}'
                 data-colors='{$colors}'
                 data-show-labels='{$show_labels}'
                 data-show-legend='{$show_legend}'
                 data-sort-legend='{$sort_legend}'
                 data-threshold='{$threshold}'
                 data-use-average='{$formatted_use_average}'
            >
                <p class='html_title' style='width: {$legend_width}px;'>{$this->title}</p>
                <div class='flot-container' id='{$id}' style='height: {$graph_height}px; width: {$width}px;'></div>
            </div>
EOF;
        } else {
            $targets = array();
            foreach ($this->metrics as $metric) {
                $targets[] = $add_integral ? "integral({$metric})" : $metric;
            }
            $data = GraphiteHelper::fetchSplitRenderDataMultipleTargets($targets, $this->time);
            $json_data_values = array();
            foreach ($data as $index => $split_data) {
                if ($add_integral) {
                    $method = count($this->legend_keys) > $index ? $this->legend_keys[$index] : $this->metrics[$index];
                } else {
                    $header = $split_data[0];
                    list($method) = explode(',', $header);
                }
                if (count($split_data) > 1) {
                    $data_points = explode(',', $split_data[1]);
                    if ($add_integral) {
                        $total = GraphiteHelper::getMaxValue($data_points);
                    } else {
                        $total = GraphiteHelper::getTotal($data_points);
                    }
                    $json_data_values[] = '{label:"' . $method . '", data:' . $total . '}';
                }
            }
            $json_data = '[' . implode(',', $json_data_values) . ']';
            $threshold_setting = isset($this->threshold) ? "threshold: {$this->threshold}," : '';
            $stroke_setting = isset($this->bgColor) ? "stroke: { color: '{$this->bgColor}' }," : '';
            $formatted_colors = array();
            if ($this->colors) {
                foreach ($this->colors as $color_description) {
                    $color = GraphFactory::getHexColorValue($color_description);
                    $formatted_colors[] = "'#{$color}'";
                }
                $colors = 'colors: [' . implode(',', $formatted_colors) . '],';
            } else {
                $colors = '';
            }
            if ($this->show_legend && !$this->show_labels) {
                $label_formatter = <<<EOF
                    labelFormatter: function(label,data) {
                        return label + ' (' + (Math.round(data.percent*100)/100) + '%)';
                    },
EOF;
            } else {
                $label_formatter = "";
            }
            return <<<EOF
        <div class='flotGraph'>
            <p class='html_title' style='width: {$legend_width}px;'>{$this->title}</p>
            <div id='{$id}' style='height: {$graph_height}px; width: {$width}px;'></div>
            <script type="text/javascript">

                var data = {$json_data};
        jQuery.plot(jQuery("#{$id}"), data,
        {
                {$colors}
                series: {
                    pie: {
                        {$stroke_setting}
                        show: true,
                        label: {
                            show: {$show_labels}
                        },
                        combine: {
                            {$threshold_setting}
                            color: '#999'
                        }
                    }
                },
                legend: {
                    {$label_formatter}
                    show: {$show_legend}
                },
                grid: {
                    hoverable: true
                }
        });
        </script>
        </div>
EOF;
        }
    }
Exemplo n.º 2
0
<?php

require_once '../phplib/Dashboard.php';
$from = isset($_GET['from']) ? $_GET['from'] : '';
$max = isset($_GET['max']) ? (double) $_GET['max'] : null;
$numerators = isset($_GET['n']) ? (array) $_GET['n'] : array();
$denominators = isset($_GET['d']) ? (array) $_GET['d'] : array();
$graphite_results = GraphiteHelper::fetchKeyedSplitRenderDataMultipleTargets(array_merge($numerators, $denominators), $from);
$details = array();
$values = array();
$current_denominator = $denominators[0];
foreach ($numerators as $index => $numerator) {
    $denominator = isset($denominators[$index]) ? $denominators[$index] : $current_denominator;
    $numerator_value = isset($graphite_results[$numerator]) ? GraphiteHelper::getTotal($graphite_results[$numerator]) / count($graphite_results[$numerator]) : 0.0;
    $denominator_value = isset($graphite_results[$denominator]) ? GraphiteHelper::getTotal($graphite_results[$denominator]) / count($graphite_results[$denominator]) : 0.0;
    $value = $denominator_value != 0.0 ? $numerator_value / $denominator_value * 100 : 0.0;
    if ($max != null) {
        $value = min($value, $max);
    }
    $values[] = $value;
    $details[] = array($numerator_value, $denominator_value);
}
$result = array(array('measures' => $values, 'details' => $details, 'ranges' => array(100)));
header("Content-type: application/json");
print json_encode($result);
Exemplo n.º 3
0
<?php

require_once '../phplib/Dashboard.php';
$from = isset($_GET['from']) ? $_GET['from'] : '';
$targets = isset($_GET['t']) ? (array) $_GET['t'] : array();
$graphite_results = GraphiteHelper::fetchKeyedSplitRenderDataMultipleTargets($targets, $from);
$totals = array();
foreach ($targets as $target) {
    $value = isset($graphite_results[$target]) ? GraphiteHelper::getTotal($graphite_results[$target]) : 0.0;
    $totals[$target] = $value;
}
$result = array('totals' => $totals);
header("Content-type: application/json");
print json_encode($result);