From b0247204996adfdbb93fa057af912034c9e39823 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Monlla=C3=B3?= Date: Fri, 23 Aug 2019 13:51:47 +0800 Subject: [PATCH] MDL-66536 analytics: Bulk-fetch samples data For one_sample_per_analysable models. --- analytics/classes/model.php | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/analytics/classes/model.php b/analytics/classes/model.php index 3678d97adba..939cf6c5681 100644 --- a/analytics/classes/model.php +++ b/analytics/classes/model.php @@ -950,11 +950,9 @@ class model { // the database, and we need to do it using one single database query (for performance reasons as well). $predictionrecords = $this->add_prediction_ids($predictionrecords); - // Get \core_analytics\prediction objects also fetching the samplesdata. This costs us - // 1 db read, but we have to pay it if we want that our insights include links to the - // suggested actions. - $predictions = array_map(function($predictionobj) { - $prediction = new \core_analytics\prediction($predictionobj, $this->prediction_sample_data($predictionobj)); + $samplesdata = $this->predictions_sample_data($predictionrecords); + $predictions = array_map(function($predictionobj) use ($samplesdata) { + $prediction = new \core_analytics\prediction($predictionobj, $samplesdata[$predictionobj->sampleid]); return $prediction; }, $predictionrecords); } else { @@ -1411,6 +1409,23 @@ class model { return $samplesdata[$predictionobj->sampleid]; } + /** + * Returns the samples data of the provided predictions. + * + * @param \stdClass[] $predictionrecords + * @return array + */ + public function predictions_sample_data(array $predictionrecords): array { + + $sampleids = []; + foreach ($predictionrecords as $predictionobj) { + $sampleids[] = $predictionobj->sampleid; + } + list($sampleids, $samplesdata) = $this->get_analyser()->get_samples($sampleids); + + return $samplesdata; + } + /** * Returns the description of a sample *