MDL-76901 reportbuilder: cache custom report element persistents.
During request lifecycle ensure that column, filter and condition persistents are only loaded a single time to prevent additional database reads. Provide invalidation method, used by the report helpers, to ensure that the persistents are re-loaded appropriately. Co-authored-by: Marina Glancy <[email protected]>
This commit is contained in:
co-authored by
Marina Glancy
parent
0780e87f06
commit
bfb5197738
@@ -35,6 +35,18 @@ use core_reportbuilder\local\report\filter;
|
||||
*/
|
||||
abstract class datasource extends base {
|
||||
|
||||
/** @var float[] $elementsmodified Track the time elements of specific reports have been added, updated, removed */
|
||||
private static $elementsmodified = [];
|
||||
|
||||
/** @var array $activecolumns */
|
||||
private $activecolumns;
|
||||
|
||||
/** @var array $activefilters */
|
||||
private $activefilters;
|
||||
|
||||
/** @var array $activeconditions */
|
||||
private $activeconditions;
|
||||
|
||||
/**
|
||||
* Return user friendly name of the datasource
|
||||
*
|
||||
@@ -129,22 +141,32 @@ abstract class datasource extends base {
|
||||
* @return column[]
|
||||
*/
|
||||
public function get_active_columns(): array {
|
||||
$columns = [];
|
||||
$reportid = $this->get_report_persistent()->get('id');
|
||||
|
||||
$activecolumns = column_model::get_records(['reportid' => $this->get_report_persistent()->get('id')], 'columnorder');
|
||||
// Determine whether we already retrieved the columns since the report was last modified.
|
||||
self::$elementsmodified += [$reportid => -1];
|
||||
if ($this->activecolumns !== null && $this->activecolumns['builttime'] > self::$elementsmodified[$reportid]) {
|
||||
return $this->activecolumns['values'];
|
||||
}
|
||||
|
||||
$this->activecolumns = ['builttime' => microtime(true), 'values' => []];
|
||||
|
||||
$activecolumns = column_model::get_records(['reportid' => $reportid], 'columnorder');
|
||||
foreach ($activecolumns as $index => $column) {
|
||||
$instance = $this->get_column($column->get('uniqueidentifier'));
|
||||
|
||||
// Ensure the column is still present and available.
|
||||
if ($instance !== null && $instance->get_is_available()) {
|
||||
$instance->set_persistent($column);
|
||||
|
||||
// We should clone the report column to ensure if it's added twice to a report, each operates independently.
|
||||
$columns[] = clone $instance
|
||||
$this->activecolumns['values'][] = clone $instance
|
||||
->set_index($index)
|
||||
->set_persistent($column)
|
||||
->set_aggregation($column->get('aggregation'));
|
||||
}
|
||||
}
|
||||
|
||||
return $columns;
|
||||
return $this->activecolumns['values'];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -207,18 +229,28 @@ abstract class datasource extends base {
|
||||
* @return filter[]
|
||||
*/
|
||||
public function get_active_filters(): array {
|
||||
$filters = [];
|
||||
$reportid = $this->get_report_persistent()->get('id');
|
||||
|
||||
$activefilters = filter_model::get_filter_records($this->get_report_persistent()->get('id'), 'filterorder');
|
||||
// Determine whether we already retrieved the filters since the report was last modified.
|
||||
self::$elementsmodified += [$reportid => -1];
|
||||
if ($this->activefilters !== null && $this->activefilters['builttime'] > self::$elementsmodified[$reportid]) {
|
||||
return $this->activefilters['values'];
|
||||
}
|
||||
|
||||
$this->activefilters = ['builttime' => microtime(true), 'values' => []];
|
||||
|
||||
$activefilters = filter_model::get_filter_records($reportid, 'filterorder');
|
||||
foreach ($activefilters as $filter) {
|
||||
$instance = $this->get_filter($filter->get('uniqueidentifier'));
|
||||
|
||||
// Ensure the filter is still present and available.
|
||||
if ($instance !== null && $instance->get_is_available()) {
|
||||
$filters[$instance->get_unique_identifier()] = $instance
|
||||
->set_persistent($filter);
|
||||
$this->activefilters['values'][$instance->get_unique_identifier()] =
|
||||
$instance->set_persistent($filter);
|
||||
}
|
||||
}
|
||||
|
||||
return $filters;
|
||||
return $this->activefilters['values'];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -296,17 +328,28 @@ abstract class datasource extends base {
|
||||
* @return filter[]
|
||||
*/
|
||||
public function get_active_conditions(): array {
|
||||
$conditions = [];
|
||||
$reportid = $this->get_report_persistent()->get('id');
|
||||
|
||||
$activeconditions = filter_model::get_condition_records($this->get_report_persistent()->get('id'), 'filterorder');
|
||||
// Determine whether we already retrieved the conditions since the report was last modified.
|
||||
self::$elementsmodified += [$reportid => -1];
|
||||
if ($this->activeconditions !== null && $this->activeconditions['builttime'] > self::$elementsmodified[$reportid]) {
|
||||
return $this->activeconditions['values'];
|
||||
}
|
||||
|
||||
$this->activeconditions = ['builttime' => microtime(true), 'values' => []];
|
||||
|
||||
$activeconditions = filter_model::get_condition_records($reportid, 'filterorder');
|
||||
foreach ($activeconditions as $condition) {
|
||||
$instance = $this->get_condition($condition->get('uniqueidentifier'));
|
||||
|
||||
// Ensure the condition is still present and available.
|
||||
if ($instance !== null && $instance->get_is_available()) {
|
||||
$conditions[$instance->get_unique_identifier()] = $instance->set_persistent($condition);
|
||||
$this->activeconditions['values'][$instance->get_unique_identifier()] =
|
||||
$instance->set_persistent($condition);
|
||||
}
|
||||
}
|
||||
|
||||
return $conditions;
|
||||
return $this->activeconditions['values'];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -328,4 +371,13 @@ abstract class datasource extends base {
|
||||
$this->add_all_from_entity($entity->get_entity_name());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that report elements have been modified, e.g. columns/filters/conditions have been added, removed or updated
|
||||
*
|
||||
* @param int $reportid
|
||||
*/
|
||||
final public static function report_elements_modified(int $reportid): void {
|
||||
self::$elementsmodified[$reportid] = microtime(true);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user