|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spiral\Statistics; |
|
4
|
|
|
|
|
5
|
|
|
use Spiral\Statistics\Database\Event; |
|
6
|
|
|
use Spiral\Statistics\Database\Occurrence; |
|
7
|
|
|
use Spiral\Statistics\Database\Sources\EventSource; |
|
8
|
|
|
use Spiral\Statistics\Database\Sources\OccurrenceSource; |
|
9
|
|
|
|
|
10
|
|
|
class Track |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var OccurrenceSource */ |
|
13
|
|
|
private $occurrenceSource; |
|
14
|
|
|
|
|
15
|
|
|
/** @var EventSource */ |
|
16
|
|
|
private $eventSource; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Track constructor. |
|
20
|
|
|
* |
|
21
|
|
|
* @param OccurrenceSource $source |
|
22
|
|
|
* @param EventSource $eventSource |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct(OccurrenceSource $source, EventSource $eventSource) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->occurrenceSource = $source; |
|
27
|
|
|
$this->eventSource = $eventSource; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param string $name |
|
32
|
|
|
* @param float $value |
|
33
|
|
|
* @param \DateTime|null $datetime |
|
34
|
|
|
*/ |
|
35
|
|
|
public function event(string $name, float $value, \DateTime $datetime = null) |
|
36
|
|
|
{ |
|
37
|
|
|
$occurrence = $this->occurrenceSource->getByTimestamp($this->datetime($datetime)); |
|
38
|
|
|
|
|
39
|
|
|
if (!$occurrence->primaryKey()) { |
|
40
|
|
|
$this->addEvent($occurrence, $name, $value); |
|
41
|
|
|
} else { |
|
42
|
|
|
$this->addOrUpdateEvent($occurrence, $name, $value); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$occurrence->save(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param array $events Should be formatted ad "event_name" => "event_value" |
|
50
|
|
|
* @param \DateTime|null $datetime |
|
51
|
|
|
*/ |
|
52
|
|
|
public function events(array $events, \DateTime $datetime = null) |
|
53
|
|
|
{ |
|
54
|
|
|
if (empty($events)) { |
|
55
|
|
|
//nothing to track |
|
56
|
|
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$occurrence = $this->occurrenceSource->getByTimestamp($this->datetime($datetime)); |
|
60
|
|
|
|
|
61
|
|
|
if (!$occurrence->primaryKey()) { |
|
62
|
|
|
foreach ($events as $name => $value) { |
|
63
|
|
|
$this->addEvent($occurrence, $name, floatval($value)); |
|
64
|
|
|
} |
|
65
|
|
|
} else { |
|
66
|
|
|
/** @var Event $event */ |
|
67
|
|
|
foreach ($events as $name => $value) { |
|
68
|
|
|
$this->addOrUpdateEvent($occurrence, $name, floatval($value)); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$occurrence->save(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param \DateTime|null $datetime |
|
77
|
|
|
* @return \DateTime |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function datetime(\DateTime $datetime = null): \DateTime |
|
80
|
|
|
{ |
|
81
|
|
|
return $datetime ?? new \DateTime('now'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param Occurrence $occurrence |
|
86
|
|
|
* @param string $name |
|
87
|
|
|
* @param float $value |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function addEvent(Occurrence $occurrence, string $name, float $value) |
|
90
|
|
|
{ |
|
91
|
|
|
/** @var Event $event */ |
|
92
|
|
|
$event = $this->eventSource->create(compact('name', 'value')); |
|
93
|
|
|
$occurrence->events->add($event); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param Occurrence $occurrence |
|
98
|
|
|
* @param string $name |
|
99
|
|
|
* @param float $value |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function addOrUpdateEvent(Occurrence $occurrence, string $name, float $value) |
|
102
|
|
|
{ |
|
103
|
|
|
/** @var Event $event */ |
|
104
|
|
|
$event = $occurrence->events->matchOne(compact('name')); |
|
105
|
|
|
if (empty($event)) { |
|
106
|
|
|
$this->addEvent($occurrence, $name, $value); |
|
107
|
|
|
} else { |
|
108
|
|
|
$event->value += $value; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |