Skip to content

Commit 4c28f3e

Browse files
committed
refactor(DataSource\CarbonIntensity\ElectricityMaps\CronTask): migrate task
1 parent 8edfa0b commit 4c28f3e

File tree

5 files changed

+117
-14
lines changed

5 files changed

+117
-14
lines changed

install/install/create_automatic_actions.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,18 @@
7777
// 'param' => 10000, // Maximum rows to generate per execution
7878
// ]
7979
// ],
80-
[
81-
'itemtype' => CronTask::class,
82-
'name' => 'DownloadElectricityMap',
83-
'frequency' => DAY_TIMESTAMP / 2, // Twice a day
84-
'options' => [
85-
'mode' => GlpiCronTask::MODE_EXTERNAL,
86-
'allowmode' => GlpiCronTask::MODE_INTERNAL + GlpiCronTask::MODE_EXTERNAL,
87-
'logs_lifetime' => 30,
88-
'comment' => __('Collect carbon intensities from ElectricityMap', 'carbon'),
89-
'param' => 10000, // Maximum rows to generate per execution
90-
]
91-
],
80+
// [
81+
// 'itemtype' => CronTask::class,
82+
// 'name' => 'DownloadElectricityMap',
83+
// 'frequency' => DAY_TIMESTAMP / 2, // Twice a day
84+
// 'options' => [
85+
// 'mode' => GlpiCronTask::MODE_EXTERNAL,
86+
// 'allowmode' => GlpiCronTask::MODE_INTERNAL + GlpiCronTask::MODE_EXTERNAL,
87+
// 'logs_lifetime' => 30,
88+
// 'comment' => __('Collect carbon intensities from ElectricityMap', 'carbon'),
89+
// 'param' => 10000, // Maximum rows to generate per execution
90+
// ]
91+
// ],
9292
[
9393
'itemtype' => CronTask::class,
9494
'name' => 'EmbodiedImpact',

install/migration/update_1.1.1_to_1.2.0/06_migrate_automatic_actions.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,15 @@
4343
'itemtype' => 'GlpiPlugin\\Carbon\\DataSource\\CarbonIntensity\\Rte\\CronTask',
4444
]);
4545
}
46+
47+
$task = new GlpiCronTask();
48+
$task->getFromDBByCrit([
49+
'itemtype' => 'GlpiPlugin\\Carbon\\CronTask',
50+
'name' => 'DownloadElectricityMap',
51+
]);
52+
if (!$task->isNewItem()) {
53+
$task->update([
54+
'id' => $task->getID(),
55+
'itemtype' => 'GlpiPlugin\\Carbon\\DataSource\\CarbonIntensity\\ElectricityMaps\\CronTask',
56+
]);
57+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
/**
4+
* -------------------------------------------------------------------------
5+
* Carbon plugin for GLPI
6+
*
7+
* @copyright Copyright (C) 2024-2025 Teclib' and contributors.
8+
* @license https://www.gnu.org/licenses/gpl-3.0.txt GPLv3+
9+
* @link https://github.com/pluginsGLPI/carbon
10+
*
11+
* -------------------------------------------------------------------------
12+
*
13+
* LICENSE
14+
*
15+
* This file is part of Carbon plugin for GLPI.
16+
*
17+
* This program is free software: you can redistribute it and/or modify
18+
* it under the terms of the GNU General Public License as published by
19+
* the Free Software Foundation, either version 3 of the License, or
20+
* (at your option) any later version.
21+
*
22+
* This program is distributed in the hope that it will be useful,
23+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
24+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25+
* GNU General Public License for more details.
26+
*
27+
* You should have received a copy of the GNU General Public License
28+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
29+
*
30+
* -------------------------------------------------------------------------
31+
*/
32+
33+
namespace GlpiPlugin\Carbon\DataSource\CarbonIntensity\ElectricityMaps;
34+
35+
use CronTask as GlpiCronTask;
36+
use GlpiPlugin\Carbon\CarbonIntensity;
37+
use GlpiPlugin\Carbon\CronTask as CarbonCronTask;
38+
use GlpiPlugin\Carbon\DataSource\CarbonIntensity\ClientFactory;
39+
use GlpiPlugin\Carbon\DataSource\CronTaskInterface;
40+
41+
class CronTask implements CronTaskInterface
42+
{
43+
public static function enumerateTasks(): array
44+
{
45+
// TODO: This data shoud replace the occurrence in CronTask::cronInfo()
46+
return [
47+
[
48+
'itemtype' => self::class,
49+
'name' => 'DownloadElectricityMap',
50+
'frequency' => DAY_TIMESTAMP / 2,
51+
'options' => [
52+
'mode' => GlpiCronTask::MODE_EXTERNAL,
53+
'allowmode' => GlpiCronTask::MODE_INTERNAL + GlpiCronTask::MODE_EXTERNAL,
54+
'logs_lifetime' => 30,
55+
'comment' => __('Collect carbon intensities from Electricity Maps', 'carbon'),
56+
'param' => 10000, // Maximum rows to generate per execution
57+
]
58+
]
59+
];
60+
}
61+
62+
/**
63+
* Get description of an automatic action
64+
*
65+
* @param string $name
66+
* @return array
67+
*/
68+
public static function cronInfo(string $name): array
69+
{
70+
switch ($name) {
71+
case 'DownloadRte':
72+
return [
73+
'description' => __('Download carbon emissions from Electricity Maps', 'carbon'),
74+
'parameter' => __('Maximum number of entries to download', 'carbon'),
75+
];
76+
}
77+
return [];
78+
}
79+
80+
/**
81+
* Automatic action for Electricity Maps datasource
82+
*
83+
* @return int
84+
*/
85+
public static function cronDownloadElectricityMap(GlpiCronTask $task): int
86+
{
87+
$client = ClientFactory::create('ElectricityMaps');
88+
return CarbonCronTask::downloadCarbonIntensityFromSource($task, $client, new CarbonIntensity());
89+
}
90+
}

src/DataSource/CarbonIntensity/Rte/CronTask.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public static function enumerateTasks(): array
6565
* @param string $name
6666
* @return array
6767
*/
68-
public static function cronInfo(string $name)
68+
public static function cronInfo(string $name): array
6969
{
7070
switch ($name) {
7171
case 'DownloadRte':

tests/install/PluginInstallTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
use Glpi\Plugin\Hooks;
5454
use Glpi\System\Diagnostic\DatabaseSchemaIntegrityChecker;
5555
use GlpiPlugin\Carbon\DataSource\CarbonIntensity\Rte\CronTask as RteCronTask;
56+
use GlpiPlugin\Carbon\DataSource\CarbonIntensity\ElectricityMaps\CronTask as ElectricityMapsCronTask;
5657
use GlpiPlugin\Carbon\CarbonIntensity;
5758
use GlpiPlugin\Carbon\Source;
5859
use GlpiPlugin\Carbon\Source_Zone;
@@ -262,7 +263,7 @@ private function checkAutomaticAction()
262263

263264
$cronTask = new GLPICronTask();
264265
$cronTask->getFromDBByCrit([
265-
'itemtype' => CronTask::class,
266+
'itemtype' => ElectricityMapsCronTask::class,
266267
'name' => 'DownloadElectricityMap',
267268
]);
268269
$this->assertFalse($cronTask->isNewItem());

0 commit comments

Comments
 (0)