Skip to content

Commit 3c59586

Browse files
committed
feat(Location): remove location / carbon intensity relation based on country or state
1 parent a54f781 commit 3c59586

File tree

9 files changed

+218
-168
lines changed

9 files changed

+218
-168
lines changed

src/Impact/History/Computer.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ public static function getHistorizableDiagnosis(CommonDBTM $item): ?array
220220

221221
// Each state is analyzed, with bool results
222222
// false means that data is missing or invalid for historization
223+
// TODO : rename is_deleted, is_template into is_not_deleted is_not_template
223224
$status['is_deleted'] = ($data['is_deleted'] === 0);
224225
$status['is_template'] = ($data['is_template'] === 0);
225226
$status['has_location'] = !GlpiLocation::isNewID($data['location_id']);

src/Location.php

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -303,18 +303,9 @@ public function onGlpiLocationPreUpdate(CommonDBTM $item, Geocoder $geocoder)
303303
*/
304304
protected function enableCarbonIntensityDownload(CommonDBTM $item): bool
305305
{
306-
if (!in_array('country', array_keys($item->fields))) {
307-
return false;
308-
}
309-
$zone = Zone::getByLocation($item);
310-
if ($zone === null) {
311-
return false;
312-
}
313306
$source_zone = new Source_Zone();
314-
$source_zone->getFromDBByCrit([
315-
Zone::getForeignKeyField() => $zone->fields['id'],
316-
Source::getForeignKeyField() => $zone->fields['plugin_carbon_sources_id_historical'],
317-
]);
307+
/** @var GlpiLocation $item */
308+
$source_zone->getFromDbByLocation($item);
318309
if ($source_zone->isNewItem()) {
319310
return false;
320311
}
@@ -329,65 +320,61 @@ protected function enableCarbonIntensityDownload(CommonDBTM $item): bool
329320
*/
330321
public function isCarbonIntensityDownloadEnabled(CommonDBTM $item): bool
331322
{
332-
$zone = Zone::getByLocation($item);
333-
if ($zone === null) {
334-
return false;
335-
}
336323
$source_zone = new Source_Zone();
337-
$source_zone->getFromDBByCrit([
338-
Zone::getForeignKeyField() => $zone->fields['id'],
339-
Source::getForeignKeyField() => $zone->fields['plugin_carbon_sources_id_historical'],
340-
]);
341-
if ($source_zone->isNewItem()) {
324+
/** @var GlpiLocation $item */
325+
if (!$source_zone->getFromDbByLocation($item)) {
342326
return false;
343327
}
344328
return $source_zone->fields['is_download_enabled'] === 1;
345329
}
346330

347331
/**
348-
* Tells id a location has fallback carbon intensity data
332+
* Tells if a location has fallback carbon intensity data
349333
*
350-
* @param CommonDBTM $item
334+
* @param GlpiLocation $item
351335
* @return boolean
352336
*/
353-
public function hasFallbackCarbonIntensityData(CommonDBTM $item): bool
337+
public function hasFallbackCarbonIntensityData(GlpiLocation $item): bool
354338
{
355339
/** @var DBmysql $DB */
356340
global $DB;
357341

358-
$zone = Zone::getByLocation($item);
359-
if ($zone === null) {
360-
return false;
361-
}
362342
$carbon_intensity_table = CarbonIntensity::getTable();
363-
$carbon_intensity_source_zone_table = Source_Zone::getTable();
364-
$carbon_intensity_source_table = Source::getTable();
343+
$source_zone_table = Source_Zone::getTable();
344+
$source_table = Source::getTable();
345+
$location_table = Location::getTable();
365346
$request = [
366347
'COUNT' => 'count',
367348
'FROM' => $carbon_intensity_table,
368349
'INNER JOIN' => [
369-
$carbon_intensity_source_zone_table => [
350+
$source_zone_table => [
370351
'FKEY' => [
371352
$carbon_intensity_table => 'plugin_carbon_zones_id',
372-
$carbon_intensity_source_zone_table => 'plugin_carbon_zones_id',
353+
$source_zone_table => 'plugin_carbon_zones_id',
373354
]
374355
],
375-
$carbon_intensity_source_table => [
356+
$location_table => [
357+
'FKEY' => [
358+
$location_table => 'plugin_carbon_sources_zones_id',
359+
$source_zone_table => 'id'
360+
]
361+
],
362+
$source_table => [
376363
'FKEY' => [
377-
$carbon_intensity_source_zone_table => 'plugin_carbon_sources_id',
378-
$carbon_intensity_source_table => 'id',
364+
$source_zone_table => 'plugin_carbon_sources_id',
365+
$source_table => 'id',
379366
]
380-
]
367+
],
381368
],
382369
'WHERE' => [
383370
Source::getTableField('is_fallback') => 1,
384-
Source_Zone::getTableField('plugin_carbon_zones_id') => $zone->getID(),
371+
Location::getTableField('locations_id') => $item->getID(),
385372
],
386373
'ORDER' => CarbonIntensity::getTableField('date') . ' DESC',
387374
'LIMIT' => 1,
388375
];
389376
$result = $DB->request($request);
390-
return ($result->count() > 0);
377+
return ($result->current()['count'] > 0);
391378
}
392379

393380
/**

src/Source_Zone.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
use CommonDBTM;
3838
use CronTask;
3939
use DBmysql;
40-
use GlpiPlugin\Carbon\Application\View\Extension\DataHelpersExtension;
40+
use Location as GlpiLocation;
4141
use Glpi\Application\View\TemplateRenderer;
4242
use Html;
4343

@@ -380,4 +380,35 @@ public function toggleZone(?bool $state = null): bool
380380
];
381381
return $this->update($input) !== false;
382382
}
383+
384+
/**
385+
* Get a source_zone by a location criteria
386+
*
387+
* @param GlpiLocation $item
388+
* @return bool
389+
*/
390+
public function getFromDbByLocation(GlpiLocation $item): bool
391+
{
392+
if ($item->isNewItem()) {
393+
return false;
394+
}
395+
396+
$location_table = Location::getTable();
397+
$source_zone_table = Source_Zone::getTable();
398+
$request = [
399+
'INNER JOIN' => [
400+
$location_table => [
401+
'FKEY' => [
402+
$location_table => 'plugin_carbon_sources_zones_id',
403+
$source_zone_table => 'id'
404+
]
405+
],
406+
],
407+
'WHERE' => [
408+
Location::getTableField('locations_id') => $item->getID(),
409+
],
410+
];
411+
412+
return $this->getFromDBByRequest($request);
413+
}
383414
}

src/Zone.php

Lines changed: 67 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -160,42 +160,73 @@ public function rawSearchOptions()
160160
return $tab;
161161
}
162162

163-
/**
164-
* Get a zone by a location criteria
165-
*
166-
* @param CommonDBTM $item
167-
* @return Zone|null
168-
* @todo : de-staticify the method
169-
*/
170-
public static function getByLocation(CommonDBTM $item): ?Zone
171-
{
172-
if ($item->isNewItem()) {
173-
return null;
174-
}
175-
176-
$request = self::getByLocationRequest();
177-
$request['WHERE'] = [
178-
Location::getTableField('locations_id') => $item->getID(),
179-
];
180-
$zone = new self();
181-
if (!$zone->getFromDBByRequest($request)) {
182-
return null;
183-
}
184-
185-
return $zone;
186-
}
163+
// /**
164+
// * Get a zone by a location criteria
165+
// *
166+
// * @param CommonDBTM $item
167+
// * @return Zone|null
168+
// * @todo : de-staticify the method
169+
// */
170+
// public static function getByLocation(CommonDBTM $item): ?Zone
171+
// {
172+
// if ($item->isNewItem()) {
173+
// return null;
174+
// }
175+
176+
// $request = self::getByLocationRequest();
177+
// $request['WHERE'] = [
178+
// Location::getTableField('locations_id') => $item->getID(),
179+
// ];
180+
// $zone = new self();
181+
// if (!$zone->getFromDBByRequest($request)) {
182+
// return null;
183+
// }
184+
185+
// return $zone;
186+
// }
187+
188+
// /**
189+
// * Get the request fragment to find a zone by location
190+
// *
191+
// * @return array Request fragment
192+
// */
193+
// private static function getByLocationRequest(): array
194+
// {
195+
// $location_table = Location::getTable();
196+
// $source_zone_table = Source_Zone::getTable();
197+
// $zone_table = Zone::getTable();
198+
// return [
199+
// 'INNER JOIN' => [
200+
// $source_zone_table => [
201+
// 'FKEY' => [
202+
// $zone_table => 'id',
203+
// $source_zone_table => 'plugin_carbon_zones_id',
204+
// ]
205+
// ],
206+
// $location_table => [
207+
// 'FKEY' => [
208+
// $location_table => 'plugin_carbon_sources_zones_id',
209+
// $source_zone_table => 'id'
210+
// ]
211+
// ],
212+
// ],
213+
// ];
214+
// }
187215

188216
/**
189-
* Get the request fragment to find a zone by location
217+
* Get the request fragment to find a zone by asset
190218
*
219+
* @param class-string $itemtype asset type
191220
* @return array Request fragment
192221
*/
193-
private static function getByLocationRequest(): array
222+
private static function getByAssetRequest(string $itemtype): array
194223
{
195-
$location_table = Location::getTable();
196-
$source_zone_table = Source_Zone::getTable();
197-
$zone_table = Zone::getTable();
198-
return [
224+
$dbUtil = new DbUtils();
225+
$location_table = $dbUtil->getTableForItemType(Location::class);
226+
$source_zone_table = $dbUtil->getTableForItemType(Source_Zone::class);
227+
$zone_table = $dbUtil->getTableForItemType(Zone::class);
228+
$itemtype_table = $dbUtil->getTableForItemType($itemtype);
229+
$request = [
199230
'INNER JOIN' => [
200231
$source_zone_table => [
201232
'FKEY' => [
@@ -209,27 +240,14 @@ private static function getByLocationRequest(): array
209240
$source_zone_table => 'id'
210241
]
211242
],
243+
$itemtype_table => [
244+
'FKEY' => [
245+
$itemtype_table => 'locations_id',
246+
$location_table => 'locations_id',
247+
]
248+
]
212249
],
213250
];
214-
}
215-
216-
/**
217-
* Get the request fragment to find a zone by asset
218-
*
219-
* @param class-string $itemtype asset type
220-
* @return array Request fragment
221-
*/
222-
private static function getByAssetRequest(string $itemtype): array
223-
{
224-
$location_table = Location::getTable();
225-
$itemtype_table = (new DbUtils())->getTableForItemType($itemtype);
226-
$request = self::getByLocationRequest();
227-
$request['INNER JOIN'][$itemtype_table] = [
228-
'FKEY' => [
229-
$itemtype_table => 'locations_id',
230-
$location_table => 'locations_id',
231-
]
232-
];
233251

234252
return $request;
235253
}

tests/units/Impact/History/ComputerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ public function testComputerWithLocationAndZoneIsNotHistorizable()
494494
'has_category' => false,
495495
'has_inventory_entry_date' => false,
496496
'ci_download_enabled' => true,
497-
'ci_fallback_available' => true,
497+
'ci_fallback_available' => false,
498498
];
499499
$result = $history->getHistorizableDiagnosis($computer);
500500
$this->assertEquals($expected, $result);
@@ -744,7 +744,7 @@ public function testComputerWithEverythingIsHistorizable()
744744
'has_category' => false,
745745
'has_inventory_entry_date' => true,
746746
'ci_download_enabled' => true,
747-
'ci_fallback_available' => true,
747+
'ci_fallback_available' => false,
748748
];
749749
$result = $history->getHistorizableDiagnosis($computer);
750750
$this->assertEquals($expected, $result);

tests/units/Impact/History/NetworkEquipmentTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,6 @@ public function testNetDeviceWithLocationWithZoneIsNotHistorizable()
307307
{
308308
$history = new NetworkEquipment();
309309

310-
$glpi_location = $this->createItem(GlpiLocation::class);
311310
$source = new Source(); // This source exists after a fresh install
312311
$source->getFromDBByCrit([
313312
'name' => 'RTE'
@@ -321,6 +320,7 @@ public function testNetDeviceWithLocationWithZoneIsNotHistorizable()
321320
$source::getForeignKeyField() => $source->getID(),
322321
$zone::getForeignKeyField() => $zone->getID()
323322
]);
323+
$glpi_location = $this->createItem(GlpiLocation::class);
324324
$location = $this->createItem(Location::class, [
325325
'locations_id' => $glpi_location->getID(),
326326
'plugin_carbon_sources_zones_id' => $source_zone->getID()
@@ -340,7 +340,7 @@ public function testNetDeviceWithLocationWithZoneIsNotHistorizable()
340340
'has_type_power_consumption' => false,
341341
'has_inventory_entry_date' => false,
342342
'ci_download_enabled' => true,
343-
'ci_fallback_available' => true,
343+
'ci_fallback_available' => false,
344344
];
345345
$this->assertEquals($expected, $result);
346346
$expected = !in_array(false, $result, true);

0 commit comments

Comments
 (0)