Skip to content

Commit 4aae283

Browse files
committed
feat: provide a default implemention of metric::get_description
Metric classes no longer need to implement `get_description`. By default, a string with the ID `"metric:{$name}_desc"` is expected to exist in the language file of the defining component. Our metrics no longer override that method. Unit tests are simplified. Documentation reflects the change.
1 parent 1ced492 commit 4aae283

14 files changed

Lines changed: 91 additions & 130 deletions

README.md

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ Let's say we have our own `local_example` plugin and the metric class is suppose
169169
```php
170170
namespace local_example\metrics;
171171
172-
use core\lang_string;
173172
use tool_monitoring\metric;
174173
use tool_monitoring\metric_type;
175174
use tool_monitoring\metric_value;
@@ -182,10 +181,6 @@ class blocks_used extends metric {
182181
return metric_type::GAUGE;
183182
}
184183
185-
public static function get_description(): lang_string {
186-
return new lang_string('metric:blocks_used_help', 'local_example');
187-
}
188-
189184
public function calculate(): array {
190185
global $DB;
191186
[$insql, $params] = $DB->get_in_or_equal(['course_list', 'course_summary']);
@@ -214,20 +209,24 @@ We want to partition the metric by the block _name_ and therefore return an arra
214209
The description is what is shown in the admin dashboard.
215210
It is also what the `monitoringexporter_prometheus` exporter uses to generate its metric `HELP` string.
216211

217-
In our example above, we return the [localized string][moodle docs string api] with the ID `metric:blocks_used_help`.
218-
We just need to actually add the text to be displayed to the plugin's language file.
212+
By default, a metric is expected to come with a [localized string][moodle docs string api] with the ID `"metric:{$name}_desc"` where `{$name}` is the name of the metric.
213+
In our example above, there needs to be a string with the ID `metric:blocks_used_desc`.
214+
So we need to actually add the text to be displayed to the plugin's language file.
219215

220216
<details open>
221217
<summary><code>lang/en/local_example.php</code> (Click to expand/collapse)</summary>
222218

223219
```php
224220
defined('MOODLE_INTERNAL') || die();
225221
// ...
226-
$string['metric:blocks_used_help'] = 'Current number of blocks used on the site.';
222+
$string['metric:blocks_used_desc'] = 'Current number of blocks used on the site.';
227223
```
228224

229225
</details>
230226

227+
> [!TIP]
228+
> You can specify a different string by overriding the `get_description` method in your metric class.
229+
231230
#### Registering the metric
232231

233232
The new metric class needs to be picked up by the `metric_collection` hook.
@@ -367,10 +366,6 @@ class blocks_used extends metric_with_config {
367366
return metric_type::GAUGE;
368367
}
369368

370-
public static function get_description(): lang_string {
371-
return new lang_string('metric:blocks_used_help', 'local_example');
372-
}
373-
374369
public function calculate(): metric_value {
375370
global $DB;
376371
$config = $this->parse_config(blocks_used_config::class);

classes/local/metrics/courses.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
namespace tool_monitoring\local\metrics;
3131

32-
use core\lang_string;
3332
use dml_exception;
3433
use tool_monitoring\metric_type;
3534
use tool_monitoring\metric;
@@ -48,26 +47,18 @@
4847
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
4948
*/
5049
class courses extends metric {
51-
/**
52-
* {@inheritDoc}
53-
*/
50+
#[\Override]
5451
public static function get_type(): metric_type {
5552
return metric_type::GAUGE;
5653
}
5754

5855
/**
59-
* {@inheritDoc}
60-
*/
61-
public static function get_description(): lang_string {
62-
return new lang_string('metric:courses_description', 'tool_monitoring');
63-
}
64-
65-
/**
66-
* {@inheritDoc}
56+
* Produces the current metric values.
6757
*
68-
* @return metric_value[]
58+
* @return metric_value[] Two metric values, one for visible courses and one for hidden courses.
6959
* @throws dml_exception
7060
*/
61+
#[\Override]
7162
public function calculate(): array {
7263
global $DB;
7364
$sql = "SELECT SUM(visible) AS numvisible,

classes/local/metrics/overdue_tasks.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
namespace tool_monitoring\local\metrics;
3131

32-
use core\lang_string;
3332
use dml_exception;
3433
use Generator;
3534
use tool_monitoring\metric;
@@ -52,26 +51,18 @@
5251
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
5352
*/
5453
class overdue_tasks extends metric {
55-
/**
56-
* {@inheritDoc}
57-
*/
58-
public static function get_description(): lang_string {
59-
return new lang_string('metric:overdue_tasks_description', 'tool_monitoring');
60-
}
61-
62-
/**
63-
* {@inheritDoc}
64-
*/
54+
#[\Override]
6555
public static function get_type(): metric_type {
6656
return metric_type::GAUGE;
6757
}
6858

6959
/**
70-
* {@inheritDoc}
60+
* Produces the current metric value.
7161
*
7262
* @return Generator<metric_value> Yields one {@see metric_value} for adhoc and then one for scheduled tasks.
7363
* @throws dml_exception
7464
*/
65+
#[\Override]
7566
public function calculate(): Generator {
7667
global $DB;
7768
$where = 'nextruntime <= :next_runtime';

classes/local/metrics/quiz_attempts_in_progress.php

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
namespace tool_monitoring\local\metrics;
3131

3232
use core\exception\coding_exception;
33-
use core\lang_string;
3433
use dml_exception;
3534
use tool_monitoring\exceptions\metric_config_not_implemented;
3635
use tool_monitoring\metric_type;
@@ -53,29 +52,20 @@
5352
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
5453
*/
5554
class quiz_attempts_in_progress extends metric_with_config {
56-
/**
57-
* {@inheritDoc}
58-
*/
55+
#[\Override]
5956
public static function get_type(): metric_type {
6057
return metric_type::GAUGE;
6158
}
6259

6360
/**
64-
* {@inheritDoc}
65-
*/
66-
public static function get_description(): lang_string {
67-
return new lang_string('metric:quiz_attempts_in_progress_description', 'tool_monitoring');
68-
}
69-
70-
/**
71-
* {@inheritDoc}
61+
* Produces the current metric value.
7262
*
73-
* @return metric_value
63+
* @return metric_value Number of ongoing quiz attempts within the configured time windows.
7464
* @throws coding_exception
7565
* @throws dml_exception
7666
* @throws metric_config_not_implemented
77-
* /
7867
*/
68+
#[\Override]
7969
public function calculate(): metric_value {
8070
global $DB;
8171
$config = $this->parse_config(quiz_attempts_in_progress_config::class);
@@ -95,9 +85,7 @@ public function calculate(): metric_value {
9585
);
9686
}
9787

98-
/**
99-
* {@inheritDoc}
100-
*/
88+
#[\Override]
10189
public static function get_default_config(): quiz_attempts_in_progress_config {
10290
return new quiz_attempts_in_progress_config();
10391
}

classes/local/metrics/user_accounts.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929

3030
namespace tool_monitoring\local\metrics;
3131

32-
use core\lang_string;
3332
use dml_exception;
3433
use Generator;
3534
use tool_monitoring\metric_type;
@@ -49,26 +48,18 @@
4948
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
5049
*/
5150
class user_accounts extends metric {
52-
/**
53-
* {@inheritDoc}
54-
*/
51+
#[\Override]
5552
public static function get_type(): metric_type {
5653
return metric_type::GAUGE;
5754
}
5855

5956
/**
60-
* {@inheritDoc}
61-
*/
62-
public static function get_description(): lang_string {
63-
return new lang_string('metric:user_accounts_description', 'tool_monitoring');
64-
}
65-
66-
/**
67-
* {@inheritDoc}
57+
* Produces the current metric values.
6858
*
6959
* @return Generator<metric_value> Yields a {@see metric_value} for each combination of auth type and suspended/deleted state.
7060
* @throws dml_exception
7161
*/
62+
#[\Override]
7263
public function calculate(): Generator {
7364
global $DB;
7465
$authtypes = get_enabled_auth_plugins();

classes/local/metrics/users_online.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
namespace tool_monitoring\local\metrics;
3131

3232
use core\exception\coding_exception;
33-
use core\lang_string;
3433
use dml_exception;
3534
use tool_monitoring\exceptions\metric_config_not_implemented;
3635
use tool_monitoring\metric_type;
@@ -50,28 +49,20 @@
5049
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
5150
*/
5251
class users_online extends metric_with_config {
53-
/**
54-
* {@inheritDoc}
55-
*/
52+
#[\Override]
5653
public static function get_type(): metric_type {
5754
return metric_type::GAUGE;
5855
}
5956

6057
/**
61-
* {@inheritDoc}
62-
*/
63-
public static function get_description(): lang_string {
64-
return new lang_string('metric:users_online_description', 'tool_monitoring');
65-
}
66-
67-
/**
68-
* {@inheritDoc}
58+
* Produces the current metric values.
6959
*
7060
* @return metric_value[] One metric value per configured time window, labeled with that same time window, in ascending order.
7161
* @throws coding_exception
7262
* @throws dml_exception
7363
* @throws metric_config_not_implemented
7464
*/
65+
#[\Override]
7566
public function calculate(): array {
7667
global $DB;
7768
$config = $this->parse_config(users_online_config::class);
@@ -97,9 +88,12 @@ public function calculate(): array {
9788
}
9889

9990
/**
100-
* {@inheritDoc}
101-
* {@noinspection PhpUnhandledExceptionInspection}
91+
* Returns the default config for the metric.
92+
*
93+
* @return users_online_config Config object.
94+
* @throws coding_exception
10295
*/
96+
#[\Override]
10397
public static function get_default_config(): users_online_config {
10498
return new users_online_config(60, 300, 900, 3600);
10599
}

classes/metric.php

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
namespace tool_monitoring;
3131

3232
use core\component;
33+
use core\exception\coding_exception;
3334
use core\lang_string;
3435
use tool_monitoring\hook\metric_collection;
3536
use Traversable;
@@ -88,24 +89,34 @@ public static function collect(metric_collection $hook): static {
8889
abstract public function calculate(): iterable|metric_value;
8990

9091
/**
91-
* Returns the localized description of the metric.
92+
* Returns the type of the metric.
9293
*
93-
* @return lang_string Metric description/help text.
94+
* @return metric_type
9495
*/
95-
abstract public static function get_description(): lang_string;
96+
abstract public static function get_type(): metric_type;
9697

9798
/**
98-
* Returns the type of the metric.
99+
* Returns the localized description of the metric.
99100
*
100-
* @return metric_type
101+
* Subclasses may override this. Defaults to a language string with the ID `"metric:{$name}_desc"` where `$name` is the
102+
* metric's name as returned by the {@see static::get_name `get_name`} method, residing in the language file of the defining
103+
* component as returned by the {@see static::get_component `get_component`} method.
104+
*
105+
* @return lang_string Metric description/help text.
106+
* @throws coding_exception
101107
*/
102-
abstract public static function get_type(): metric_type;
108+
public static function get_description(): lang_string {
109+
$name = static::get_name();
110+
return new lang_string("metric:{$name}_desc", static::get_component());
111+
}
103112

104113
/**
105114
* Returns the name of the metric to be used as an identifier.
106115
*
107116
* Subclasses may override this. It _should_ be descriptive and only consist of letters and underscores; it _must_ be unique for
108-
* the defining component as returned by {@see get_component}; it _must_ be a maximum of 100 characters long.
117+
* the defining component as returned by the {@see static::get_component `get_component`} method; it _must_ be no longer than
118+
* 100 characters.
119+
*
109120
* Defaults to the unqualified class name.
110121
*
111122
* @return string Unique metric name/identifier.
@@ -119,7 +130,7 @@ public static function get_name(): string {
119130
}
120131

121132
/**
122-
* Returns the name of the Moodle component, i.e. the plugin or core component, which defines this metric.
133+
* Returns the name of the Moodle component/plugin, which defines the metric.
123134
*
124135
* @return string Moodle component name.
125136
*/

lang/en/tool_monitoring.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,17 @@
4444
$string['event:metric_disabled'] = 'Metric disabled';
4545
$string['event:metric_enabled'] = 'Metric enabled';
4646

47-
$string['metric:courses_description'] = 'Current number of courses';
48-
$string['metric:overdue_tasks_description'] = 'Number of tasks (excluding disabled ones) for which the next runtime is not in the future';
47+
$string['metric:courses_desc'] = 'Current number of courses';
48+
$string['metric:overdue_tasks_desc'] = 'Number of tasks (excluding disabled ones) for which the next runtime is not in the future';
4949
$string['metric:quiz_attempts_in_progress_config:maxdeadlineseconds'] = 'Maximum deadline time';
5050
$string['metric:quiz_attempts_in_progress_config:maxdeadlineseconds_help'] = 'Do not count attempts that have a deadline in more than this number of seconds.';
5151
$string['metric:quiz_attempts_in_progress_config:maxidleseconds'] = 'Maximum idle time';
5252
$string['metric:quiz_attempts_in_progress_config:maxidleseconds_help'] = 'Do not count attempts that are idle longer than this number of seconds.';
53-
$string['metric:quiz_attempts_in_progress_description'] = 'Number of ongoing quiz attempts with an approaching deadline';
54-
$string['metric:user_accounts_description'] = 'Current number of user accounts';
53+
$string['metric:quiz_attempts_in_progress_desc'] = 'Number of ongoing quiz attempts with an approaching deadline';
54+
$string['metric:user_accounts_desc'] = 'Current number of user accounts';
5555
$string['metric:users_online_config:timewindows'] = 'Time window (seconds)';
5656
$string['metric:users_online_config:timewindows_help'] = 'Number of seconds since the last user access for that user to be counted as online. Multiple values can be specified, separated by commas; the metric will produce a separate metric value for each time window.';
57-
$string['metric:users_online_description'] = 'Number of users that have recently accessed the site';
57+
$string['metric:users_online_desc'] = 'Number of users that have recently accessed the site';
5858

5959
$string['monitoring:manage_metrics'] = 'Manage and configure monitoring metrics';
6060

tests/local/metrics/courses_test.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,6 @@ public function test_get_type(): void {
5555
self::assertSame(metric_type::GAUGE, $metric->get_type());
5656
}
5757

58-
public function test_get_description(): void {
59-
$metric = new courses();
60-
$description = $metric->get_description();
61-
self::assertSame('metric:courses_description', $description->get_identifier());
62-
self::assertSame('tool_monitoring', $description->get_component());
63-
}
64-
6558
public function test_calculate(): void {
6659
$this->resetAfterTest();
6760
$generator = $this->getDataGenerator();

tests/local/metrics/overdue_tasks_test.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,6 @@ public function test_get_type(): void {
6666
self::assertSame(metric_type::GAUGE, $metric->get_type());
6767
}
6868

69-
public function test_get_description(): void {
70-
$metric = new overdue_tasks();
71-
$description = $metric->get_description();
72-
self::assertSame('metric:overdue_tasks_description', $description->get_identifier());
73-
self::assertSame('tool_monitoring', $description->get_component());
74-
}
75-
7669
public function test_calculate(): void {
7770
global $DB;
7871
$this->resetAfterTest();

0 commit comments

Comments
 (0)