6
6
7
7
use DateInterval ;
8
8
use DatePeriod ;
9
+ use Generator ;
9
10
use Icinga \Module \Icingadb \Common \Auth ;
10
11
use Icinga \Module \Icingadb \Common \Database ;
11
12
use Icinga \Module \Icingadb \Widget \EmptyState ;
15
16
use Icinga \Module \Reporting \Timerange ;
16
17
use ipl \Html \Form ;
17
18
use ipl \Html \Html ;
19
+ use ipl \Orm \Query ;
20
+ use ipl \Sql \Expression ;
18
21
use ipl \Stdlib \Filter \Rule ;
19
22
use ipl \Web \Filter \QueryString ;
20
23
@@ -57,9 +60,9 @@ abstract protected function createReportRow($row);
57
60
* @param Timerange $timerange
58
61
* @param Rule|null $filter
59
62
*
60
- * @return iterable
63
+ * @return Query
61
64
*/
62
- abstract protected function fetchSla (Timerange $ timerange , Rule $ filter = null );
65
+ abstract protected function fetchSla (Timerange $ timerange , Rule $ filter = null ): Query ;
63
66
64
67
protected function fetchReportData (Timerange $ timerange , array $ config = null )
65
68
{
@@ -69,6 +72,26 @@ protected function fetchReportData(Timerange $timerange, array $config = null)
69
72
$ filter = trim ((string ) $ config ['filter ' ]) ?: '* ' ;
70
73
$ filter = $ filter !== '* ' ? QueryString::parse ($ filter ) : null ;
71
74
75
+ $ yieldSla = function (Timerange $ timerange , Rule $ filter = null ) use ($ config ): Generator {
76
+ $ sla = $ this ->fetchSla ($ timerange , $ filter );
77
+
78
+ if ($ config ['only-violation ' ] === '1 ' ) {
79
+ $ threshold = $ config ['threshold ' ] ?? static ::DEFAULT_THRESHOLD ;
80
+
81
+ $ sla ->assembleSelect ();
82
+ $ sla ->getSelectBase ()->where (new Expression (
83
+ '(%s) < %F ' , [$ sla ->getColumns ()['sla ' ]->getStatement (), $ threshold ]
84
+ ));
85
+ //$sla->filter(Filter::lessThan('sla', $threshold));
86
+ //$sla->getSelectBase()->where(['sla < ?' => $threshold]) requires to wrap Model again and
87
+ // order by sla after
88
+ }
89
+
90
+ foreach ($ sla as $ row ) {
91
+ yield $ row ;
92
+ }
93
+ };
94
+
72
95
if (isset ($ config ['breakdown ' ]) && $ config ['breakdown ' ] !== 'none ' ) {
73
96
switch ($ config ['breakdown ' ]) {
74
97
case 'day ' :
@@ -96,7 +119,7 @@ protected function fetchReportData(Timerange $timerange, array $config = null)
96
119
$ rd ->setDimensions ($ dimensions );
97
120
98
121
foreach ($ this ->yieldTimerange ($ timerange , $ interval , $ boundary ) as list ($ start , $ end )) {
99
- foreach ($ this -> fetchSla (new Timerange ($ start , $ end ), $ filter ) as $ row ) {
122
+ foreach ($ yieldSla (new Timerange ($ start , $ end ), $ filter ) as $ row ) {
100
123
$ row = $ this ->createReportRow ($ row );
101
124
102
125
if ($ row === null ) {
@@ -111,7 +134,7 @@ protected function fetchReportData(Timerange $timerange, array $config = null)
111
134
}
112
135
}
113
136
} else {
114
- foreach ($ this -> fetchSla ($ timerange , $ filter ) as $ row ) {
137
+ foreach ($ yieldSla ($ timerange , $ filter ) as $ row ) {
115
138
$ rows [] = $ this ->createReportRow ($ row );
116
139
}
117
140
}
@@ -129,7 +152,7 @@ protected function fetchReportData(Timerange $timerange, array $config = null)
129
152
* @param string|null $boundary English text datetime description for calculating bounds to get
130
153
* calendar days, weeks or months instead of relative times according to interval
131
154
*
132
- * @return \ Generator
155
+ * @return Generator
133
156
*/
134
157
protected function yieldTimerange (Timerange $ timerange , DateInterval $ interval , $ boundary = null )
135
158
{
@@ -188,6 +211,12 @@ public function initConfigForm(Form $form)
188
211
'min ' => '1 ' ,
189
212
'max ' => '12 '
190
213
]);
214
+
215
+ $ form ->addElement ('checkbox ' , 'only-violation ' , [
216
+ 'label ' => t ('Show only critical SLA ' ),
217
+ 'checkedValue ' => '1 ' ,
218
+ 'uncheckedValue ' => '0 '
219
+ ]);
191
220
}
192
221
193
222
public function getData (Timerange $ timerange , array $ config = null )
0 commit comments