Skip to content

Commit 80ea714

Browse files
Add ids in VolatileStateResults instead of Controller and remove them from the results
1 parent 68895d3 commit 80ea714

File tree

2 files changed

+25
-10
lines changed

2 files changed

+25
-10
lines changed

library/Icingadb/Redis/VolatileStateResults.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@ class VolatileStateResults extends ResultSet
3636
/** @var string Object type service */
3737
protected const TYPE_SERVICE = 'service';
3838

39-
/** @var array Columns to be selected if they were explicitly set, if empty all columns are selected */
40-
protected array $columns;
39+
/** @var array|null Columns to be selected if they were explicitly set, if empty all columns are selected */
40+
protected ?array $columns;
41+
42+
/** @var bool Whether the model's ID should be contained in the results */
43+
protected bool $includeModelID = true;
4144

4245
public static function fromQuery(Query $query)
4346
{
@@ -46,6 +49,20 @@ public static function fromQuery(Query $query)
4649
$self->redisUnavailable = Backend::getRedis()->isUnavailable();
4750
$self->columns = $query->getColumns();
4851

52+
if (! empty($self->columns)) {
53+
// The id is necessary to apply the redis-updates
54+
if ($query->getModel() instanceof Host && empty(array_intersect(['host.id', 'id'], $self->columns))) {
55+
$query->withColumns('host.id');
56+
$self->includeModelID = false;
57+
} elseif (
58+
$query->getModel() instanceof Service &&
59+
empty(array_intersect(['service.id', 'id'], $self->columns))
60+
) {
61+
$query->withColumns('service.id');
62+
$self->includeModelID = false;
63+
}
64+
}
65+
4966
return $self;
5067
}
5168

@@ -66,7 +83,12 @@ public function current()
6683
$this->rewind();
6784
}
6885

69-
return parent::current();
86+
$result = parent::current();
87+
if (! $this->includeModelID) {
88+
unset($result['id']);
89+
}
90+
91+
return $result;
7092
}
7193

7294
public function next(): void

library/Icingadb/Web/Controller.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,7 @@ public function createColumnControl(Query $query, ViewModeSwitcher $viewModeSwit
105105
}
106106

107107
// When exporting as CSV or JSON, and the user requested specific columns, only those should be included
108-
// The model's id must always be selected, to ensure redis updates can be applied.
109108
if ($this->format === 'csv' || $this->format === 'json') {
110-
if ($query->getModel() instanceof Host && ! in_array('host.id', $columns)) {
111-
$columns[] = 'host.id';
112-
} elseif ($query->getModel() instanceof Service && ! in_array('service.id', $columns)) {
113-
$columns[] = 'service.id';
114-
}
115-
116109
$query->columns($columns);
117110
} else {
118111
$query->withColumns($columns);

0 commit comments

Comments
 (0)