-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
104 additions
and
41 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
* | ||
|
@@ -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); | ||
} | ||
|
||
|
@@ -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, | ||
|
@@ -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; | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|