Skip to content
9 changes: 8 additions & 1 deletion install/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,18 @@ public function install(array $args = []): bool
global $DB;

$dbFile = plugin_carbon_getSchemaPath();
if ($dbFile === null || !$DB->runFile($dbFile)) {
if ($dbFile === null) {
$this->migration->addWarningMessage("Error creating tables : " . $DB->error());
return false;
}

try {
$DB->runFile($dbFile);
} catch (\RuntimeException $e) {
$this->migration->addWarningMessage("Error creating tables : " . $e->getMessage());
return false;
}

// Execute all install sub tasks
$install_dir = __DIR__ . '/install/';
$update_scripts = scandir($install_dir);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Data source:
https://ourworldindata.org/grapher/carbon-intensity-electricity
44 changes: 22 additions & 22 deletions install/install/init_datasources.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,18 @@
Install::linkSourceZone($source_id, $zone_id, $code);

// Insert into the database
$success = $DB->updateOrInsert($table, [
'intensity' => $intensity,
'data_quality' => 2 // constant GlpiPlugin\Carbon\DataTracking::DATA_QUALITY_ESTIMATED
], [
'date' => "$year-01-01 00:00:00",
'plugin_carbon_sources_id' => $source_id,
'plugin_carbon_zones_id' => $zone_id,
]);

if ($success === false) {
try {
$DB->updateOrInsert($table, [
'intensity' => $intensity,
'data_quality' => 2 // constant GlpiPlugin\Carbon\DataTracking::DATA_QUALITY_ESTIMATED
], [
'date' => "$year-01-01 00:00:00",
'plugin_carbon_sources_id' => $source_id,
'plugin_carbon_zones_id' => $zone_id,
]);
} catch (\RuntimeException $e) {
$file = null; // close the file
throw new \RuntimeException("Failed to insert data for year $year");
throw new \RuntimeException("Failed to insert data for year $year; reason: " . $e->getMessage(), $e->getCode(), $e);
}
}
if ($progress_bar) {
Expand All @@ -129,17 +129,17 @@

$quebec_carbon_intensity = include(dirname(__DIR__) . '/data/carbon_intensity/quebec.php');
foreach ($quebec_carbon_intensity as $year => $intensity) {
$success = $DB->updateOrInsert($table, [
'intensity' => $intensity,
'data_quality' => 2 // constant GlpiPlugin\Carbon\DataTracking::DATA_QUALITY_ESTIMATED
], [
'date' => "$year-01-01 00:00:00",
'plugin_carbon_sources_id' => $source_id,
'plugin_carbon_zones_id' => $zone_id_quebec,
]);

if ($success === false) {
try {
$DB->updateOrInsert($table, [
'intensity' => $intensity,
'data_quality' => 2 // constant GlpiPlugin\Carbon\DataTracking::DATA_QUALITY_ESTIMATED
], [
'date' => "$year-01-01 00:00:00",
'plugin_carbon_sources_id' => $source_id,
'plugin_carbon_zones_id' => $zone_id_quebec,
]);
} catch (\RuntimeException $e) {
$file = null; // close the file
throw new \RuntimeException("Failed to insert data for year $year");
throw new \RuntimeException("Failed to insert data for year $year; reason: " . $e->getMessage(), $e->getCode(), $e);
}
}
8 changes: 7 additions & 1 deletion install/migration/update_1.0.0_to_1.0.1.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,16 @@ function update100to101(Migration $migration)
}

$dbFile = plugin_carbon_getSchemaPath($to_version);
if ($dbFile === null || !$DB->runFile($dbFile)) {
if ($dbFile === null) {
$migration->addWarningMessage("Error creating tables : " . $DB->error());
$updateresult = false;
}
try {
$DB->runFile($dbFile);
} catch (\RuntimeException $e) {
$migration->addWarningMessage("Error creating tables : " . $e->getMessage());
$updateresult = false;
}

// ************ Keep it at the end **************
$migration->executeMigration();
Expand Down
9 changes: 8 additions & 1 deletion install/migration/update_1.0.1_to_1.1.0.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,17 @@ function update101to110(Migration $migration)
}

$dbFile = plugin_carbon_getSchemaPath($to_version);
if ($dbFile === null || !$DB->runFile($dbFile)) {
if ($dbFile === null) {
$migration->addWarningMessage("Error creating tables : " . $DB->error());
$updateresult = false;
}
try {
$DB->runFile($dbFile);
} catch (\RuntimeException $e) {
$migration->addWarningMessage("Error creating tables : " . $e->getMessage());
$updateresult = false;
}


// ************ Keep it at the end **************
$migration->executeMigration();
Expand Down
9 changes: 8 additions & 1 deletion install/migration/update_1.1.1_to_1.2.0.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,17 @@ function update111to120(Migration $migration)
}

$dbFile = plugin_carbon_getSchemaPath($to_version);
if ($dbFile === null || !$DB->runFile($dbFile)) {
if ($dbFile === null) {
$migration->addWarningMessage("Error creating tables : " . $DB->error());
$updateresult = false;
}
try {
$DB->runFile($dbFile);
} catch (\RuntimeException $e) {
$migration->addWarningMessage("Error creating tables : " . $e->getMessage());
$updateresult = false;
}


// ************ Keep it at the end **************
$migration->executeMigration();
Expand Down
118 changes: 118 additions & 0 deletions install/migration/update_1.1.1_to_1.2.0/09_add_impact_criterias.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?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/>.
*
* -------------------------------------------------------------------------
*/

/** @var DBmysql $DB */
/** @var Migration $migration */

use Glpi\Dashboard\Item as DashboardItem;

$new_criterias = [
'gwppb' => '(unit g CO2 eq) Climate change - Contribution of biogenic emissions',
'gwppf' => '(unit g CO2 eq) Climate change - Contribution of fossil fuel emissions',
'gwpplu' => '(unit g CO2 eq) Climate change - Contribution of emissions from land use change',
'ir' => '(unit g U235 eq) Emissions of radionizing substances',
'lu' => '(unit none) Land use',
'odp' => '(unit g CFC-11 eq) Depletion of the ozone layer',
'pm' => '(unit Disease occurrence) Fine particle emissions',
'pocp' => '(unit g NMVOC eq) Photochemical ozone formation',
'wu' => '(unit L) Use of water resources',
'mips' => '(unit g) Material input per unit of service',
'adpe' => '(unit g SB eq) Use of mineral and metal resources',
'adpf' => '(unit J) Use of fossil resources (including nuclear)',
'ap' => '(unit mol H+ eq) Acidification',
'ctue' => '(unit CTUe) Freshwater ecotoxicity',
// ctuh_c => '(unit CTUh) Human toxicity - non-carcinogenic effects',
'epf' => '(unit g P eq) Eutrophication of freshwater',
'epm' => '(unit g N eq) Eutrophication of marine waters',
'ept' => '(unit mol N eq) Terrestrial eutrophication',
];

$table = 'glpi_plugin_carbon_embodiedimpacts';
$previous_criteria = 'pe';
foreach ($new_criterias as $criteria => $comment) {
$migration->addField(
$table,
$criteria,
'float DEFAULT \'0\'',
[
'comment' => $comment,
'after' => $previous_criteria . '_quality',
]
);
$migration->addField(
$table,
$criteria . '_quality',
'int unsigned NOT NULL DEFAULT \'0\'',
[
'comment' => 'DataTtacking\\AbstractTracked::DATA_QUALITY_* constants',
'after' => $criteria,
]
);
// $migration->dropField($table, $criteria);
// $migration->dropField($table, $criteria . '_quality');
$previous_criteria = $criteria;
}

// Uniformize existing impact : make floats signed
$old_criterias = [
'gwp' => '(unit g CO2 eq) Global warming potential',
'adp' => '(unit g Sb eq) Abiotic depletion potential',
'pe' => '(unit J) Primary energy',
];
foreach ($old_criterias as $criteria => $comment) {
$migration->changeField($table, $criteria, $criteria, 'float DEFAULT \'0\'', [
'comment' => $comment,
]);
}

// Rename cards for the report
$dashboard_item = new DashboardItem();
$rows = $dashboard_item->find([
'card_id' => 'plugin_carbon_report_embodied_gwp_impact'
]);
foreach ($rows as $row) {
$dashboard_item->update([
'id' => $row['id'],
'card_id' => 'plugin_carbon_report_embodied_gwp_impact',
]);
}

$dashboard_item = new DashboardItem();
$rows = $dashboard_item->find([
'card_id' => 'plugin_carbon_report_embodied_abiotic_depletion'
]);
foreach ($rows as $row) {
$dashboard_item->update([
'id' => $row['id'],
'card_id' => 'plugin_carbon_report_embodied_adp_impact',
]);
}
9 changes: 8 additions & 1 deletion install/migration/update_x.x.x_to_y.y.y.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,17 @@ function update001to100(Migration $migration)
}

$dbFile = plugin_carbon_getSchemaPath($to_version);
if ($dbFile === null || !$DB->runFile($dbFile)) {
if ($dbFile === null) {
$migration->addWarningMessage("Error creating tables : " . $DB->error());
$updateresult = false;
}
try {
$DB->runFile($dbFile);
} catch (\RuntimeException $e) {
$migration->addWarningMessage("Error creating tables : " . $e->getMessage());
$updateresult = false;
}


// ************ Keep it at the end **************
$migration->executeMigration();
Expand Down
60 changes: 47 additions & 13 deletions install/mysql/plugin_carbon_empty.sql
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,51 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_carbon_computerusageprofiles` (

CREATE TABLE IF NOT EXISTS `glpi_plugin_carbon_embodiedimpacts` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`itemtype` varchar(255) DEFAULT NULL,
`itemtype` varchar(255) DEFAULT NULL,
`items_id` int unsigned NOT NULL DEFAULT '0',
`engine` varchar(255) DEFAULT NULL,
`engine_version` varchar(255) DEFAULT NULL,
`date_mod` timestamp NULL DEFAULT NULL,
`gwp` float unsigned DEFAULT '0' COMMENT '(unit gCO2eq) Global warming potential',
`engine` varchar(255) DEFAULT NULL,
`engine_version` varchar(255) DEFAULT NULL,
`date_mod` timestamp NULL DEFAULT NULL,
`gwp` float DEFAULT '0' COMMENT '(unit g CO2 eq) Global warming potential',
`gwp_quality` int unsigned NOT NULL DEFAULT '0' COMMENT 'DataTtacking\\AbstractTracked::DATA_QUALITY_* constants',
`adp` float unsigned DEFAULT '0' COMMENT '(unit gSbeq) Abiotic depletion potential',
`adp` float DEFAULT '0' COMMENT '(unit g Sb eq) Abiotic depletion potential',
`adp_quality` int unsigned NOT NULL DEFAULT '0' COMMENT 'DataTtacking\\AbstractTracked::DATA_QUALITY_* constants',
`pe` float unsigned DEFAULT '0' COMMENT '(unit J) Primary energy',
`pe` float DEFAULT '0' COMMENT '(unit J) Primary energy',
`pe_quality` int unsigned NOT NULL DEFAULT '0' COMMENT 'DataTtacking\\AbstractTracked::DATA_QUALITY_* constants',
`gwppb` float DEFAULT '0' COMMENT '(unit g CO2 eq) Climate change - Contribution of biogenic emissions',
`gwppb_quality` int unsigned NOT NULL DEFAULT '0' COMMENT 'DataTtacking\\AbstractTracked::DATA_QUALITY_* constants',
`gwppf` float DEFAULT '0' COMMENT '(unit g CO2 eq) Climate change - Contribution of fossil fuel emissions',
`gwppf_quality` int unsigned NOT NULL DEFAULT '0' COMMENT 'DataTtacking\\AbstractTracked::DATA_QUALITY_* constants',
`gwpplu` float DEFAULT '0' COMMENT '(unit g CO2 eq) Climate change - Contribution of emissions from land use change',
`gwpplu_quality` int unsigned NOT NULL DEFAULT '0' COMMENT 'DataTtacking\\AbstractTracked::DATA_QUALITY_* constants',
`ir` float DEFAULT '0' COMMENT '(unit g U235 eq) Emissions of radionizing substances',
`ir_quality` int unsigned NOT NULL DEFAULT '0' COMMENT 'DataTtacking\\AbstractTracked::DATA_QUALITY_* constants',
`lu` float DEFAULT '0' COMMENT '(unit none) Land use',
`lu_quality` int unsigned NOT NULL DEFAULT '0' COMMENT 'DataTtacking\\AbstractTracked::DATA_QUALITY_* constants',
`odp` float DEFAULT '0' COMMENT '(unit g CFC-11 eq) Depletion of the ozone layer',
`odp_quality` int unsigned NOT NULL DEFAULT '0' COMMENT 'DataTtacking\\AbstractTracked::DATA_QUALITY_* constants',
`pm` float DEFAULT '0' COMMENT '(unit Disease occurrence) Fine particle emissions',
`pm_quality` int unsigned NOT NULL DEFAULT '0' COMMENT 'DataTtacking\\AbstractTracked::DATA_QUALITY_* constants',
`pocp` float DEFAULT '0' COMMENT '(unit g NMVOC eq) Photochemical ozone formation',
`pocp_quality` int unsigned NOT NULL DEFAULT '0' COMMENT 'DataTtacking\\AbstractTracked::DATA_QUALITY_* constants',
`wu` float DEFAULT '0' COMMENT '(unit L) Use of water resources',
`wu_quality` int unsigned NOT NULL DEFAULT '0' COMMENT 'DataTtacking\\AbstractTracked::DATA_QUALITY_* constants',
`mips` float DEFAULT '0' COMMENT '(unit g) Material input per unit of service',
`mips_quality` int unsigned NOT NULL DEFAULT '0' COMMENT 'DataTtacking\\AbstractTracked::DATA_QUALITY_* constants',
`adpe` float DEFAULT '0' COMMENT '(unit g SB eq) Use of mineral and metal resources',
`adpe_quality` int unsigned NOT NULL DEFAULT '0' COMMENT 'DataTtacking\\AbstractTracked::DATA_QUALITY_* constants',
`adpf` float DEFAULT '0' COMMENT '(unit J) Use of fossil resources (including nuclear)',
`adpf_quality` int unsigned NOT NULL DEFAULT '0' COMMENT 'DataTtacking\\AbstractTracked::DATA_QUALITY_* constants',
`ap` float DEFAULT '0' COMMENT '(unit mol H+ eq) Acidification',
`ap_quality` int unsigned NOT NULL DEFAULT '0' COMMENT 'DataTtacking\\AbstractTracked::DATA_QUALITY_* constants',
`ctue` float DEFAULT '0' COMMENT '(unit CTUe) Freshwater ecotoxicity',
`ctue_quality` int unsigned NOT NULL DEFAULT '0' COMMENT 'DataTtacking\\AbstractTracked::DATA_QUALITY_* constants',
`epf` float DEFAULT '0' COMMENT '(unit g P eq) Eutrophication of freshwater',
`epf_quality` int unsigned NOT NULL DEFAULT '0' COMMENT 'DataTtacking\\AbstractTracked::DATA_QUALITY_* constants',
`epm` float DEFAULT '0' COMMENT '(unit g N eq) Eutrophication of marine waters',
`epm_quality` int unsigned NOT NULL DEFAULT '0' COMMENT 'DataTtacking\\AbstractTracked::DATA_QUALITY_* constants',
`ept` float DEFAULT '0' COMMENT '(unit mol N eq) Terrestrial eutrophication',
`ept_quality` int unsigned NOT NULL DEFAULT '0' COMMENT 'DataTtacking\\AbstractTracked::DATA_QUALITY_* constants',
PRIMARY KEY (`id`),
UNIQUE KEY `unicity` (`itemtype`, `items_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Expand All @@ -161,12 +195,12 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_carbon_monitormodels` (
`gwp` int DEFAULT '0' COMMENT '(unit gCO2eq) Global warming potential',
`gwp_source` mediumtext DEFAULT NULL COMMENT 'any information to describe the source, URL preferred',
`gwp_quality` int DEFAULT '0' COMMENT 'DataTtacking\\AbstractTracked::DATA_QUALITY_* constants',
`adp` int DEFAULT '0' COMMENT '(unit gSbEq) Abiotic depletion potential',
`adp_source` mediumtext DEFAULT NULL COMMENT 'any information to describe the source, URL preferred',
`adp_quality` int DEFAULT '0' COMMENT 'DataTtacking\\AbstractTracked::DATA_QUALITY_* constants',
`pe` int DEFAULT '0' COMMENT '(unit J) Primary energy',
`pe_source` mediumtext DEFAULT NULL COMMENT 'any information to describe the source, URL preferred',
`pe_quality` int DEFAULT '0' COMMENT 'DataTtacking\\AbstractTracked::DATA_QUALITY_* constants',
`adp` int DEFAULT '0' COMMENT '(unit gSbEq) Abiotic depletion potential',
`adp_source` mediumtext DEFAULT NULL COMMENT 'any information to describe the source, URL preferred',
`adp_quality` int DEFAULT '0' COMMENT 'DataTtacking\\AbstractTracked::DATA_QUALITY_* constants',
`pe` int DEFAULT '0' COMMENT '(unit J) Primary energy',
`pe_source` mediumtext DEFAULT NULL COMMENT 'any information to describe the source, URL preferred',
`pe_quality` int DEFAULT '0' COMMENT 'DataTtacking\\AbstractTracked::DATA_QUALITY_* constants',
PRIMARY KEY (`id`),
UNIQUE KEY `unicity` (`monitormodels_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Expand Down
Loading