reports: Generalize dates, saved reports
[ninja.git] / modules / reports / libraries / HttpApiEvent_options.php
bloba1ec6050882a59db30eecb4cca86e11b14f5e168
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 /**
14 * Means to translate options back and forth between Report_options
15 * terms and HTTP API parameters. Handles both input and output translation.
17 static $http_api_options = array(
18 'alert_types' => array(
19 'options' => array(
20 'both' => 3,
21 'host' => 1,
22 'service' => 2
25 'state_types' => array(
26 'options' => array(
27 'both' => 3,
28 'hard' => 2,
29 'soft' => 1
32 'host_states' => array(
33 'options' => array(
34 'all' => 7,
35 'problem' => 6,
36 'up' => 1,
37 'down' => 2,
38 'unreachable' => 4
41 'service_states' => array(
42 'options' => array(
43 'all' => 15,
44 'problem' => 14,
45 'ok' => 1,
46 'warning' => 2,
47 'critical' => 4,
48 'unknown' => 8
53 /**
54 * Select report options' properties to include and adjust
55 * them for the HTTP API
57 * @param $options array = false
59 function __construct($options = false)
61 parent::__construct(false); // allright, this is a bit hackish, but parent's constructor tries to set options
62 // but we're not actually ready for that yet (see below? we modify the properties which are used by set_options
64 // whitelist properties to use, reuse the previous definitions
65 $this->properties = array_intersect_key(
66 $this->properties,
67 array_flip(array(
68 'report_period',
69 'alert_types',
70 'state_types',
71 'host_states',
72 'service_states',
73 'host_name',
74 'service_description',
75 'hostgroup',
76 'servicegroup',
77 'start_time',
78 'end_time',
79 'include_comments'
82 $this->properties['include_comments'] = array(
83 'type' => 'bool',
84 'default' => false,
85 'description' => "Include events' comments"
87 $this->properties['alert_types']['description'] = _('Show events for this kind of objects');
88 $this->properties['host_states']['description'] = _('Limit the result set to a certain kind of host states');
89 $this->properties['service_states']['description'] = _('Limit the result set to a certain kind of service states');
90 $this->properties['state_types']['description'] = _('Restrict events based on which state the event is in (soft vs hard)');
92 $limit = $this->limit = (int) Op5Config::instance()->getConfig('http_api.report.limit');
93 if($limit > self::MAX_EVENTS || $limit < 1) {
94 $this->limit = self::MAX_EVENTS;
95 $limit = $this->limit."; you can decrease this value in http_api.yml";
96 } else {
97 $limit .= "; you can increase this value in http_api.yml";
99 $this->properties['limit'] = array(
100 'type' => 'int',
101 'default' => $this->limit,
102 'description' => 'Include at most this many events (between 1 and '.$limit.')'
104 $this->properties['offset'] = array(
105 'type' => 'int',
106 'default' => 0,
107 'description' => 'Skip the first <em>offset</em> events matching the rest of the query, well suited for pagination'
110 if($options) {
111 // finally make the call which *can not* be set in parent::__construct() until all properties and
112 // other boilerplate is set up
113 $this->set_options($options);
118 * Listen for "http api" options/properties, instead of "report" options
120 * @param $input array = false
121 * @return array
123 static function discover_options($input = false)
125 $options = array();
126 if($input) {
127 $options = $input;
128 } elseif($_POST) {
129 $options = $_POST;
130 } elseif($_GET) {
131 $options = $_GET;
133 if(isset($options['start_time']) && !isset($options['end_time'])) {
134 $options['end_time'] = time();
136 if(isset($options['start_time']) || isset($options['end_time'])) {
137 // @todo workaround a nasty bug, implement this in Report_options directly
138 $options['report_period'] = 'custom';
140 if(isset($options['host_name'])) {
141 $options['host_name'] = (array) $options['host_name'];
143 if(isset($options['service_description'])) {
144 $options['service_description'] = (array) $options['service_description'];
147 // translate "all" to valid int-bitmap, for example
148 foreach($options as $key => $value) {
149 if(isset(self::$http_api_options[$key]) &&
150 isset(self::$http_api_options[$key]['options']) &&
151 isset(self::$http_api_options[$key]['options'][$value])
153 $options[$key] = self::$http_api_options[$key]['options'][$value];
156 return $options;
160 * @param $value mixed
161 * @param $type string
162 * @return string
164 function format_default($value, $type)
166 if($type == 'bool') {
167 return (int) $value;
169 if($type == 'array' || $type == 'objsel') {
170 if(empty($value)) {
171 return "[empty]";
173 return implode(", ", $value);
175 if($type == 'string' && !$value) {
176 return '[empty]';
178 if($type == 'enum') {
179 return "'$value'";
181 if($type == 'int' && empty($value) && $value !== 0) {
182 return "[empty]";
184 return (string) $value;
188 * Not as forgiving as the parent. (Why is parent forgiving?)
190 * @param $options array
191 * @throws Api_Error_Response
193 function set_options($options) {
194 foreach($options as $name => $value) {
195 if(!$this->set($name, $value)) {
196 throw new Api_Error_Response("Invalid value for option '$name'", 400);
202 * Final step in the "from merlin.report_data row to API-output" process
204 * @param $row array
205 * @return array
207 function to_output($row)
209 // transform values
210 $type = $row['service_description'] ? 'service' : 'host';
211 $row['event_type'] = Reports_Model::event_type_to_string($row['event_type'], $type, true);
212 $row['state'] = strtolower(Current_status_Model::status_text($row['state'], true, $type));
214 // rename properties
215 $row['in_scheduled_downtime'] = (int) $row['downtime_depth'];
216 unset($row['downtime_depth']);
217 if(isset($row['username'])) {
218 // comments are included and we've got one of them!
219 // let's produce some hierarchy
220 $row['comment'] = array(
221 'username' => $row['username'],
222 'comment' => $row['user_comment'],
223 'timestamp' => $row['comment_timestamp'],
226 unset($row['username'], $row['user_comment'], $row['comment_timestamp']);
228 return $row;
232 * @todo be able to throw exceptions here to give feedback of
233 * *which* error we experienced, since, you know, there's at
234 * least one user (you) exposed to this API.. Help yourself
236 * @param $key string
237 * @param $value mixed
238 * @return boolean
240 protected function validate_value($key, &$value)
242 if($key == 'limit') {
243 if(!$value) {
244 $value = $this->limit;
245 return true;
247 if(!is_numeric($value)) {
248 return false;
250 $value = (int) $value;
251 if($value > $this->limit || $value < 1) {
252 return false;
254 return true;
256 if($key == 'start_time' && isset($this->options['end_time']) && $value > $this->options['end_time']) {
257 return false;
259 if($key == 'end_time' && isset($this->options['start_time']) && $value < $this->options['start_time']) {
260 return false;
262 return parent::validate_value($key, $value);