1 <?php
defined('SYSPATH') OR die('No direct access allowed.');
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
14 class Histogram_Controller
extends Base_reports_Controller
16 private $labels = array();
17 public $type = 'histogram';
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');
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']) {
80 for ($i = 1;$i<=12;$i++
) $breakdown_keys[] = $i;
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;
87 # using integer equivalent to date('N')
88 $breakdown_keys = array(1, 2, 3, 4, 5, 6, 7);
91 # build hour strings like '00', '01' etc
92 for ($i=0;$i<=24;$i++
) $breakdown_keys[] = substr('00'.$i, -2);
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();
106 switch ($this->options
['report_type']) {
110 $state_names = array(
111 Reports_Model
::HOST_UP
=> _('UP'),
112 Reports_Model
::HOST_DOWN
=> _('DOWN'),
113 Reports_Model
::HOST_UNREACHABLE
=> _('UNREACHABLE')
117 case 'servicegroups':
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';
130 $report_members = $this->options
->get_report_members();
131 if (empty($report_members)) {
133 $_SESSION['report_err_msg'] = _("You didn't select any objects to include in the report");
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);
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
188 private function _get_xaxis_ticks($data)
192 foreach ($data as $key => $values) {
193 switch ($this->options
['breakdown']) {
195 $return[] = array($i, $key);
196 $this->labels
[] = $key;
199 $return[] = array($i, $key);
200 $this->labels
[] = "'".$key."'";
203 $return[] = array($i, $key);
204 $this->labels
[] = $key;
207 $return[] = array($i, $key.':00');
208 $this->labels
[] = $key.':00';
217 * Prepare data structore for use in histogram
219 private function _prepare_graph_data($data)
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);
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];
243 return parent
::_helptexts($id);