1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
<?php
namespace Icinga\Module\Director\Web\Table;
use DateTime;
use gipfl\IcingaWeb2\Link;
use Icinga\Module\Director\Auth\Permission;
use Icinga\Module\Director\Util;
use IntlDateFormatter;
use ipl\Html\Html;
use ipl\Html\HtmlElement;
class ActivityLogTable extends IntlZfQueryBasedTable
{
protected $filters = [];
protected $lastDeployedId;
protected $extraParams = [];
protected $columnCount;
protected $hasObjectFilter = false;
protected $searchColumns = [
'author',
'object_name',
'object_type',
];
protected $ranges = [];
/** @var ?object */
protected $currentRange = null;
/** @var ?HtmlElement */
protected $currentRangeCell = null;
/** @var int */
protected $rangeRows = 0;
protected $continueRange = false;
protected $currentRow;
public function __construct($db)
{
parent::__construct($db);
}
public function assemble()
{
$this->getAttributes()->add('class', 'activity-log');
}
public function setLastDeployedId($id)
{
$this->lastDeployedId = $id;
return $this;
}
protected function fetchQueryRows()
{
$rows = parent::fetchQueryRows();
// Hint -> DESC, that's why they are inverted
if (empty($rows)) {
return $rows;
}
$last = $rows[0]->id;
$first = $rows[count($rows) - 1]->id;
$db = $this->db();
$this->ranges = $db->fetchAll(
$db->select()
->from('director_activity_log_remark')
->where('first_related_activity <= ?', $last)
->where('last_related_activity >= ?', $first)
);
return $rows;
}
public function renderRow($row)
{
$this->currentRow = $row;
$this->splitByDay($row->ts_change_time);
$action = 'action-' . $row->action. ' ';
if ($row->id > $this->lastDeployedId) {
$action .= 'undeployed';
} else {
$action .= 'deployed';
}
$columns = [
$this::td($this->makeLink($row))->setSeparator(' '),
];
if (! $this->hasObjectFilter) {
$columns[] = $this->makeRangeInfo($row->id);
}
$columns[] = $this::td($this->getTime($row->ts_change_time));
return $this::tr($columns)->addAttributes(['class' => $action]);
}
/**
* Hint: cloned from parent class and modified
* @param int $timestamp
*/
protected function renderDayIfNew($timestamp)
{
$day = $this->getDateFormatter()->format((new DateTime())->setTimestamp($timestamp));
if ($this->lastDay !== $day) {
$this->nextHeader()->add(
$this::th($day, [
'colspan' => $this->hasObjectFilter ? 2 : 3,
'class' => 'table-header-day'
])
);
$this->lastDay = $day;
if ($this->currentRangeCell) {
if ($this->currentRange->first_related_activity <= $this->currentRow->id) {
$this->currentRangeCell->addAttributes(['class' => 'continuing']);
$this->continueRange = true;
} else {
$this->continueRange = false;
}
}
$this->currentRangeCell = null;
$this->currentRange = null;
$this->rangeRows = 0;
$this->nextBody();
}
}
protected function makeRangeInfo($id)
{
$range = $this->getRangeForId($id);
if ($range === null) {
if ($this->currentRangeCell) {
$this->currentRangeCell->getAttributes()->remove('class', 'continuing');
}
$this->currentRange = null;
$this->currentRangeCell = null;
$this->rangeRows = 0;
return $this::td();
}
if ($range === $this->currentRange) {
$this->growCurrentRange();
return null;
}
$this->startRange($range);
return $this->currentRangeCell;
}
protected function startRange($range)
{
$this->currentRangeCell = $this::td($this->renderRangeComment($range), [
'colspan' => $this->rangeRows = 1,
'class' => 'comment-cell'
]);
if ($this->continueRange) {
$this->currentRangeCell->addAttributes(['class' => 'continued']);
$this->continueRange = false;
}
$this->currentRange = $range;
}
protected function renderRangeComment($range)
{
// The only purpose of this container is to avoid hovered rows from influencing
// the comments background color, as we're using the alpha channel to lighten it
// This can be replaced once we get theme-safe colors for such messages
return Html::tag('div', [
'class' => 'range-comment-container',
], Link::create($this->continueRange ? '' : $range->remark, '#', null, [
'title' => $range->remark,
'class' => 'range-comment'
]));
}
protected function growCurrentRange()
{
$this->rangeRows++;
$this->currentRangeCell->setAttribute('rowspan', $this->rangeRows);
}
protected function getRangeForId($id)
{
foreach ($this->ranges as $range) {
if ($id >= $range->first_related_activity && $id <= $range->last_related_activity) {
return $range;
}
}
return null;
}
protected function makeLink($row)
{
$type = $row->object_type;
$name = $row->object_name;
if (substr($type, 0, 7) === 'icinga_') {
$type = substr($type, 7);
}
if (Util::hasPermission(Permission::SHOW_CONFIG)) {
// Later on replacing, service_set -> serviceset
// multi column key :(
if ($type === 'service' || $this->hasObjectFilter) {
$object = "\"$name\"";
} else {
$object = Link::create(
"\"$name\"",
'director/' . str_replace('_', '', $type),
['name' => $name],
['title' => $this->translate('Jump to this object')]
);
}
return [
'[' . $row->author . ']',
Link::create(
$row->action,
'director/config/activity',
array_merge(['id' => $row->id], $this->extraParams),
['title' => $this->translate('Show details related to this change')]
),
str_replace('_', ' ', $type),
$object
];
} else {
return sprintf(
'[%s] %s %s "%s"',
$row->author,
$row->action,
$type,
$name
);
}
}
public function filterObject($type, $name)
{
$this->hasObjectFilter = true;
$this->filters[] = ['l.object_type = ?', $type];
$this->filters[] = ['l.object_name = ?', $name];
return $this;
}
public function filterHost($name)
{
$db = $this->db();
$filter = '%"host":' . json_encode($name) . '%';
$this->filters[] = ['('
. $db->quoteInto('l.old_properties LIKE ?', $filter)
. ' OR '
. $db->quoteInto('l.new_properties LIKE ?', $filter)
. ')', null];
return $this;
}
public function getColumns()
{
return [
'author' => 'l.author',
'action' => 'l.action_name',
'object_name' => 'l.object_name',
'object_type' => 'l.object_type',
'id' => 'l.id',
'change_time' => 'l.change_time',
'ts_change_time' => 'UNIX_TIMESTAMP(l.change_time)',
];
}
public function prepareQuery()
{
$query = $this->db()->select()->from(
['l' => 'director_activity_log'],
$this->getColumns()
)->order('change_time DESC')->order('id DESC')->limit(100);
foreach ($this->filters as $filter) {
$query->where($filter[0], $filter[1]);
}
return $query;
}
}
|