forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDL-82626 hub: Registration collects AI usage data
- Loading branch information
1 parent
ceb30b0
commit 2b1d330
Showing
3 changed files
with
182 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -19,7 +19,7 @@ | |
/** | ||
* Class containing unit tests for the site registration class. | ||
* | ||
* @package core | ||
* @package core | ||
* @copyright 2023 Matt Porritt <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
* @covers \core\hub\registration | ||
|
@@ -85,4 +85,63 @@ public function test_get_plugin_usage(): void { | |
$this->assertEquals(0, $pluginusage['mod']['feedback']['enabled']); | ||
$this->assertEquals(1, $pluginusage['mod']['assign']['enabled']); | ||
} | ||
|
||
/** | ||
* Test the AI usage data is calculated correctly. | ||
*/ | ||
public function test_get_ai_usage(): void { | ||
global $CFG, $DB; | ||
$this->resetAfterTest(); | ||
$clock = $this->mock_clock_with_frozen(); | ||
|
||
// Record some generated text. | ||
$record = new \stdClass(); | ||
$record->provider = 'openai'; | ||
$record->actionname = 'generate_text'; | ||
$record->actionid = 1; | ||
$record->userid = 1; | ||
$record->contextid = 1; | ||
$record->success = true; | ||
$record->timecreated = $clock->time() - 5; | ||
$record->timecompleted = $clock->time(); | ||
$DB->insert_record('ai_action_register', $record); | ||
|
||
// Record a generated image. | ||
$record->actionname = 'generate_image'; | ||
$record->actionid = 2; | ||
$record->timecreated = $clock->time() - 20; | ||
$DB->insert_record('ai_action_register', $record); | ||
// Record another image. | ||
$record->actionid = 3; | ||
$record->timecreated = $clock->time() - 10; | ||
$DB->insert_record('ai_action_register', $record); | ||
|
||
// Record some errors. | ||
$record->actionname = 'generate_image'; | ||
$record->actionid = 4; | ||
$record->success = false; | ||
$record->errorcode = 403; | ||
$DB->insert_record('ai_action_register', $record); | ||
$record->actionid = 5; | ||
$record->errorcode = 403; | ||
$DB->insert_record('ai_action_register', $record); | ||
$record->actionid = 6; | ||
$record->errorcode = 404; | ||
$DB->insert_record('ai_action_register', $record); | ||
|
||
// Get our site info and check the expected calculations are correct. | ||
$siteinfo = registration::get_site_info(); | ||
$aisuage = json_decode($siteinfo['aiusage']); | ||
// Check generated text. | ||
$this->assertEquals(1, $aisuage->openai->generate_text->success_count); | ||
$this->assertEquals(0, $aisuage->openai->generate_text->fail_count); | ||
// Check generated images. | ||
$this->assertEquals(2, $aisuage->openai->generate_image->success_count); | ||
$this->assertEquals(3, $aisuage->openai->generate_image->fail_count); | ||
$this->assertEquals(15, $aisuage->openai->generate_image->average_time); | ||
$this->assertEquals(403, $aisuage->openai->generate_image->predominant_error); | ||
// Check time range is set correctly. | ||
$this->assertEquals($clock->time() - WEEKSECS, $aisuage->time_range->timefrom); | ||
$this->assertEquals($clock->time(), $aisuage->time_range->timeto); | ||
} | ||
} |