1<?php
2/*
3** Zabbix
4** Copyright (C) 2001-2021 Zabbix SIA
5**
6** This program is free software; you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation; either version 2 of the License, or
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
16** You should have received a copy of the GNU General Public License
17** along with this program; if not, write to the Free Software
18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19**/
20
21$widget = (new CWidget())->setTitle(_('IT services availability report').': '.$data['service']['name']);
22
23$controls = (new CList())
24	->addItem([
25		new CLabel(_('Period'), 'period'),
26		(new CDiv())->addClass(ZBX_STYLE_FORM_INPUT_MARGIN),
27		new CComboBox('period', $data['period'], 'submit()', [
28			'daily' => _('Daily'),
29			'weekly' => _('Weekly'),
30			'monthly' => _('Monthly'),
31			'yearly' => _('Yearly')
32		])
33	]);
34
35if ($data['period'] != 'yearly') {
36	$years = [];
37	for ($y = (date('Y') - $data['YEAR_LEFT_SHIFT']); $y <= date('Y'); $y++) {
38		$years[$y] = $y;
39	}
40	$controls->addItem([
41		new CLabel(_('Year'), 'year'),
42		(new CDiv())->addClass(ZBX_STYLE_FORM_INPUT_MARGIN),
43		new CComboBox('year', $data['year'], 'submit();', $years)
44	]);
45}
46
47$controls->addItem(get_icon('fullscreen', ['fullscreen' => $data['fullscreen']]));
48
49$widget->setControls(
50	(new CForm())
51		->cleanItems()
52		->setMethod('get')
53		->addVar('action', 'report.services')
54		->addVar('serviceid', $data['service']['serviceid'])
55		->addVar('fullscreen', $data['fullscreen'])
56		->addItem($controls)
57);
58
59$header = [
60	'yearly' => [_('Year'), null, _('Ok'), _('Problems'), _('Downtime'), _('SLA'), _('Acceptable SLA')],
61	'monthly' => [_('Month'), null, _('Ok'), _('Problems'), _('Downtime'), _('SLA'), _('Acceptable SLA')],
62	'weekly' => [_('From'), _('Till'), _('Ok'), _('Problems'), _('Downtime'), _('SLA'), _('Acceptable SLA')],
63	'daily' => [_('Day'), null, _('Ok'), _('Problems'), _('Downtime'), _('SLA'), _('Acceptable SLA')]
64];
65
66// create table
67$table = (new CTableInfo())->setHeader($header[$data['period']]);
68
69order_result($data['sla']['sla'], 'from', ZBX_SORT_DOWN);
70
71foreach ($data['sla']['sla'] as $sla) {
72	switch ($data['period']) {
73		case 'yearly':
74			$from = zbx_date2str(_x('Y', DATE_FORMAT_CONTEXT), $sla['from']);
75			$to = null;
76			break;
77
78		case 'monthly':
79			$from =  zbx_date2str(_x('F', DATE_FORMAT_CONTEXT), $sla['from']);
80			$to = null;
81			break;
82
83		case 'daily':
84			$from = zbx_date2str(DATE_FORMAT, $sla['from']);
85			$to = null;
86			break;
87
88		case 'weekly':
89			$from = zbx_date2str(DATE_TIME_FORMAT, $sla['from']);
90			$to = zbx_date2str(DATE_TIME_FORMAT, $sla['to']);
91			break;
92	}
93
94	$ok = ($sla['okTime'] != 0)
95		? (new CSpan(
96			sprintf('%dd %dh %dm',
97				$sla['okTime'] / SEC_PER_DAY,
98				($sla['okTime'] % SEC_PER_DAY) / SEC_PER_HOUR,
99				($sla['okTime'] % SEC_PER_HOUR) / SEC_PER_MIN
100			)
101		))->addClass(ZBX_STYLE_GREEN)
102		: '';
103
104	$problems = ($sla['problemTime'] != 0)
105		? (new CSpan(
106			sprintf('%dd %dh %dm',
107				$sla['problemTime'] / SEC_PER_DAY,
108				($sla['problemTime'] % SEC_PER_DAY) / SEC_PER_HOUR,
109				($sla['problemTime'] % SEC_PER_HOUR) /SEC_PER_MIN
110			)
111		))->addClass(ZBX_STYLE_RED)
112		: '';
113
114	$downtime = ($sla['downtimeTime'] != 0)
115		? (new CSpan(
116			sprintf('%dd %dh %dm',
117				$sla['downtimeTime'] / SEC_PER_DAY,
118				($sla['downtimeTime'] % SEC_PER_DAY) / SEC_PER_HOUR,
119				($sla['downtimeTime'] % SEC_PER_HOUR) / SEC_PER_MIN
120			)
121		))->addClass(ZBX_STYLE_GREY)
122		: '';
123
124	$percentage = $data['service']['showsla']
125		? (new CSpan(sprintf('%2.4f', $sla['sla'])))
126			->addClass($sla['sla'] >= $data['service']['goodsla'] ? ZBX_STYLE_GREEN : ZBX_STYLE_RED)
127		: '';
128
129	$goodsla = $data['service']['showsla'] ? $data['service']['goodsla'] : '';
130
131	$table->addRow([$from, $to, $ok, $problems, $downtime, $percentage, $goodsla]);
132}
133
134$widget
135	->addItem($table)
136	->show();
137