Skip to content

Commit

Permalink
WIP: Make fields for incident age condition intuitive
Browse files Browse the repository at this point in the history
  • Loading branch information
raviks789 committed Apr 3, 2024
1 parent 9819b5f commit 1d26d1c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 21 deletions.
45 changes: 25 additions & 20 deletions application/forms/EventRuleConfigElements/EscalationCondition.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ protected function assemble(): void
]
);

$valUnit = null;
switch ($this->getPopulatedValue('column_' . $i)) {
case 'incident_severity':
/** @var BaseFormElement $val */
Expand Down Expand Up @@ -154,28 +155,25 @@ protected function assemble(): void
case 'incident_age':
/** @var BaseFormElement $val */
$val = $this->createElement(
'text',
'number',
$valName,
[
'required' => true,
'class' => ['autosubmit', 'right-operand'],
'validators' => [
new CallbackValidator(function ($value, $validator) {
if (! preg_match('~^\d+(?:\.?\d*)?[hms]{1}$~', $value)) {
$validator->addMessage(
$this->translate(
'Only numbers with optional fractions (separated by a dot)'
. ' and one of these suffixes are allowed: h, m, s'
)
);

return false;
}

$validator->clearMessages();
return true;
})
]
'class' => ['right-operand'],
'value' => 1
]
);

$valUnit = $this->createElement(
'select',
'age_unit_' . $i,
[
'options' => [
's' => 's',
'm' => 'm',
'h' => 'h'
],
'class' => 'age-unit'
]
);

Expand All @@ -191,6 +189,7 @@ protected function assemble(): void
'value' => 'incident_age'
]);

$this->registerElement($valUnit);
break;
default:
/** @var BaseFormElement $val */
Expand All @@ -213,6 +212,7 @@ protected function assemble(): void
$col,
$op,
$val,
$valUnit,

Check failure on line 215 in application/forms/EventRuleConfigElements/EscalationCondition.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Parameter #4 $conditionUnit of class Icinga\Module\Notifications\Widget\ItemList\EscalationConditionListItem constructor expects ipl\Html\FormElement\BaseFormElement|null, ipl\Html\Contract\FormElement|null given.

Check failure on line 215 in application/forms/EventRuleConfigElements/EscalationCondition.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Parameter #4 $conditionUnit of class Icinga\Module\Notifications\Widget\ItemList\EscalationConditionListItem constructor expects ipl\Html\FormElement\BaseFormElement|null, ipl\Html\Contract\FormElement|null given.

Check failure on line 215 in application/forms/EventRuleConfigElements/EscalationCondition.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Parameter #4 $conditionUnit of class Icinga\Module\Notifications\Widget\ItemList\EscalationConditionListItem constructor expects ipl\Html\FormElement\BaseFormElement|null, ipl\Html\Contract\FormElement|null given.

Check failure on line 215 in application/forms/EventRuleConfigElements/EscalationCondition.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Parameter #4 $conditionUnit of class Icinga\Module\Notifications\Widget\ItemList\EscalationConditionListItem constructor expects ipl\Html\FormElement\BaseFormElement|null, ipl\Html\Contract\FormElement|null given.

Check failure on line 215 in application/forms/EventRuleConfigElements/EscalationCondition.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Parameter #4 $conditionUnit of class Icinga\Module\Notifications\Widget\ItemList\EscalationConditionListItem constructor expects ipl\Html\FormElement\BaseFormElement|null, ipl\Html\Contract\FormElement|null given.

Check failure on line 215 in application/forms/EventRuleConfigElements/EscalationCondition.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Parameter #4 $conditionUnit of class Icinga\Module\Notifications\Widget\ItemList\EscalationConditionListItem constructor expects ipl\Html\FormElement\BaseFormElement|null, ipl\Html\Contract\FormElement|null given.
$removeButton
);
}
Expand All @@ -230,6 +230,10 @@ protected function assemble(): void
$this->conditions[$nextCount]->conditionType->setName('column_' . $n);
$this->conditions[$nextCount]->operator->setName('operator_' . $n);
$this->conditions[$nextCount]->conditionVal->setName('val_' . $n);
if ($this->conditions[$nextCount]->conditionUnit) {
$this->conditions[$nextCount]->conditionUnit->setName('age_unit_' . $n);
}

if ($conditionCount === 1) {
$this->conditions[$nextCount]->removeButton = null;
} elseif ($this->conditions[$nextCount]->removeButton) {
Expand Down Expand Up @@ -308,7 +312,8 @@ public function getCondition(): string

$filterStr = $chosenType
. $this->getValue('operator_' . $count)
. ($this->getValue('val_' . $count) ?? ($chosenType === 'incident_severity' ? 'ok' : ''));
. ($this->getValue('val_' . $count) ?? ($chosenType === 'incident_severity' ? 'ok' : ''))
. $this->getValue('age_unit_' . $count, '');

$filter->add(QueryString::parse($filterStr));
}
Expand Down
9 changes: 8 additions & 1 deletion application/forms/EventRuleConfigForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,14 @@ public function populate($values): self
}

$conditionFormValues['operator_' . $count] = QueryString::getRuleSymbol($filter);
$conditionFormValues['val_' . $count] = $filter->getValue();
$conditionValue = $filter->getValue();
$incidentAgeCondition = preg_split('~^\d+(?:\.?\d*)?[hms]$~', $filter->getValue());

Check failure on line 344 in application/forms/EventRuleConfigForm.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.2 on ubuntu-latest

Parameter #2 $subject of function preg_split expects string, mixed given.

Check failure on line 344 in application/forms/EventRuleConfigForm.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.3 on ubuntu-latest

Parameter #2 $subject of function preg_split expects string, mixed given.

Check failure on line 344 in application/forms/EventRuleConfigForm.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 7.4 on ubuntu-latest

Parameter #2 $subject of function preg_split expects string, mixed given.

Check failure on line 344 in application/forms/EventRuleConfigForm.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.0 on ubuntu-latest

Parameter #2 $subject of function preg_split expects string, mixed given.

Check failure on line 344 in application/forms/EventRuleConfigForm.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.1 on ubuntu-latest

Parameter #2 $subject of function preg_split expects string, mixed given.

Check failure on line 344 in application/forms/EventRuleConfigForm.php

View workflow job for this annotation

GitHub Actions / Static analysis for php 8.2 on ubuntu-latest

Parameter #2 $subject of function preg_split expects string, mixed given.
if ($incidentAgeCondition !== false && count($incidentAgeCondition) === 2) {
$conditionFormValues['val_' . $count] = $incidentAgeCondition[0];
$conditionFormValues['age_unit_' . $count] = $incidentAgeCondition[1];
} else {
$conditionFormValues['val_' . $count] = $conditionValue;
}
}

$formValues['escalation-condition_' . bin2hex($position)] = $conditionFormValues;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,20 @@ class EscalationConditionListItem extends BaseHtmlElement
/** @var BaseFormElement Condition value */
public $conditionVal;

/** @var ?BaseFormElement Condition value */
public $conditionUnit;

public function __construct(
BaseFormElement $conditionType,
BaseFormElement $operator,
BaseFormElement $conditionVal,
?BaseFormElement $conditionUnit,
?SubmitButtonElement $removeButton
) {
$this->conditionType = $conditionType;
$this->operator = $operator;
$this->conditionVal = $conditionVal;
$this->conditionUnit = $conditionUnit;
$this->removeButton = $removeButton;
}

Expand All @@ -44,6 +49,7 @@ protected function assemble(): void
$this->conditionType,
$this->operator,
$this->conditionVal,
$this->conditionUnit,
$this->removeButton
]);
}
Expand Down
6 changes: 6 additions & 0 deletions public/css/event-rule-config.less
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@
margin: 0;
}

.age-unit {
border-radius: 0.4em;
min-width: 3.5em;
margin-left: 1px;
}

.errors + .remove-button {
margin: 0;
}
Expand Down

0 comments on commit 1d26d1c

Please sign in to comment.