Skip to content

Commit

Permalink
Add overridable check class
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen Herbert committed Dec 6, 2023
1 parent 3443ed9 commit 5ccdd8f
Showing 1 changed file with 110 additions and 0 deletions.
110 changes: 110 additions & 0 deletions classes/check/overridable_check.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
*
* @package tool_heartbeat
* @copyright 2023 Owen Herbert <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*
*/

namespace tool_heartbeat\check;

use core\check\check;
use core\check\result;
use stdClass;
use tool_heartbeat\object\override;

abstract class overridable_check extends check {

// an array of failing tasks where the key is classname and value is description
protected $failingTasks = [];

// add a failing task name and message
protected function add_failing_task($failingTaskName, $failingTaskMessage) {
$this->failingTasks[$failingTaskName] = $failingTaskMessage;
}

// triage the failing task in the database
protected function triage_failing_tasks() {

// For each failing task name create a new override if it does not exist
foreach ($this->failingTasks as $failingTaskName => $failingTaskMessage) {
$override = override::get_record(['name' => $failingTaskName]);

// When a failing task is triaged we mute it for a month
$currentTimestamp = time();
$oneMonthLaterTimestamp = strtotime('+1 month', $currentTimestamp);

// If it does not yet exist, we must create it
if (!$override) {

// set default properties
$data = new stdClass();
$data->name = $failingTaskName;
$data->override = 'WARNING';
$data->note = $failingTaskMessage;
$data->url = '';
$data->mute_until = $oneMonthLaterTimestamp;
$data->resolved_at = null;

// Persist to database.
$override = new override(0, $data);
$override->create();
} else {
// If it already exists then we update mute for month
if ($oneMonthLaterTimestamp > $override->get('mute_until')) {
//$override->set('mute_until', $oneMonthLaterTimestamp);
}

$override->set('resolved_at', null);
$override->set('note', $failingTaskMessage);
$override->update();
}
}

// Update the existing overrides and check if they are resolved
$failingTaskNames = array_keys($this->failingTasks);

$overrides = override::get_records();
foreach ($overrides as $override) {
$name = $override->get('name');
if (!in_array($name, $failingTaskNames)) {
// update resolved_at value
$override->set('resolved_at', $currentTimestamp);
$override->update();
}
}
}

// get overridden outputs
protected function get_overridden_outputs() {
$outputMessages = [];
$currentTimestamp = time();
$overrides = override::get_records(['resolved_at' => null]);

foreach ($overrides as $override) {
// check if the class is muted
$isMuted = $override->get('mute_until') > $currentTimestamp;
if(!$isMuted) {
$outputMessages[] = $override->get('note');
}
}

return $outputMessages;
}
}

0 comments on commit 5ccdd8f

Please sign in to comment.