Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 33 additions & 26 deletions install/install/create_automatic_actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,15 @@

use GlpiPlugin\Carbon\CronTask;
use CronTask as GlpiCronTask;
use GlpiPlugin\Carbon\DataSource\CronTaskProvider;

$automatic_actions = [
$cron_task_classes = CronTaskProvider::getCronTaskTypes();
$automatic_actions = [];
foreach ($cron_task_classes as $cron_task_class) {
$automatic_actions = array_merge($automatic_actions, $cron_task_class::enumerateTasks());
}

$automatic_actions = array_merge($automatic_actions, [
[
'itemtype' => CronTask::class,
'name' => 'LocationCountryCode',
Expand All @@ -58,30 +65,30 @@
'param' => 10000, // Maximum rows to generate per execution
]
],
[
'itemtype' => CronTask::class,
'name' => 'DownloadRte',
'frequency' => DAY_TIMESTAMP,
'options' => [
'mode' => GlpiCronTask::MODE_EXTERNAL,
'allowmode' => GlpiCronTask::MODE_INTERNAL + GlpiCronTask::MODE_EXTERNAL,
'logs_lifetime' => 30,
'comment' => __('Collect carbon intensities from RTE', 'carbon'),
'param' => 10000, // Maximum rows to generate per execution
]
],
[
'itemtype' => CronTask::class,
'name' => 'DownloadElectricityMap',
'frequency' => DAY_TIMESTAMP / 2, // Twice a day
'options' => [
'mode' => GlpiCronTask::MODE_EXTERNAL,
'allowmode' => GlpiCronTask::MODE_INTERNAL + GlpiCronTask::MODE_EXTERNAL,
'logs_lifetime' => 30,
'comment' => __('Collect carbon intensities from ElectricityMap', 'carbon'),
'param' => 10000, // Maximum rows to generate per execution
]
],
// [
// 'itemtype' => CronTask::class,
// 'name' => 'DownloadRte',
// 'frequency' => DAY_TIMESTAMP,
// 'options' => [
// 'mode' => GlpiCronTask::MODE_EXTERNAL,
// 'allowmode' => GlpiCronTask::MODE_INTERNAL + GlpiCronTask::MODE_EXTERNAL,
// 'logs_lifetime' => 30,
// 'comment' => __('Collect carbon intensities from RTE', 'carbon'),
// 'param' => 10000, // Maximum rows to generate per execution
// ]
// ],
// [
// 'itemtype' => CronTask::class,
// 'name' => 'DownloadElectricityMap',
// 'frequency' => DAY_TIMESTAMP / 2, // Twice a day
// 'options' => [
// 'mode' => GlpiCronTask::MODE_EXTERNAL,
// 'allowmode' => GlpiCronTask::MODE_INTERNAL + GlpiCronTask::MODE_EXTERNAL,
// 'logs_lifetime' => 30,
// 'comment' => __('Collect carbon intensities from ElectricityMap', 'carbon'),
// 'param' => 10000, // Maximum rows to generate per execution
// ]
// ],
[
'itemtype' => CronTask::class,
'name' => 'EmbodiedImpact',
Expand All @@ -94,7 +101,7 @@
'param' => 10000, // Maximum rows to generate per execution
]
],
];
]);

foreach ($automatic_actions as $action) {
$task = new GlpiCronTask();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/**
* -------------------------------------------------------------------------
* Carbon plugin for GLPI
*
* @copyright Copyright (C) 2024-2025 Teclib' and contributors.
* @license https://www.gnu.org/licenses/gpl-3.0.txt GPLv3+
* @link https://github.com/pluginsGLPI/carbon
*
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of Carbon plugin for GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* -------------------------------------------------------------------------
*/

use CronTask as GLPICronTask;

$task = new GlpiCronTask();
$task->getFromDBByCrit([
'itemtype' => 'GlpiPlugin\\Carbon\\CronTask',
'name' => 'DownloadRte',
]);
if (!$task->isNewItem()) {
$task->update([
'id' => $task->getID(),
'itemtype' => 'GlpiPlugin\\Carbon\\DataSource\\CarbonIntensity\\Rte\\CronTask',
]);
}

$task = new GlpiCronTask();
$task->getFromDBByCrit([
'itemtype' => 'GlpiPlugin\\Carbon\\CronTask',
'name' => 'DownloadElectricityMap',
]);
if (!$task->isNewItem()) {
$task->update([
'id' => $task->getID(),
'itemtype' => 'GlpiPlugin\\Carbon\\DataSource\\CarbonIntensity\\ElectricityMaps\\CronTask',
]);
}
3 changes: 3 additions & 0 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@
*/

use Config as GlpiConfig;
use CronTask as GlpiCronTask;
use GlpiPlugin\Carbon\Dashboard\Widget;
use Glpi\Plugin\Hooks;
use GlpiPlugin\Carbon\Config;
use GlpiPlugin\Carbon\CronTask;
use GlpiPlugin\Carbon\UsageInfo;
use GlpiPlugin\Carbon\Profile;
use GlpiPlugin\Carbon\Report;
Expand Down Expand Up @@ -147,6 +149,7 @@ function plugin_carbon_registerClasses()
Plugin::registerClass(Config::class, ['addtabon' => GlpiConfig::class]);
Plugin::registerClass(Profile::class, ['addtabon' => GlpiProfile::class]);
Plugin::registerClass(Location::class, ['addtabon' => GlpiLocation::class]);
Plugin::registerClass(CronTask::class, ['addtabon' => GlpiCronTask::class]);

foreach (PLUGIN_CARBON_TYPES as $itemtype) {
$core_type_class = $itemtype . 'Type';
Expand Down
4 changes: 2 additions & 2 deletions src/CarbonIntensity.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function rawSearchOptions()
'name' => __('Intensity', 'carbon'),
'massiveaction' => false, // implicit field is id
'datatype' => 'decimal',
'unit' => 'gCO<sub>2</sub>eq/KWh',
'unit' => 'gCOeq/KWh',
];

return $tab;
Expand Down Expand Up @@ -257,7 +257,7 @@ public function downloadOneZone(ClientInterface $data_source, string $zone_name,
}
}

return 0;
return $total_count;
}

/**
Expand Down
104 changes: 71 additions & 33 deletions src/CronTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@

namespace GlpiPlugin\Carbon;

use CommonDBTM;
use CommonGLPI;
use CronTask as GlpiCronTask;
use Config as GlpiConfig;
use Geocoder\Geocoder;
use GlpiPlugin\Carbon\DataSource\CarbonIntensity\ClientFactory;
use GlpiPlugin\Carbon\DataSource\CarbonIntensity\ClientInterface;
use GlpiPlugin\Carbon\DataSource\CronTaskProvider;
use GlpiPlugin\Carbon\Impact\Embodied\AbstractEmbodiedImpact;
use GlpiPlugin\Carbon\Impact\Embodied\Engine as EmbodiedEngine;
use GlpiPlugin\Carbon\Impact\Usage\UsageImpactInterface as UsageImpactInterface;
Expand All @@ -45,10 +48,45 @@
use Location as GlpiLocation;
use Toolbox as GlpiToolbox;

class CronTask
class CronTask extends CommonGLPI
{
private $getGeocoder = [Location::class, 'getGeocoder'];

public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
{
// Delegate to the client's crontask class the tab name to return
// But keep here the logic to decide if a tab name shall be returned
// to reduce class loading
if (!is_a($item, GlpiCronTask::class)) {
return '';
}
if (!in_array($item->fields['itemtype'], CronTaskProvider::getCronTaskTypes())) {
return '';
}
$client_cron_task = new $item->fields['itemtype']();
return $client_cron_task->getTabNameForItem($item);
}

public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0)
{
if (is_a($item, GlpiCronTask::class)) {
/** @var GlpiCronTask $item */
$cron_task = new self();
$cron_task->showForCronTask($item);
}
return true;
}

public function showForCronTask(CommonDBTM $item)
{
$itemtype = $item->fields['itemtype'];
if (!in_array($itemtype, CronTaskProvider::getCronTaskTypes())) {
return;
}
$crontask = new $itemtype();
$crontask->showForCronTask($item);
}

/**
* Get description of an automatic action
*
Expand All @@ -65,17 +103,17 @@ public static function cronInfo(string $name)
'parameter' => __('Maximum number of locations to solve', 'carbon'),
];

case 'DownloadRte':
return [
'description' => __('Download carbon emissions from RTE', 'carbon'),
'parameter' => __('Maximum number of entries to download', 'carbon'),
];
// case 'DownloadRte':
// return [
// 'description' => __('Download carbon emissions from RTE', 'carbon'),
// 'parameter' => __('Maximum number of entries to download', 'carbon'),
// ];

case 'DownloadElectricityMap':
return [
'description' => __('Download carbon emissions from ElectricityMap', 'carbon'),
'parameter' => __('Maximum number of entries to download', 'carbon'),
];
// case 'DownloadElectricityMap':
// return [
// 'description' => __('Download carbon emissions from ElectricityMap', 'carbon'),
// 'parameter' => __('Maximum number of entries to download', 'carbon'),
// ];

case 'DownloadWatttime':
return [
Expand Down Expand Up @@ -198,27 +236,27 @@ public static function cronEmbodiedImpact(GlpiCronTask $task): int
return ($count > 0 ? 1 : 0);
}

/**
* Automatic action for RTE datasource
*
* @return int
*/
public static function cronDownloadRte(GlpiCronTask $task): int
{
$client = ClientFactory::create('Rte');
return self::downloadCarbonIntensityFromSource($task, $client, new CarbonIntensity());
}

/**
* Automatic action for ElectricityMap datasource
*
* @return int
*/
public static function cronDownloadElectricityMap(GlpiCronTask $task): int
{
$client = ClientFactory::create('ElectricityMaps');
return self::downloadCarbonIntensityFromSource($task, $client, new CarbonIntensity());
}
// /**
// * Automatic action for RTE datasource
// *
// * @return int
// */
// public static function cronDownloadRte(GlpiCronTask $task): int
// {
// $client = ClientFactory::create('Rte');
// return self::downloadCarbonIntensityFromSource($task, $client, new CarbonIntensity());
// }

// /**
// * Automatic action for ElectricityMap datasource
// *
// * @return int
// */
// public static function cronDownloadElectricityMap(GlpiCronTask $task): int
// {
// $client = ClientFactory::create('ElectricityMaps');
// return self::downloadCarbonIntensityFromSource($task, $client, new CarbonIntensity());
// }

/**
* Automatic action for Watttime datasource
Expand All @@ -239,7 +277,7 @@ public static function cronDownloadWatttime(GlpiCronTask $task): int
* @param CarbonIntensity $intensity
* @return integer
*/
protected static function downloadCarbonIntensityFromSource(GlpiCronTask $task, ClientInterface $data_source, CarbonIntensity $intensity): int
public static function downloadCarbonIntensityFromSource(GlpiCronTask $task, ClientInterface $data_source, CarbonIntensity $intensity): int
{
$task->setVolume(0); // start with zero
$remaining = $task->fields['param'];
Expand Down
45 changes: 45 additions & 0 deletions src/DataSource/AbstractCronTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* -------------------------------------------------------------------------
* Carbon plugin for GLPI
*
* @copyright Copyright (C) 2024-2025 Teclib' and contributors.
* @license https://www.gnu.org/licenses/gpl-3.0.txt GPLv3+
* @link https://github.com/pluginsGLPI/carbon
*
* -------------------------------------------------------------------------
*
* LICENSE
*
* This file is part of Carbon plugin for GLPI.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* -------------------------------------------------------------------------
*/

namespace GlpiPlugin\Carbon\DataSource;

use CommonDBTM;
use CommonGLPI;
use GlpiPlugin\Carbon\Source_Zone;

abstract class AbstractCronTask extends CommonGLPI implements CronTaskInterface
{
public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
{
return '';
}
}
3 changes: 2 additions & 1 deletion src/DataSource/CarbonIntensity/ElectricityMaps/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ public function getSupportedZones(): array
public function fetchDay(DateTimeImmutable $day, string $zone): array
{
$source_zone = new Source_Zone();
$zone_code = $source_zone->getFromDbBySourceAndZone($this->getSourceName(), $zone);
$source_zone->getFromDbBySourceAndZone($this->getSourceName(), $zone);
$zone_code = $source_zone->fields['code'];

if ($zone_code === null) {
throw new AbortException('Invalid zone');
Expand Down
Loading