Skip to content

Commit 44dbe19

Browse files
committed
feat(Dashboard\Grid): extend usage and total widgets to all criteria
1 parent 6846248 commit 44dbe19

5 files changed

Lines changed: 323 additions & 154 deletions

File tree

install/migration/update_1.1.1_to_1.2.0/09_add_impact_criterias.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,61 @@
142142
'card_options' => json_encode($card_options),
143143
]);
144144
}
145+
146+
// Rename cards for the standard dashboard : usage indicators
147+
148+
$dashboard_item = new DashboardItem();
149+
$rows = $dashboard_item->find([
150+
'card_id' => 'plugin_carbon_total_usage_power'
151+
]);
152+
foreach ($rows as $row) {
153+
$dashboard_item->update([
154+
'id' => $row['id'],
155+
'card_id' => 'plugin_carbon_usage_pe_impact',
156+
]);
157+
}
158+
159+
$dashboard_item = new DashboardItem();
160+
$rows = $dashboard_item->find([
161+
'card_id' => 'plugin_carbon_total_usage_carbon_emission'
162+
]);
163+
foreach ($rows as $row) {
164+
$dashboard_item->update([
165+
'id' => $row['id'],
166+
'card_id' => 'plugin_carbon_usage_gwp_impact',
167+
]);
168+
}
169+
170+
$dashboard_item = new DashboardItem();
171+
$rows = $dashboard_item->find([
172+
'card_id' => 'plugin_carbon_total_usage_adp_impact'
173+
]);
174+
foreach ($rows as $row) {
175+
$dashboard_item->update([
176+
'id' => $row['id'],
177+
'card_id' => 'plugin_carbon_usage_adp_impact',
178+
]);
179+
}
180+
181+
// Rename cards for the standard dashboard : Embodied + usage indicators
182+
$dashboard_item = new DashboardItem();
183+
$rows = $dashboard_item->find([
184+
'card_id' => 'plugin_carbon_total_gwp_impact'
185+
]);
186+
foreach ($rows as $row) {
187+
$dashboard_item->update([
188+
'id' => $row['id'],
189+
'card_id' => 'plugin_carbon_all_scopes_gwp_impact',
190+
]);
191+
}
192+
193+
$dashboard_item = new DashboardItem();
194+
$rows = $dashboard_item->find([
195+
'card_id' => 'plugin_carbon_total_adp_impact'
196+
]);
197+
foreach ($rows as $row) {
198+
$dashboard_item->update([
199+
'id' => $row['id'],
200+
'card_id' => 'plugin_carbon_all_scopes_adp_impact',
201+
]);
202+
}

src/Dashboard/DemoProvider.php

Lines changed: 106 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,29 +44,30 @@
4444

4545
class DemoProvider
4646
{
47+
/** @var array<string, array> $impact_values Embodied and usage impact values*/
4748
private static array $impact_values = [
48-
'gwp' => 616000000,
49-
'adp' => 12.748,
50-
'pe' => 491000000,
51-
'gwppb' => null,
52-
'gwppf' => null,
53-
'gwpplu' => null,
54-
'ir' => null,
55-
'lu' => -101,
56-
'odp' => null,
57-
'pm' => null,
58-
'pocp' => null,
59-
'wu' => null,
60-
'mips' => null,
61-
'adpe' => null,
62-
'adpf' => null,
63-
'ap' => null,
64-
'ctue' => null,
65-
// 'ctuh_c' => null,
66-
// 'ctuh_nc' => null,
67-
'epf' => null,
68-
'epm' => null,
69-
'ept' => null,
49+
'gwp' => [616000000, null],
50+
'adp' => [12.748, null],
51+
'pe' => [491000000, null],
52+
'gwppb' => [null, null],
53+
'gwppf' => [null, null],
54+
'gwpplu' => [null, null],
55+
'ir' => [null, null],
56+
'lu' => [-101, null],
57+
'odp' => [null, null],
58+
'pm' => [null, null],
59+
'pocp' => [null, null],
60+
'wu' => [null, null],
61+
'mips' => [null, null],
62+
'adpe' => [null, null],
63+
'adpf' => [null, null],
64+
'ap' => [null, null],
65+
'ctue' => [null, null],
66+
// 'ctuh_c' => [null, null],
67+
// 'ctuh_nc' => [null, null],
68+
'epf' => [null, null],
69+
'epm' => [null, null],
70+
'ept' => [null, null],
7071
];
7172

7273
public static function getUsageAbioticDepletion(array $params = [], array $crit = []): array
@@ -400,8 +401,8 @@ public static function getUsageCarbonEmission(array $params = []): array
400401
];
401402
}
402403

403-
/**
404-
* Total embodied abiotic depletion potential in antimony equivalent
404+
/**
405+
* Get the value of an impact criteria for the embodied scope
405406
*
406407
* @param array $params
407408
* @param array $crit
@@ -420,7 +421,87 @@ public static function getImpactOfEmbodiedCriteria(string $impact_type, array $p
420421
$crit['itemtype'] = array_intersect($crit['itemtype'], PLUGIN_CARBON_TYPES);
421422
}
422423

423-
$value = self::$impact_values[$impact_type];
424+
$value = self::$impact_values[$impact_type][0];
425+
if ($value === null) {
426+
$value = 'N/A';
427+
} else {
428+
$value = Toolbox::getHumanReadableValue(
429+
$value,
430+
Type::getImpactUnit($impact_type)
431+
);
432+
}
433+
434+
return [
435+
'number' => $value,
436+
'label' => $params['label'],
437+
'icon' => $params['icon'],
438+
'tooltip' => Type::getCriteriaTooltip($impact_type),
439+
'pictogram_file' => Type::getCriteriaPictogram($impact_type),
440+
'doc_url' => Type::getCriteriaInfoLink($impact_type),
441+
];
442+
}
443+
444+
/**
445+
* Get the value of an impact criteria for the usage scope
446+
*
447+
* @param array $params
448+
* @param array $crit
449+
* @return array
450+
*/
451+
public static function getImpactOfUsageCriteria(string $impact_type, array $params = [], array $crit = []): array
452+
{
453+
$default_params = [
454+
'label' => Type::getEmbodiedImpactLabel($impact_type),
455+
'icon' => Type::getCriteriaIcon($impact_type),
456+
];
457+
$params = array_merge($default_params, $params);
458+
if (count($crit['itemtype'] ?? []) === 0) {
459+
$crit['itemtype'] = PLUGIN_CARBON_TYPES;
460+
} else {
461+
$crit['itemtype'] = array_intersect($crit['itemtype'], PLUGIN_CARBON_TYPES);
462+
}
463+
464+
$value = self::$impact_values[$impact_type][1];
465+
if ($value === null) {
466+
$value = 'N/A';
467+
} else {
468+
$value = Toolbox::getHumanReadableValue(
469+
$value,
470+
Type::getImpactUnit($impact_type)
471+
);
472+
}
473+
474+
return [
475+
'number' => $value,
476+
'label' => $params['label'],
477+
'icon' => $params['icon'],
478+
'tooltip' => Type::getCriteriaTooltip($impact_type),
479+
'pictogram_file' => Type::getCriteriaPictogram($impact_type),
480+
'doc_url' => Type::getCriteriaInfoLink($impact_type),
481+
];
482+
}
483+
484+
/**
485+
* Get the value of an impact criteria for the embodied + usage scopes
486+
*
487+
* @param array $params
488+
* @param array $crit
489+
* @return array
490+
*/
491+
public static function getImpactOfEmbodiedAndUsageCriteria(string $impact_type, array $params = [], array $crit = []): array
492+
{
493+
$default_params = [
494+
'label' => Type::getEmbodiedImpactLabel($impact_type),
495+
'icon' => Type::getCriteriaIcon($impact_type),
496+
];
497+
$params = array_merge($default_params, $params);
498+
if (count($crit['itemtype'] ?? []) === 0) {
499+
$crit['itemtype'] = PLUGIN_CARBON_TYPES;
500+
} else {
501+
$crit['itemtype'] = array_intersect($crit['itemtype'], PLUGIN_CARBON_TYPES);
502+
}
503+
504+
$value = self::$impact_values[$impact_type][1];
424505
if ($value === null) {
425506
$value = 'N/A';
426507
} else {

src/Dashboard/Grid.php

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -171,47 +171,45 @@ protected static function getStandardCards(): array
171171
];
172172
}
173173

174-
$new_cards += [
175-
// Usage impact
176-
'plugin_carbon_total_usage_power' => [
177-
'widgettype' => ['bigNumber'],
178-
'group' => $group,
179-
'label' => __('Usage power consumption', 'carbon'),
180-
'provider' => Provider::class . '::getUsagePower',
181-
],
182-
'plugin_carbon_total_usage_carbon_emission' => [
183-
'widgettype' => ['bigNumber'],
184-
'group' => $group,
185-
'label' => __('Usage carbon emission', 'carbon'),
186-
'provider' => Provider::class . '::getUsageCarbonEmission',
187-
],
188-
'plugin_carbon_total_usage_adp_impact' => [
174+
// Usage impact
175+
foreach (Type::getImpactTypes() as $impact_type) {
176+
$key = "plugin_carbon_usage_{$impact_type}_impact";
177+
if (isset($new_cards[$key])) {
178+
trigger_error("The card $key already exists", E_USER_WARNING);
179+
}
180+
$new_cards[$key] = [
189181
'widgettype' => ['bigNumber'],
190182
'group' => $group,
191-
'label' => __('Usage abiotic depletion potential', 'carbon'),
192-
'provider' => Provider::class . '::getUsageAbioticDepletion',
193-
],
183+
'label' => Type::getUsageImpactLabel($impact_type),
184+
'provider' => Provider::class . '::getImpactOfUsageCriteria',
185+
'args' => [
186+
'impact_type' => $impact_type
187+
]
188+
];
189+
}
194190

195-
// embodied + usage impact
196-
'plugin_carbon_total_gwp_impact' => [
197-
'widgettype' => ['bigNumber'],
198-
'group' => $group,
199-
'label' => __('Global warming potential', 'carbon'),
200-
'provider' => Provider::class . '::getTotalGlobalWarming',
201-
],
202-
'plugin_carbon_total_adp_impact' => [
191+
// embodied + usage impact
192+
foreach (Type::getImpactTypes() as $impact_type) {
193+
$key = "plugin_carbon_all_scopes_{$impact_type}_impact";
194+
if (isset($new_cards[$key])) {
195+
trigger_error("The card $key already exists", E_USER_WARNING);
196+
}
197+
$new_cards[$key] = [
203198
'widgettype' => ['bigNumber'],
204199
'group' => $group,
205-
'label' => __('Abiotic depletion potential', 'carbon'),
206-
'provider' => Provider::class . '::getTotalAbioticDepletion',
207-
],
208-
];
200+
'label' => Type::getEmbodiedAndUsageImpactLabel($impact_type),
201+
'provider' => Provider::class . '::getImpactOfEmbodiedAndUsageCriteria',
202+
'args' => [
203+
'impact_type' => $impact_type
204+
]
205+
];
206+
}
209207

210208
return $new_cards;
211209
}
212210

213211
/**
214-
* getdescription of cards for dashboard in the reporting page of the plugin
212+
* Get description of cards for dashboard in the reporting page of the plugin
215213
*
216214
* @return array
217215
*/

0 commit comments

Comments
 (0)