Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

- Make the plugin compatible with the Content Security Policy

<<<<<<< HEAD
## [1.11.1] - 2026-11-04

### Added
Expand All @@ -26,6 +27,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added

- GLPI 12 compatibility
=======
### Fixed

- Faster dashboards and report pages: report configurations and rights are read once per page
- Faster "Backlogs", "Computers by OS", "Tickets per service", "Ticket status by technician" and "Computers by entity" reports, with the same results
- Fewer queries on every GLPI page and on the core statistics page

## [1.10.4] - 2026-11-04
>>>>>>> cc0b845 (Perf: read report configs once, rewrite heavy report queries (#411))

### Fixed

Expand Down
3 changes: 3 additions & 0 deletions ajax/common.tabs.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,8 @@

Session::checkLoginUser();

// the tab only reads the session: release its lock for the other requests of the user
session_write_close();

$common = new PluginMreportingCommon();
$common->showCentral($_REQUEST);
15 changes: 15 additions & 0 deletions inc/baseclass.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,19 @@ public function __construct($config = [])
$config['randname'],
);
}

/**
* SQL expression of the end of the selected period ('Y-m-d 23:59:59') containing a date field,
* the same value as criteria_list_date / criteria_list_date2 give for `date` / `solvedate`.
*
* @param string $field quoted column name, e.g. `glpi_tickets`.`date`
*/
protected function getPeriodEndSql(string $field): string
{
return match ($this->period_interval) {
'WEEK' => "DATE_FORMAT($field + INTERVAL (6 - WEEKDAY($field)) DAY, '{$this->period_datetime}')",
'MONTH' => "CONCAT(LAST_DAY(DATE_FORMAT($field, '{$this->period_datetime}')), ' 23:59:59')",
default => "DATE_FORMAT($field, '{$this->period_datetime}')",
};
}
}
19 changes: 13 additions & 6 deletions inc/common.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ public static function getMenuContent()
*/
public static function parseAllClasses($inc_dir)
{
// the files are read once per request: getAllReports() and the config list call this several times
static $cache = [];
if (isset($cache[$inc_dir])) {
return $cache[$inc_dir];
}

$classes = [];
$matches = [];

Expand All @@ -124,7 +130,7 @@ public static function parseAllClasses($inc_dir)
}
}

return $classes;
return $cache[$inc_dir] = $classes;
}

/**
Expand Down Expand Up @@ -203,15 +209,16 @@ public function getAllReports($with_url = true, $params = [])
$reports[$classname]['functions'][$i]['short_classname'] = $scn;
$reports[$classname]['functions'][$i]['is_active'] = false;

$config = new PluginMreportingConfig();
if ($config->getFromDBByFunctionAndClassname($f_name, $classname)) {
if ($config->fields['is_active'] == 1) {
// configs and rights come from per-request caches: one query each instead of 4 per report
$config_fields = PluginMreportingConfig::getCachedConfig($f_name, $classname);
if ($config_fields !== null) {
if ($config_fields['is_active'] == 1) {
$reports[$classname]['functions'][$i]['is_active'] = true;
$reports[$classname]['functions'][$i]['id'] = $config->fields['id'];
$reports[$classname]['functions'][$i]['id'] = $config_fields['id'];
}
$reports[$classname]['functions'][$i]['right'] = READ;
if (isset($_SESSION['glpiactiveprofile'])) {
$reports[$classname]['functions'][$i]['right'] = PluginMreportingProfile::canViewReports($_SESSION['glpiactiveprofile']['id'], $config->fields['id']);
$reports[$classname]['functions'][$i]['right'] = PluginMreportingProfile::canViewReports($_SESSION['glpiactiveprofile']['id'], $config_fields['id']);
}
}

Expand Down
82 changes: 68 additions & 14 deletions inc/config.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ class PluginMreportingConfig extends CommonDBTM
{
public static string $rightname = 'config';

/**
* Every report config, keyed by lowercase "classname|name" (the database comparison ignores case),
* loaded with one query the first time a read-only lookup needs it during the request.
* false marks a key matching several configs, which getFromDBByCrit() refuses too.
*
* @var array<string, array<string, mixed>|false>|null
*/
private static ?array $configs_cache = null;

public static function getTypeName($nb = 0)
{
return __s('Configuration', 'mreporting');
Expand Down Expand Up @@ -271,6 +280,50 @@ public function getFromDBByFunctionAndClassname($function, $classname)
]);
}

/**
* Fields of the config of a report, for read-only lookups: every config is loaded with one query per request.
*
* @return array<string, mixed>|null null when no config, or several configs, match
*/
public static function getCachedConfig(string $function, string $classname): ?array
{
/** @var DBmysql $DB */
global $DB;

if (self::$configs_cache === null) {
self::$configs_cache = [];
foreach ($DB->request(['FROM' => self::getTable()]) as $row) {
$key = strtolower($row['classname'] . '|' . $row['name']);
self::$configs_cache[$key] = isset(self::$configs_cache[$key]) ? false : $row;
}
}

return (self::$configs_cache[strtolower($classname . '|' . $function)] ?? null) ?: null;
}

public static function resetConfigCache(): void
{
self::$configs_cache = null;
}

public function post_addItem()
{
self::resetConfigCache();
parent::post_addItem();
}

public function post_updateItem($history = true)
{
self::resetConfigCache();
parent::post_updateItem($history);
}

public function post_purgeItem()
{
self::resetConfigCache();
parent::post_purgeItem();
}

/**
* add First config Link
*@return void
Expand Down Expand Up @@ -873,17 +926,18 @@ public static function initConfigParams($name, $classname)
'graphtype' => 'SVG',
];

$self = new self();
if ($self->getFromDBByFunctionAndClassname($name, $classname)) {
$crit['area'] = $self->fields['show_area'];
$crit['spline'] = $self->fields['spline'];
$crit['show_label'] = $self->fields['show_label'];
$crit['flip_data'] = $self->fields['flip_data'];
$crit['unit'] = $self->fields['unit'];
$crit['delay'] = $self->fields['default_delay'];
$crit['condition'] = $self->fields['condition'];
$crit['show_graph'] = $self->fields['show_graph'];
$crit['graphtype'] = $self->fields['graphtype'];
// read about 5 times per displayed report: served by the per-request cache
$fields = self::getCachedConfig($name, $classname);
if ($fields !== null) {
$crit['area'] = $fields['show_area'];
$crit['spline'] = $fields['spline'];
$crit['show_label'] = $fields['show_label'];
$crit['flip_data'] = $fields['flip_data'];
$crit['unit'] = $fields['unit'];
$crit['delay'] = $fields['default_delay'];
$crit['condition'] = $fields['condition'];
$crit['show_graph'] = $fields['show_graph'];
$crit['graphtype'] = $fields['graphtype'];
$crit['randname'] = $classname . $name;
}

Expand Down Expand Up @@ -912,9 +966,9 @@ public static function showGraphConfigValue($name, $classname)

// DEBUG_MREPORTING is constant. It is true if debug mode is enabled, false otherwise. For PHPStan, this constant is always true or false.
/* @phpstan-ignore-next-line */
$self = new self();
if ($self->getFromDBByFunctionAndClassname($name, $classname)) {
return $self->fields['show_graph'];
$fields = self::getCachedConfig($name, $classname);
if ($fields !== null) {
return $fields['show_graph'];
}

return false;
Expand Down
22 changes: 5 additions & 17 deletions inc/helpdesk.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,31 +474,17 @@ public function reportHgbarTicketNumberByService($config = [])
$delay = PluginMreportingCommon::getCriteriaDate('glpi_tickets.date', $config['delay'], $config['randname']);

foreach ($this->filters as $class => $filter) {
$query = [
'SELECT' => [
Ticket::getTable() . '.id',
],
'FROM' => Ticket::getTable(),
'WHERE' => [
Ticket::getTable() . '.type' => 1,
],
];

$result = $DB->request($query);
$datas['labels2'][$filter['label']] = $filter['label'];

$ticket_ids = [];
foreach ($result as $ticket) {
$ticket_ids[] = $ticket['id'];
}

// tickets that are not of type 1; `type` is NOT NULL, so this is the former
// "id NOT IN (ids of every type 1 ticket)" without loading and sending back every id
$query = [
'COUNT' => 'count',
'FROM' => Ticket::getTable(),
'WHERE' => [
[
'NOT' => [
Ticket::getTable() . '.id' => $ticket_ids,
Ticket::getTable() . '.type' => 1,
],
],
Ticket::getTable() . '.entities_id' => PluginMreportingCommon::formatWhereEntitiesArray($this->where_entities),
Expand Down Expand Up @@ -885,11 +871,13 @@ public function reportVstackbarTicketStatusByTechnician($config = [])
//get technician list
$technicians = [];

// one row per technician, not per assignment
$query = [
'SELECT' => [
new QueryExpression("CONCAT(" . User::getTable() . ".firstname, ' ', " . User::getTable() . ".realname) as fullname"),
User::getTable() . ".name as username",
],
'DISTINCT' => true,
'FROM' => Ticket::getTable(),
'INNER JOIN' => [
Ticket_User::getTable() => [
Expand Down
Loading