Skip to content

Commit

Permalink
Implement override for checks and tidy up forms #169
Browse files Browse the repository at this point in the history
  • Loading branch information
bwalkerl committed Feb 26, 2024
1 parent 42b7f71 commit e2be806
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 243 deletions.
35 changes: 30 additions & 5 deletions classes/checker.php
Expand Up @@ -57,9 +57,7 @@ public static function get_check_messages(): array {
}

// Remove any supressed checks from the list.
$checks = array_filter($checks, function($check) {
return !in_array(get_class($check), self::supressed_checks());
});
$checks = self::remove_supressed_checks($checks);

// Execute each check and store their messages.
$messages = [];
Expand Down Expand Up @@ -132,7 +130,7 @@ private static function exception_to_message(string $prefix, Throwable $e): resu
private static function process_check_and_get_result(check $check): resultmessage {
$res = new resultmessage();

$checkresult = $check->get_result();
$checkresult = self::get_overridden_result($check);

// Map check result to nagios level.
$map = [
Expand Down Expand Up @@ -297,5 +295,32 @@ private static function supressed_checks(): array {
\tool_task\check\adhocqueue::class,
];
}
}

/**
* Removes supressed checks from an array
* @param array $checks
* @return array of checks without supressed checks
*/
public static function remove_supressed_checks(array $checks): array {
// Remove any supressed checks from the list.
return array_filter($checks, function($check) {
return !in_array(get_class($check), self::supressed_checks());
});
}

/**
* Gets a check result while applying specified overrides.
* @param check $check
* @return result with overrides
*/
public static function get_overridden_result(check $check): result {
$ref = $check->get_ref();
$result = $check->get_result();

$override = \tool_heartbeat\object\override::get_active_override($ref);
if (isset($override)) {
return new result($override->get('override'), $result->get_summary(), $result->get_details());
}
return $result;
}
}
51 changes: 16 additions & 35 deletions classes/form/override_form.php
Expand Up @@ -24,6 +24,7 @@

namespace tool_heartbeat\form;

use core\check\result;
use core\form\persistent;

/**
Expand All @@ -48,33 +49,20 @@ public function definition() {
$mform->addElement('hidden', 'usermodified');
$mform->setConstant('usermodified', $this->_customdata['usermodified']);

// Name.
$mform->addElement('text', 'name', 'Name');
$mform->addRule(
'name',
get_string('formoverridefieldnamerequired', 'tool_heartbeat'),
'required',
null,
'client'
);

// Ref.
$mform->addElement('text', 'ref', 'Ref');
$mform->addRule(
'ref',
get_string('formoverridefieldrefrequired', 'tool_heartbeat'),
'required',
null,
'client'
);
$mform->addElement('static', 'ref', get_string('check'));
$mform->setConstant('ref', $this->_customdata['ref']);
$mform->hardFreeze('ref');

// Override.
$mform->addElement('select', 'override', 'Override', [
'OK' => 'Ok',
'WARNING' => 'Warning',
'CRITICAL' => 'Critical',
'ERROR' => 'Error',
'UNKNOWN' => 'Unknown',
$mform->addElement('select', 'override', get_string('override', 'tool_heartbeat'), [
result::NA => 'Not applicable',
result::OK => 'OK',
result::INFO => 'Info',
result::UNKNOWN => 'Unknown',
result::WARNING => 'Warning',
result::CRITICAL => 'Critical',
result::ERROR => 'Error',
]);
$mform->addRule(
'override',
Expand All @@ -85,7 +73,7 @@ public function definition() {
);

// Note.
$mform->addElement('textarea', 'note', 'Note');
$mform->addElement('textarea', 'note', get_string('notes', 'core_notes'));
$mform->addRule(
'note',
get_string('formoverridefieldnoterequired', 'tool_heartbeat'),
Expand All @@ -95,17 +83,10 @@ public function definition() {
);

// URL.
$mform->addElement('text', 'url', 'URL');
$mform->addRule(
'url',
get_string('formoverridefieldurlrequired', 'tool_heartbeat'),
'required',
null,
'client'
);
$mform->addElement('text', 'url', get_string('url'));

// Mute until.
$mform->addElement('date_time_selector', 'expires_at', 'Mute Until');
// Override until.
$mform->addElement('date_selector', 'expires_at', get_string('expiresat', 'tool_heartbeat'));
$mform->addRule(
'expires_at',
get_string('formoverridefieldmuteuntilrequired', 'tool_heartbeat'),
Expand Down
78 changes: 56 additions & 22 deletions classes/object/override.php
Expand Up @@ -37,17 +37,25 @@ class override extends Persistent {
*/
const TABLE = 'tool_heartbeat_overrides';

/**
* Create an instance of this class with the default expires at.
*
* @param int $id If set, this is the id of an existing record, used to load the data.
* @param \stdClass $record If set will be passed to {@link self::from_record()}.
*/

public function __construct(int $id = 0, \stdClass $record = null) {
$this->set('expires_at', strtotime('+1 month', time()));
parent::__construct($id, $record);
}

/**
* Return the definition of the properties of this model.
*
* @return array
*/
protected static function define_properties(): array {
return [
'name' => [
'null' => NULL_NOT_ALLOWED,
'type' => PARAM_TEXT,
],
'ref' => [
'null' => NULL_NOT_ALLOWED,
'type' => PARAM_TEXT,
Expand All @@ -69,38 +77,64 @@ protected static function define_properties(): array {
'type' => PARAM_INT,
],
'resolved_at' => [
'null' => NULL_ALLOWED,
'null' => NULL_NOT_ALLOWED,
'type' => PARAM_INT,
'default' => null,
'default' => 0,
],
];
}

/**
* Returns the status of the override
* Removes a mute from a check.
*
* @return string
* @return void
*/
public function get_status(): string {
$currenttime = time();
$muteuntil = $this->get('expires_at');
$resolvedat = $this->get('resolved_at');
public function unmute() {
$this->set('resolved_at', time());
$this->save();
}

if ($resolvedat) {
return get_string('overridestatusresolved', 'tool_heartbeat');
}
/**
* Gets an active override based on a ref.
*
* @param string $ref ref
* @return override|null
*/
public static function get_active_override($ref): ?override {
$overrides = self::get_active_overrides();
$id = $overrides[$ref]->id ?? null;
return !empty($id) ? new override($id) : null;
}

/**
* Returns a list of references for all active overrides.
*
* @return array
*/
public static function get_active_override_refs(): array {
return array_keys(self::get_active_overrides());
}

if ($currenttime > $muteuntil) {
// If current time exceeds muted time then the status is overdue.
return get_string('overridestatusoverdue', 'tool_heartbeat');
} else {
// If it doesn't then status is muted.
return get_string('overridestatusmuted', 'tool_heartbeat');
/**
* Returns a list of active overrides with limited information.
*
* @return array array of overrides, with check ref as the key
*/
protected static function get_active_overrides(): array {
GLOBAL $DB;
static $overrides;

if (isset($overrides)) {
return $overrides;
}

$conditions = "expires_at >= ? AND resolved_at = 0";
$overrides = $DB->get_records_select(self::TABLE, $conditions, [time()], '', 'ref, override, id');
return $overrides;
}

/**
* Returns the status of the override
* Returns the time until mute ends
*
* @return string
*/
Expand Down
107 changes: 0 additions & 107 deletions classes/table/override_table.php

This file was deleted.

0 comments on commit e2be806

Please sign in to comment.