MDL-59779 analytics: Bulk-insert predictions

This commit is contained in:
David Monllao
2017-08-17 20:55:38 +02:00
committed by David Monllao
parent fbc889a268
commit cab7abec70
3 changed files with 22 additions and 7 deletions
+2
View 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
+18 -4
View 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);
}
/**
+2 -3
View 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));