Skip to content

Commit

Permalink
PR fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
marcghaly committed May 27, 2022
1 parent 04ff748 commit 3bd7340
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 41 deletions.
9 changes: 9 additions & 0 deletions classes/collector/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,13 @@ public function record_metrics(array $metrics) {
public function is_ready(): bool {
return true;
}

/**
* Abilitity for a collector to save old data.
*
* @return bool
*/
public function supports_saved_metric_record(): bool {
return false;
}
}
19 changes: 19 additions & 0 deletions classes/metric/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,23 @@ public function get_settings_url() {
* @return metric_item
*/
abstract public function get_metric_item(): metric_item;

/**
* Metric's ability to be backfilled.
*
* @return bool
*/
public function is_backfillable(): bool {
return false;
}

/**
* Returns records for backfilled metric.
*
* @param array $data Array of data specifying timeframe and sample to backfill.
* @return array|null
*/
public function get_saved_metric_data(array $data): ?array {
return null;
}
}
62 changes: 37 additions & 25 deletions classes/metric/concurrent_users_metrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@

namespace tool_cloudmetrics\metric;

use cltr_database\collector;

/**
* Metric class for new users.
* Metric class for users concurrently active.
*
* @package metric_foobar
* @author Marc-Alexandre Ghaly <[email protected]>
* @copyright 2022, Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class concurrent_users_metrics extends builtin {
public $sample;
public $interval;
public $mintimestamp;
public $maxtimestamp;
public $sameconfig;

/**
* The metric's name.
*
Expand All @@ -52,6 +56,12 @@ public function get_type(): int {
* @return metric_item
*/
public function get_metric_item($record = null): metric_item {
if (is_null($record)) {
global $DB;
$now = time();
$users = $DB->count_records_select('logstore_standard_log', 'timecreated > ?', [$now - $this->get_time_window()]);
return new metric_item($this->get_name(), $now, $users, $this);
}
return new metric_item($this->get_name(), $record->time, $record->value, $this);
}

Expand All @@ -68,11 +78,11 @@ public function is_backfillable(): bool {
* Returns records for backfilled metric.
*
* @param array $data Array of data specifying timeframe and sample to backfill.
* @return array|null
* @return array|null $metricitems Array of metrics items.
*/
public function get_backfilled_metric(array $data): ?array {
public function get_saved_metric_data(array $data): ?array {
global $DB;
$collector = new collector();

$secondsinterval = [
MINSECS * 5,
HOURSECS,
Expand Down Expand Up @@ -107,28 +117,30 @@ public function get_backfilled_metric(array $data): ?array {
ORDER BY "time" ASC';

$records = $DB->get_records_sql($sql, null, 0, $sample);

$metricitems = [];
foreach ($records as $record) {
$metricitems[] = $this->get_metric_item($record);
}
$this->mintimestamp = $metricitems[0]->time;
$this->maxtimestamp = end($metricitems)->time;
$this->sample = $sample;
$this->interval = $interval;
return $metricitems;

}

/**
* Stores what data has been sent to collector.
*
*/
public function set_data_sent_config() {
// Store what data has been sent min and max timestamp range + sample.
$mintimestamp = array_keys($records)[0];
$maxtimestamp = key(array_slice($records, -1, 1, true));
$noconfig = false;
$dbconfig = get_config('tool_cloudmetrics', $this->get_name() . '_range');
$currentconfig = $mintimestamp . '-' . $maxtimestamp . '-' . $sample . '-' . $interval;
$sameconfig = ($dbconfig === $currentconfig) ? true : false;
if (!$dbconfig) {
$noconfig = true;
}
if (!$sameconfig || $noconfig) {
$transaction = $DB->start_delegated_transaction();
$collector->delete_metrics($this->get_name());
$currentconfig = $this->mintimestamp . '-' . $this->maxtimestamp . '-' . $this->sample . '-' . $this->interval;
$this->sameconfig = ($dbconfig === $currentconfig) ? true : false;
if (!$this->sameconfig) {
set_config($this->get_name() . '_range', $currentconfig, 'tool_cloudmetrics');
foreach ($records as $record) {
$metricitem = $this->get_metric_item($record);
$collector->record_metric($metricitem);
}
$transaction->allow_commit();
return $records;
} else {
return null;
}
}
}
9 changes: 6 additions & 3 deletions collector/database/chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
$select->set_label(get_string('select_metric_for_display', 'cltr_database'));
$data = [];
$backfilledmetric = $metrics[$metricname]->is_backfillable();

$collector = new \cltr_database\collector();
if ($backfilledmetric) {
$mform = new metric_backfill_form();
if ($fromform = $mform->get_data()) {
Expand All @@ -83,6 +83,10 @@
'starttime' => $fromform->assesstimestart,
'finishtime' => $fromform->assesstimefinish
];
$metricitems = $metrics[$metricname]->get_saved_metric_data($data);
if ($collector->supports_saved_metric_record()) {
$collector->record_saved_metrics($metrics[$metricname], $metricitems);
}
}
}

Expand All @@ -109,8 +113,7 @@

$periodselect->set_label(get_string('select_graph_period', 'cltr_database'));

$collector = new \cltr_database\collector();
$records = $collector->get_metrics($metricname, $metrics[$metricname], $defaultperiod, $data);
$records = $collector->get_metrics($metricname, $defaultperiod);

$values = [];
$labels = [];
Expand Down
38 changes: 29 additions & 9 deletions collector/database/classes/collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,10 @@ public function delete_metrics(string $metricname) {
* @throws \coding_exception
* @throws \dml_exception
*/
public function get_metrics($metricnames = null, $metricclass = null, $since = false, $data = []): array {
public function get_metrics($metricnames = null, $since = false): array {
global $DB;
$starting = '';

if (!is_null($metricclass)) {
if ($metricclass->is_backfillable()) {
$backfilleddata = $metricclass->get_backfilled_metric($data);
if (is_array($backfilleddata)) {
return $backfilleddata;
}
}
}
if (is_null($metricnames)) {
$metricnames = [];
$metrics = metric\manager::get_metrics(true);
Expand All @@ -101,4 +93,32 @@ public function get_metrics($metricnames = null, $metricclass = null, $since = f
return $DB->get_records_sql($sql, $params);
}

/**
* Records retrieved data in collector.
*
* @param object $metricclass Class representing metric.
* @param array $data Array of metric items.
*/
public function record_saved_metrics(object $metricclass, array $metricitems = []) {
global $DB;

$transaction = $DB->start_delegated_transaction();
// Sets what data has been sent to collector.
$metricclass->set_data_sent_config();
if (is_array($metricitems) && !$metricclass->sameconfig) {
$this->delete_metrics($metricclass->get_name());
$this->record_metrics($metricitems);
}
$transaction->allow_commit();

}

/**
* Abilitity for a collector to retrieve old data.
*
* @return bool
*/
public function supports_saved_metric_record(): bool {
return true;
}
}
8 changes: 4 additions & 4 deletions collector/database/classes/form/metric_backfill_form.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
/**
* Form for specifying user metric data to be backfilled.
*
* @package cltr_database
* @copyright 2022 Catalyst IT Australia Pty Ltd
* @author Ghaly Marc-Alexandre <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @package cltr_database
* @copyright 2022 Catalyst IT Australia Pty Ltd
* @author Ghaly Marc-Alexandre <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

class metric_backfill_form extends moodleform {
Expand Down

0 comments on commit 3bd7340

Please sign in to comment.