Merge "Make it possible to sort on simple custom columns"
[ninja.git] / modules / reports / libraries / HttpApiEvent_options.php
blob1a3a0e6659b999aadc25f927df0d88304a97da83
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 {
8 const MAX_EVENTS = 10000; /**< Pagination limit for events retrieved from HTTP API. Hardcoded, deal with it */
10 private $limit;
12 /**
13 * Means to translate options back and forth between Report_options
14 * terms and HTTP API parameters. Handles both input and output translation.
16 static $http_api_options = array(
17 'alert_types' => array(
18 'options' => array(
19 'both' => 3,
20 'host' => 1,
21 'service' => 2
24 'state_types' => array(
25 'options' => array(
26 'both' => 3,
27 'hard' => 2,
28 'soft' => 1
31 'host_states' => array(
32 'options' => array(
33 'all' => 7,
34 'problem' => 6,
35 'up' => 1,
36 'down' => 2,
37 'unreachable' => 4
40 'service_states' => array(
41 'options' => array(
42 'all' => 15,
43 'problem' => 14,
44 'ok' => 1,
45 'warning' => 2,
46 'critical' => 4,
47 'unknown' => 8
52 /**
53 * Select report options' properties to include and adjust
54 * them for the HTTP API
56 * @param $options array = false
58 function __construct($options = false)
60 parent::__construct(false); // allright, this is a bit hackish, but parent's constructor tries to set options
61 // but we're not actually ready for that yet (see below? we modify the properties which are used by set_options
63 // whitelist properties to use, reuse the previous definitions
64 $this->properties = array_intersect_key(
65 $this->properties,
66 array_flip(array(
67 'report_period',
68 'alert_types',
69 'state_types',
70 'host_states',
71 'service_states',
72 'host_name',
73 'service_description',
74 'hostgroup',
75 'servicegroup',
76 'start_time',
77 'end_time',
78 'include_comments'
81 $this->properties['include_comments'] = array(
82 'type' => 'bool',
83 'default' => false,
84 'description' => "Include events' comments"
86 $this->properties['alert_types']['description'] = _('Show events for this kind of objects');
87 $this->properties['host_states']['description'] = _('Limit the result set to a certain kind of host states');
88 $this->properties['service_states']['description'] = _('Limit the result set to a certain kind of service states');
89 $this->properties['state_types']['description'] = _('Restrict events based on which state the event is in (soft vs hard)');
91 $limit = $this->limit = (int) Op5Config::instance()->getConfig('http_api.report.limit');
92 if($limit > self::MAX_EVENTS || $limit < 1) {
93 $this->limit = self::MAX_EVENTS;
94 $limit = $this->limit."; you can decrease this value in http_api.yml";
95 } else {
96 $limit .= "; you can increase this value in http_api.yml";
98 $this->properties['limit'] = array(
99 'type' => 'int',
100 'default' => $this->limit,
101 'description' => 'Include at most this many events (between 1 and '.$limit.')'
103 $this->properties['offset'] = array(
104 'type' => 'int',
105 'default' => 0,
106 'description' => 'Skip the first <em>offset</em> events matching the rest of the query, well suited for pagination'
109 if($options) {
110 // finally make the call which *can not* be set in parent::__construct() until all properties and
111 // other boilerplate is set up
112 $this->set_options($options);
117 * Listen for "http api" options/properties, instead of "report" options
119 * @param $input array = false
120 * @return array
122 static function discover_options($input = false)
124 $options = array();
125 if($input) {
126 $options = $input;
127 } elseif($_POST) {
128 $options = $_POST;
129 } elseif($_GET) {
130 $options = $_GET;
132 if(isset($options['start_time']) && !isset($options['end_time'])) {
133 $options['end_time'] = time();
135 if(isset($options['start_time']) || isset($options['end_time'])) {
136 // @todo workaround a nasty bug, implement this in Report_options directly
137 $options['report_period'] = 'custom';
139 if(isset($options['host_name'])) {
140 $options['host_name'] = (array) $options['host_name'];
142 if(isset($options['service_description'])) {
143 $options['service_description'] = (array) $options['service_description'];
146 // translate "all" to valid int-bitmap, for example
147 foreach($options as $key => $value) {
148 if(isset(self::$http_api_options[$key]) &&
149 isset(self::$http_api_options[$key]['options']) &&
150 isset(self::$http_api_options[$key]['options'][$value])
152 $options[$key] = self::$http_api_options[$key]['options'][$value];
155 return $options;
159 * @param $value mixed
160 * @param $type string
161 * @return string
163 function format_default($value, $type)
165 if($type == 'bool') {
166 return (int) $value;
168 if($type == 'array' || $type == 'objsel') {
169 if(empty($value)) {
170 return "[empty]";
172 return implode(", ", $value);
174 if($type == 'string' && !$value) {
175 return '[empty]';
177 if($type == 'enum') {
178 return "'$value'";
180 if($type == 'int' && empty($value) && $value !== 0) {
181 return "[empty]";
183 return (string) $value;
187 * Not as forgiving as the parent. (Why is parent forgiving?)
189 * @param $options array
190 * @throws Api_Error_Response
192 function set_options($options) {
193 foreach($options as $name => $value) {
194 if(!$this->set($name, $value)) {
195 throw new Api_Error_Response("Invalid value for option '$name'", 400);
201 * Final step in the "from merlin.report_data row to API-output" process
203 * @param $row array
204 * @return array
206 function to_output($row)
208 // transform values
209 $type = $row['service_description'] ? 'service' : 'host';
210 $row['event_type'] = Reports_Model::event_type_to_string($row['event_type'], $type, true);
211 $row['state'] = strtolower(Current_status_Model::status_text($row['state'], true, $type));
213 // rename properties
214 $row['in_scheduled_downtime'] = (int) $row['downtime_depth'];
215 unset($row['downtime_depth']);
216 if(isset($row['username'])) {
217 // comments are included and we've got one of them!
218 // let's produce some hierarchy
219 $row['comment'] = array(
220 'username' => $row['username'],
221 'comment' => $row['user_comment'],
222 'timestamp' => $row['comment_timestamp'],
225 unset($row['username'], $row['user_comment'], $row['comment_timestamp']);
227 return $row;
231 * @todo be able to throw exceptions here to give feedback of
232 * *which* error we experienced, since, you know, there's at
233 * least one user (you) exposed to this API.. Help yourself
235 * @param $key string
236 * @param $value mixed
237 * @return boolean
239 protected function validate_value($key, &$value)
241 if($key == 'limit') {
242 if(!$value) {
243 $value = $this->limit;
244 return true;
246 if(!is_numeric($value)) {
247 return false;
249 $value = (int) $value;
250 if($value > $this->limit || $value < 1) {
251 return false;
253 return true;
255 if($key == 'start_time' && isset($this->options['end_time']) && $value > $this->options['end_time']) {
256 return false;
258 if($key == 'end_time' && isset($this->options['start_time']) && $value < $this->options['start_time']) {
259 return false;
261 return parent::validate_value($key, $value);