final - speedup code for api
This commit is contained in:
+119
-13
@@ -1125,6 +1125,7 @@ class Server extends CommonDBTM
|
||||
|
||||
$itemtypes = Config::getEnabledItemtypes();
|
||||
$missingAssets = [];
|
||||
$candidatesByType = [];
|
||||
|
||||
foreach ($itemtypes as $itemtype) {
|
||||
if (!class_exists($itemtype)) {
|
||||
@@ -1174,25 +1175,34 @@ class Server extends CommonDBTM
|
||||
$locationName = self::getCachedLocationName($assetLocationId, $cacheLocation);
|
||||
$stateName = self::getCachedName('State', (int) ($assetRow['states_id'] ?? 0), $cacheState);
|
||||
$userName = self::getCachedName('User', (int) ($assetRow['users_id'] ?? 0), $cacheUser);
|
||||
$groupName = self::getAssetGroupName($itemtype, $assetId, $cacheGroup);
|
||||
|
||||
$ip = self::getAssetIp($itemtype, $assetId);
|
||||
|
||||
$missingAssets[] = [
|
||||
'itemtype' => $itemtype,
|
||||
'items_id' => $assetId,
|
||||
'name' => $name,
|
||||
'entity' => $entityName,
|
||||
'location' => $locationName,
|
||||
'otherserial' => (string) ($assetRow['otherserial'] ?? ''),
|
||||
'ip' => $ip,
|
||||
'state' => $stateName,
|
||||
'user' => $userName,
|
||||
'group' => $groupName,
|
||||
'itemtype' => $itemtype,
|
||||
'items_id' => $assetId,
|
||||
'name' => $name,
|
||||
'entity' => $entityName,
|
||||
'location' => $locationName,
|
||||
'otherserial' => (string) ($assetRow['otherserial'] ?? ''),
|
||||
'state' => $stateName,
|
||||
'user' => $userName,
|
||||
];
|
||||
$candidatesByType[$itemtype][] = $assetId;
|
||||
}
|
||||
}
|
||||
|
||||
$batchIps = self::batchLoadIps($candidatesByType);
|
||||
$batchGroups = self::batchLoadGroups($candidatesByType);
|
||||
|
||||
foreach ($missingAssets as &$asset) {
|
||||
$key = $asset['itemtype'] . ':' . $asset['items_id'];
|
||||
$asset['ip'] = $batchIps[$key] ?? '';
|
||||
$groupId = $batchGroups[$key] ?? 0;
|
||||
$asset['group'] = $groupId > 0
|
||||
? self::getCachedName('Group', $groupId, $cacheGroup)
|
||||
: '';
|
||||
}
|
||||
unset($asset);
|
||||
|
||||
if (count($missingAssets) === 0) {
|
||||
echo '<div class="alert alert-success">';
|
||||
echo htmlspecialchars(__('All assets in this location are linked or already on the UrBackup server.', 'urbackup'));
|
||||
@@ -1325,6 +1335,102 @@ JAVASCRIPT;
|
||||
return $cache[$id];
|
||||
}
|
||||
|
||||
/**
|
||||
* Batch-load IPs for multiple assets across itemtypes.
|
||||
*
|
||||
* @param array<string, list<int>> $candidatesByType itemtype => [items_id, ...]
|
||||
*
|
||||
* @return array<string, string> key "itemtype:items_id" => IP
|
||||
*/
|
||||
private static function batchLoadIps(array $candidatesByType): array
|
||||
{
|
||||
global $DB;
|
||||
|
||||
$ips = [];
|
||||
|
||||
foreach ($candidatesByType as $itemtype => $ids) {
|
||||
if (count($ids) === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$iterator = $DB->request([
|
||||
'SELECT' => ['np.items_id', 'ipa.name'],
|
||||
'FROM' => 'glpi_ipaddresses AS ipa',
|
||||
'INNER JOIN' => [
|
||||
'glpi_networknames AS nn' => [
|
||||
'ON' => [
|
||||
'nn' => 'items_id',
|
||||
'ipa' => 'id',
|
||||
['AND' => ['ipa.itemtype' => 'NetworkName']],
|
||||
],
|
||||
],
|
||||
'glpi_networkports AS np' => [
|
||||
'ON' => [
|
||||
'np' => 'id',
|
||||
'nn' => 'items_id',
|
||||
['AND' => ['nn.itemtype' => 'NetworkPort']],
|
||||
],
|
||||
],
|
||||
],
|
||||
'WHERE' => [
|
||||
'np.itemtype' => $itemtype,
|
||||
'np.items_id' => $ids,
|
||||
],
|
||||
]);
|
||||
|
||||
foreach ($iterator as $row) {
|
||||
$ip = (string) ($row['name'] ?? '');
|
||||
if ($ip === '') {
|
||||
continue;
|
||||
}
|
||||
$key = $itemtype . ':' . $row['items_id'];
|
||||
if (!isset($ips[$key])) {
|
||||
$ips[$key] = $ip;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $ips;
|
||||
}
|
||||
|
||||
/**
|
||||
* Batch-load group IDs for multiple assets across itemtypes.
|
||||
*
|
||||
* @param array<string, list<int>> $candidatesByType itemtype => [items_id, ...]
|
||||
*
|
||||
* @return array<string, int> key "itemtype:items_id" => groups_id
|
||||
*/
|
||||
private static function batchLoadGroups(array $candidatesByType): array
|
||||
{
|
||||
global $DB;
|
||||
|
||||
$groups = [];
|
||||
|
||||
foreach ($candidatesByType as $itemtype => $ids) {
|
||||
if (count($ids) === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$iterator = $DB->request([
|
||||
'FROM' => 'glpi_groups_items',
|
||||
'WHERE' => [
|
||||
'itemtype' => $itemtype,
|
||||
'items_id' => $ids,
|
||||
'type' => \Group_Item::GROUP_TYPE_NORMAL,
|
||||
],
|
||||
]);
|
||||
|
||||
foreach ($iterator as $row) {
|
||||
$key = $itemtype . ':' . $row['items_id'];
|
||||
if (!isset($groups[$key])) {
|
||||
$groups[$key] = (int) ($row['groups_id'] ?? 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $groups;
|
||||
}
|
||||
|
||||
private static function getAssetGroupName(string $itemtype, int $items_id, array &$cache): string
|
||||
{
|
||||
global $DB;
|
||||
|
||||
Reference in New Issue
Block a user