Skip to content

Commit d7a8370

Browse files
committed
🔧 Move to int based enum instead of period of seconds
1 parent bdbc66a commit d7a8370

File tree

4 files changed

+243
-244
lines changed

4 files changed

+243
-244
lines changed

‎src/Enums/Period.php‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,18 @@
77
use Carbon\CarbonImmutable;
88
use Carbon\CarbonPeriodImmutable;
99

10-
enum Period
10+
enum Period: int
1111
{
12-
case HOUR;
13-
case SIXHOUR;
14-
case HALFDAY;
15-
case DAY;
16-
case WEEK;
17-
case MONTH;
18-
case QUARTER;
19-
case HALFYEAR;
20-
case YEAR;
21-
case TAXYEAR;
12+
case HOUR = 0;
13+
case SIXHOUR = 1;
14+
case HALFDAY = 2;
15+
case DAY = 3;
16+
case WEEK = 4;
17+
case MONTH = 5;
18+
case QUARTER = 6;
19+
case HALFYEAR = 7;
20+
case YEAR = 8;
21+
case TAXYEAR = 9;
2222

2323
/**
2424
* Function label.
@@ -130,7 +130,7 @@ public function getBucketForTimestamp($timestamp): int
130130
self::HOUR => $now->startOfHour()->getTimestamp(),
131131
self::SIXHOUR => $now->startOfDay()->addHours((int) ($now->hour / 6) * 6)->getTimestamp(),
132132
self::HALFDAY => ($now->hour < 12 ? $now->startOfDay() : $now->startOfDay()->addHours(12))->getTimestamp(),
133-
self::DAY => $now->startOfDay() ->getTimestamp(),
133+
self::DAY => $now->startOfDay()->getTimestamp(),
134134
self::WEEK => $now->startOfWeek()->getTimestamp(),
135135
self::MONTH => $now->startOfMonth()->getTimestamp(),
136136
self::QUARTER => $now->startOfQuarter()->getTimestamp(),

‎src/Storage/DatabaseStorage.php‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,12 @@ protected function preaggregate(Collection $entries, string $aggregate, Closure
168168

169169
$bucket = $period->getBucketForTimestamp($entry->timestamp);
170170

171-
$key = $entry->type.':'.$period->period().':'.$bucket.':'.$entry->key;
171+
$key = $entry->type.':'.$period->value.':'.$bucket.':'.$entry->key;
172172

173173
if (! isset($aggregates[$key])) {
174174
$aggregates[$key] = $callback([
175175
'bucket' => $bucket,
176-
'period' => $period->period(),
176+
'period' => $period->value,
177177
'type' => $entry->type,
178178
'aggregate' => $aggregate,
179179
'key' => $entry->key,
@@ -391,7 +391,7 @@ public function trim(): void
391391
foreach (Period::cases() as $period) {
392392
$this->connection()
393393
->table('datum_aggregates')
394-
->where('period', $period->period())
394+
->where('period', $period->value)
395395
->where('bucket', '<=', $period->getBucketForTimestamp($now->subMinutes($period->period() * $period->maxCount())->getTimestamp()))
396396
->delete();
397397
}
@@ -451,7 +451,7 @@ public function graph(array $types, string $aggregate, Period $interval): Collec
451451
throw new InvalidArgumentException("Invalid aggregate type [$aggregate], allowed types: [".implode(', ', $allowed).'].');
452452
}
453453

454-
$period = $interval->period();
454+
$period = $interval->value;
455455
$buckets = $interval->getBuckets();
456456
$padding = collect()
457457
->range(0, $interval->maxDataPoints() - 1)
@@ -463,7 +463,7 @@ public function graph(array $types, string $aggregate, Period $interval): Collec
463463
->select(['bucket', 'type', 'key', 'value'])
464464
->whereIn('type', $types)
465465
->where('aggregate', $aggregate)
466-
->where('period', $period)
466+
->where('period', $period - 1)
467467
->where('bucket', '>=', $buckets[0]->getTimestamp())
468468
->orderBy('bucket')
469469
->get()
@@ -533,10 +533,9 @@ public function aggregate(
533533

534534
$query->fromSub(function (Builder $query) use ($type, $aggregates, $interval) {
535535
$now = CarbonImmutable::now();
536-
$period = $interval->period();
536+
$period = $interval->value;
537537
$windowStart = (int) ($now->getTimestamp() - $interval->totalSeconds() + 1);
538538
$oldestBucket = $interval->currentBucket() - $interval->totalSeconds();
539-
ray($interval->currentBucket(), $interval->totalSeconds(), $period, $oldestBucket);
540539

541540
// Tail
542541
$query->select('key_hash');
@@ -649,7 +648,7 @@ public function aggregateTypes(
649648

650649
$query->fromSub(function (Builder $query) use ($types, $aggregate, $interval) {
651650
$now = CarbonImmutable::now();
652-
$period = $interval->period();
651+
$period = $interval->value;
653652
$windowStart = (int) ($now->getTimestamp() - $interval->totalSeconds());
654653
$oldestBucket = $interval->currentBucket() - $interval->totalSeconds();
655654

@@ -741,7 +740,7 @@ public function aggregateTotal(
741740
'avg' => "avg({$this->wrap('value')})",
742741
}." as {$this->wrap($aggregate)}")
743742
->from('datum_aggregates')
744-
->where('period', $interval->period())
743+
->where('period', $interval->value)
745744
->when(
746745
is_array($types),
747746
fn ($query) => $query->whereIn('type', $types),

‎tests/Feature/Ingests/DatabaseTest.php‎

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -171,21 +171,21 @@
171171
Date::setTestNow('2000-01-01 02:23:59'); // Bucket: 2000-01-01 00:00:00
172172
Datum::record('foo', 'xxxx', 1)->count();
173173
Datum::ingest();
174-
expect(Datum::ignore(fn () => DB::table('datum_aggregates')->where('period', $period->period())->count()))->toBe(1);
174+
expect(Datum::ignore(fn () => DB::table('datum_aggregates')->where('period', $period->value)->count()))->toBe(1);
175175

176176
Date::setTestNow('2000-04-01 02:24:00'); // Bucket: 2000-04-01 00:00:00
177177
Datum::record('foo', 'xxxx', 1)->count();
178178
Datum::ingest();
179-
expect(Datum::ignore(fn () => DB::table('datum_aggregates')->where('period', $period->period())->count()))->toBe(2);
179+
expect(Datum::ignore(fn () => DB::table('datum_aggregates')->where('period', $period->value)->count()))->toBe(2);
180180

181181
Datum::stopRecording();
182182
Date::setTestNow('2000-12-30 23:59:59'); // 1 second before the oldest bucket become irrelevant.
183183
App::make(DatabaseStorage::class)->trim();
184-
expect(DB::table('datum_aggregates')->where('period', $period->period())->count())->toBe(2);
184+
expect(DB::table('datum_aggregates')->where('period', $period->value)->count())->toBe(2);
185185

186186
Date::setTestNow('2000-12-31 00:00:00'); // The second the oldest bucket become irrelevant.
187187
App::make(DatabaseStorage::class)->trim();
188-
expect(DB::table('datum_aggregates')->where('period', $period->period())->count())->toBe(1);
188+
expect(DB::table('datum_aggregates')->where('period', $period->value)->count())->toBe(1);
189189
});
190190

191191
it('trims aggregates once the half year bucket is no longer relevant', function () {
@@ -194,21 +194,21 @@
194194
Date::setTestNow('2000-01-01 02:23:59'); // Bucket: 2000-01-01 00:00:00
195195
Datum::record('foo', 'xxxx', 1)->count();
196196
Datum::ingest();
197-
expect(Datum::ignore(fn () => DB::table('datum_aggregates')->where('period', $period->period())->count()))->toBe(1);
197+
expect(Datum::ignore(fn () => DB::table('datum_aggregates')->where('period', $period->value)->count()))->toBe(1);
198198

199199
Date::setTestNow('2000-07-01 02:24:00'); // Bucket: 2000-07-01 00:00:00
200200
Datum::record('foo', 'xxxx', 1)->count();
201201
Datum::ingest();
202-
expect(Datum::ignore(fn () => DB::table('datum_aggregates')->where('period', $period->period())->count()))->toBe(2);
202+
expect(Datum::ignore(fn () => DB::table('datum_aggregates')->where('period', $period->value)->count()))->toBe(2);
203203

204204
Datum::stopRecording();
205205
Date::setTestNow('2000-12-30 23:59:59'); // 1 second before the oldest bucket become irrelevant.
206206
App::make(DatabaseStorage::class)->trim();
207-
expect(DB::table('datum_aggregates')->where('period', $period->period())->count())->toBe(2);
207+
expect(DB::table('datum_aggregates')->where('period', $period->value)->count())->toBe(2);
208208

209209
Date::setTestNow('2000-12-31 00:00:00'); // The second the oldest bucket become irrelevant.
210210
App::make(DatabaseStorage::class)->trim();
211-
expect(DB::table('datum_aggregates')->where('period', $period->period())->count())->toBe(1);
211+
expect(DB::table('datum_aggregates')->where('period', $period->value)->count())->toBe(1);
212212
});
213213

214214
it('trims aggregates once the year bucket is no longer relevant', function () {
@@ -217,21 +217,21 @@
217217
Date::setTestNow('2000-01-01 02:23:59'); // Bucket: 2000-01-01 00:00:00
218218
Datum::record('foo', 'xxxx', 1)->count();
219219
Datum::ingest();
220-
expect(Datum::ignore(fn () => DB::table('datum_aggregates')->where('period', $period->period())->count()))->toBe(1);
220+
expect(Datum::ignore(fn () => DB::table('datum_aggregates')->where('period', $period->value)->count()))->toBe(1);
221221

222222
Date::setTestNow('2001-01-01 02:24:00'); // Bucket: 2000-07-01 00:00:00
223223
Datum::record('foo', 'xxxx', 1)->count();
224224
Datum::ingest();
225-
expect(Datum::ignore(fn () => DB::table('datum_aggregates')->where('period', $period->period())->count()))->toBe(2);
225+
expect(Datum::ignore(fn () => DB::table('datum_aggregates')->where('period', $period->value)->count()))->toBe(2);
226226

227227
Datum::stopRecording();
228228
Date::setTestNow('2009-12-28 23:59:59'); // 1 second before the oldest bucket become irrelevant.
229229
App::make(DatabaseStorage::class)->trim();
230-
expect(DB::table('datum_aggregates')->where('period', $period->period())->count())->toBe(2);
230+
expect(DB::table('datum_aggregates')->where('period', $period->value)->count())->toBe(2);
231231

232232
Date::setTestNow('2009-12-29 00:00:00'); // The second the oldest bucket become irrelevant.
233233
App::make(DatabaseStorage::class)->trim();
234-
expect(DB::table('datum_aggregates')->where('period', $period->period())->count())->toBe(1);
234+
expect(DB::table('datum_aggregates')->where('period', $period->value)->count())->toBe(1);
235235
});
236236

237237
it('trims aggregates once the tax year bucket is no longer relevant', function () {
@@ -240,19 +240,19 @@
240240
Date::setTestNow('2000-01-01 02:23:59'); // Bucket: 1999-04-01 00:00:00
241241
Datum::record('foo', 'xxxx', 1)->count();
242242
Datum::ingest();
243-
expect(Datum::ignore(fn () => DB::table('datum_aggregates')->where('period', $period->period())->count()))->toBe(1);
243+
expect(Datum::ignore(fn () => DB::table('datum_aggregates')->where('period', $period->value)->count()))->toBe(1);
244244

245245
Date::setTestNow('2001-01-01 02:24:00'); // Bucket: 2000-04-01 00:00:00
246246
Datum::record('foo', 'xxxx', 1)->count();
247247
Datum::ingest();
248-
expect(Datum::ignore(fn () => DB::table('datum_aggregates')->where('period', $period->period())->count()))->toBe(2);
248+
expect(Datum::ignore(fn () => DB::table('datum_aggregates')->where('period', $period->value)->count()))->toBe(2);
249249

250250
Datum::stopRecording();
251251
Date::setTestNow('2009-03-23 23:59:59'); // 1 second before the oldest bucket become irrelevant.
252252
App::make(DatabaseStorage::class)->trim();
253-
expect(DB::table('datum_aggregates')->where('period', $period->period())->count())->toBe(2);
253+
expect(DB::table('datum_aggregates')->where('period', $period->value)->count())->toBe(2);
254254

255255
Date::setTestNow('2009-03-24 00:00:00'); // The second the oldest bucket become irrelevant.
256256
App::make(DatabaseStorage::class)->trim();
257-
expect(DB::table('datum_aggregates')->where('period', $period->period())->count())->toBe(1);
257+
expect(DB::table('datum_aggregates')->where('period', $period->value)->count())->toBe(1);
258258
});

0 commit comments

Comments
 (0)