MDL-64996 analytics: Don't mark static model as untrained after clearing
authorDavid Mudrák <david@moodle.com>
Tue, 5 Mar 2019 18:57:52 +0000 (19:57 +0100)
committerDavid Mudrák <david@moodle.com>
Mon, 25 Mar 2019 07:56:11 +0000 (08:56 +0100)
Static predictions models (i.e. those using a target based on
assumptions, not facts) are always considered as trained. Clearing them
must not mark them as untrained. Doing so would make them being skipped
by the prediction scheduled task.

analytics/classes/model.php
analytics/tests/model_test.php

index 3756b90..6230b75 100644 (file)
@@ -1626,7 +1626,10 @@ class model {
         // 1 db read per context.
         $this->purge_insights_cache();
 
-        $this->model->trained = 0;
+        if (!$this->is_static()) {
+            $this->model->trained = 0;
+        }
+
         $this->model->timemodified = time();
         $this->model->usermodified = $USER->id;
         $DB->update_record('analytics_models', $this->model);
index 19ceb61..2fc0fa9 100644 (file)
@@ -169,10 +169,34 @@ class analytics_model_testcase extends advanced_testcase {
         $this->assertEmpty($DB->count_records('analytics_predict_samples'));
         $this->assertEmpty($DB->count_records('analytics_used_files'));
 
+        // Check that the model is marked as not trained after clearing (as it is not a static one).
+        $this->assertEquals(0, $DB->get_field('analytics_models', 'trained', array('id' => $this->modelobj->id)));
+
         set_config('enabled_stores', '', 'tool_log');
         get_log_manager(true);
     }
 
+    /**
+     * Test behaviour of {\core_analytics\model::clear()} for static models.
+     */
+    public function test_clear_static() {
+        global $DB;
+        $this->resetAfterTest();
+
+        $statictarget = new test_static_target_shortname();
+        $indicators['test_indicator_max'] = \core_analytics\manager::get_indicator('test_indicator_max');
+        $model = \core_analytics\model::create($statictarget, $indicators, '\core\analytics\time_splitting\quarters');
+        $modelobj = $model->get_model_obj();
+
+        // Static models are always considered trained.
+        $this->assertEquals(1, $DB->get_field('analytics_models', 'trained', array('id' => $modelobj->id)));
+
+        $model->clear();
+
+        // Check that the model is still marked as trained even after clearing.
+        $this->assertEquals(1, $DB->get_field('analytics_models', 'trained', array('id' => $modelobj->id)));
+    }
+
     public function test_model_manager() {
         $this->resetAfterTest(true);