Added filterable to summary and histogram controllers
[ninja.git] / modules / reports / libraries / HttpApiEvent_options.php
blobe1428eb94becf6897e0910fe46204ccd8ca0edef
1 <?php defined('SYSPATH') OR die('No direct access allowed.');
3 /**
4 * The report options for the Event type of reports in the HTTP API
5 */
6 class HttpApiEvent_options extends Summary_options {
7 public static $type = 'httpapievent';
9 const MAX_EVENTS = 10000; /**< Pagination limit for events retrieved from HTTP API. Hardcoded, deal with it */
11 private $limit;
13 public function setup_properties()
15 parent::setup_properties();
16 $this->properties = array_intersect_key(
17 $this->properties,
18 array_flip(array(
19 'report_period',
20 'alert_types',
21 'state_types',
22 'host_states',
23 'service_states',
24 'start_time',
25 'end_time',
26 'include_comments',
27 'objects',
28 'report_type',
31 $this->properties['include_comments'] = array(
32 'type' => 'bool',
33 'default' => false,
34 'description' => "Include events' comments"
36 $this->properties['alert_types']['options'] = array(
37 1 => 'host',
38 2 => 'service',
39 3 => 'both',
41 $this->properties['state_types']['options'] = array(
42 1 => 'soft',
43 2 => 'hard',
44 3 => 'both',
46 $this->properties['host_states']['options'] = array(
47 7 => 'all',
48 6 => 'problem',
49 1 => 'up',
50 2 => 'down',
51 4 => 'unreachable',
54 $this->properties['service_states']['options'] = array(
55 15 => 'all',
56 14 => 'problem',
57 1 => 'ok',
58 2 => 'warning',
59 4 => 'critical',
60 8 => 'unknown',
63 $this->properties['report_period']['default'] = 'custom';
64 $this->properties['report_period']['options'] = array_combine(
65 array_keys($this->properties['report_period']['options']),
66 array_keys($this->properties['report_period']['options']));
68 $limit = $this->limit = (int) Op5Config::instance()->getConfig('http_api.report.limit');
69 if($limit > self::MAX_EVENTS || $limit < 1) {
70 $this->limit = self::MAX_EVENTS;
71 $limit = $this->limit."; you can decrease this value in http_api.yml";
72 } else {
73 $limit .= "; you can increase this value in http_api.yml";
75 $this->properties['limit'] = array(
76 'type' => 'int',
77 'default' => $this->limit,
78 'description' => 'Include at most this many events (between 1 and '.$limit.')'
80 $this->properties['offset'] = array(
81 'type' => 'int',
82 'default' => 0,
83 'description' => 'Skip the first <em>offset</em> events matching the rest of the query, well suited for pagination'
86 foreach (array('host_name', 'service_description', 'hostgroup', 'servicegroup') as $objtype) {
87 $this->properties[$objtype] = $this->properties['objects'];
88 $type = explode('_', $objtype);
89 $this->properties[$objtype]['description'] = ucfirst($type[0]).'s to include (note: array)';
91 $this->properties['objects']['generated'] = true;
92 $this->properties['objects']['default'] = Report_options::ALL_AUTHORIZED;
93 $this->properties['report_type']['generated'] = true;
94 $this->properties['report_type']['default'] = 'hosts';
97 /**
98 * @param $value mixed
99 * @param $type string
100 * @return string
102 function format_default($value, $type)
104 if($type == 'bool') {
105 return (int) $value;
107 if($type == 'array' || $type == 'objsel') {
108 if(empty($value)) {
109 return "[empty]";
111 return implode(", ", $value);
113 if($type == 'string' && !$value) {
114 return '[empty]';
116 if($type == 'enum') {
117 return "'$value'";
119 if($type == 'int' && empty($value) && $value !== 0) {
120 return "[empty]";
122 return (string) $value;
126 * Not as forgiving as the parent. (Why is parent forgiving?)
128 * @param $options array
129 * @throws Api_Error_Response
131 function set_options($options) {
132 foreach($options as $name => $value) {
133 if(!$this->set($name, $value)) {
134 throw new Api_Error_Response("Invalid value for option '$name'", 400);
141 * Final step in the "from merlin.report_data row to API-output" process
143 * @param $row array
144 * @return array
146 function to_output($row)
148 // transform values
149 $type = $row['service_description'] ? 'service' : 'host';
150 $row['event_type'] = Reports_Model::event_type_to_string($row['event_type'], $type, true);
151 $row['state'] = strtolower(Current_status_Model::status_text($row['state'], true, $type));
153 // rename properties
154 $row['in_scheduled_downtime'] = null;
155 unset($row['downtime_depth']);
156 if(isset($row['username'])) {
157 // comments are included and we've got one of them!
158 // let's produce some hierarchy
159 $row['comment'] = array(
160 'username' => $row['username'],
161 'comment' => $row['user_comment'],
162 'timestamp' => $row['comment_timestamp'],
165 unset($row['username'], $row['user_comment'], $row['comment_timestamp']);
167 return $row;
171 * @todo be able to throw exceptions here to give feedback of
172 * *which* error we experienced, since, you know, there's at
173 * least one user (you) exposed to this API.. Help yourself
175 * @param $key string
176 * @param $value mixed
177 * @return boolean
179 protected function validate_value($key, &$value)
181 switch ($this->properties[$key]['type']) {
182 case 'enum':
183 $v = array_search($value, $this->properties[$key]['options']);
184 if ($v === false)
185 return false;
186 else
187 $value = $v;
188 break;
190 if ($key == 'objects' && !is_array($value))
191 $value = array($value);
192 if($key == 'limit') {
193 if(!$value) {
194 $value = $this->limit;
195 return true;
197 if(!is_numeric($value)) {
198 return false;
200 $value = (int) $value;
201 if($value > $this->limit || $value < 1) {
202 return false;
204 return true;
206 if($key == 'start_time' && $this['report_period'] == 'custom') {
207 if (!isset($this->options['end_time']))
208 $this->options['end_time'] = time();
209 elseif ($value > $this->options['end_time'])
210 return false;
212 if($key == 'end_time' && isset($this->options['start_time']) && $value < $this->options['start_time']) {
213 return false;
215 return parent::validate_value($key, $value);