|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spiral\Statistics\Database\Sources; |
|
4
|
|
|
|
|
5
|
|
|
use Spiral\Database\Injections\Parameter; |
|
6
|
|
|
use Spiral\ORM\Entities\RecordSelector; |
|
7
|
|
|
use Spiral\ORM\Entities\RecordSource; |
|
8
|
|
|
use Spiral\ORM\ORMInterface; |
|
9
|
|
|
use Spiral\Statistics\Database\Occurrence; |
|
10
|
|
|
use Spiral\Statistics\DatetimeConverter; |
|
11
|
|
|
use Spiral\Statistics\Extract\Range; |
|
12
|
|
|
|
|
13
|
|
|
class OccurrenceSource extends RecordSource |
|
14
|
|
|
{ |
|
15
|
|
|
const RECORD = Occurrence::class; |
|
16
|
|
|
|
|
17
|
|
|
/** @var DatetimeConverter */ |
|
18
|
|
|
private $converter; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* OccurrenceSource constructor. |
|
22
|
|
|
* |
|
23
|
|
|
* @param ORMInterface $orm |
|
24
|
|
|
* @param DatetimeConverter $converter |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(ORMInterface $orm, DatetimeConverter $converter) |
|
27
|
|
|
{ |
|
28
|
|
|
parent::__construct(static::RECORD, $orm); |
|
29
|
|
|
|
|
30
|
|
|
$this->converter = $converter; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param \DateTimeInterface $datetime |
|
35
|
|
|
* @return Occurrence|null |
|
36
|
|
|
*/ |
|
37
|
|
|
public function findByTimestamp(\DateTimeInterface $datetime) |
|
38
|
|
|
{ |
|
39
|
|
|
$entity = $this->findOne(['timestamp' => $datetime]); |
|
40
|
|
|
|
|
41
|
|
|
return $entity; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param \DateTime $datetime |
|
46
|
|
|
* @return Occurrence |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getByTimestamp(\DateTime $datetime): Occurrence |
|
49
|
|
|
{ |
|
50
|
|
|
$entity = $this->findByTimestamp($datetime); |
|
51
|
|
|
if (empty($entity)) { |
|
52
|
|
|
$entity = $this->createFromTimestamp($datetime); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return $entity; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param \DateTimeInterface $datetime |
|
60
|
|
|
* @return Occurrence |
|
61
|
|
|
*/ |
|
62
|
|
|
public function createFromTimestamp(\DateTimeInterface $datetime): Occurrence |
|
63
|
|
|
{ |
|
64
|
|
|
$entity = $this->create([ |
|
65
|
|
|
'timestamp' => $this->converter->convert($datetime), |
|
66
|
|
|
'day_mark' => $this->converter->convert($datetime, 'day'), |
|
67
|
|
|
'week_mark' => $this->converter->convert($datetime, 'week'), |
|
68
|
|
|
'month_mark' => $this->converter->convert($datetime, 'month'), |
|
69
|
|
|
'year_mark' => $this->converter->convert($datetime, 'year'), |
|
70
|
|
|
]); |
|
71
|
|
|
|
|
72
|
|
|
return $entity; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $periodField |
|
77
|
|
|
* @param \DateTimeInterface $periodValue |
|
78
|
|
|
* @param \DateTimeInterface $start |
|
79
|
|
|
* @param \DateTimeInterface $end |
|
80
|
|
|
* @param array $events |
|
81
|
|
|
* @return RecordSelector |
|
82
|
|
|
*/ |
|
83
|
|
|
public function findByGroupedInterval( |
|
84
|
|
|
string $periodField, |
|
85
|
|
|
\DateTimeInterface $periodValue, |
|
86
|
|
|
\DateTimeInterface $start, |
|
87
|
|
|
\DateTimeInterface $end, |
|
88
|
|
|
array $events = [] |
|
89
|
|
|
): RecordSelector |
|
90
|
|
|
{ |
|
91
|
|
|
$selector = $this->find([ |
|
92
|
|
|
$periodField => $periodValue, |
|
93
|
|
|
'timestamp' => ['between' => [$start, $end]] |
|
94
|
|
|
]); |
|
95
|
|
|
|
|
96
|
|
|
if (!empty($events)) { |
|
97
|
|
|
$selector->with( |
|
98
|
|
|
'events', |
|
99
|
|
|
['where' => ['{@}.name' => ['IN' => new Parameter($events)]]] |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $selector; |
|
104
|
|
|
} |
|
105
|
|
|
} |