Skip to content
14 changes: 10 additions & 4 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,16 @@ function plugin_carbon_install(array $args = []): bool
// do not output messages
ob_start();
}
if ($version === '0.0.0') {
$success = $install->install($args);
} else {
$success = $install->upgrade($version, $args);
try {
if ($version === '0.0.0') {
$success = $install->install($args);
} else {
$success = $install->upgrade($version, $args);
}
} catch (\RuntimeException $e) {
$error_footer = '<br />' . __('Please check the logs for more details. Fill an issue in the repository of the plugin.', 'carbon');
Session::addMessageAfterRedirect($e->getMessage() . $error_footer, false, ERROR);
$success = false;
}

if ($silent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,34 @@
]
);
$migration->migrationOneTable($table);

// Blacklist assets by type - to avoid computation of impacts
$table = 'glpi_plugin_carbon_computertypes';
$migration->addField(
$table,
'is_ignore',
'bool',
[
'after' => 'category'
]
);

$table = 'glpi_plugin_carbon_monitortypes';
$migration->addField(
$table,
'is_ignore',
'bool',
[
'after' => 'power_consumption'
]
);

$table = 'glpi_plugin_carbon_networkequipmenttypes';
$migration->addField(
$table,
'is_ignore',
'bool',
[
'after' => 'power_consumption'
]
);
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
]);

$location_table = 'glpi_plugin_carbon_locations';
/** @var Migration $migration */
$migration->migrationOneTable($location_table);
foreach ($iterator as $row) {
$where = [
'locations_id' => $row['locations_id'],
Expand Down
3 changes: 3 additions & 0 deletions install/mysql/plugin_carbon_empty.sql
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_carbon_computertypes` (
`computertypes_id` int unsigned NOT NULL DEFAULT '0',
`power_consumption` int DEFAULT '0',
`category` int NOT NULL DEFAULT '0' COMMENT 'ComputerType::CATEGORY_* constants',
`is_ignore` tinyint NOT NULL DEFAULT '0' COMMENT 'Ignored from calculations',
PRIMARY KEY (`id`),
UNIQUE KEY `unicity` (`computertypes_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Expand Down Expand Up @@ -142,6 +143,7 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_carbon_monitortypes` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`monitortypes_id` int unsigned NOT NULL DEFAULT '0',
`power_consumption` int DEFAULT '0',
`is_ignore` tinyint NOT NULL DEFAULT '0' COMMENT 'Ignored from calculations',
PRIMARY KEY (`id`),
UNIQUE KEY `unicity` (`monitortypes_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Expand All @@ -150,6 +152,7 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_carbon_networkequipmenttypes` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`networkequipmenttypes_id` int unsigned NOT NULL DEFAULT '0',
`power_consumption` int DEFAULT '0',
`is_ignore` tinyint NOT NULL DEFAULT '0' COMMENT 'Ignored from calculations',
PRIMARY KEY (`id`),
UNIQUE KEY `unicity` (`networkequipmenttypes_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
Expand Down
8 changes: 7 additions & 1 deletion src/AbstractType.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ abstract class AbstractType extends CommonDBChild
{
public static $rightname = 'dropdown';


public static function getIcon(): string
{
return 'fa-solid fa-solar-panel';
}

/**
* @todo fix type name
*/
Expand All @@ -55,7 +61,7 @@ public function getTabNameForItem(CommonGLPI $item, $withtemplate = 0)
$tabName = '';
if (!$withtemplate) {
if ($item->getType() == static::$itemtype) {
$tabName = __('Carbon', 'carbon');
return self::createTabEntry(__('Carbon', 'carbon'), 0);
}
}
return $tabName;
Expand Down
21 changes: 4 additions & 17 deletions src/Command/CollectCarbonIntensityCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,16 @@ protected function interact(InputInterface $input, OutputInterface $output)
$question = new ChoiceQuestion(__('Zone:', 'carbon'), $this->zones);
$value = $question_helper->ask($input, $output, $question);
$input->setArgument('zone', $value);
} else {
$input->setArgument('zone', null);
}
}
}

protected function execute(InputInterface $input, OutputInterface $output)
{
if ($input->getArgument('zone') === null) {
if (count($this->zones) > 1 && $input->getArgument('zone') === null) {
// Null is not a valid key if there are several zones available for the source
return Command::FAILURE;
}

Expand All @@ -132,25 +135,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
$this->source_id = $data_source->getID();

// Create the zone if it does not exist
$zone_code = $input->getArgument('zone');
$zone = new Zone();
$zone->getFromDBByCrit(['name' => $this->zones[$zone_code]]);
if (!$zone->getID()) {
$zone->add([
'name' => $this->zones[$zone_code],
]);
if ($zone->isNewItem()) {
$message = __("Zone not found", 'carbon');
$output->writeln("<error>$message</error>");
return Command::FAILURE;
}
$source_zone = new Source_Zone();
$source_zone->add([
getForeignKeyFieldForItemType(Source::class) => $data_source->getID(),
getForeignKeyFieldForItemType(Zone::class) => $zone->getID(),
]);
}
$carbon_intensity = new CarbonIntensity();

// Create relation between source and zone if t does not exist
Expand Down
6 changes: 3 additions & 3 deletions src/Dashboard/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@
class Dashboard
{
/**
* Returns total carbon emission per computer type.
* Returns total usage carbon emission per computer type.
*
* @return array of:
* - float 'number': total carbon emission of the type
* - string 'url': url to redirect when clicking on the slice
* - string 'label': name of the computer type
*/
public static function getTotalCarbonEmissionPerType()
public static function getTotalUsageCarbonEmissionPerType()
{
return Provider::getSumEmissionsPerType();
return Provider::getSumUsageEmissionsPerType();
}

/**
Expand Down
5 changes: 0 additions & 5 deletions src/Dashboard/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,8 @@
namespace GlpiPlugin\Carbon\Dashboard;

use Computer;
use DateTimeImmutable;
use Glpi\Dashboard\Filter;
use GlpiPlugin\Carbon\CarbonIntensity;
use GlpiPlugin\Carbon\Config;
use GlpiPlugin\Carbon\Toolbox;
use Monitor;
use NetworkEquipment;
use Session;

class Grid
Expand Down
Loading