MDL-59779 analytics: Bulk-insert predictions
This commit is contained in:
committed by
David Monllao
parent
fbc889a268
commit
cab7abec70
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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));
|
||||
|
||||
Reference in New Issue
Block a user