Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,13 @@ function plugin_carbon_hook_pre_purge_assettype(CommonDBTM $item)
]);
}

function plugin_carbon_MassiveActions($itemtype)
/**
* Massive actions enumeration hook
*
* @param class-string $itemtype
* @return array
*/
function plugin_carbon_MassiveActions($itemtype): array
{
switch ($itemtype) {
case GlpiComputer::class:
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ parameters:
bootstrapFiles:
- ../../stubs/glpi_constants.php
- ../../vendor/autoload.php
- stubs/OptionalClass.php.stub
paths:
- src
- front
Expand All @@ -17,5 +18,6 @@ parameters:
- ../../src
stubFiles:
- ../../stubs/glpi_constants.php
- ./stubs/OptionalClass.php.stub
rules:
- GlpiProject\Tools\PHPStan\Rules\GlobalVarTypeRule
3 changes: 3 additions & 0 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
use Config as GlpiConfig;
use CronTask as GlpiCronTask;
use Glpi\Plugin\Hooks;
use GlpiPlugin\Carbon\CloudInventoryConnector;
use GlpiPlugin\Carbon\Config;
use GlpiPlugin\Carbon\CronTask;
use GlpiPlugin\Carbon\Dashboard\Grid;
Expand Down Expand Up @@ -115,6 +116,8 @@ function plugin_carbon_setupHooks()
LcaClientFactory::getSecuredConfigs()
);

$PLUGIN_HOOKS[Hooks::POST_INIT]['carbon'] = [CloudInventoryConnector::class, 'checkPluginAvailability'];

// add new cards to the dashboard
$PLUGIN_HOOKS[Hooks::DASHBOARD_CARDS]['carbon'] = [Grid::class, 'getDashboardCards'];
$PLUGIN_HOOKS[Hooks::DASHBOARD_TYPES]['carbon'] = [Widget::class, 'WidgetTypes'];
Expand Down
2 changes: 1 addition & 1 deletion src/CarbonIntensity.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ public function downloadOneZone(ClientInterface $data_source, Source_Zone $sourc
$gap_end = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $gap['end']);
$count = $data_source->fullDownload($source_zone, $gap_start, $gap_end, $this, $limit, $progress_bar);
$total_count += $count;
if ($total_count >= $limit) {
if ($limit > 0 && $total_count >= $limit) {
return $total_count;
}
}
Expand Down
57 changes: 57 additions & 0 deletions src/CloudInventoryConnector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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/>.
*
* -------------------------------------------------------------------------
*/

namespace GlpiPlugin\Carbon;

use Plugin;

/**
* Establish the functional bridge between Carbon and the plugin CloudInventory
*/
class CloudInventoryConnector
{
public static function checkPluginAvailability(): void
{
/** @var array $CFG_GLPI */
global $CFG_GLPI;

$CFG_GLPI['plugin:carbon']['use_cloudinventory'] = Plugin::isPluginActive('cloudinventory');
}

public function pluginAvailable(): bool
{
/** @var array $CFG_GLPI */
global $CFG_GLPI;

return $CFG_GLPI['plugin:carbon']['use_cloudinventory'] ?? false;
}
}
2 changes: 2 additions & 0 deletions src/ComputerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class ComputerType extends AbstractChildDropdown
public const CATEGORY_LAPTOP = 3;
public const CATEGORY_TABLET = 4;
public const CATEGORY_SMARTPHONE = 5;
public const CATEGORY_CLOUD = 6;

public static function getCategories(): array
{
Expand All @@ -60,6 +61,7 @@ public static function getCategories(): array
self::CATEGORY_LAPTOP => __('Laptop', 'carbon'),
self::CATEGORY_TABLET => __('Tablet', 'carbon'),
self::CATEGORY_SMARTPHONE => __('Smartphone', 'carbon'),
self::CATEGORY_CLOUD => __('Cloud server', 'carbon'),
];
}

Expand Down
18 changes: 0 additions & 18 deletions src/Dashboard/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,4 @@ public static function getTotalPowerPerModel(): array
{
return Provider::getSumPowerPerModel([ComputerModel::getTableField('power_consumption') => ['>', '0']]);
}

public static function cardCarbonintensityProvider(array $params = [])
{
$default_params = [
'label' => __('Carbon dioxyde intensity', 'carbon'),
'icon' => "fas fa-computer",
'color' => '#ea9999',
];
$params = array_merge($default_params, $params);

$data = Provider::getCarbonIntensity($params);

return [
'data' => $data,
'label' => $params['label'],
'icon' => $params['icon'],
];
}
}
7 changes: 0 additions & 7 deletions src/Dashboard/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,6 @@ public static function WidgetTypes(?array $types = null): array
'width' => 16,
'height' => 12,
],
'usage_abiotic_depletion' => [
'label' => __('Usage abiotic depletion potential', 'carbon'),
'function' => self::class . '::displayUsageAbioticDepletion',
'image' => '',
'width' => 6,
'height' => 3,
],

'impact_criteria_number' => [
'label' => __('Impact criteria', 'carbon'),
Expand Down
4 changes: 3 additions & 1 deletion src/DataSource/CarbonIntensity/ElectricityMaps/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,9 @@ public function fetchRange(DateTimeImmutable $start, DateTimeImmutable $stop, So
}

$downloaded_year_month = $start->format('Y-m');
if (count($full_response) > 0 && $downloaded_year_month < date('Y-m')) {
if (count($full_response) > 0 && $downloaded_year_month < date('Y-m') && date('j') > 1) {
// Cache only if the month being processed is older than the month of now
// and we are at least the 2nd day of the current month (to handle +14 hours overlap)
$json = json_encode($full_response);
file_put_contents($cache_file, $json);
}
Expand Down
3 changes: 2 additions & 1 deletion src/DataSource/CarbonIntensity/Rte/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,9 @@ public function fetchRange(DateTimeImmutable $start, DateTimeImmutable $stop, So
}
} else {
$downloaded_year_month = $start->format('Y-m');
if ($downloaded_year_month < date('Y-m')) {
if (count($response) > 0 && $downloaded_year_month < date('Y-m') && date('j') > 1) {
// Cache only if the month being processed is older than the month of now
// and we are at least the 2nd day of the current month (to handle +14 hours overlap)
$json = json_encode($response);
file_put_contents($cache_file, $json);
}
Expand Down
28 changes: 27 additions & 1 deletion src/DataSource/Lca/Boaviztapi/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
use GlpiPlugin\Carbon\Zone;
use Override;
use RuntimeException;
use Session;

class Client extends AbstractClient
{
Expand Down Expand Up @@ -257,6 +258,16 @@ public static function getZones()
public function parseResponse(array $response, string $scope): array
{
$impacts = [];
if (!isset($response['impacts'])) {
if (Session::getLoginUserID(true)) {
$message = __('An error occured while processing the response from Boaviztapi', 'carbon');
$message .= '<br>' . ($response['detail'] ?? 'unknown error');
Session::addMessageAfterRedirect($message, true, ERROR);
}
trigger_error('Error parsing the response ' . var_export($response, true), E_USER_WARNING);
return $impacts;
}

$types = Type::getImpactTypes();
foreach ($response['impacts'] as $type => $impact) {
if (!in_array($type, $types)) {
Expand All @@ -273,7 +284,7 @@ public function parseResponse(array $response, string $scope): array
return $impacts;
}

protected function parseCriteria(string $name, $impact): ?TrackedFloat
protected function parseCriteria(string $name, array|string $impact): ?TrackedFloat
{
if ($impact === 'not implemented') {
return null;
Expand All @@ -300,4 +311,19 @@ public static function dropdownBoaviztaZone(string $name, array $options = [])
{
return Dropdown::showFromArray($name, self::getZones(), $options);
}

/**
* Get instances known types of a cloud provider
*
* @param string $provider
* @return array isntances types
*/
public function getCloudInstances(string $provider): array
{
$response = $this->get('cloud/instance/all_instances', [
'query' => ['provider' => $provider],
]);

return $response;
}
}
4 changes: 4 additions & 0 deletions src/Impact/Embodied/Boavizta/AbstractAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ abstract class AbstractAsset extends AbstractEmbodiedImpact implements AssetInte
/** @var Client instance of the HTTP client */
protected ?Client $client = null;

protected const USAGE_NULL = [
'avg_power' => 0,
];

// abstract public static function getEngine(CommonDBTM $item): EngineInterface;

/**
Expand Down
Loading