Added filterable to summary and histogram controllers
[ninja.git] / modules / reports / controllers / histogram.php
blob4e8e28fd68c26d973305b86521b06b2ac11dab70
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->template->js[] = 'application/media/js/jquery.datePicker.js';
43 $this->template->js[] = 'application/media/js/jquery.timePicker.js';
44 $this->template->js[] = $this->add_path('reports/js/common.js');
45 $this->template->js[] = 'application/media/js/lib.set.js';
46 $this->template->js[] = 'application/media/js/jquery.filterable.js';
48 $this->template->css[] = 'application/media/css/jquery.filterable.css';
49 $this->template->css[] = $this->add_path('reports/css/datePicker.css');
51 $this->js_strings .= reports::js_strings();
52 $this->js_strings .= "var _reports_error = '"._('Error')."';\n";
54 $this->template->inline_js = $this->inline_js;
55 $this->template->js_strings = $this->js_strings;
56 $this->template->title = _('Reporting » Histogram » Setup');
59 /**
60 * Generate the event history report
62 public function generate($input = false)
64 $this->setup_options_obj($input);
65 $this->template->disable_refresh = true;
66 $this->template->js[] = 'application/media/js/jquery.flot.min.js';
67 $this->template->js[] = 'application/media/js/jquery.datePicker.js';
68 $this->template->js[] = 'application/media/js/jquery.timePicker.js';
69 $this->template->js[] = 'application/media/js/excanvas.compiled.js';
70 $this->template->js[] = $this->add_path('reports/js/common.js');
71 $this->template->js[] = ninja::add_path('histogram/js/histogram.js', 'reports');
72 $this->template->css[] = $this->add_path('reports/css/datePicker.css');
73 $rpt = new Summary_Reports_Model($this->options);
75 $title = _('Alert histogram');
77 $breakdown_keys = false;
78 switch ($this->options['breakdown']) {
79 case 'monthly':
80 for ($i = 1;$i<=12;$i++) $breakdown_keys[] = $i;
81 break;
82 case 'dayofmonth':
83 # build day numbers 1-31 (always 31 slots for each month as in histogram.c)
84 for ($i = 1;$i<=31;$i++) $breakdown_keys[] = $i;
85 break;
86 case 'dayofweek':
87 # using integer equivalent to date('N')
88 $breakdown_keys = array(1, 2, 3, 4, 5, 6, 7);
89 break;
90 case 'hourly':
91 # build hour strings like '00', '01' etc
92 for ($i=0;$i<=24;$i++) $breakdown_keys[] = substr('00'.$i, -2);
93 break;
95 $histogram_data = $rpt->histogram($breakdown_keys);
97 # pull the data from the returned array
98 $data = isset($histogram_data['data']) ? $histogram_data['data'] : array();
99 $min = isset($histogram_data['min']) ? $histogram_data['min'] : array();
100 $max = isset($histogram_data['max']) ? $histogram_data['max'] : array();
101 $avg = isset($histogram_data['avg']) ? $histogram_data['avg'] : array();
102 $sum = isset($histogram_data['sum']) ? $histogram_data['sum'] : array();
104 $sub_type = false;
105 $is_group = false;
106 switch ($this->options['report_type']) {
107 case 'hostgroups':
108 $is_group = true;
109 case 'hosts':
110 $state_names = array(
111 Reports_Model::HOST_UP => _('UP'),
112 Reports_Model::HOST_DOWN => _('DOWN'),
113 Reports_Model::HOST_UNREACHABLE => _('UNREACHABLE')
115 $sub_type = 'host';
116 break;
117 case 'servicegroups':
118 $is_group = true;
119 case 'services':
120 $state_names = array(
121 Reports_Model::SERVICE_OK => _('OK'),
122 Reports_Model::SERVICE_WARNING => _('WARNING'),
123 Reports_Model::SERVICE_CRITICAL => _('CRITICAL'),
124 Reports_Model::SERVICE_UNKNOWN => _('UNKNOWN')
126 $sub_type = 'service';
127 break;
130 $report_members = $this->options->get_report_members();
131 if (empty($report_members)) {
132 if (!$is_group)
133 $_SESSION['report_err_msg'] = _("You didn't select any objects to include in the report");
134 else
135 $_SESSION['report_err_msg'] = sprintf(_("The groups you selected (%s) had no members, so cannot create a report from them"), implode(', ', $this->options['objects']));
136 return url::redirect(Router::$controller.'/index?' . http_build_query($this->options->options));
139 $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";
140 $this->js_strings .= "var graph_xlables = ".json_encode($this->labels).";\n";
142 $this->js_strings .= reports::js_strings();
144 $data = $this->_prepare_graph_data($data);
145 $datasets = array();
147 $states = array_keys($state_names);
148 foreach ($data as $key => $val) {
149 $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));
152 $this->js_strings .= 'var datasets = '.json_encode($datasets).";\n";
154 $this->template->content = $this->add_view('reports/index');
155 $base = $this->template->content;
157 $base->header = $this->add_view('reports/header');
158 $base->header->title = $title;
159 $base->header->report_time_formatted = $this->format_report_time(nagstat::date_format());
160 $base->header->skip_csv = true;
161 $base->header->skip_pdf = true;
163 $base->content = $this->add_view("histogram/histogram");
164 $content = $base->content;
166 $content->min = $min;
167 $content->max = $max;
168 $content->avg = $avg;
169 $content->sum = $sum;
170 $content->states = $state_names;
171 $content->available_states = array_keys($min);
172 $content->objects = $this->options['objects'];
173 $timeformat_str = nagstat::date_format();
174 $content->report_time = date($timeformat_str, $this->options['start_time']).' '._('to').' '.date($timeformat_str, $this->options['end_time']);
176 $this->template->content->report_options = $this->add_view('histogram/options');
177 $tpl_options = $this->template->content->report_options;
179 $this->template->inline_js = $this->inline_js;
180 $this->template->js_strings = $this->js_strings;
181 $this->template->title = _('Reporting » Histogram » Report');
185 * Replace all integer indicies with proper
186 * translated strings
188 private function _get_xaxis_ticks($data)
190 $return = false;
191 $i = 0;
192 foreach ($data as $key => $values) {
193 switch ($this->options['breakdown']) {
194 case 'dayofmonth':
195 $return[] = array($i, $key);
196 $this->labels[] = $key;
197 break;
198 case 'monthly':
199 $return[] = array($i, $key);
200 $this->labels[] = "'".$key."'";
201 break;
202 case 'dayofweek':
203 $return[] = array($i, $key);
204 $this->labels[] = $key;
205 break;
206 case 'hourly':
207 $return[] = array($i, $key.':00');
208 $this->labels[] = $key.':00';
209 break;
211 $i++;
213 return $return;
217 * Prepare data structore for use in histogram
219 private function _prepare_graph_data($data)
221 $return = array();
222 $i = 0; # graph data needs to have 0 indicies
223 foreach ($data as $key => $value) {
224 foreach ($value as $k => $v) {
225 $return[$k][] = array($i, $v);
227 $i++;
229 return $return;
233 * Translated helptexts for this controller
235 public static function _helptexts($id)
237 # Tag unfinished helptexts with @@@HELPTEXT:<key> to make it
238 # easier to find those later
239 $helptexts = array();
240 if (array_key_exists($id, $helptexts)) {
241 echo $helptexts[$id];
242 } else
243 return parent::_helptexts($id);