44
common/Database/Metrics/Traits/GeneratesTrendResults.php
Executable file
44
common/Database/Metrics/Traits/GeneratesTrendResults.php
Executable file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Database\Metrics\Traits;
|
||||
|
||||
use Carbon\CarbonInterface;
|
||||
use Common\Database\Metrics\MetricDateRange;
|
||||
|
||||
trait GeneratesTrendResults
|
||||
{
|
||||
protected function getAllPossibleDateResults(
|
||||
MetricDateRange $dateRange,
|
||||
): array {
|
||||
$format = $dateRange->getGroupingFormat();
|
||||
|
||||
// dates in range will already be in correct timezone
|
||||
$possibleDateResults = [];
|
||||
foreach ($dateRange->period as $date) {
|
||||
$possibleDateResults[
|
||||
(string) $date->format($format)
|
||||
] = $this->formatTrendResult($dateRange->granularity, $date);
|
||||
}
|
||||
|
||||
return $possibleDateResults;
|
||||
}
|
||||
|
||||
protected function formatTrendResult(
|
||||
string $granularity,
|
||||
CarbonInterface $date,
|
||||
int $value = 0,
|
||||
): array {
|
||||
if ($granularity === $this->dateRange::WEEK) {
|
||||
return [
|
||||
'date' => $date->startOfWeek()->toISOString(),
|
||||
'endDate' => $date->endOfWeek()->toISOString(),
|
||||
'value' => $value,
|
||||
];
|
||||
} else {
|
||||
return [
|
||||
'date' => $date->toISOString(),
|
||||
'value' => $value,
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
32
common/Database/Metrics/Traits/RoundingPrecision.php
Executable file
32
common/Database/Metrics/Traits/RoundingPrecision.php
Executable file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Common\Database\Metrics\Traits;
|
||||
|
||||
trait RoundingPrecision
|
||||
{
|
||||
public int $roundingPrecision = 0;
|
||||
public int $roundingMode = PHP_ROUND_HALF_UP;
|
||||
|
||||
/**
|
||||
* Set the precision level used when rounding the value.
|
||||
*/
|
||||
public function precision(
|
||||
int $precision = 0,
|
||||
int $mode = PHP_ROUND_HALF_UP,
|
||||
): static {
|
||||
$this->roundingPrecision = $precision;
|
||||
|
||||
if (
|
||||
in_array($mode, [
|
||||
PHP_ROUND_HALF_UP,
|
||||
PHP_ROUND_HALF_DOWN,
|
||||
PHP_ROUND_HALF_EVEN,
|
||||
PHP_ROUND_HALF_ODD,
|
||||
])
|
||||
) {
|
||||
$this->roundingMode = $mode;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user