Skip to content

Commit

Permalink
Implement BeforeMetricCalculate event that allows changing run option…
Browse files Browse the repository at this point in the history
…s of a metric
  • Loading branch information
DZunke committed Apr 8, 2024
1 parent e767baf commit 9960e36
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,12 @@ The event section is work in progress as there is currently no real way to regis
become available later so that plugins are also enabled to hook into the events instead of delivering metrics,
reporting or storages to the process.

| Event | Description |
|------------------------------------------------------------|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Panaly\Configuration\ConfigurationFile\ConfigurationLoaded | The event is dispatched directly after the `ConfigurationFile` was loaded. It allows to overwrite the full configuration by delivering a new instance that will then be taken for the process. |
| Panaly\Configuration\ConfigurationFile\RuntimeLoaded | After the configuration was fully loaded and converted to the `RuntimeConfiguration` this event is triggered, it is the last possibility to change the metric running process. |
| Panaly\Configuration\ConfigurationFile\MetricResultCreated | When the collection, or execution, of configured metric groups is finished the event is triggered with all information and the result can be modified before the storage and reporting runs. The full environment is given her with the `ConfigurationFile`, the `RuntimeConfiguration` and the `Result` |
| Event | Description |
|--------------------------------------------------------------|:---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Panaly\Configuration\ConfigurationFile\BeforeMetricCalculate | Before the Collector executes a metric collection method it will dispatch this even which gives the possibility to change the metric options directly before the metric is executed. |
| Panaly\Configuration\ConfigurationFile\ConfigurationLoaded | The event is dispatched directly after the `ConfigurationFile` was loaded. It allows to overwrite the full configuration by delivering a new instance that will then be taken for the process. |
| Panaly\Configuration\ConfigurationFile\RuntimeLoaded | After the configuration was fully loaded and converted to the `RuntimeConfiguration` this event is triggered, it is the last possibility to change the metric running process. |
| Panaly\Configuration\ConfigurationFile\MetricResultCreated | When the collection, or execution, of configured metric groups is finished the event is triggered with all information and the result can be modified before the storage and reporting runs. The full environment is given her with the `ConfigurationFile`, the `RuntimeConfiguration` and the `Result` |

## Thanks and License

Expand Down
7 changes: 6 additions & 1 deletion src/Collector/Collector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Panaly\Configuration\ConfigurationFile;
use Panaly\Configuration\RuntimeConfiguration;
use Panaly\Event\BeforeMetricCalculate;
use Panaly\Result\Group;
use Panaly\Result\Metric;
use Panaly\Result\Result;
Expand All @@ -31,7 +32,11 @@ public function collect(): Result

foreach ($executingGroup->metrics as $executingMetric) {
$metricHandler = $this->runtimeConfiguration->getMetric($executingMetric->metric);
$metricResult = $metricHandler->calculate($executingMetric->options);

$event = new BeforeMetricCalculate($metricHandler, $executingMetric->options);
$this->runtimeConfiguration->getEventDispatcher()->dispatch($event);

$metricResult = $metricHandler->calculate($event->getOptions());

$group->addMetric(new Metric(
$executingMetric->title ?? $metricHandler->getDefaultTitle(),
Expand Down
32 changes: 32 additions & 0 deletions src/Event/BeforeMetricCalculate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Panaly\Event;

use Panaly\Plugin\Plugin\Metric;

final class BeforeMetricCalculate
{
public function __construct(
public readonly Metric $plugin,
private array $options,
) {
}

/** @return array<string, mixed> */
public function getOptions(): array
{
return $this->options;
}

public function getOption(string $option): mixed
{
return $this->options[$option] ?? null;
}

public function setOption(string $option, mixed $value): void
{
$this->options[$option] = $value;
}
}

0 comments on commit 9960e36

Please sign in to comment.