Skip to content

Commit 6b687f2

Browse files
committed
feat(CarbonIntensity): report gaps in cron tasks of each client
1 parent 4dbe586 commit 6b687f2

File tree

7 files changed

+127
-19
lines changed

7 files changed

+127
-19
lines changed

setup.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@
3131
*/
3232

3333
use Config as GlpiConfig;
34+
use CronTask as GlpiCronTask;
3435
use GlpiPlugin\Carbon\Dashboard\Widget;
3536
use Glpi\Plugin\Hooks;
3637
use GlpiPlugin\Carbon\Config;
38+
use GlpiPlugin\Carbon\CronTask;
3739
use GlpiPlugin\Carbon\UsageInfo;
3840
use GlpiPlugin\Carbon\Profile;
3941
use GlpiPlugin\Carbon\Report;
@@ -147,6 +149,7 @@ function plugin_carbon_registerClasses()
147149
Plugin::registerClass(Config::class, ['addtabon' => GlpiConfig::class]);
148150
Plugin::registerClass(Profile::class, ['addtabon' => GlpiProfile::class]);
149151
Plugin::registerClass(Location::class, ['addtabon' => GlpiLocation::class]);
152+
Plugin::registerClass(CronTask::class, ['addtabon' => GlpiCronTask::class]);
150153

151154
foreach (PLUGIN_CARBON_TYPES as $itemtype) {
152155
$core_type_class = $itemtype . 'Type';

src/CronTask.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@
3232

3333
namespace GlpiPlugin\Carbon;
3434

35+
use CommonDBTM;
36+
use CommonGLPI;
3537
use CronTask as GlpiCronTask;
3638
use Config as GlpiConfig;
3739
use Geocoder\Geocoder;
3840
use GlpiPlugin\Carbon\DataSource\CarbonIntensity\ClientFactory;
3941
use GlpiPlugin\Carbon\DataSource\CarbonIntensity\ClientInterface;
42+
use GlpiPlugin\Carbon\DataSource\CronTaskProvider;
4043
use GlpiPlugin\Carbon\Impact\Embodied\AbstractEmbodiedImpact;
4144
use GlpiPlugin\Carbon\Impact\Embodied\Engine as EmbodiedEngine;
4245
use GlpiPlugin\Carbon\Impact\Usage\UsageImpactInterface as UsageImpactInterface;
@@ -45,10 +48,45 @@
4548
use Location as GlpiLocation;
4649
use Toolbox as GlpiToolbox;
4750

48-
class CronTask
51+
class CronTask extends CommonGLPI
4952
{
5053
private $getGeocoder = [Location::class, 'getGeocoder'];
5154

55+
public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
56+
{
57+
// Delegate to the client's crontask class the tab name to return
58+
// But keep here the logic to decide if a tab name shall be returned
59+
// to reduce class loading
60+
if (!is_a($item, GlpiCronTask::class)) {
61+
return '';
62+
}
63+
if (!in_array($item->fields['itemtype'], CronTaskProvider::getCronTaskTypes())) {
64+
return '';
65+
}
66+
$client_cron_task = new $item->fields['itemtype']();
67+
return $client_cron_task->getTabNameForItem($item);
68+
}
69+
70+
public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0)
71+
{
72+
if (is_a($item, GlpiCronTask::class)) {
73+
/** @var GlpiCronTask $item */
74+
$cron_task = new self();
75+
$cron_task->showForCronTask($item);
76+
}
77+
return true;
78+
}
79+
80+
public function showForCronTask(CommonDBTM $item)
81+
{
82+
$itemtype = $item->fields['itemtype'];
83+
if (!in_array($itemtype, CronTaskProvider::getCronTaskTypes())) {
84+
return;
85+
}
86+
$crontask = new $itemtype();
87+
$crontask->showForCronTask($item);
88+
}
89+
5290
/**
5391
* Get description of an automatic action
5492
*

src/DataSource/AbstractCronTask.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,12 @@
3434

3535
use CommonDBTM;
3636
use CommonGLPI;
37+
use GlpiPlugin\Carbon\Source_Zone;
3738

3839
abstract class AbstractCronTask extends CommonGLPI implements CronTaskInterface
3940
{
4041
public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
4142
{
4243
return '';
4344
}
44-
45-
public function showForCronTask(CommonDBTM $item)
46-
{
47-
return;
48-
}
49-
}
45+
}

src/DataSource/CarbonIntensity/ElectricityMaps/Client.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,8 @@ public function getSupportedZones(): array
199199
public function fetchDay(DateTimeImmutable $day, string $zone): array
200200
{
201201
$source_zone = new Source_Zone();
202-
$zone_code = $source_zone->getFromDbBySourceAndZone($this->getSourceName(), $zone);
202+
$source_zone->getFromDbBySourceAndZone($this->getSourceName(), $zone);
203+
$zone_code = $source_zone->fields['code'];
203204

204205
if ($zone_code === null) {
205206
throw new AbortException('Invalid zone');

src/DataSource/CarbonIntensity/ElectricityMaps/CronTask.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,16 @@
3232

3333
namespace GlpiPlugin\Carbon\DataSource\CarbonIntensity\ElectricityMaps;
3434

35+
use CommonDBTM;
3536
use CommonGLPI;
3637
use CronTask as GlpiCronTask;
3738
use GlpiPlugin\Carbon\CarbonIntensity;
3839
use GlpiPlugin\Carbon\CronTask as CarbonCronTask;
3940
use GlpiPlugin\Carbon\DataSource\AbstractCronTask;
4041
use GlpiPlugin\Carbon\DataSource\CarbonIntensity\ClientFactory;
4142
use GlpiPlugin\Carbon\DataSource\CronTaskInterface;
43+
use GlpiPlugin\Carbon\DataSource\RestApiClient;
44+
use GlpiPlugin\Carbon\Source_Zone;
4245

4346
class CronTask extends AbstractCronTask implements CronTaskInterface
4447
{
@@ -80,7 +83,7 @@ public static function enumerateTasks(): array
8083
public static function cronInfo(string $name): array
8184
{
8285
switch ($name) {
83-
case 'DownloadRte':
86+
case 'DownloadElectricityMap':
8487
return [
8588
'description' => __('Download carbon emissions from Electricity Maps', 'carbon'),
8689
'parameter' => __('Maximum number of entries to download', 'carbon'),
@@ -99,4 +102,20 @@ public static function cronDownloadElectricityMap(GlpiCronTask $task): int
99102
$client = ClientFactory::create('ElectricityMaps');
100103
return CarbonCronTask::downloadCarbonIntensityFromSource($task, $client, new CarbonIntensity());
101104
}
105+
106+
public function showForCronTask(CommonDBTM $item)
107+
{
108+
switch ($item->fields['name']) {
109+
case 'DownloadElectricityMap':
110+
$client = new Client(new RestApiClient());
111+
$source_name = ($client)->getSourceName();
112+
foreach ($client->getSupportedZones() as $zone_name) {
113+
$source_zone = new Source_Zone();
114+
if (!$source_zone->getFromDbBySourceAndZone($source_name, $zone_name)) {
115+
continue;
116+
}
117+
$source_zone->showGaps();
118+
}
119+
}
120+
}
102121
}

src/DataSource/CarbonIntensity/Rte/CronTask.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
use GlpiPlugin\Carbon\DataSource\CarbonIntensity\ClientFactory;
4242
use GlpiPlugin\Carbon\DataSource\CronTaskInterface;
4343
use GlpiPlugin\Carbon\DataSource\CronTaskProvider;
44+
use GlpiPlugin\Carbon\DataSource\RestApiClient;
45+
use GlpiPlugin\Carbon\Source_Zone;
4446

4547
class CronTask extends AbstractCronTask implements CronTaskInterface
4648
{
@@ -56,12 +58,17 @@ public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
5658

5759
public function showForCronTask(CommonDBTM $item)
5860
{
59-
if (!in_array($item->fields['itemtype'], CronTaskProvider::getCronTaskTypes())) {
60-
return;
61-
}
62-
6361
switch ($item->fields['name']) {
6462
case 'DownloadRte':
63+
$client = new Client(new RestApiClient());
64+
$source_name = ($client)->getSourceName();
65+
foreach ($client->getSupportedZones() as $zone_name) {
66+
$source_zone = new Source_Zone();
67+
if (!$source_zone->getFromDbBySourceAndZone($source_name, $zone_name)) {
68+
continue;
69+
}
70+
$source_zone->showGaps();
71+
}
6572
}
6673
}
6774

src/Source_Zone.php

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,9 @@ public static function showForZone(CommonDBTM $item)
303303
*
304304
* @param string $source_name
305305
* @param string $zone_name
306-
* @return string
306+
* @return bool
307307
*/
308-
public function getFromDbBySourceAndZone(string $source_name, string $zone_name): ?string
308+
public function getFromDbBySourceAndZone(string $source_name, string $zone_name): bool
309309
{
310310
/** @var DBmysql $DB */
311311
global $DB;
@@ -314,7 +314,7 @@ public function getFromDbBySourceAndZone(string $source_name, string $zone_name)
314314
$source_table = Source::getTable();
315315
$source_zone_table = self::getTable();
316316
$request = [
317-
'SELECT' => Source_Zone::getTableField('code'),
317+
'SELECT' => Source_Zone::getTable() . '.id',
318318
'FROM' => $source_zone_table,
319319
'INNER JOIN' => [
320320
$source_table => [
@@ -337,9 +337,11 @@ public function getFromDbBySourceAndZone(string $source_name, string $zone_name)
337337
'LIMIT' => '1'
338338
];
339339
$iterator = $DB->request($request);
340-
$zone_code = $iterator->current()['code'] ?? null;
341-
342-
return $zone_code;
340+
if ($iterator->count() !== 1) {
341+
return false;
342+
}
343+
$id = $iterator->current()['id'];
344+
return $this->getFromDB($id);
343345
}
344346

345347
/**
@@ -499,4 +501,46 @@ public function getOrCreate(array $params, array $where): ?self
499501
$this->update(array_merge($where, $params, ['id' => $this->getID()]));
500502
return $this;
501503
}
504+
505+
/**
506+
* Show gaps in carbon intensities stored for the source and zone of the current instance
507+
*
508+
* @return void
509+
*/
510+
public function showGaps()
511+
{
512+
$canedit = false;
513+
$oldest_asset_date = (new Toolbox())->getOldestAssetDate();
514+
$carbon_intensity = new CarbonIntensity();
515+
$entries = $carbon_intensity->findGaps(
516+
$this->fields['plugin_carbon_sources_id'],
517+
$this->fields['plugin_carbon_zones_id'],
518+
$oldest_asset_date
519+
);
520+
$total = $entries->count();
521+
522+
$renderer = TemplateRenderer::getInstance();
523+
$renderer->display('components/datatable.html.twig', [
524+
'is_tab' => true,
525+
'nopager' => true,
526+
'nofilter' => true,
527+
'nosort' => true,
528+
'columns' => [
529+
'start' => __('Start'),
530+
'end' => __('End', 'carbon'),
531+
],
532+
'footers' => [
533+
['', '', '', __('Total'), $total, '']
534+
],
535+
'footer_class' => 'fw-bold',
536+
'entries' => $entries,
537+
'total_number' => count($entries),
538+
'filtered_number' => count($entries),
539+
'showmassiveactions' => $canedit,
540+
'massiveactionparams' => [
541+
'num_displayed' => count($entries),
542+
'container' => 'mass' . static::class . mt_rand(),
543+
]
544+
]);
545+
}
502546
}

0 commit comments

Comments
 (0)