summaryrefslogtreecommitdiffstats
path: root/library/Graphite/Web/Widget/GraphImage.php
blob: af64e69c8d39a1988625f9f9d3234c45a09f7644 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php

namespace Icinga\Module\Graphite\Web\Widget;

use Icinga\Module\Graphite\Graphing\Chart;
use Icinga\Web\Url;
use Icinga\Web\UrlParams;
use Icinga\Web\Widget\AbstractWidget;
use RuntimeException;

class GraphImage extends AbstractWidget
{
    /**
     * The chart to be rendered
     *
     * @var Chart
     */
    protected $chart;

    /**
     * The rendered PNG image
     *
     * @var string|null
     */
    protected $rendered;

    /**
     * Constructor
     *
     * @param   Chart   $chart  The chart to be rendered
     */
    public function __construct(Chart $chart)
    {
        $this->chart = $chart;
    }

    /**
     * Render the graph lazily
     *
     * @return string
     */
    public function render()
    {
        if ($this->rendered === null) {
            $now = time();

            $from = (int) $this->chart->getFrom();
            if ($from < 0) {
                $from += $now;
            }

            $until = (string) $this->chart->getUntil();

            if ($until === '') {
                $until = $now;
            } else {
                $until = (int) $until;
                if ($until < 0) {
                    $until += $now;
                }
            }

            $variables = $this->chart->getMetricVariables();
            $template = $this->chart->getTemplate();
            $graphiteWebClient = $this->chart->getGraphiteWebClient();
            $params = (new UrlParams())->addValues([
                'from'                  => $from,
                'until'                 => $until,
                'bgcolor'               => $this->chart->getBackgroundColor() ?? 'black',
                'fgcolor'               => $this->chart->getForegroundColor() ?? 'white',
                'majorGridLineColor'    => $this->chart->getMajorGridLineColor() ?? '0000003F',
                'minorGridLineColor'    => $this->chart->getMinorGridLineColor() ?? 'black',
                'width'                 => $this->chart->getWidth(),
                'height'                => $this->chart->getHeight(),
                'hideLegend'            => (string) ! $this->chart->getShowLegend(),
                'tz'                    => date_default_timezone_get(),
                '_salt'                 => "$now.000",
                'vTitle'                => 'Percent',
                'lineMode'              => 'connected',
                'drawNullAsZero'        => 'false',
                'graphType'             => 'line',
                '_ext'                  => 'whatever.svg'
            ]);

            foreach ($template->getUrlParams() as $key => $value) {
                $params->set($key, $value->resolve($variables));
            }

            $metrics = $this->chart->getMetrics();
            $allVars = [];

            foreach ($template->getCurves() as $curveName => $curve) {
                if (!isset($metrics[$curveName])) {
                    continue;
                }

                $vars = $curve[0]->reverseResolve($metrics[$curveName]);

                if ($vars !== false) {
                    $allVars = array_merge($allVars, $vars);
                }
            }

            foreach ($metrics as $curveName => $metric) {
                $allVars['metric'] = $metric;
                $params->add('target', $template->getCurves()[$curveName][1]->resolve($allVars));
            }

            $url = Url::fromPath('/render')->setParams($params);
            $headers = [
                'Accept-language'   => 'en',
                'Content-type'      => 'application/x-www-form-urlencoded'
            ];

            for (;;) {
                try {
                    $this->rendered = $graphiteWebClient->request($url, 'GET', $headers);
                } catch (RuntimeException $e) {
                    if (preg_match('/\b500\b/', $e->getMessage())) {
                        // A 500 Internal Server Error, probably because of
                        // a division by zero because of a too low time range to render.

                        $until = (int) $url->getParam('until');
                        $diff = $until - (int) $url->getParam('from');

                        // Try to render a higher time range, but give up
                        // once our default (1h) has been reached (non successfully).
                        if ($diff < 3600) {
                            $url->setParam('from', $until - $diff * 2);
                            continue;
                        }
                    }

                    throw $e;
                }

                break;
            }
        }

        return $this->rendered;
    }
}