Skip to content

Commit 0ecbf0f

Browse files
committed
fix(Install): bad relation between Quebec and Hydro Quebec
1 parent 06a5133 commit 0ecbf0f

File tree

6 files changed

+214
-11
lines changed

6 files changed

+214
-11
lines changed

install/install/init_datasources.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@
128128

129129
$source_id = Install::getOrCreateSource('Hydro Quebec');
130130
$zone_id_quebec = Install::getOrCreateZone('Quebec', $source_id);
131-
Install::linkSourceZone($source_id, $zone_id);
131+
Install::linkSourceZone($source_id, $zone_id_quebec);
132132

133133
$quebec_carbon_intensity = include(dirname(__DIR__) . '/data/carbon_intensity/quebec.php');
134134
foreach ($quebec_carbon_intensity as $year => $intensity) {
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
function update100to101(Migration $migration)
34+
{
35+
/** @var DBmysql $DB */
36+
global $DB;
37+
38+
$updateresult = true;
39+
$from_version = '1.0.0';
40+
$to_version = '1.0.1';
41+
$update_dir = __DIR__ . "/update_{$from_version}_to_{$to_version}/";
42+
43+
//TRANS: %s is the number of new version
44+
$migration->displayTitle(sprintf(__('Update to %s'), $to_version));
45+
$migration->setVersion($to_version);
46+
47+
// New tables from enpty.sql file after the migration
48+
// If a script requires a new table, it may create it by itself
49+
50+
$update_scripts = scandir($update_dir);
51+
natcasesort($update_scripts);
52+
foreach ($update_scripts as $update_script) {
53+
if (preg_match('/\.php$/', $update_script) !== 1) {
54+
continue;
55+
}
56+
require $update_dir . $update_script;
57+
}
58+
59+
$dbFile = plugin_carbon_getSchemaPath($to_version);
60+
if ($dbFile === null || !$DB->runFile($dbFile)) {
61+
$migration->displayWarning("Error creating tables : " . $DB->error());
62+
$updateresult = false;
63+
}
64+
65+
// ************ Keep it at the end **************
66+
$migration->executeMigration();
67+
68+
return $updateresult;
69+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
use GlpiPlugin\Carbon\CarbonEmission;
34+
use GlpiPlugin\Carbon\CarbonIntensitySource;
35+
use GlpiPlugin\Carbon\CarbonIntensitySource_Zone;
36+
use GlpiPlugin\Carbon\Zone;
37+
38+
/** @var DBmysql $DB */
39+
global $DB;
40+
41+
$db_utils = new DbUtils();
42+
43+
// Update and fix the bad relation
44+
$source_table = $db_utils->getTableForItemType(CarbonIntensitySource::class);
45+
$zone_table = $db_utils->getTableForItemType(Zone::class);
46+
$source_zone_table = $db_utils->getTableForItemType(CarbonIntensitySource_Zone::class);
47+
$source_iterator = $DB->request([
48+
'SELECT' => 'id',
49+
'FROM' => $source_table,
50+
'WHERE' => ['name' => 'Hydro Quebec']
51+
]);
52+
$zone_iterator = $DB->request([
53+
'SELECT' => 'id',
54+
'FROM' => $zone_table,
55+
'WHERE' => ['name' => 'Quebec']
56+
]);
57+
if ($source_iterator->count() && $zone_iterator->count()) {
58+
$DB->update($source_zone_table, [
59+
'plugin_carbon_zones_id' => $zone_iterator->current()['id'],
60+
], [
61+
'plugin_carbon_carbonintensitysources_id' => $source_iterator->current()['id'],
62+
]);
63+
}
64+
65+
// delete carbon intensity results for computers in Quebec
66+
$itemtypes = [
67+
Computer::class,
68+
Monitor::class,
69+
NetworkEquipment::class
70+
];
71+
72+
$carbon_emission_table = $db_utils->getTableForItemType(CarbonEmission::class);
73+
$location_table = $db_utils->getTableForItemType(Location::class);
74+
foreach ($itemtypes as $itemtype) {
75+
$item_table = $db_utils->getTableForItemType($itemtype);
76+
$request = [
77+
'SELECT' => $item_table . '.id',
78+
'FROM' => $item_table,
79+
'INNER JOIN' => [
80+
$location_table => [
81+
'FKEY' => [
82+
$location_table => 'id',
83+
$item_table => 'locations_id'
84+
]
85+
]
86+
],
87+
'WHERE' => [
88+
Location::getTableField('state') => 'Quebec',
89+
]
90+
];
91+
$subquery = new QuerySubQuery($request);
92+
$DB->delete(
93+
$carbon_emission_table,
94+
[
95+
'itemtype' => $itemtype,
96+
'items_id' => $subquery
97+
]
98+
);
99+
}

tests/install/PluginInstallTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public function testInstallPlugin()
135135
$this->checkInitialCarbonIntensities();
136136
$this->checkDisplayPrefs();
137137
$this->checkPredefinedUsageProfiles();
138+
$this->checkSourceZoneRelation();
138139
}
139140

140141
public function testConfigurationExists()
@@ -770,4 +771,38 @@ public function checkDashboard()
770771
'Montenegro',
771772
'South Sudan'
772773
];
774+
775+
public function checkSourceZoneRelation()
776+
{
777+
/** @var DBmysql */
778+
global $DB;
779+
780+
$source_table = CarbonIntensitySource::getTable();
781+
$zone_table = Zone::getTable();
782+
$source_zone_table = CarbonIntensitySource_Zone::getTable();
783+
784+
$iterator = $DB->request([
785+
'SELECT' => $source_zone_table . '.id',
786+
'FROM' => $source_zone_table,
787+
'INNER JOIN' => [
788+
$source_table => [
789+
'FKEY' => [
790+
$source_zone_table => 'plugin_carbon_carbonintensitysources_id',
791+
$source_table => 'id'
792+
]
793+
],
794+
$zone_table => [
795+
'FKEY' => [
796+
$source_zone_table => 'plugin_carbon_zones_id',
797+
$zone_table => 'id'
798+
]
799+
]
800+
],
801+
'WHERE' => [
802+
$source_table . '.name' => 'Hydro Quebec',
803+
$zone_table . '.name' => 'Quebec',
804+
],
805+
]);
806+
$this->assertEquals(1, $iterator->count());
807+
}
773808
}

tests/units/Impact/History/ComputerTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,11 @@ public function testEvaluateItem()
145145
[
146146
'date' => '2024-02-01 00:00:00',
147147
'energy_per_day' => 0.495,
148-
'emission_per_day' => 234.138,
148+
'emission_per_day' => 17.0775,
149149
],[
150150
'date' => '2024-02-02 00:00:00',
151151
'energy_per_day' => 0.495,
152-
'emission_per_day' => 234.138,
152+
'emission_per_day' => 17.0775,
153153
], [
154154
'date' => '2024-02-03 00:00:00',
155155
'energy_per_day' => 0,
@@ -161,15 +161,15 @@ public function testEvaluateItem()
161161
], [
162162
'date' => '2024-02-05 00:00:00',
163163
'energy_per_day' => 0.495,
164-
'emission_per_day' => 234.138,
164+
'emission_per_day' => 17.0775,
165165
], [
166166
'date' => '2024-02-06 00:00:00',
167167
'energy_per_day' => 0.495,
168-
'emission_per_day' => 234.138,
168+
'emission_per_day' => 17.0775,
169169
], [
170170
'date' => '2024-02-07 00:00:00',
171171
'energy_per_day' => 0.495,
172-
'emission_per_day' => 234.138,
172+
'emission_per_day' => 17.0775,
173173
],
174174
];
175175
foreach ($emissions as $emission) {

tests/units/Impact/History/MonitorTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,11 @@ public function testEvaluateItem()
168168
[
169169
'date' => '2024-02-01 00:00:00',
170170
'energy_per_day' => 0.495,
171-
'emission_per_day' => 234.138,
171+
'emission_per_day' => 17.0775,
172172
],[
173173
'date' => '2024-02-02 00:00:00',
174174
'energy_per_day' => 0.495,
175-
'emission_per_day' => 234.138,
175+
'emission_per_day' => 17.0775,
176176
], [
177177
'date' => '2024-02-03 00:00:00',
178178
'energy_per_day' => 0,
@@ -184,15 +184,15 @@ public function testEvaluateItem()
184184
], [
185185
'date' => '2024-02-05 00:00:00',
186186
'energy_per_day' => 0.495,
187-
'emission_per_day' => 234.138,
187+
'emission_per_day' => 17.0775,
188188
], [
189189
'date' => '2024-02-06 00:00:00',
190190
'energy_per_day' => 0.495,
191-
'emission_per_day' => 234.138,
191+
'emission_per_day' => 17.0775,
192192
], [
193193
'date' => '2024-02-07 00:00:00',
194194
'energy_per_day' => 0.495,
195-
'emission_per_day' => 234.138,
195+
'emission_per_day' => 17.0775,
196196
],
197197
];
198198
foreach ($emissions as $emission) {

0 commit comments

Comments
 (0)