|
37 | 37 | use DbUtils; |
38 | 38 | use CommonDBTM; |
39 | 39 | use DBmysqlIterator; |
| 40 | +use GlpiPlugin\Carbon\AbstractModel; |
40 | 41 | use GlpiPlugin\Carbon\DataTracking\AbstractTracked; |
| 42 | +use GlpiPlugin\Carbon\DataTracking\TrackedFloat; |
41 | 43 | use GlpiPlugin\Carbon\EmbodiedImpact; |
42 | 44 | use GlpiPlugin\Carbon\Impact\Type; |
43 | 45 | use Toolbox as GlpiToolbox; |
@@ -181,15 +183,30 @@ public function evaluateItem(int $id): bool |
181 | 183 | if ($item === false) { |
182 | 184 | return false; |
183 | 185 | } |
184 | | - try { |
185 | | - $impacts = $this->doEvaluation($item); |
186 | | - } catch (\RuntimeException $e) { |
187 | | - return false; |
188 | | - } |
189 | 186 |
|
190 | | - if ($impacts === null) { |
191 | | - // Nothing calculated |
192 | | - return false; |
| 187 | + // Check the asset does not has embodied impact data in its model |
| 188 | + $glpi_model_class = $itemtype . 'Model'; |
| 189 | + $glpi_model_class_fk = getForeignKeyFieldForItemType($glpi_model_class); |
| 190 | + /** |
| 191 | + * @var class-string<AbstractModel> $model_class |
| 192 | + */ |
| 193 | + $model_class = 'GlpiPlugin\\Carbon\\' . $glpi_model_class; |
| 194 | + $glpi_model_id = $item->fields[$glpi_model_class_fk]; |
| 195 | + $model = new $model_class; |
| 196 | + |
| 197 | + if ($model->getFromDBByCrit([$glpi_model_class_fk => $glpi_model_id]) !== false) { |
| 198 | + $impacts = $this->getModelImpacts($model); |
| 199 | + } else { |
| 200 | + try { |
| 201 | + $impacts = $this->doEvaluation($item); |
| 202 | + } catch (\RuntimeException $e) { |
| 203 | + return false; |
| 204 | + } |
| 205 | + |
| 206 | + if ($impacts === null) { |
| 207 | + // Nothing calculated |
| 208 | + return false; |
| 209 | + } |
193 | 210 | } |
194 | 211 |
|
195 | 212 | // Find an existing row, if any |
@@ -312,4 +329,14 @@ public function resetForItem(int $items_id): bool |
312 | 329 | 'items_id' => $items_id |
313 | 330 | ]); |
314 | 331 | } |
| 332 | + |
| 333 | + protected function getModelImpacts(CommonDBTM $model): array |
| 334 | + { |
| 335 | + $impacts = []; |
| 336 | + $types = Type::getImpactTypes(); |
| 337 | + foreach ($types as $key => $type) { |
| 338 | + $impacts[$key] = new TrackedFloat($model->fields[$type], null, $model->fields[$type . '_quality']); |
| 339 | + }; |
| 340 | + return $impacts; |
| 341 | + } |
315 | 342 | } |
0 commit comments