histogram: Make histograms crash less
[ninja.git] / modules / reports / controllers / histogram.php
blobc6c2a16523fd528163e0a074618d0205c32c2054
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 private $labels = array();
17 public $type = 'histogram';
19 /**
20 * Setup options for histogram report
22 public function index($input = false)
24 $this->setup_options_obj($input);
26 $this->template->disable_refresh = true;
27 $this->template->content = $this->add_view('reports/setup');
28 $template = $this->template->content;
29 if(isset($_SESSION['report_err_msg'])) {
30 $template->error_msg = $_SESSION['report_err_msg'];
31 unset($_SESSION['report_err_msg']);
34 $template->saved_reports = $this->options->get_all_saved();
35 $scheduled_info = false;
36 if ($this->options['report_id']) {
37 $scheduled_info = Scheduled_reports_Model::report_is_scheduled($this->type, $this->options['report_id']);
39 $template->scheduled_info = $scheduled_info;
40 $template->report_options = $this->add_view('histogram/options');
42 $this->xtra_js[] = 'application/media/js/jquery.datePicker.js';
43 $this->xtra_js[] = 'application/media/js/jquery.timePicker.js';
44 $this->xtra_js[] = $this->add_path('reports/js/common.js');
46 $this->xtra_css[] = $this->add_path('reports/css/datePicker.css');
48 $this->js_strings .= reports::js_strings();
49 $this->js_strings .= "var _reports_error = '"._('Error')."';\n";
51 $this->template->inline_js = $this->inline_js;
52 $this->template->js_strings = $this->js_strings;
53 $this->template->title = _('Reporting » Histogram » Setup');
56 /**
57 * Generate the event history report
59 public function generate($input = false)
61 $this->setup_options_obj($input);
62 $this->template->disable_refresh = true;
63 $this->xtra_js[] = 'application/media/js/jquery.flot.min.js';
64 $this->xtra_js[] = 'application/media/js/jquery.datePicker.js';
65 $this->xtra_js[] = 'application/media/js/jquery.timePicker.js';
66 $this->xtra_js[] = 'application/media/js/excanvas.compiled.js';
67 $this->xtra_js[] = $this->add_path('reports/js/common.js');
68 $this->xtra_js[] = ninja::add_path('histogram/js/histogram.js', 'reports');
69 $this->xtra_css[] = $this->add_path('reports/css/datePicker.css');
70 $rpt = new Summary_Reports_Model($this->options);
72 $title = _('Alert histogram');
74 $breakdown_keys = false;
75 switch ($this->options['breakdown']) {
76 case 'monthly':
77 for ($i = 1;$i<=12;$i++) $breakdown_keys[] = $i;
78 break;
79 case 'dayofmonth':
80 # build day numbers 1-31 (always 31 slots for each month as in histogram.c)
81 for ($i = 1;$i<=31;$i++) $breakdown_keys[] = $i;
82 break;
83 case 'dayofweek':
84 # using integer equivalent to date('N')
85 $breakdown_keys = array(1, 2, 3, 4, 5, 6, 7);
86 break;
87 case 'hourly':
88 # build hour strings like '00', '01' etc
89 for ($i=0;$i<=24;$i++) $breakdown_keys[] = substr('00'.$i, -2);
90 break;
92 $histogram_data = $rpt->histogram($breakdown_keys);
94 # pull the data from the returned array
95 $data = isset($histogram_data['data']) ? $histogram_data['data'] : array();
96 $min = isset($histogram_data['min']) ? $histogram_data['min'] : array();
97 $max = isset($histogram_data['max']) ? $histogram_data['max'] : array();
98 $avg = isset($histogram_data['avg']) ? $histogram_data['avg'] : array();
99 $sum = isset($histogram_data['sum']) ? $histogram_data['sum'] : array();
101 $sub_type = false;
102 $is_group = false;
103 switch ($this->options['report_type']) {
104 case 'hostgroups':
105 $is_group = true;
106 case 'hosts':
107 $state_names = array(
108 Reports_Model::HOST_UP => _('UP'),
109 Reports_Model::HOST_DOWN => _('DOWN'),
110 Reports_Model::HOST_UNREACHABLE => _('UNREACHABLE')
112 $sub_type = 'host';
113 break;
114 case 'servicegroups':
115 $is_group = true;
116 case 'services':
117 $state_names = array(
118 Reports_Model::SERVICE_OK => _('OK'),
119 Reports_Model::SERVICE_WARNING => _('WARNING'),
120 Reports_Model::SERVICE_CRITICAL => _('CRITICAL'),
121 Reports_Model::SERVICE_UNKNOWN => _('UNKNOWN')
123 $sub_type = 'service';
124 break;
127 $report_members = $this->options->get_report_members();
128 if (empty($report_members)) {
129 if (!$is_group)
130 $_SESSION['report_err_msg'] = _("You didn't select any objects to include in the report");
131 else
132 $_SESSION['report_err_msg'] = sprintf(_("The groups you selected (%s) had no members, so cannot create a report from them"), implode(', ', $this->options['objects']));
133 return url::redirect(Router::$controller.'/index?' . http_build_query($this->options->options));
136 $this->js_strings .= "var graph_options = {legend: {show: true,container: $('#overviewLegend')},xaxis:{ticks:".json_encode($this->_get_xaxis_ticks($data))."},bars:{align:'center'}, grid: { hoverable: true, clickable: true }, yaxis:{min:0}};\n";
137 $this->js_strings .= "var graph_xlables = ".json_encode($this->labels).";\n";
139 $this->js_strings .= reports::js_strings();
141 $data = $this->_prepare_graph_data($data);
142 $datasets = array();
144 $states = array_keys($state_names);
145 foreach ($data as $key => $val) {
146 $datasets[ucfirst(strtolower($state_names[$key]))] = array('label' => ucfirst(strtolower($state_names[$key])), 'data' => $val, 'color' => reports::_state_colors($sub_type, $states[$key]), 'bars' => array('show' => true));
149 $this->js_strings .= 'var datasets = '.json_encode($datasets).";\n";
151 $this->template->content = $this->add_view('reports/index');
152 $base = $this->template->content;
154 $base->header = $this->add_view('reports/header');
155 $base->header->title = $title;
156 $base->header->report_time_formatted = $this->format_report_time(nagstat::date_format());
157 $base->header->skip_csv = true;
158 $base->header->skip_pdf = true;
160 $base->content = $this->add_view("histogram/histogram");
161 $content = $base->content;
163 $content->min = $min;
164 $content->max = $max;
165 $content->avg = $avg;
166 $content->sum = $sum;
167 $content->states = $state_names;
168 $content->available_states = array_keys($min);
169 $content->objects = $this->options['objects'];
170 $timeformat_str = nagstat::date_format();
171 $content->report_time = date($timeformat_str, $this->options['start_time']).' '._('to').' '.date($timeformat_str, $this->options['end_time']);
173 $this->template->content->report_options = $this->add_view('histogram/options');
174 $tpl_options = $this->template->content->report_options;
176 $this->template->inline_js = $this->inline_js;
177 $this->template->js_strings = $this->js_strings;
178 $this->template->title = _('Reporting » Histogram » Report');
182 * Replace all integer indicies with proper
183 * translated strings
185 private function _get_xaxis_ticks($data)
187 $return = false;
188 $i = 0;
189 foreach ($data as $key => $values) {
190 switch ($this->options['breakdown']) {
191 case 'dayofmonth':
192 $return[] = array($i, $key);
193 $this->labels[] = $key;
194 break;
195 case 'monthly':
196 $return[] = array($i, $key);
197 $this->labels[] = "'".$key."'";
198 break;
199 case 'dayofweek':
200 $return[] = array($i, $key);
201 $this->labels[] = $key;
202 break;
203 case 'hourly':
204 $return[] = array($i, $key.':00');
205 $this->labels[] = $key.':00';
206 break;
208 $i++;
210 return $return;
214 * Prepare data structore for use in histogram
216 private function _prepare_graph_data($data)
218 $return = array();
219 $i = 0; # graph data needs to have 0 indicies
220 foreach ($data as $key => $value) {
221 foreach ($value as $k => $v) {
222 $return[$k][] = array($i, $v);
224 $i++;
226 return $return;
230 * Translated helptexts for this controller
232 public static function _helptexts($id)
234 # Tag unfinished helptexts with @@@HELPTEXT:<key> to make it
235 # easier to find those later
236 $helptexts = array();
237 if (array_key_exists($id, $helptexts)) {
238 echo $helptexts[$id];
239 } else
240 return parent::_helptexts($id);