MDL-59779 analytics: Bulk-insert predictions
authorDavid Monllao <davidm@moodle.com>
Thu, 17 Aug 2017 15:44:25 +0000 (17:44 +0200)
committerDavid Monllao <david.monllao@gmail.com>
Thu, 17 Aug 2017 18:55:38 +0000 (20:55 +0200)
analytics/classes/local/target/base.php
analytics/classes/model.php
analytics/tests/model_test.php

index 466255e..f3c3f17 100644 (file)
@@ -130,6 +130,8 @@ abstract class base extends \core_analytics\calculable {
     /**
      * Callback to execute once a prediction has been returned from the predictions processor.
      *
+     * Note that the analytics_predictions db record is not yet inserted.
+     *
      * @param int $modelid
      * @param int $sampleid
      * @param int $rangeindex
index aae6682..ab85a58 100644 (file)
@@ -751,9 +751,12 @@ class model {
                 list($sampleid, $rangeindex) = $this->get_time_splitting()->infer_sample_info($uniquesampleid);
 
                 // Store the predicted values.
-                $samplecontext = $this->save_prediction($sampleid, $rangeindex, $prediction->prediction,
+                list($record, $samplecontext) = $this->prepare_prediction_record($sampleid, $rangeindex, $prediction->prediction,
                     $prediction->predictionscore, json_encode($indicatorcalculations[$uniquesampleid]));
 
+                // We will later bulk-insert them all.
+                $records[$uniquesampleid] = $record;
+
                 // Also store all samples context to later generate insights or whatever action the target wants to perform.
                 $samplecontexts[$samplecontext->id] = $samplecontext;
 
@@ -762,6 +765,8 @@ class model {
             }
         }
 
+        $this->save_predictions($records);
+
         return $samplecontexts;
     }
 
@@ -888,7 +893,7 @@ class model {
      * @param string $calculations
      * @return \context
      */
-    protected function save_prediction($sampleid, $rangeindex, $prediction, $predictionscore, $calculations) {
+    protected function prepare_prediction_record($sampleid, $rangeindex, $prediction, $predictionscore, $calculations) {
         global $DB;
 
         $context = $this->get_analyser()->sample_access_context($sampleid);
@@ -902,9 +907,18 @@ class model {
         $record->predictionscore = $predictionscore;
         $record->calculations = $calculations;
         $record->timecreated = time();
-        $DB->insert_record('analytics_predictions', $record);
 
-        return $context;
+        return array($record, $context);
+    }
+
+    /**
+     * Save the prediction objects.
+     *
+     * @param \stdClass[] $records
+     */
+    protected function save_predictions($records) {
+        global $DB;
+        $DB->insert_records('analytics_predictions', $records);
     }
 
     /**
index d4cf05a..b8e1768 100644 (file)
@@ -150,12 +150,11 @@ class analytics_model_testcase extends advanced_testcase {
 
         global $DB;
 
-        // 2 built-in models + the testing one.
-        $this->assertCount(3, $DB->get_records('analytics_models'));
+        $count = $DB->count_records('analytics_models');
 
         // No new models added if the builtin ones already exist.
         \core_analytics\manager::add_builtin_models();
-        $this->assertCount(3, $DB->get_records('analytics_models'));
+        $this->assertCount($count, $DB->get_records('analytics_models'));
 
         $target = \core_analytics\manager::get_target('\core\analytics\target\no_teaching');
         $this->assertTrue(\core_analytics\model::exists($target));