Merge "Make it possible to sort on simple custom columns"
[ninja.git] / modules / reports / controllers / histogram.php
blob7fadd1c48fb86b5c927be63a403aa375b711050e
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
2 /**
3 * Histogram controller
4 * Requires authentication
6 * op5, and the op5 logo are trademarks, servicemarks, registered servicemarks
7 * or registered trademarks of op5 AB.
8 * All other trademarks, servicemarks, registered trademarks, and registered
9 * servicemarks mentioned herein may be the property of their respective owner(s).
10 * The information contained herein is provided AS IS with NO WARRANTY OF ANY
11 * KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY, AND FITNESS FOR A
12 * PARTICULAR PURPOSE.
14 class Histogram_Controller extends Base_reports_Controller
16 public $data = false; /**< Awesomely named variable for passing the result of the histogram method in the summary report model */
17 private $labels = array();
18 public $type = 'histogram';
20 /**
21 * Setup options for histogram report
23 public function index($input = false)
25 $this->setup_options_obj($input);
27 $this->template->disable_refresh = true;
28 $this->template->content = $this->add_view('histogram/setup');
29 $template = $this->template->content;
31 $this->template->js_header = $this->add_view('js_header');
32 $this->xtra_js[] = 'application/media/js/jquery.datePicker.js';
33 $this->xtra_js[] = 'application/media/js/jquery.timePicker.js';
34 $this->xtra_js[] = $this->add_path('reports/js/common.js');
35 $this->xtra_js[] = $this->add_path('histogram/js/histogram.js');
37 $this->template->css_header = $this->add_view('css_header');
38 $this->xtra_css[] = $this->add_path('reports/css/datePicker.css');
39 $this->xtra_css[] = $this->add_path('histogram/css/histogram.css');
40 $this->template->css_header->css = $this->xtra_css;
42 $this->js_strings .= reports::js_strings();
43 $this->js_strings .= "var _reports_error = '"._('Error')."';\n";
45 $this->template->inline_js = $this->inline_js;
46 $this->template->js_strings = $this->js_strings;
47 $this->template->title = _('Reporting » Histogram » Setup');
50 /**
51 * Generate the event history report
53 public function generate($input = false)
55 $this->setup_options_obj($input);
56 $this->template->disable_refresh = true;
57 $this->template->js_header = $this->add_view('js_header');
58 $this->template->css_header = $this->add_view('css_header');
59 $this->xtra_js[] = 'application/media/js/jquery.flot.min.js';
60 $this->xtra_js[] = 'application/media/js/jquery.datePicker.js';
61 $this->xtra_js[] = 'application/media/js/jquery.timePicker.js';
62 $this->xtra_js[] = $this->add_path('reports/js/common.js');
63 $this->xtra_js[] = $this->add_path('histogram/js/histogram.js');
64 $this->xtra_css[] = $this->add_path('reports/css/datePicker.css');
65 $this->xtra_css[] = $this->add_path('histogram/css/histogram.css');
66 $this->template->css_header->css = $this->xtra_css;
67 $rpt = new Summary_Reports_Model($this->options);
69 $hostgroup = false;
70 $hostname = false;
71 $servicegroup = false;
72 $service = false;
74 $group_name = false;
75 $title = _('Event history for ');
76 $objects = false;
77 switch ($this->options['report_type']) {
78 case 'hostgroups':
79 $sub_type = "host";
80 $hostgroup = $this->options['hostgroup'];
81 $group_name = $hostgroup;
82 $title .= _('Hostgroup(s): ');
83 $this->object_varname = 'host_name';
84 $objects = $this->options['hostgroup'];
85 break;
86 case 'servicegroups':
87 $sub_type = "service";
88 $servicegroup = $this->options['servicegroup'];
89 $group_name = $servicegroup;
90 $title .= _('Servicegroup(s): ');
91 $this->object_varname = 'service_description';
92 $objects = $this->options['servicegroup'];
93 break;
94 case 'hosts':
95 $sub_type = "host";
96 $hostname = $this->options['host_name'];
97 $title .= _('Host(s): ');
98 $this->object_varname = 'host_name';
99 if (is_array($this->options['host_name'])) {
100 $objects = $this->options['host_name'];
101 } else {
102 $objects[] = $this->options['host_name'];
104 break;
105 case 'services':
106 $sub_type = "service";
107 $service = $this->options['service_description'];
108 $title .= _('Service(s): ');
109 $tmp_obj = false;
110 if (is_array($service)) {
111 foreach ($service as $s) {
112 if (strstr($s, ';')) {
113 $tmp = explode(';', $s);
114 $tmp_obj[] = "'".$tmp[1]."' "._('On Host')." '".$tmp[0]."' ";
115 } else {
116 $tmp_obj[] = "'".$s."' "._('On Host')." '".$this->options['host_name']."' ";
119 if (!empty($tmp_obj)) {
120 $objects = $tmp_obj;
122 } else {
123 if (strstr($service, ';')) {
124 $tmp = explode(';', $service);
125 $objects[] = "'".$tmp[1]."' "._('On Host')." '".$tmp[0]."' ";
126 } else {
127 $objects[] = "'".$service."' "._('On Host')." '".$this->options['host_name']."' ";
130 $this->object_varname = 'service_description';
131 break;
132 default:
133 return url::redirect(Router::$controller.'/index');
136 $breakdown_keys = false;
137 switch ($this->options['breakdown']) {
138 case 'monthly':
139 for ($i = 1;$i<=12;$i++) $breakdown_keys[] = $i;
140 break;
141 case 'dayofmonth':
142 # build day numbers 1-31 (always 31 slots for each month as in histogram.c)
143 for ($i = 1;$i<=31;$i++) $breakdown_keys[] = $i;
144 break;
145 case 'dayofweek':
146 # using integer equivalent to date('N')
147 $breakdown_keys = array(1, 2, 3, 4, 5, 6, 7);
148 break;
149 case 'hourly':
150 # build hour strings like '00', '01' etc
151 for ($i=0;$i<=24;$i++) $breakdown_keys[] = substr('00'.$i, -2);
152 break;
154 $histogram_data = $rpt->histogram($breakdown_keys);
156 $min = false;
157 $max = false;
158 $avg = false;
159 $sum = false;
161 if (!empty($histogram_data)) {
162 # pull the data from the returned array
163 $this->data = isset($histogram_data['data']) ? $histogram_data['data'] : false;
164 $min = isset($histogram_data['min']) ? $histogram_data['min'] : false;
165 $max = isset($histogram_data['max']) ? $histogram_data['max'] : false;
166 $avg = isset($histogram_data['avg']) ? $histogram_data['avg'] : false;
167 $sum = isset($histogram_data['sum']) ? $histogram_data['sum'] : false;
170 $sub_type = false;
171 switch ($this->options['report_type']) {
172 case 'hosts': case 'hostgroups':
173 $state_names = array(
174 Reports_Model::HOST_UP => _('UP'),
175 Reports_Model::HOST_DOWN => _('DOWN'),
176 Reports_Model::HOST_UNREACHABLE => _('UNREACHABLE')
178 $sub_type = 'host';
179 break;
180 case 'services': case 'servicegroups':
181 $state_names = array(
182 Reports_Model::SERVICE_OK => _('OK'),
183 Reports_Model::SERVICE_WARNING => _('WARNING'),
184 Reports_Model::SERVICE_CRITICAL => _('CRITICAL'),
185 Reports_Model::SERVICE_UNKNOWN => _('UNKNOWN')
187 $sub_type = 'service';
188 break;
191 $this->inline_js .= "var graph_options = {legend: {show: true,container: $('#overviewLegend')},xaxis:{ticks:".$this->_get_xaxis_ticks()."},bars:{align:'center'}, grid: { hoverable: true, clickable: true }, yaxis:{min:0}};";
192 $this->js_strings .= "var graph_xlables = new Array(".implode(',', $this->labels).");";
194 $this->js_strings .= reports::js_strings();
196 $data = $this->_prepare_graph_data();
197 $datasets = array();
198 $this->inline_js .= "var datasets = {";
200 $states = array_keys($state_names);
201 foreach ($data as $key => $val) {
202 $datasets[] = "'".ucfirst(strtolower($state_names[$key]))."': {label: '".ucfirst(strtolower($state_names[$key]))."', data: [".implode(',', $val)."], color:'".reports::_state_colors($sub_type, $states[$key])."', bars: { show: true}}";
205 $this->inline_js .= implode(',', $datasets).'};';
207 $this->inline_js .= "var choiceContainer = $('#choices');
208 $.each(datasets, function(key, val) {
209 choiceContainer.append('<br/><input type=\"checkbox\" name=\"' + key +
210 '\" checked=\"checked\" id=\"id' + key + '\">' +
211 '<label for=\"id' + key + '\">'
212 + val.label + '</label>');
214 choiceContainer.find(\"input\").click(plotAccordingToChoices);
216 function plotAccordingToChoices() {
217 var data = [];
219 choiceContainer.find(\"input:checked\").each(function () {
220 var key = $(this).attr(\"name\");
221 if (key && datasets[key])
222 data.push(datasets[key]);
225 if (data.length > 0)
226 $.plot($('#histogram_graph'), data, graph_options);
229 plotAccordingToChoices();";
231 $this->template->content = $this->add_view('histogram/index');
233 $base = $this->template->content;
234 $base->content = $this->add_view("histogram/histogram");
235 $content = $base->content;
236 $content->state_names = $state_names;
238 $content->min = $min;
239 $content->max = $max;
240 $content->avg = $avg;
241 $content->sum = $sum;
242 $content->states = $state_names;
243 $content->available_states = array_keys($min);
244 $content->title = $title;
245 $content->objects = $objects;
246 $timeformat_str = nagstat::date_format();
247 $content->report_time = date($timeformat_str, $this->options['start_time']).' '._('to').' '.date($timeformat_str, $this->options['end_time']);
249 $this->template->content->report_options = $this->add_view('histogram/options');
250 $tpl_options = $this->template->content->report_options;
252 $tpl_options->sub_type = $sub_type;
254 $this->template->inline_js = $this->inline_js;
255 $this->template->js_strings = $this->js_strings;
256 $this->template->title = _('Reporting » Histogram » Report');
260 * Replace all integer indicies with proper
261 * translated strings
263 public function _get_xaxis_ticks()
265 if (empty($this->data)) {
266 return false;
269 $return = false;
270 $i = 0;
271 foreach ($this->data as $key => $data) {
272 switch ($this->options['breakdown']) {
273 case 'dayofmonth':
274 $return[] = '['.$i.', '.$key.']';
275 $this->labels[] = "'".$key."'";
276 break;
277 case 'monthly':
278 $return[] = '['.$i.', "'.$key.'"]';
279 $this->labels[] = "'".$key."'";
280 break;
281 case 'dayofweek':
282 $return[] = '['.$i.', "'.$key.'"]';
283 $this->labels[] = "'".$key."'";
284 break;
285 case 'hourly':
286 $return[] = '['.$i.', "'.$key.':00'.'"]';
287 $this->labels[] = "'".$key.':00'."'";
288 break;
290 $i++;
292 return '['.implode(',', $return).']';
296 * Prepare data structore for use in histogram
298 public function _prepare_graph_data($data=false)
300 if (empty($this->data)) {
301 return false;
304 $return = false;
305 $i = 0; # graph data needs to have 0 indicies
306 foreach ($this->data as $key => $data) {
307 foreach ($data as $k => $v) {
308 $return[$k][] = '['.$i.','.$v.']';
310 $i++;
312 return $return;
316 * Translated helptexts for this controller
318 public static function _helptexts($id)
320 # Tag unfinished helptexts with @@@HELPTEXT:<key> to make it
321 # easier to find those later
322 $helptexts = array();
323 if (array_key_exists($id, $helptexts)) {
324 echo $helptexts[$id];
325 } else
326 return parent::_helptexts($id);