-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalyticsBucket.php
More file actions
148 lines (122 loc) · 3.8 KB
/
Copy pathAnalyticsBucket.php
File metadata and controls
148 lines (122 loc) · 3.8 KB
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
<?php
namespace Featurit\Client\Modules\Analytics;
use DateTime;
use DateTimeInterface;
use Exception;
use Featurit\Client\Modules\Segmentation\Entities\FeatureFlag;
use JsonSerializable;
class AnalyticsBucket implements JsonSerializable
{
private DateTimeInterface $startDateTime;
private ?DateTimeInterface $endDateTime = null;
/**
* [
* "$hour" => [
* "$featureName" => [
* "$featureVersion" => [
* "$isActive" => $count,
* ],
* ],
* ],
* ]
* @var array
*/
private array $requests = [];
public function __construct(
DateTimeInterface $startDateTime,
?DateTimeInterface $endDateTime = null
)
{
$this->startDateTime = clone $startDateTime;
if (! is_null($endDateTime)) {
$this->endDateTime = clone $endDateTime;
}
}
public function startDateTime(): DateTimeInterface
{
return $this->startDateTime;
}
public function addFeatureFlagRequest(
FeatureFlag $featureFlag,
DateTime $currentTime = null
): void
{
if ($this->isClosed()) {
return;
}
if (is_null($currentTime)) {
$currentTime = new DateTime();
}
// Save the Feature Flag Request.
$hourKey = $this->generateHourKey($currentTime);
$flagNameKey = $this->generateFeatureFlagNameKey($featureFlag);
$flagVersionKey = $this->generateFeatureFlagVersionKey($featureFlag);
$flagIsActiveKey = $this->generateFeatureFlagIsActiveKey($featureFlag);
if (!isset($this->requests["$hourKey"])) {
$this->requests["$hourKey"] = [];
}
if (!isset($this->requests["$hourKey"]["$flagNameKey"])) {
$this->requests["$hourKey"]["$flagNameKey"] = [];
}
if (!isset($this->requests["$hourKey"]["$flagNameKey"]["$flagVersionKey"])) {
$this->requests["$hourKey"]["$flagNameKey"]["$flagVersionKey"] = [];
}
if (!isset($this->requests["$hourKey"]["$flagNameKey"]["$flagVersionKey"]["$flagIsActiveKey"])) {
$this->requests["$hourKey"]["$flagNameKey"]["$flagVersionKey"]["$flagIsActiveKey"] = 1;
} else {
$this->requests["$hourKey"]["$flagNameKey"]["$flagVersionKey"]["$flagIsActiveKey"]++;
}
}
public function openBucket(): void
{
if (!$this->isClosed()) {
return;
}
$this->endDateTime = null;
}
public function closeBucket(?DateTimeInterface $endDateTime = null): void
{
if ($this->isClosed()) {
return;
}
if (! is_null($endDateTime)) {
$this->endDateTime = $endDateTime;
return;
}
$this->endDateTime = new DateTime();
}
private function isClosed(): bool
{
return ! is_null($this->endDateTime);
}
/**
* @throws Exception
*/
public function jsonSerialize(): array
{
if (! $this->isClosed()) {
throw new Exception("Can't serialize an open bucket.");
}
return [
"start" => $this->startDateTime,
"end" => $this->endDateTime,
"reqs" => $this->requests,
];
}
public function generateHourKey(DateTime $currentTime): string
{
return $currentTime->format("Y-m-d H:00:00");
}
public function generateFeatureFlagNameKey(FeatureFlag $featureFlag): string
{
return $featureFlag->name();
}
public function generateFeatureFlagVersionKey(FeatureFlag $featureFlag): string
{
return $featureFlag->selectedFeatureFlagVersion()->name();
}
public function generateFeatureFlagIsActiveKey(FeatureFlag $featureFlag): string
{
return $featureFlag->isActive() ? "t" : "f";
}
}