Skip to content
Open
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed

- CI: fix Psalm cache directory, drop the stray `glpi-project/tools` composer dependency, declare a unique composer autoloader suffix
- Improve performance of the Static groups, Contact logs and Computer groups tabs (paginated, loaded in a few queries)
- Evaluate each dynamic group only once when an agent requests its database inventory parameters
- Show an error when a database inventory request fails, and send each credential type only once
- Do not lock the user session while waiting for the agent answer

## [1.1.4] - 2026-08-03

Expand Down
3 changes: 3 additions & 0 deletions ajax/agent.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
throw new NotFoundHttpException();
}

// The agent call can take seconds: release the session lock, the code below only reads the session
Session::writeClose();

$answer = [];

if ($_POST['action'] === PluginDatabaseinventoryInventoryAction::MA_PARTIAL) {
Expand Down
33 changes: 17 additions & 16 deletions inc/computergroup.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,30 +199,31 @@ public function countDynamicItem()

$iterator = $DB->request($params);
foreach ($iterator as $computergroup_dynamic) {
$search_params = Search::manageParams('Computer', json_decode((string) $computergroup_dynamic['search'], true, 512, JSON_THROW_ON_ERROR));
$data = Search::prepareDatasForSearch('Computer', $search_params);
Search::constructSQL($data);
Search::constructData($data);
$count += $data['data']['totalcount'];
$count += self::countComputersForSearch((string) $computergroup_dynamic['search']);
}

return $count;
}

public function countStaticItem()
/**
* Number of computers matched by a dynamic group search (JSON criteria).
*/
public static function countComputersForSearch(string $search): int
{
/** @var DBmysql $DB */
global $DB;
$search_params = Search::manageParams('Computer', json_decode($search, true, 512, JSON_THROW_ON_ERROR));
$data = Search::prepareDatasForSearch('Computer', $search_params);
Search::constructSQL($data);
Search::constructData($data);

$params = [
'SELECT' => '*',
'FROM' => PluginDatabaseinventoryComputerGroupStatic::getTable(),
'WHERE' => ['plugin_databaseinventory_computergroups_id' => $this->fields['id']],
];

$iterator = $DB->request($params);
return (int) $data['data']['totalcount'];
}

return count($iterator);
public function countStaticItem()
{
return countElementsInTable(
PluginDatabaseinventoryComputerGroupStatic::getTable(),
['plugin_databaseinventory_computergroups_id' => $this->fields['id']],
);
}

public static function install(Migration $migration)
Expand Down
71 changes: 55 additions & 16 deletions inc/computergroupstatic.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,31 +127,70 @@ private static function showForItem(PluginDatabaseinventoryComputerGroup $comput
return false;
}

$staticsgroups = new PluginDatabaseinventoryComputerGroupStatic();
$staticgrouplist = $staticsgroups->find(
[
'plugin_databaseinventory_computergroups_id' => $ID,
$static_table = self::getTable();
$computer_table = Computer::getTable();
$entity_table = Entity::getTable();
$where = [$static_table . '.plugin_databaseinventory_computergroups_id' => $ID];

// every member is excluded from the "add" dropdown, displayed or not
$used = [];
foreach ($DB->request(['SELECT' => 'computers_id', 'FROM' => $static_table, 'WHERE' => $where]) as $row) {
$used[] = $row['computers_id'];
}

// one page of members, with their computer and entity, in one query
$criteria = [
'FROM' => $static_table,
'INNER JOIN' => [
$computer_table => [
'ON' => [
$static_table => 'computers_id',
$computer_table => 'id',
],
],
],
);
'LEFT JOIN' => [
$entity_table => [
'ON' => [
$computer_table => 'entities_id',
$entity_table => 'id',
],
],
],
'WHERE' => $where,
];
$total = $DB->request($criteria + ['COUNT' => 'total'])->current()['total'];

$start = max(0, (int) ($_GET['start'] ?? 0));
if ($start >= $total) {
$start = 0;
}

$iterator = $DB->request($criteria + [
'SELECT' => [
$computer_table . '.id',
$computer_table . '.name',
$computer_table . '.is_dynamic',
$computer_table . '.serial',
$computer_table . '.otherserial',
$entity_table . '.completename AS entityname',
$static_table . '.id AS idcompgroupstatic',
],
'ORDER' => $static_table . '.id',
'START' => $start,
'LIMIT' => (int) $_SESSION['glpilist_limit'],
]);

$computers = new Computer();
$listofcomputers = [];
$used = [];
foreach ($staticgrouplist as $staticgroup) {
$used[] = $staticgroup['computers_id'];
if ($computers->getFromDB($staticgroup['computers_id'])) {
$listofcomputers[] = $computers->fields + [
'entityname' => Entity::getById($computers->fields['entities_id'])->fields['completename'],
'link' => $computers->getLinkURL(),
'idcompgroupstatic' => $staticgroup['id'],
];
}
foreach ($iterator as $row) {
$listofcomputers[] = $row + ['link' => Computer::getFormURLWithID($row['id'])];
}

TemplateRenderer::getInstance()->display(
'@databaseinventory/computergroupstatic.html.twig',
[
'item' => PluginDatabaseinventoryComputerGroup::getById($ID),
'pager' => $total > 0 ? Html::printAjaxPager('', $start, $total, '', false) : '',
'computerslist' => $listofcomputers,
'groupstaticclass' => PluginDatabaseinventoryComputerGroupStatic::class,
'canread' => $computergroup->can($ID, READ),
Expand Down
166 changes: 110 additions & 56 deletions inc/contactlog.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,41 +121,25 @@ private static function showForDatabaseParams(PluginDatabaseinventoryDatabasePar
return false;
}

$contactlog = new PluginDatabaseinventoryContactLog();
$contactloglist = $contactlog->find(
[
'plugin_databaseinventory_databaseparams_id' => $ID,
],
// lines whose credential and agent are both gone are not displayed
[$listofctlog, $pager] = self::getContactLogPage(
['plugin_databaseinventory_databaseparams_id' => $ID],
Agent::class,
'agents_id',
false,
);

$credential = new PluginDatabaseinventoryCredential();
$agent = new Agent();
$listofctlog = [];
foreach ($contactloglist as $dbpctlog) {
if ($credential->getFromDB($dbpctlog['plugin_databaseinventory_credentials_id'])) {
$linkcred = $credential->getLinkURL();
$credname = $credential->fields['name'];
}

if ($agent->getFromDB($dbpctlog['agents_id'])) {
$linkagent = $agent->getLinkURL();
$agentname = $agent->fields['name'];
}

if (isset($linkcred) || isset($linkagent)) {
$listofctlog[] = $dbpctlog + [
'linkcred' => $linkcred ?? '',
'linkagent' => $linkagent ?? '',
'credname' => $credname ?? '',
'agentname' => $agentname ?? '',
];
}
foreach ($listofctlog as &$ctlog) {
$ctlog['linkagent'] = $ctlog['linkother'];
$ctlog['agentname'] = $ctlog['othername'];
}

unset($ctlog);

TemplateRenderer::getInstance()->display(
'@databaseinventory/contactlog.html.twig',
[
'itemtype' => PluginDatabaseinventoryDatabaseParam::getType(),
'pager' => $pager,
'contactlogs' => $listofctlog,
'canread' => $databaseparams->can($ID, READ),
],
Expand All @@ -164,48 +148,118 @@ private static function showForDatabaseParams(PluginDatabaseinventoryDatabasePar
return true;
}

/**
* Load one page of contact logs with the names of their credential and of the other linked item
* (agent or database param), in one query.
*
* @param array $where criteria on the contact log table
* @param class-string<CommonDBTM> $other_itemtype Agent or PluginDatabaseinventoryDatabaseParam
* @param string $other_fk foreign key of the other item in the contact log table
* @param bool $require_credential true to skip lines whose credential is gone,
* false to skip lines whose credential and other item are both gone
*
* @return array{0: array<int, array<string, mixed>>, 1: string} the lines and the pager HTML
*/
private static function getContactLogPage(array $where, string $other_itemtype, string $other_fk, bool $require_credential): array
{
/** @var DBmysql $DB */
global $DB;

$log_table = self::getTable();
$credential_table = PluginDatabaseinventoryCredential::getTable();
$other_table = $other_itemtype::getTable();

$full_where = [];
foreach ($where as $field => $value) {
$full_where[$log_table . '.' . $field] = $value;
}

if ($require_credential) {
$full_where[] = ['NOT' => [$credential_table . '.id' => null]];
} else {
$full_where[] = ['OR' => [
['NOT' => [$credential_table . '.id' => null]],
['NOT' => [$other_table . '.id' => null]],
]];
}

$criteria = [
'FROM' => $log_table,
'LEFT JOIN' => [
$credential_table => [
'ON' => [
$log_table => 'plugin_databaseinventory_credentials_id',
$credential_table => 'id',
],
],
$other_table => [
'ON' => [
$log_table => $other_fk,
$other_table => 'id',
],
],
],
'WHERE' => $full_where,
];
$total = $DB->request($criteria + ['COUNT' => 'total'])->current()['total'];

$start = max(0, (int) ($_GET['start'] ?? 0));
if ($start >= $total) {
$start = 0;
}

$iterator = $DB->request($criteria + [
'SELECT' => [
$log_table . '.*',
$credential_table . '.id AS credential_id',
$credential_table . '.name AS credname',
$other_table . '.id AS other_id',
$other_table . '.name AS othername',
],
// newest first: the log grows at every agent contact
'ORDER' => [$log_table . '.date_creation DESC', $log_table . '.id DESC'],
'START' => $start,
'LIMIT' => (int) $_SESSION['glpilist_limit'],
]);

$lines = [];
foreach ($iterator as $row) {
$row['linkcred'] = $row['credential_id'] !== null ? PluginDatabaseinventoryCredential::getFormURLWithID($row['credential_id']) : '';
$row['credname'] ??= '';
$row['linkother'] = $row['other_id'] !== null ? $other_itemtype::getFormURLWithID($row['other_id']) : '';
$row['othername'] ??= '';
$lines[] = $row;
}

return [$lines, $total > 0 ? Html::printAjaxPager('', $start, $total, '', false) : ''];
}

private static function showForAgent(Agent $agent)
{
$ID = $agent->getField('id');
if (!$agent->can($ID, UPDATE)) {
return false;
}

$contactlog = new PluginDatabaseinventoryContactLog();
$contactloglist = $contactlog->find(
[
'agents_id' => $ID,
],
// lines whose credential is gone are not displayed
[$listofctlog, $pager] = self::getContactLogPage(
['agents_id' => $ID],
PluginDatabaseinventoryDatabaseParam::class,
'plugin_databaseinventory_databaseparams_id',
true,
);

$credential = new PluginDatabaseinventoryCredential();
$dbparam = new PluginDatabaseinventoryDatabaseParam();
$listofctlog = [];
foreach ($contactloglist as $dbpctlog) {
if ($credential->getFromDB($dbpctlog['plugin_databaseinventory_credentials_id'])) {
$linkcred = $credential->getLinkURL();
$credname = $credential->fields['name'];
}

if ($dbparam->getFromDB($dbpctlog['plugin_databaseinventory_databaseparams_id'])) {
$linkdbparam = $dbparam->getLinkURL();
$dbparamname = $dbparam->fields['name'];
}

if (isset($linkcred)) {
$listofctlog[] = $dbpctlog + [
'linkcred' => $linkcred,
'linkdbparam' => $linkdbparam ?? '',
'credname' => $credname ?? '',
'dbparamname' => $dbparamname ?? '',
];
}
foreach ($listofctlog as &$ctlog) {
$ctlog['linkdbparam'] = $ctlog['linkother'];
$ctlog['dbparamname'] = $ctlog['othername'];
}

unset($ctlog);

TemplateRenderer::getInstance()->display(
'@databaseinventory/contactlog.html.twig',
[
'itemtype' => Agent::getType(),
'pager' => $pager,
'contactlogs' => $listofctlog,
'canread' => $agent->can($ID, READ),
],
Expand Down
4 changes: 3 additions & 1 deletion inc/databaseparam.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,9 @@ public function getCredentialTypeLinked()
$credential_type_table . '.id',
$credential_type_table . '.name',
],
'FROM' => $credential_type_table,
// several credentials of the same type must not repeat the type sent to the agent
'DISTINCT' => true,
'FROM' => $credential_type_table,
'JOIN' => [
$credential_table => [
'ON' => [
Expand Down
Loading