Skip to content

Commit

Permalink
Actually let ObjectsCommand derivates process multiple objects at once
Browse files Browse the repository at this point in the history
  • Loading branch information
Al2Klimov committed Jul 25, 2023
1 parent a4a4f9e commit 6b42914
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 73 deletions.
8 changes: 3 additions & 5 deletions application/forms/Command/Object/AcknowledgeProblemForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,11 @@ protected function assembleSubmitButton()

protected function getCommands(Traversable $objects): Traversable
{
foreach ($objects as $object) {
if (! $this->isGrantedOn('icingadb/command/acknowledge-problem', $object)) {
continue;
}
$granted = $this->filterGrantedOn('icingadb/command/acknowledge-problem', $objects);

if ($granted->valid()) {
$command = new AcknowledgeProblemCommand();
$command->setObjects([$object]);
$command->setObjects($granted);
$command->setComment($this->getValue('comment'));
$command->setAuthor($this->getAuth()->getUser()->getUsername());
$command->setNotify($this->getElement('notify')->isChecked());
Expand Down
8 changes: 3 additions & 5 deletions application/forms/Command/Object/AddCommentForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,11 @@ protected function assembleSubmitButton()

protected function getCommands(Traversable $objects): Traversable
{
foreach ($objects as $object) {
if (! $this->isGrantedOn('icingadb/command/comment/add', $object)) {
continue;
}
$granted = $this->filterGrantedOn('icingadb/command/comment/add', $objects);

if ($granted->valid()) {
$command = new AddCommentCommand();
$command->setObjects([$object]);
$command->setObjects($granted);
$command->setComment($this->getValue('comment'));
$command->setAuthor($this->getAuth()->getUser()->getUsername());

Expand Down
26 changes: 16 additions & 10 deletions application/forms/Command/Object/CheckNowForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@

namespace Icinga\Module\Icingadb\Forms\Command\Object;

use CallbackFilterIterator;
use Generator;
use Icinga\Module\Icingadb\Command\Object\ScheduleCheckCommand;
use Icinga\Module\Icingadb\Forms\Command\CommandForm;
use Icinga\Web\Notification;
use ipl\Orm\Model;
use ipl\Web\Widget\Icon;
use Iterator;
use IteratorIterator;
use Traversable;

class CheckNowForm extends CommandForm
Expand Down Expand Up @@ -45,19 +50,20 @@ protected function assembleSubmitButton()

protected function getCommands(Traversable $objects): Traversable
{
foreach ($objects as $object) {
if (
! $this->isGrantedOn('icingadb/command/schedule-check', $object)
&& (
! $object->active_checks_enabled
|| ! $this->isGrantedOn('icingadb/command/schedule-check/active-only', $object)
)
) {
continue;
$granted = (function () use ($objects): Generator {
foreach ($objects as $object) {
if ($this->isGrantedOn('icingadb/command/schedule-check', $object) || (
$object->active_checks_enabled
&& $this->isGrantedOn('icingadb/command/schedule-check/active-only', $object)
)) {
yield $object;
}
}
})();

if ($granted->valid()) {
$command = new ScheduleCheckCommand();
$command->setObjects([$object]);
$command->setObjects($granted);
$command->setCheckTime(time());
$command->setForced();

Expand Down
17 changes: 10 additions & 7 deletions application/forms/Command/Object/ProcessCheckResultForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Icinga\Module\Icingadb\Forms\Command\Object;

use CallbackFilterIterator;
use Generator;
use Icinga\Module\Icingadb\Command\Object\ProcessCheckResultCommand;
use Icinga\Module\Icingadb\Forms\Command\CommandForm;
use Icinga\Module\Icingadb\Model\Host;
Expand Down Expand Up @@ -136,16 +138,17 @@ protected function assembleSubmitButton()

protected function getCommands(Traversable $objects): Traversable
{
foreach ($objects as $object) {
if (
! $object->passive_checks_enabled
|| ! $this->isGrantedOn('icingadb/command/process-check-result', $object)
) {
continue;
$granted = (function () use ($objects): Generator {
foreach ($this->filterGrantedOn('icingadb/command/process-check-result', $objects) as $object) {
if ($object->passive_checks_enabled) {
yield $object;
}
}
})();

if ($granted->valid()) {
$command = new ProcessCheckResultCommand();
$command->setObjects([$object]);
$command->setObjects($granted);
$command->setStatus($this->getValue('status'));
$command->setOutput($this->getValue('output'));
$command->setPerformanceData($this->getValue('perfdata'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,11 @@ protected function assembleSubmitButton()

protected function getCommands(Traversable $objects): Traversable
{
foreach ($objects as $object) {
if (! $this->isGrantedOn('icingadb/command/remove-acknowledgement', $object)) {
continue;
}
$granted = $this->filterGrantedOn('icingadb/command/remove-acknowledgement', $objects);

if ($granted->valid()) {
$command = new RemoveAcknowledgementCommand();
$command->setObjects([$object]);
$command->setObjects($granted);
$command->setAuthor($this->getAuth()->getUser()->getUsername());

yield $command;
Expand Down
26 changes: 16 additions & 10 deletions application/forms/Command/Object/ScheduleCheckForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@

namespace Icinga\Module\Icingadb\Forms\Command\Object;

use CallbackFilterIterator;
use DateInterval;
use DateTime;
use Generator;
use Icinga\Module\Icingadb\Command\Object\ScheduleCheckCommand;
use Icinga\Module\Icingadb\Forms\Command\CommandForm;
use Icinga\Module\Icingadb\Model\Host;
use Icinga\Web\Notification;
use ipl\Html\Attributes;
use ipl\Html\HtmlElement;
use ipl\Html\Text;
use ipl\Orm\Model;
use ipl\Web\FormDecorator\IcingaFormDecorator;
use ipl\Web\Widget\Icon;
use Iterator;
use IteratorIterator;
use Traversable;

class ScheduleCheckForm extends CommandForm
Expand Down Expand Up @@ -108,19 +113,20 @@ protected function assembleSubmitButton()

protected function getCommands(Traversable $objects): Traversable
{
foreach ($objects as $object) {
if (
! $this->isGrantedOn('icingadb/command/schedule-check', $object)
&& (
! $object->active_checks_enabled
|| ! $this->isGrantedOn('icingadb/command/schedule-check/active-only', $object)
)
) {
continue;
$granted = (function () use ($objects): Generator {
foreach ($objects as $object) {
if ($this->isGrantedOn('icingadb/command/schedule-check', $object) || (
$object->active_checks_enabled
&& $this->isGrantedOn('icingadb/command/schedule-check/active-only', $object)
)) {
yield $object;
}
}
})();

if ($granted->valid()) {
$command = new ScheduleCheckCommand();
$command->setObjects([$object]);
$command->setObjects($granted);
$command->setForced($this->getElement('force_check')->isChecked());
$command->setCheckTime($this->getValue('check_time')->getTimestamp());

Expand Down
8 changes: 3 additions & 5 deletions application/forms/Command/Object/ScheduleHostDowntimeForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,17 @@ protected function assembleElements()

protected function getCommands(Traversable $objects): Traversable
{
foreach ($objects as $object) {
if (! $this->isGrantedOn('icingadb/command/downtime/schedule', $object)) {
continue;
}
$granted = $this->filterGrantedOn('icingadb/command/downtime/schedule', $objects);

if ($granted->valid()) {
if (($childOptions = (int) $this->getValue('child_options'))) {
$command = new PropagateHostDowntimeCommand();
$command->setTriggered($childOptions === 1);
} else {
$command = new ScheduleHostDowntimeCommand();
}

$command->setObjects([$object]);
$command->setObjects($granted);
$command->setComment($this->getValue('comment'));
$command->setAuthor($this->getAuth()->getUser()->getUsername());
$command->setStart($this->getValue('start')->getTimestamp());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,11 @@ protected function assembleSubmitButton()

protected function getCommands(Traversable $objects): Traversable
{
foreach ($objects as $object) {
if (! $this->isGrantedOn('icingadb/command/downtime/schedule', $object)) {
continue;
}
$granted = $this->filterGrantedOn('icingadb/command/downtime/schedule', $objects);

if ($granted->valid()) {
$command = new ScheduleServiceDowntimeCommand();
$command->setObjects([$object]);
$command->setObjects($granted);
$command->setComment($this->getValue('comment'));
$command->setAuthor($this->getAuth()->getUser()->getUsername());
$command->setStart($this->getValue('start')->getTimestamp());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,11 @@ protected function assembleSubmitButton()

protected function getCommands(Traversable $objects): Traversable
{
foreach ($objects as $object) {
if (! $this->isGrantedOn('icingadb/command/send-custom-notification', $object)) {
continue;
}
$granted = $this->filterGrantedOn('icingadb/command/send-custom-notification', $objects);

if ($granted->valid()) {
$command = new SendCustomNotificationCommand();
$command->setObjects([$object]);
$command->setObjects($granted);
$command->setComment($this->getValue('comment'));
$command->setForced($this->getElement('forced')->isChecked());
$command->setAuthor($this->getAuth()->getUser()->getUsername());
Expand Down
30 changes: 14 additions & 16 deletions application/forms/Command/Object/ToggleObjectFeaturesForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,26 +158,24 @@ protected function assembleSubmitButton()

protected function getCommands(Traversable $objects): Traversable
{
foreach ($objects as $object) {
foreach ($this->features as $feature => $spec) {
if ($this->getElement($feature) instanceof CheckboxElement) {
$featureState = $this->getElement($feature)->isChecked();
} else {
$featureState = $this->getElement($feature)->getValue();
}
foreach ($this->features as $feature => $spec) {
if ($this->getElement($feature) instanceof CheckboxElement) {
$state = $this->getElement($feature)->isChecked();
} else {
$state = $this->getElement($feature)->getValue();
}

if (
! $this->isGrantedOn($spec['permission'], $object)
|| $featureState === self::LEAVE_UNCHANGED
|| (int) $featureState === (int) $this->featureStatus[$feature]
) {
continue;
}
if ($state === self::LEAVE_UNCHANGED || (int) $state === (int) $this->featureStatus[$feature]) {
continue;
}

$granted = $this->filterGrantedOn($spec['permission'], $objects);

if ($granted->valid()) {
$command = new ToggleObjectFeatureCommand();
$command->setObjects([$object]);
$command->setObjects($granted);
$command->setFeature($feature);
$command->setEnabled((int) $featureState);
$command->setEnabled((int) $state);

$this->submittedFeatures[] = $command;

Expand Down

0 comments on commit 6b42914

Please sign in to comment.