Skip to content

Commit 795834d

Browse files
committed
Merge branch 'release/1.2.0-beta.1' into support/1.2.0
2 parents 1e18843 + 0e912c1 commit 795834d

16 files changed

Lines changed: 1350 additions & 1189 deletions

File tree

CHANGELOG.md

Lines changed: 507 additions & 1 deletion
Large diffs are not rendered by default.

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@
3535
"license-headers": "vendor/bin/licence-headers-check --ansi --no-interaction",
3636
"build-schema": "tools/build-db-schema.php carbon | plantuml -p -tpng > docs/db-schema.png"
3737
},
38-
"version": "1.0.0-beta.3"
39-
}
38+
"version": "1.2.0-beta.1"
39+
}

install/Install.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public function upgrade(string $from_version, array $args = []): bool
194194
}
195195
}
196196

197-
// Cherry pick install sub tasts to run
197+
// Cherry pick sub tasts to run from fresh install workflow
198198
// Useful to rewrite missing data in DB
199199
$install_dir = __DIR__ . '/install/';
200200
$update_scripts = scandir($install_dir);

package-lock.json

Lines changed: 729 additions & 1152 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "carbon",
33
"description": "Measurement of carbon emissions of GLPI devices",
44
"license": "GPL-3.0-or-later",
5-
"version": "1.0.0-beta.3",
5+
"version": "1.2.0-beta.1",
66
"dependencies": {
77
"apexcharts": "^3.49.0"
88
},

rector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
__DIR__ . '/src',
4848
__DIR__ . '/tools',
4949
])
50+
->withSkipPath(__DIR__ . '/vendor')
5051
->withPhpVersion(PhpVersion::PHP_82)
5152
->withCache(
5253
sys_get_temp_dir() . '/rector',

setup.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@
4646
use Location as GlpiLocation;
4747
use Profile as GlpiProfile;
4848

49-
define('PLUGIN_CARBON_VERSION', '1.2.0-dev');
49+
// Version of the plugin (major.minor.bugfix)
50+
define('PLUGIN_CARBON_VERSION', '1.2.0-beta.1');
51+
// Schema version of this version (major.minor.bugfix)
5052
define('PLUGIN_CARBON_SCHEMA_VERSION', '1.2.0');
5153

5254
// Minimal GLPI version, inclusive
53-
define("PLUGIN_CARBON_MIN_GLPI_VERSION", "11.0.0-beta");
55+
define("PLUGIN_CARBON_MIN_GLPI_VERSION", '11.0.0');
5456
// Maximum GLPI version, exclusive
55-
define("PLUGIN_CARBON_MAX_GLPI_VERSION", "12.0.0");
57+
define("PLUGIN_CARBON_MAX_GLPI_VERSION", '12.0.0');
5658

5759
define('PLUGIN_CARBON_DECIMALS', 3);
5860

src/DataSource/CarbonIntensity/AbstractCronTask.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,27 @@ abstract class AbstractCronTask extends DatasourceAbstractCronTask implements Cr
5050

5151
protected static string $downloadMethod;
5252

53+
/**
54+
* Filter out the gaps to remove fales gaps caused by DST switch
55+
* Needed after a call to Toolbox::findTemporalGapsInTable()
56+
* TODO: replace its inner implementation with this method on each call, when necessary
57+
* In the SQL function DATE_ADD() we may do the following process
58+
* DATE_ADD('2022-03-27 01:00:00', INTERVAL 1 HOUR) and '2022-03-27 01:00:00' + INTERVAL 1 HOUR
59+
* while we use Europe/Paris timezone (or any timezone usinf DST)
60+
* Both expressions return '2022-03-27 02:00:00' and it matches the exact time where we switch to summer time
61+
* '2022-03-27 02:00:00' should be actually '2022-03-27 03:00:00', but this is not what happens with MySQL 8.0
62+
* Therefore when the date '2022-03-27 02:00:00' is converted into a DateTime object in PHP with Europe/Paris timezone
63+
* it is converted into '2022-03-27 03:00:00'.
64+
* When the start of a gap and the end of a gap, both converted into a DateTime object, are equal
65+
* then this means that we are switching to summer time and the gap is irrelevant
66+
* The code below tracks such intervals and filters them out
67+
*
68+
* @param array $gaps
69+
* @param Source_Zone $source_zone
70+
* @return array
71+
*/
72+
abstract protected function dstFilter(array $gaps, Source_Zone $source_zone): array;
73+
5374
public function showForCronTask(CommonDBTM $item)
5475
{
5576
switch ($item->fields['name']) {
@@ -85,6 +106,7 @@ public function showGapsReport()
85106
$source_zone,
86107
$oldest_asset_date
87108
);
109+
$entries = $this->dstFilter($entries, $source_zone);
88110
$total = count($entries);
89111
$zone = Zone::getById($zone_id);
90112

src/DataSource/CarbonIntensity/ElectricityMaps/Client.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ public function fetchRange(DateTimeImmutable $start, DateTimeImmutable $stop, So
285285
// If cached file exists, use it
286286
if (file_exists($cache_file)) {
287287
$full_response = json_decode(file_get_contents($cache_file), true);
288-
return $full_response;
288+
return $full_response['data'];
289289
} else {
290290
$cache_dir = dirname($cache_file);
291291
if (!is_dir($cache_dir)) {
@@ -382,19 +382,14 @@ protected function formatOutput(array $response, int $step): array
382382
// This is needed to detect later the switching to winter time
383383
$response = $this->shiftToLocalTimezone($response);
384384
$intensities = [];
385-
foreach ($response['data'] as $record) {
386-
$datetime = $record['datetime'];
387-
if (!$datetime instanceof DateTimeInterface) {
388-
var_dump(DateTime::getLastErrors());
389-
continue;
390-
}
385+
array_walk($response, function ($record) use (&$intensities) {
391386
$data_quality = $this->getDataQuality($record);
392387
$intensities[] = [
393-
'datetime' => $datetime->format(DateTime::ATOM),
388+
'datetime' => $record['datetime']->format('Y-m-d\TH:00:00'),
394389
'intensity' => $record['carbonIntensity'],
395390
'data_quality' => $data_quality,
396391
];
397-
}
392+
});
398393

399394
return $intensities;
400395
}
@@ -403,7 +398,7 @@ protected function formatOutput(array $response, int $step): array
403398
* convert dates to the timezone of GLPI
404399
*
405400
* @param array $response
406-
* @return array array of records: ['date_heure' => string, 'taux_co2' => number, 'datetime' => DateTime]
401+
* @return array array of records: ['datetime' => DateTime, 'carbonintensity' => number]
407402
*/
408403
protected function shiftToLocalTimezone(array $response): array
409404
{
@@ -412,7 +407,7 @@ protected function shiftToLocalTimezone(array $response): array
412407

413408
$shifted_response = [];
414409
$local_timezone = new DateTimeZone($DB->guessTimezone());
415-
array_walk($response['data'], function ($item, $key) use (&$shifted_response, $local_timezone) {
410+
array_walk($response, function ($item, $key) use (&$shifted_response, $local_timezone) {
416411
$shifted_date_object = DateTime::createFromFormat('Y-m-d\TH:i:s.vp', $item['datetime'])
417412
->setTimezone($local_timezone);
418413
$shifted_date_string = $shifted_date_object->format('Y-m-d H:i:sP');
@@ -423,7 +418,7 @@ protected function shiftToLocalTimezone(array $response): array
423418
$shifted_response[$shifted_date_string] = $item;
424419
});
425420

426-
return ['zone' => $response['zone'], 'data' => $shifted_response];
421+
return $shifted_response;
427422
}
428423

429424
/**

src/DataSource/CarbonIntensity/ElectricityMaps/CronTask.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use CronTask as GlpiCronTask;
3737
use GlpiPlugin\Carbon\DataSource\CarbonIntensity\AbstractCronTask;
3838
use GlpiPlugin\Carbon\DataSource\CronTaskInterface;
39+
use GlpiPlugin\Carbon\Source_Zone;
3940

4041
class CronTask extends AbstractCronTask implements CronTaskInterface
4142
{
@@ -89,4 +90,12 @@ public static function cronInfo(string $name): array
8990
}
9091
return [];
9192
}
93+
94+
protected function dstFilter(array $gaps, Source_Zone $source_zone): array
95+
{
96+
// TODO: find a way to handle electricitymaps territory codes
97+
// They don't match any norm (US-CAL, US-CAR)
98+
// The codes with a 3rd name (US-CAL-CISO) seems to mention an energy distribution or production entity
99+
return $gaps;
100+
}
92101
}

0 commit comments

Comments
 (0)