-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #108 from catalyst/settings-check-api-310
Connection timeout + check API integration
- Loading branch information
Showing
10 changed files
with
364 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?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/>. | ||
|
||
namespace search_elastic; | ||
|
||
use admin_setting; | ||
use core\check\check; | ||
use core\check\result; | ||
use core\output\notification; | ||
|
||
/** | ||
* Admin setting for check api. | ||
* | ||
* @package search_elastic | ||
* @copyright Matthew Hilton <matthew.hilton@catalyst-au.net> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class admin_setting_check extends admin_setting { | ||
/** @var check $check The check to display */ | ||
private $check; | ||
|
||
/** | ||
* Creates check setting. | ||
* | ||
* @param string $name name of setting | ||
* @param string $heading title of setting | ||
* @param check $check check to display | ||
*/ | ||
public function __construct(string $name, string $heading, check $check) { | ||
$this->nosave = true; | ||
$this->check = $check; | ||
parent::__construct($name, $heading, '', ''); | ||
} | ||
|
||
/** | ||
* Returns setting (unused) | ||
* | ||
* @return true | ||
*/ | ||
public function get_setting() { | ||
return true; | ||
} | ||
|
||
/** | ||
* Writes the setting (unused) | ||
* | ||
* @param mixed $data | ||
*/ | ||
public function write_setting($data) { | ||
return ''; | ||
} | ||
|
||
/** | ||
* Ouputs the admin setting HTML to be rendered. | ||
* | ||
* @param mixed $data | ||
* @param string $query | ||
* @return string html | ||
*/ | ||
public function output_html($data, $query = '') { | ||
global $OUTPUT; | ||
|
||
// Run the check and get the result. | ||
$checkresult = $this->check->get_result(); | ||
|
||
$resulthtml = $OUTPUT->check_result($checkresult); | ||
$resultinfo = $checkresult->get_summary(); | ||
$out = $resulthtml . ' ' . $resultinfo; | ||
|
||
switch($checkresult->get_status()){ | ||
case result::CRITICAL: | ||
case result::ERROR: | ||
$out = $OUTPUT->notification($out, notification::NOTIFY_ERROR, false); | ||
break; | ||
|
||
case result::OK: | ||
$out = $OUTPUT->notification($out, notification::NOTIFY_SUCCESS, false); | ||
break; | ||
} | ||
return format_admin_setting($this, $this->visiblename, '', $out); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?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/>. | ||
|
||
namespace search_elastic\check; | ||
|
||
use action_link; | ||
use core\check\check; | ||
use core\check\result; | ||
use moodle_url; | ||
use search_elastic\engine; | ||
|
||
/** | ||
* Elasticsearch plugin server ready check. | ||
* | ||
* @package search_elastic | ||
* @copyright Matthew Hilton <matthew.hilton@catalyst-au.net> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
*/ | ||
class server_ready_check extends check { | ||
/** @var object|false Custom Guzzle stack to use for requests. Used only for unit testing. */ | ||
private $stack; | ||
|
||
/** | ||
* Create check. | ||
* @param object|false $stack Custom HTTP stack for unit testing. | ||
*/ | ||
public function __construct($stack = false) { | ||
$this->stack = $stack; | ||
} | ||
|
||
/** | ||
* Performs check and returns result. | ||
* | ||
* @return result | ||
*/ | ||
public function get_result(): result { | ||
// Check the plugin is configured. | ||
if (empty(get_config('search_elastic', 'hostname'))) { | ||
return new result(result::NA, get_string('connection:na', 'search_elastic')); | ||
} | ||
|
||
// Query the server to see the HTTP response. | ||
$engine = new engine(); | ||
$url = $engine->get_url(); | ||
$status = $engine->get_server_status_code($this->stack); | ||
$resultstatus = $status === 200 ? result::OK : result::ERROR; | ||
|
||
// Format the check details nicely. | ||
$statusdetails = get_string('connection:status', 'search_elastic', [ | ||
'url' => $url, | ||
'status' => $status | ||
]); | ||
|
||
return new result($resultstatus, $statusdetails); | ||
} | ||
|
||
/** | ||
* Returns the link to action this check if it failed. | ||
* | ||
* @return action_link A link to the elasticsearch engine plugin settings | ||
*/ | ||
public function get_action_link(): action_link { | ||
$configstr = get_string('adminsettings', 'search_elastic'); | ||
$configurl = new moodle_url('/admin/settings.php', ['section' => 'elasticsettings']); | ||
|
||
return new action_link($configurl, $configstr); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.