Skip to content

Commit 9e18d3f

Browse files
committed
fix: prevent recalculate of impacts
1 parent 752f214 commit 9e18d3f

File tree

11 files changed

+70
-28
lines changed

11 files changed

+70
-28
lines changed

src/CronTask.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public static function cronUsageImpact(GlpiCronTask $task): int
118118
/** @var UsageImpactInterface $usage_impact */
119119
$usage_impact = new $usage_impact_type();
120120
$usage_impact->setLimit($limit_per_type);
121-
$count = $usage_impact->evaluateItems();
121+
$count = $usage_impact->evaluateItems($usage_impact->getItemsToEvaluate());
122122
$task->addVolume($count);
123123
}
124124

@@ -130,7 +130,7 @@ public static function cronUsageImpact(GlpiCronTask $task): int
130130
continue;
131131
}
132132
$usage_impact->setLimit($limit_per_type);
133-
$count = $usage_impact->evaluateItems();
133+
$count = $usage_impact->evaluateItems($usage_impact->getItemsToEvaluate());
134134
$task->addVolume($count);
135135
}
136136

@@ -157,7 +157,7 @@ public static function cronEmbodiedImpact(GlpiCronTask $task): int
157157
continue;
158158
}
159159
$embodied_impact->setLimit($limit_per_type);
160-
$count = $embodied_impact->evaluateItems();
160+
$count = $embodied_impact->evaluateItems($embodied_impact->getItemsToEvaluate());
161161
$task->addVolume($count);
162162
}
163163
return ($count > 0 ? 1 : 0);

src/Engine/V1/AbstractPermanent.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ public function getCarbonEmissionPerDay(DateTimeInterface $day, Source_Zone $sou
8181
if ($iterator->count() === 0) {
8282
// Need to fallback to an alternate source
8383
$fallback_source_zone = new Source_Zone();
84-
$fallback_source_zone->getFallbackFromDB($source_zone);
84+
if (!$fallback_source_zone->getFallbackFromDB($source_zone)) {
85+
$fallback_source_zone = null;
86+
}
8587
}
8688
} else {
8789
// The source is already a fallback (exapmple: Quebec does has any realtime source)

src/Engine/V1/AbstractSwitchable.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ protected function computeEmissionPerDay(DateTimeImmutable $start_time, TrackedI
141141
if ($iterator->count() === 0) {
142142
// Need to fallback to an alternate source
143143
$fallback_source_zone = new Source_Zone();
144-
$fallback_source_zone->getFallbackFromDB($source_zone);
144+
if (!$fallback_source_zone->getFallbackFromDB($source_zone)) {
145+
$fallback_source_zone = null;
146+
}
145147
}
146148
} else {
147149
// The source is already a fallback (exapmple: Quebec does has any realtime source)

src/Impact/Embodied/AbstractEmbodiedImpact.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function setLimit(int $limit)
106106
* @param array $crit criterias
107107
* @return DBmysqlIterator
108108
*/
109-
public function getEvaluateItems(array $crit = []): DBmysqlIterator
109+
public function getItemsToEvaluate(array $crit = []): DBmysqlIterator
110110
{
111111
/** @var DBmysql $DB */
112112
global $DB;

src/Impact/Embodied/EmbodiedImpactInterface.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,15 @@ public function setLimit(int $limit);
7272
public function getEvaluableQuery(array $crit = [], bool $entity_restrict = true): array;
7373

7474
/**
75-
* Start the evaluation of all items
75+
* Get an iterator of items to evaluate
76+
*
77+
* @param array $crit
78+
* @return DBmysqlIterator
79+
*/
80+
public function getItemsToEvaluate(array $crit = []): DBmysqlIterator;
81+
82+
/**
83+
* Start the evaluation of items of the given iterator
7684
*
7785
* @param DBmysqlIterator $iterator assets to evaluate providing their IDs
7886
* @return int count of successfully evaluated assets

src/Impact/History/AbstractAsset.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
use DateTimeImmutable;
4040
use DateTimeInterface;
4141
use DBmysql;
42-
use GlpiPlugin\Carbon\Zone;
42+
use DBmysqlIterator;
4343
use GlpiPlugin\Carbon\CarbonEmission;
4444
use GlpiPlugin\Carbon\DataTracking\TrackedFloat;
4545
use GlpiPlugin\Carbon\Engine\V1\EngineInterface;
@@ -114,11 +114,12 @@ public function setLimit(int $limit)
114114
}
115115

116116
/**
117-
* Start the historization of all items
117+
* Get an iterator of items to evaluate
118118
*
119-
* @return int count of entries generated
119+
* @param array $crit criterias
120+
* @return DBmysqlIterator
120121
*/
121-
public function evaluateItems(): int
122+
public function getItemsToEvaluate(array $crit = []): DBmysqlIterator
122123
{
123124
/** @var DBmysql $DB */
124125
global $DB;
@@ -130,10 +131,20 @@ public function evaluateItems(): int
130131
if (!is_subclass_of($itemtype, CommonDBTM::class)) {
131132
throw new \LogicException('Itemtype does not inherits from ' . CommonDBTM::class);
132133
}
134+
$iterator = $DB->request($this->getEvaluableQuery([], false));
133135

134-
$count = 0;
136+
return $iterator;
137+
}
135138

136-
$iterator = $DB->request($this->getEvaluableQuery([], false));
139+
/**
140+
* Start the historization of all items
141+
*
142+
* @return int count of entries generated
143+
*/
144+
public function evaluateItems(DBmysqlIterator $iterator): int
145+
{
146+
/** @var int $count count of successfully evaluated assets */
147+
$count = 0;
137148
foreach ($iterator as $row) {
138149
$count += $this->evaluateItem($row['id']);
139150
if ($this->limit_reached) {

src/Impact/History/AssetInterface.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
namespace GlpiPlugin\Carbon\Impact\History;
3535

3636
use CommonDBTM;
37+
use DBmysqlIterator;
3738
use GlpiPlugin\Carbon\Engine\V1\EngineInterface;
3839

3940
interface AssetInterface
@@ -42,7 +43,9 @@ public static function getEngine(CommonDBTM $item): EngineInterface;
4243

4344
public function setLimit(int $limit);
4445

45-
public function evaluateItems(): int;
46+
public function getItemsToEvaluate(array $crit = []): DBmysqlIterator;
47+
48+
public function evaluateItems(DBmysqlIterator $iterator): int;
4649

4750
public static function showHistorizableDiagnosis(CommonDBTM $item);
4851

src/Impact/Usage/AbstractUsageImpact.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use DBmysql;
3737
use DbUtils;
3838
use CommonDBTM;
39+
use DBmysqlIterator;
3940
use GlpiPlugin\Carbon\DataTracking\AbstractTracked;
4041
use GlpiPlugin\Carbon\UsageImpact;
4142
use GlpiPlugin\Carbon\Impact\Type;
@@ -100,7 +101,7 @@ public function setLimit(int $limit)
100101
$this->limit = $limit;
101102
}
102103

103-
public function evaluateItems(): int
104+
public function getItemsToEvaluate(array $crit = []): DBmysqlIterator
104105
{
105106
/** @var DBmysql $DB */
106107
global $DB;
@@ -113,6 +114,14 @@ public function evaluateItems(): int
113114
throw new \LogicException('Itemtype does not inherits from ' . CommonDBTM::class);
114115
}
115116

117+
$crit[UsageImpact::getTableField('id')] = null;
118+
$iterator = $DB->request($this->getEvaluableQuery($crit, false));
119+
120+
return $iterator;
121+
}
122+
123+
public function evaluateItems(DBmysqlIterator $iterator): int
124+
{
116125
/**
117126
* Huge quantity of SQL queries will be executed
118127
* We NEED to check memory usage to avoid running out of memory
@@ -129,7 +138,6 @@ public function evaluateItems(): int
129138
$attempts_count = 0;
130139
/** @var int $count count of successfully evaluated assets */
131140
$count = 0;
132-
$iterator = $DB->request($this->getEvaluableQuery());
133141
foreach ($iterator as $row) {
134142
if ($this->evaluateItem($row['id'])) {
135143
$count++;

src/Impact/Usage/UsageImpactInterface.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
namespace GlpiPlugin\Carbon\Impact\Usage;
3535

3636
use CommonDBTM;
37+
use DBmysqlIterator;
3738
use GlpiPlugin\Carbon\Engine\V1\EngineInterface;
3839

3940
interface UsageImpactInterface
@@ -69,12 +70,21 @@ public function setLimit(int $limit);
6970
*/
7071
public function getEvaluableQuery(array $crit = []): array;
7172

73+
/**
74+
* Get an iterator of items to evaluate
75+
*
76+
* @param array $crit
77+
* @return DBmysqlIterator
78+
*/
79+
public function getItemsToEvaluate(array $crit = []): DBmysqlIterator;
80+
7281
/**
7382
* Start the evaluation of all items
7483
*
84+
* @param DBmysqlIterator $iterator assets to evaluate providing their IDs
7585
* @return int count of successfully evaluated assets
7686
*/
77-
public function evaluateItems(): int;
87+
public function evaluateItems(DBmysqlIterator $iterator): int;
7888

7989
/**
8090
* Evaluate all impacts of the asset

tests/src/CommonTestCase.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,19 @@ protected function getUniqueString()
143143
}
144144

145145
/**
146-
* @deprecated use createItem instead
147-
*
146+
* @template T of CommonDBTM
148147
* @param class-string<T> $itemtype itemtype to create
149-
* @param array $input
148+
* @param array $crit
150149
* @return T
151150
*/
152-
protected function getItem(string $itemtype, array $input = []): CommonDBTM
151+
protected function getItem(string $itemtype, array $crit = []): CommonDBTM
153152
{
154-
return $this->createItem($itemtype, $input);
153+
$item = new $itemtype();
154+
$this->assertTrue($item->getFromDBByRequest($crit));
155+
return $item;
155156
}
156157

158+
157159
/**
158160
* Create an item of the given itemtype
159161
*

0 commit comments

Comments
 (0)