Skip to content

Commit 82979d1

Browse files
committed
WIP
1 parent ec3068e commit 82979d1

29 files changed

+259
-257
lines changed

ajax/toggleZoneDownload.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
* -------------------------------------------------------------------------
3131
*/
3232

33-
use GlpiPlugin\Carbon\CarbonIntensitySource;
34-
use GlpiPlugin\Carbon\CarbonIntensitySource_Zone;
33+
use GlpiPlugin\Carbon\Source;
34+
use GlpiPlugin\Carbon\Source_Zone;
3535

3636
include(__DIR__ . '/../../../inc/includes.php');
3737

@@ -41,7 +41,7 @@
4141
die();
4242
}
4343

44-
if (!CarbonIntensitySource::canView()) {
44+
if (!Source::canView()) {
4545
// Will die
4646
http_response_code(403);
4747
die();
@@ -52,7 +52,7 @@
5252
die();
5353
}
5454

55-
$source_zone = new CarbonIntensitySource_Zone();
55+
$source_zone = new Source_Zone();
5656
if (!$source_zone->getFromDB($_GET['id'])) {
5757
http_response_code(403);
5858
die();

hook.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@
3636
use GlpiPlugin\Carbon\Install;
3737
use GlpiPlugin\Carbon\Uninstall;
3838
use GlpiPlugin\Carbon\UsageInfo;
39-
use GlpiPlugin\Carbon\CarbonIntensitySource;
39+
use GlpiPlugin\Carbon\Source;
4040
use GlpiPlugin\Carbon\Zone;
4141
use ComputerType as GlpiComputerType;
4242
use GlpiPlugin\Carbon\CarbonEmission;
4343
use GlpiPlugin\Carbon\CarbonIntensity;
4444
use MonitorType as GlpiMonitorType;
4545
use NetworkEquipmentType as GlpiNetworkEquipmentType;
46-
use GlpiPlugin\Carbon\CarbonIntensitySource_Zone;
46+
use GlpiPlugin\Carbon\Source_Zone;
4747
use GlpiPlugin\Carbon\Config;
4848
use GlpiPlugin\Carbon\EmbodiedImpact;
4949
use GlpiPlugin\Carbon\Location;
@@ -144,7 +144,7 @@ function plugin_carbon_getDropdown()
144144
{
145145
return [
146146
ComputerUsageProfile::class => ComputerUsageProfile::getTypeName(),
147-
CarbonIntensitySource::class => CarbonIntensitySource::getTypeName(),
147+
Source::class => Source::getTypeName(),
148148
Zone::class => Zone::getTypeName(),
149149
CarbonIntensity::class => CarbonIntensity::getTypeName(),
150150
];
@@ -212,10 +212,10 @@ function plugin_carbon_hook_add_asset(CommonDBTM $item)
212212
if ($zone === null) {
213213
return;
214214
}
215-
$source_zone = new CarbonIntensitySource_Zone();
215+
$source_zone = new Source_Zone();
216216
$source_zone->getFromDBByCrit([
217217
$zone->getForeignKeyField() => $zone->fields['id'],
218-
CarbonIntensitySource::getForeignKeyField() => $zone->fields['plugin_carbon_carbonintensitysources_id_historical'],
218+
Source::getForeignKeyField() => $zone->fields['plugin_carbon_sources_id_historical'],
219219
]);
220220
if ($source_zone->isNewItem()) {
221221
return;
@@ -239,10 +239,10 @@ function plugin_carbon_hook_update_asset(CommonDBTM $item)
239239
if ($zone === null) {
240240
return;
241241
}
242-
$source_zone = new CarbonIntensitySource_Zone();
242+
$source_zone = new Source_Zone();
243243
$source_zone->getFromDBByCrit([
244244
$zone->getForeignKeyField() => $zone->fields['id'],
245-
CarbonIntensitySource::getForeignKeyField() => $zone->fields['plugin_carbon_carbonintensitysources_id_historical'],
245+
Source::getForeignKeyField() => $zone->fields['plugin_carbon_sources_id_historical'],
246246
]);
247247
if ($source_zone->isNewItem()) {
248248
return;

install/Install.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535
use Config;
3636
use DBmysql;
3737
use DirectoryIterator;
38-
use Glpi\Message\MessageType;
39-
use Glpi\Toolbox\Sanitizer;
4038
use Migration;
4139
use Plugin;
4240

@@ -251,7 +249,7 @@ private function getMigrationsToDo(string $current_version): array
251249
*/
252250
public static function getOrCreateSource(string $name, int $is_fallback = 1): int
253251
{
254-
$source = new CarbonIntensitySource();
252+
$source = new Source();
255253
$source->getFromDBByCrit(['name' => $name]);
256254
if ($source->isNewItem()) {
257255
$source->add([
@@ -280,7 +278,7 @@ public static function getOrCreateZone(string $name, int $source_id): int
280278
if ($zone->isNewItem()) {
281279
$zone->add([
282280
'name' => $name,
283-
'plugin_carbon_carbonintensitysources_id_historical' => $source_id,
281+
'plugin_carbon_sources_id_historical' => $source_id,
284282
]);
285283
/** @phpstan-ignore if.alwaysTrue */
286284
if ($zone->isNewItem()) {
@@ -300,15 +298,15 @@ public static function getOrCreateZone(string $name, int $source_id): int
300298
*/
301299
public static function linkSourceZone(int $source_id, int $zone_id): int
302300
{
303-
$source_zone = new CarbonIntensitySource_Zone();
301+
$source_zone = new Source_Zone();
304302
$source_zone->getFromDBByCrit([
305-
'plugin_carbon_carbonintensitysources_id' => $source_id,
306-
'plugin_carbon_zones_id' => $zone_id,
303+
'plugin_carbon_sources_id' => $source_id,
304+
'plugin_carbon_zones_id' => $zone_id,
307305
]);
308306
if ($source_zone->isNewItem()) {
309307
$source_zone->add([
310-
'plugin_carbon_carbonintensitysources_id' => $source_id,
311-
'plugin_carbon_zones_id' => $zone_id,
308+
'plugin_carbon_sources_id' => $source_id,
309+
'plugin_carbon_zones_id' => $zone_id,
312310
]);
313311
}
314312

install/install/init_datasources.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,20 @@
3333
use GlpiPlugin\Carbon\Install;
3434
use Symfony\Component\Console\Helper\ProgressBar;
3535
use Symfony\Component\Console\Output\ConsoleOutput;
36+
use GlpiPlugin\Carbon\Source;
37+
use GlpiPlugin\Carbon\Source_Zone;
38+
use GlpiPlugin\Carbon\Zone;
3639

3740
/** @var DBmysql $DB */
3841
global $DB;
3942

43+
// This file ys executed at the end of an upgrade.
44+
// Upgrade to 1.2.0 changed the tables modified by this code
45+
// then we need to reset DB columns cache
46+
$DB->listFields(getTableForItemType(Source::class), false);
47+
$DB->listFields(getTableForItemType(Source_Zone::class), false);
48+
$DB->listFields(getTableForItemType(Zone::class), false);
49+
4050
$source_id = Install::getOrCreateSource('RTE', 0);
4151
$zone_id = Install::getOrCreateZone('France', $source_id);
4252

@@ -94,13 +104,13 @@
94104
// Insert into the database
95105
$row_exists = (new DbUtils())->countElementsInTable($table, [
96106
'date' => "$year-01-01 00:00:00",
97-
'plugin_carbon_carbonintensitysources_id' => $source_id,
107+
'plugin_carbon_sources_id' => $source_id,
98108
'plugin_carbon_zones_id' => $zone_id
99109
]);
100110
if (!$row_exists) {
101111
$success = $DB->insert($table, [
102112
'date' => "$year-01-01 00:00:00",
103-
'plugin_carbon_carbonintensitysources_id' => $source_id,
113+
'plugin_carbon_sources_id' => $source_id,
104114
'plugin_carbon_zones_id' => $zone_id,
105115
'intensity' => $intensity,
106116
'data_quality' => 2 // constant GlpiPlugin\Carbon\DataTracking::DATA_QUALITY_ESTIMATED
@@ -111,7 +121,7 @@
111121
'data_quality' => 2 // constant GlpiPlugin\Carbon\DataTracking::DATA_QUALITY_ESTIMATED
112122
], [
113123
'date' => "$year-01-01 00:00:00",
114-
'plugin_carbon_carbonintensitysources_id' => $source_id,
124+
'plugin_carbon_sources_id' => $source_id,
115125
'plugin_carbon_zones_id' => $zone_id
116126
]);
117127
}
@@ -134,13 +144,13 @@
134144
foreach ($quebec_carbon_intensity as $year => $intensity) {
135145
$row_exists = (new DbUtils())->countElementsInTable($table, [
136146
'date' => "$year-01-01 00:00:00",
137-
'plugin_carbon_carbonintensitysources_id' => $source_id,
147+
'plugin_carbon_sources_id' => $source_id,
138148
'plugin_carbon_zones_id' => $zone_id_quebec,
139149
]);
140150
if (!$row_exists) {
141151
$success = $DB->insert($table, [
142152
'date' => "$year-01-01 00:00:00",
143-
'plugin_carbon_carbonintensitysources_id' => $source_id,
153+
'plugin_carbon_sources_id' => $source_id,
144154
'plugin_carbon_zones_id' => $zone_id_quebec,
145155
'intensity' => $intensity,
146156
'data_quality' => 2 // constant GlpiPlugin\Carbon\DataTracking::DATA_QUALITY_ESTIMATED
@@ -151,7 +161,7 @@
151161
'data_quality' => 2 // constant GlpiPlugin\Carbon\DataTracking::DATA_QUALITY_ESTIMATED
152162
], [
153163
'date' => "$year-01-01 00:00:00",
154-
'plugin_carbon_carbonintensitysources_id' => $source_id,
164+
'plugin_carbon_carbonintensitsources_id' => $source_id,
155165
'plugin_carbon_zones_id' => $zone_id
156166
]);
157167
}

install/migration/update_1.1.0_to_1.2.0.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@
3030
* -------------------------------------------------------------------------
3131
*/
3232

33-
function update001to100(Migration $migration)
33+
function update110to120(Migration $migration)
3434
{
3535
/** @var DBmysql $DB */
3636
global $DB;
3737

3838
$updateresult = true;
39-
$from_version = '0.0.1';
40-
$to_version = '1.0.0';
39+
$from_version = '1.1.0';
40+
$to_version = '1.2.0';
4141
$update_dir = __DIR__ . "/update_{$from_version}_to_{$to_version}/";
4242

4343
//TRANS: %s is the number of new version

install/migration/update_1.1.0_to_1.2.0/01_rename_carbonintensitysource.php

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,27 +39,19 @@
3939
$new_table = 'glpi_plugin_carbon_sources';
4040
$migration->renameTable($old_table, $new_table);
4141

42-
$old_itemtype = '\\GlpiPlugin\\Carbon\\CarbonIntensitySource';
43-
$new_itemtype = '\\GlpiPlugin\\Carbon\\Source';
44-
45-
// Update display preferences
46-
$DB->update(DisplayPreference::getTable(), [
47-
'itemtype' => $new_itemtype
48-
], [
49-
'itemtype' => $old_itemtype
50-
]);
51-
5242
// Rename CarbonIntensitySource_Zone
5343
$old_table = 'glpi_plugin_carbon_carbonintensitysources_zones';
5444
$new_table = 'glpi_plugin_carbon_sources_zones';
5545
$migration->renameTable($old_table, $new_table);
5646

57-
$old_itemtype = '\\GlpiPlugin\\Carbon\\CarbonIntensitySource_Zone';
58-
$new_itemtype = '\\GlpiPlugin\\Carbon\\Source_Zone';
47+
$table = 'glpi_plugin_carbon_carbonintensities';
48+
$migration->changeField($table, 'plugin_carbon_carbonintensitysources_id', 'plugin_carbon_sources_id', 'fkey');
49+
50+
$table = 'glpi_plugin_carbon_zones';
51+
$migration->changeField($table, 'plugin_carbon_carbonintensitysources_id_historical', 'plugin_carbon_sources_id_historical', 'fkey');
52+
$migration->dropKey($table, 'plugin_carbon_carbonintensitysources_id_historical');
53+
$migration->addKey($table, 'plugin_carbon_sources_id_historical', 'plugin_carbon_sources_id_historical');
54+
5955

60-
// Update display preferences
61-
$DB->update(DisplayPreference::getTable(), [
62-
'itemtype' => $new_itemtype
63-
], [
64-
'itemtype' => $old_itemtype
65-
]);
56+
$table = 'glpi_plugin_carbon_sources_zones';
57+
$migration->changeField($table, 'plugin_carbon_carbonintensitysources_id', 'plugin_carbon_sources_id', 'fkey');

src/CarbonIntensity.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
use DateTimeImmutable;
3939
use DateTimeInterface;
4040
use DBmysql;
41-
use GlpiPlugin\Carbon\CarbonIntensitySource;
41+
use GlpiPlugin\Carbon\Source;
4242
use GlpiPlugin\Carbon\Zone;
4343
use GlpiPlugin\Carbon\DataSource\CarbonIntensityInterface;
4444
use Glpi\DBAL\QueryParam;
@@ -92,9 +92,9 @@ public function rawSearchOptions()
9292

9393
$tab[] = [
9494
'id' => SearchOptions::CARBON_INTENSITY_SOURCE,
95-
'table' => CarbonIntensitySource::getTable(),
95+
'table' => Source::getTable(),
9696
'field' => 'name',
97-
'name' => CarbonIntensitySource::getTypeName(1),
97+
'name' => Source::getTypeName(1),
9898
'massiveaction' => false, // implicit field is id
9999
'datatype' => 'dropdown',
100100
];
@@ -131,15 +131,15 @@ public function rawSearchOptions()
131131
private function getKnownDatesQuery(string $zone_name, string $source_name)
132132
{
133133
$intensity_table = CarbonIntensity::getTable();
134-
$source_table = CarbonIntensitySource::getTable();
134+
$source_table = Source::getTable();
135135
$zone_table = Zone::getTable();
136136
return [
137137
'SELECT' => [$intensity_table => ['id', 'date']],
138138
'FROM' => $intensity_table,
139139
'INNER JOIN' => [
140140
$source_table => [
141141
'FKEY' => [
142-
$intensity_table => CarbonIntensitySource::getForeignKeyField(),
142+
$intensity_table => Source::getForeignKeyField(),
143143
$source_table => 'id',
144144
]
145145
],
@@ -151,7 +151,7 @@ private function getKnownDatesQuery(string $zone_name, string $source_name)
151151
]
152152
],
153153
'WHERE' => [
154-
CarbonIntensitySource::getTableField('name') => $source_name,
154+
Source::getTableField('name') => $source_name,
155155
Zone::getTableField('name') => $zone_name
156156
],
157157
];
@@ -218,7 +218,7 @@ public function downloadOneZone(CarbonIntensityInterface $data_source, string $z
218218
$total_count = 0;
219219

220220
// Check if there are gaps to fill
221-
$source = new CarbonIntensitySource();
221+
$source = new Source();
222222
$source->getFromDBByCrit(['name' => $data_source->getSourceName()]);
223223
$zone = new Zone();
224224
$zone->getFromDBByCrit(['name' => $zone_name]);
@@ -334,7 +334,7 @@ public function save(string $zone_name, string $source_name, array $data): int
334334
global $DB;
335335

336336
$count = 0;
337-
$source = new CarbonIntensitySource();
337+
$source = new Source();
338338
$source->getFromDBByCrit([
339339
'name' => $source_name,
340340
]);
@@ -357,7 +357,7 @@ public function save(string $zone_name, string $source_name, array $data): int
357357
CarbonIntensity::getTable(),
358358
[
359359
'date' => new QueryParam(),
360-
CarbonIntensitySource::getForeignKeyField() => new QueryParam(),
360+
Source::getForeignKeyField() => new QueryParam(),
361361
Zone::getForeignKeyField() => new QueryParam(),
362362
'intensity' => new QueryParam(),
363363
'data_quality' => new QueryParam(),
@@ -399,7 +399,7 @@ public function save(string $zone_name, string $source_name, array $data): int
399399
public function findGaps(int $source_id, int $zone_id, DateTimeInterface $start, ?DateTimeInterface $stop = null): array
400400
{
401401
$criteria = [
402-
CarbonIntensitySource::getForeignKeyField() => $source_id,
402+
Source::getForeignKeyField() => $source_id,
403403
Zone::getForeignKeyField() => $zone_id,
404404
];
405405
$interval = new DateInterval('PT1H');

src/Command/CollectCarbonIntensityCommand.php

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

3535
use DateTimeImmutable;
3636
use GlpiPlugin\Carbon\CarbonIntensity;
37-
use GlpiPlugin\Carbon\CarbonIntensitySource;
37+
use GlpiPlugin\Carbon\Source;
3838
use GlpiPlugin\Carbon\Zone;
3939
use GlpiPlugin\Carbon\DataSource\CarbonIntensityRTE;
4040
use GlpiPlugin\Carbon\DataSource\RestApiClient;
@@ -69,7 +69,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
6969
$output->writeln("<info>$message</info>");
7070

7171
// Create source if not exists
72-
$data_source = new CarbonIntensitySource();
72+
$data_source = new Source();
7373
$source_name = 'RTE';
7474
if (!$data_source->getFromDBByCrit(['name' => $source_name])) {
7575
$data_source->add([

src/Command/CreateFakeCarbonIntensityCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
use DateTimeZone;
3838
use GlpiPlugin\Carbon\CarbonIntensity;
3939
use GlpiPlugin\Carbon\Zone;
40-
use GlpiPlugin\Carbon\CarbonIntensitySource;
40+
use GlpiPlugin\Carbon\Source;
4141
use Symfony\Component\Console\Command\Command;
4242
use Symfony\Component\Console\Helper\ProgressBar;
4343
use Symfony\Component\Console\Input\InputInterface;
@@ -66,7 +66,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
6666

6767
$message = __("Creating data source name", 'carbon');
6868
$output->writeln("<info>$message</info>");
69-
$dataSource = new CarbonIntensitySource();
69+
$dataSource = new Source();
7070
$source_name = 'Fake data';
7171
if (!$dataSource->getFromDBByCrit(['name' => $source_name])) {
7272
$dataSource->add([
@@ -117,7 +117,7 @@ protected function generateFakeData(DateTime $start_date, DateTime $end_date)
117117
$carbon_intensity = new CarbonIntensity();
118118
foreach ($intensities as $date => $intensity) {
119119
$carbon_intensity->add([
120-
'plugin_carbon_carbonintensitysources_id' => $this->source_id,
120+
'plugin_carbon_sources_id' => $this->source_id,
121121
'plugin_carbon_zones_id' => $this->zone_id,
122122
'date' => $date, // Eco2mix seems to provide datetime in
123123
'intensity' => $intensity,

0 commit comments

Comments
 (0)