protected function trigger_insights($samplecontexts, $predictionrecords) {

        // Notify the target that all predictions have been processed.
        if ($this->get_analyser()::one_sample_per_analysable()) {

            // We need to do something unusual here. self::save_predictions uses the bulk-insert function (insert_records()) for
            // performance reasons and that function does not return us the inserted ids. We need to retrieve them from
            // 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));
                return $prediction;
            }, $predictionrecords);
        } else {
            $predictions = [];
        }

        $this->get_target()->generate_insight_notifications($this->model->id, $samplecontexts, $predictions);

        // Update cache.
        $cache = \cache::make('core', 'contextwithinsights');
        foreach ($samplecontexts as $context) {
            $modelids = $cache->get($context->id);
            if (!$modelids) {
                // The cache is empty, but we don't know if it is empty because there are no insights
                // in this context or because cache/s have been purged, we need to be conservative and
                // "pay" 1 db read to fill up the cache.
                $models = \core_analytics\manager::get_models_with_insights($context);
                $cache->set($context->id, array_keys($models));
            } else if (!in_array($this->get_id(), $modelids)) {
                array_push($modelids, $this->get_id());
                $cache->set($context->id, $modelids);
            }
        }
    }