Spaces:
No application file
No application file
File size: 5,057 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
<?php
namespace Mautic\StatsBundle\Aggregate;
use Mautic\StatsBundle\Aggregate\Collection\DAO\StatDAO;
use Mautic\StatsBundle\Aggregate\Collection\DAO\StatsDAO;
use Mautic\StatsBundle\Aggregate\Helper\CalculatorHelper;
class Calculator
{
public function __construct(
private StatsDAO $statsDAO,
private ?\DateTimeInterface $fromDateTime = null,
private ?\DateTimeInterface $toDateTime = null
) {
}
/**
* @param string $labelFormat
*
* @throws \Exception
*/
public function getSumsByYear($labelFormat = 'Y'): StatDAO
{
$statDAO = new StatDAO();
$lastYear = $this->fromDateTime ? $this->fromDateTime->format('Y') : null;
foreach ($this->statsDAO->getYears() as $thisYear => $stats) {
CalculatorHelper::fillInMissingYears($statDAO, $lastYear, $thisYear, $labelFormat);
$statDAO->addStat(
CalculatorHelper::getYearLabel($thisYear, $labelFormat),
$stats->getSum()
);
$lastYear = $thisYear;
}
if ($this->toDateTime) {
CalculatorHelper::fillInMissingYears($statDAO, $lastYear, $this->toDateTime->format('Y'), $labelFormat);
}
return $statDAO;
}
/**
* @param string $labelFormat
*
* @throws \Exception
*/
public function getSumsByMonth($labelFormat = 'Y-m'): StatDAO
{
$statDAO = new StatDAO();
$lastMonth = $this->fromDateTime ? $this->fromDateTime->format('Y-m') : null;
foreach ($this->statsDAO->getMonths() as $thisMonth => $stats) {
CalculatorHelper::fillInMissingMonths($statDAO, $lastMonth, $thisMonth, $labelFormat);
$statDAO->addStat(
CalculatorHelper::getMonthLabel($thisMonth, $labelFormat),
$stats->getSum()
);
$lastMonth = $thisMonth;
}
if ($this->toDateTime) {
CalculatorHelper::fillInMissingMonths($statDAO, $lastMonth, $this->toDateTime->format('Y-m'), $labelFormat);
}
return $statDAO;
}
/**
* @param string $labelFormat
*
* @throws \Exception
*/
public function getSumsByDay($labelFormat = 'Y-m-d'): StatDAO
{
$statDAO = new StatDAO();
$yesterday = $this->fromDateTime ? $this->fromDateTime->format('Y-m-d') : null;
foreach ($this->statsDAO->getDays() as $today => $stats) {
CalculatorHelper::fillInMissingDays($statDAO, $yesterday, $today, $labelFormat);
$statDAO->addStat(
CalculatorHelper::getDayLabel($today, $labelFormat),
$stats->getSum()
);
$yesterday = $today;
}
if ($this->toDateTime) {
CalculatorHelper::fillInMissingDays($statDAO, $yesterday, $this->toDateTime->format('Y-m-d'), $labelFormat);
}
return $statDAO;
}
/**
* @param string $labelFormat
*
* @throws \Exception
*/
public function getSumsByWeek($labelFormat = 'Y-W'): StatDAO
{
$statDAO = new StatDAO();
$yesterday = $this->fromDateTime ? $this->fromDateTime->format('Y-W') : null;
foreach ($this->statsDAO->getWeeks() as $today => $stats) {
CalculatorHelper::fillInMissingWeeks($statDAO, $yesterday, $today, $labelFormat);
$statDAO->addStat(
$today,
$stats->getCount()
);
$yesterday = $today;
}
$yesterday = (new \DateTime(CalculatorHelper::getWeekDateString($yesterday)))->modify('+1 week')->format('Y-W');
if ($this->toDateTime) {
/** @var \DateTime $tomorrow */
$tomorrow = clone $this->toDateTime;
CalculatorHelper::fillInMissingWeeks($statDAO, $yesterday, $tomorrow->modify('+1 week')->format('Y-W'), $labelFormat);
}
return $statDAO;
}
/**
* @param string $labelFormat
*
* @throws \Exception
*/
public function getCountsByHour($labelFormat = 'Y-m-d H'): StatDAO
{
$statDAO = new StatDAO();
$lastHour = $this->fromDateTime ? $this->fromDateTime->format('Y-m-d H') : null;
foreach ($this->statsDAO->getHours() as $thisHour => $stats) {
CalculatorHelper::fillInMissingHours($statDAO, $lastHour, $thisHour, $labelFormat);
$statDAO->addStat(
CalculatorHelper::getHourLabel($thisHour, $labelFormat),
$stats->getCount()
);
$lastHour = $thisHour;
}
if ($this->toDateTime) {
CalculatorHelper::fillInMissingHours($statDAO, $lastHour, $this->toDateTime->format('Y-m-d H'), $labelFormat);
}
return $statDAO;
}
public function getSum(): StatDAO
{
$statDAO = new StatDAO();
$sum = 0;
foreach ($this->statsDAO->getYears() as $stats) {
$sum += $stats->getSum();
}
return $statDAO;
}
}
|