Skip to content

Commit aa00036

Browse files
committed
🔧 Add helper to find oldest timestamp
1 parent 5ba3cbc commit aa00036

File tree

3 files changed

+133
-116
lines changed

3 files changed

+133
-116
lines changed

‎src/Enums/Period.php‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,30 @@ public function getBucketForTimestamp($timestamp): int
162162
};
163163
}
164164

165+
/**
166+
* Function getBucketForTimestamp.
167+
*/
168+
public function getOldestBucketTimestamp(): int
169+
{
170+
$now = CarbonImmutable::now();
171+
172+
return match ($this) {
173+
self::HOUR => $now->startOfHour()->subHours($this->maxCount())->getTimestamp(),
174+
self::SIXHOUR => $now->startOfDay()->addHours((int) ($now->hour / 6) * 6)->subHours($this->maxCount() * 6)->getTimestamp(),
175+
self::HALFDAY => ($now->hour < 12 ? $now->startOfDay() : $now->startOfDay()->addHours(12))->subHours($this->maxCount() * 12)->getTimestamp(),
176+
self::DAY => $now->startOfDay()->subDays($this->maxCount())->getTimestamp(),
177+
self::WEEK => $now->startOfWeek()->subWeeks($this->maxCount())->getTimestamp(),
178+
self::MONTH => $now->startOfMonth()->subMonths($this->maxCount())->getTimestamp(),
179+
self::QUARTER => $now->startOfQuarter()->subQuarters($this->maxCount())->getTimestamp(),
180+
self::HALFYEAR => $now->startOfYear()->addMonths((int) (($now->month - 1) / 6) * 6)->subMonths($this->maxCount() * 6)->getTimestamp(),
181+
self::YEAR => $now->startOfYear()->subYears($this->maxCount())->getTimestamp(),
182+
self::TAXYEAR => ($now->gte(CarbonImmutable::createFromDate($now->year, 4, 6)->startOfDay())
183+
? CarbonImmutable::createFromDate($now->year, 4, 6)
184+
: CarbonImmutable::createFromDate($now->year - 1, 4, 6)
185+
)->startOfDay()->subYears($this->maxCount())->getTimestamp(),
186+
};
187+
}
188+
165189
/**
166190
* Function getDateTimeFormat.
167191
*/

‎src/Storage/DatabaseStorage.php‎

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ public function store(Collection $items): void
5858
->insert(
5959
$this->requiresManualKeyHash()
6060
? $chunk->map(fn ($entry) => [
61-
...($attributes = $entry->attributes()),
62-
'key_hash' => md5($attributes['key']),
63-
])->all()
61+
...($attributes = $entry->attributes()),
62+
'key_hash' => md5($attributes['key']),
63+
])->all()
6464
: $chunk->map->attributes()->all()
6565
)
6666
);
@@ -108,9 +108,9 @@ public function store(Collection $items): void
108108
->upsert(
109109
$this->requiresManualKeyHash()
110110
? $chunk->map(fn ($entry) => [
111-
...($attributes = $entry->attributes()),
112-
'key_hash' => md5($attributes['key']),
113-
])->all()
111+
...($attributes = $entry->attributes()),
112+
'key_hash' => md5($attributes['key']),
113+
])->all()
114114
: $chunk->map->attributes()->all(), // @phpstan-ignore method.notFound
115115
['type', 'key_hash'],
116116
['timestamp', 'value']
@@ -162,7 +162,7 @@ protected function preaggregate(Collection $entries, string $aggregate, Closure
162162
foreach ($entries as $entry) {
163163
foreach (Period::cases() as $period) {
164164
// Exclude entries that would be trimmed.
165-
if ($entry->timestamp < CarbonImmutable::now()->subMinutes($period->period())->getTimestamp()) {
165+
if ($entry->timestamp < $period->getOldestBucketTimestamp()) {
166166
continue;
167167
}
168168

@@ -392,7 +392,7 @@ public function trim(): void
392392
$this->connection()
393393
->table('datum_aggregates')
394394
->where('period', $period->value)
395-
->where('bucket', '<=', $period->getBucketForTimestamp($now->subMinutes($period->period() * $period->maxCount())->getTimestamp()))
395+
->where('bucket', '<=', $period->getOldestBucketTimestamp())
396396
->delete();
397397
}
398398
}
@@ -460,11 +460,11 @@ public function graph(array $types, string $aggregate, Period $interval): Collec
460460
$structure = collect($types)->mapWithKeys(fn ($type) => [$type => $padding]);
461461

462462
return $this->connection()->table('datum_aggregates') // @phpstan-ignore return.type
463-
->select(['bucket', 'type', 'key', 'value'])
463+
->select(['bucket', 'type', 'key', 'value'])
464464
->whereIn('type', $types)
465465
->where('aggregate', $aggregate)
466466
->where('period', $period->graphPeriod()?->value)
467-
->where('bucket', '>=', $buckets[0]->getTimestamp())
467+
->where('bucket', '>=', ray()->pass($buckets[0]->getTimestamp()))
468468
->orderBy('bucket')
469469
->get()
470470
->groupBy('key')
@@ -523,12 +523,12 @@ public function aggregate(
523523

524524
foreach ($aggregates as $aggregate) {
525525
$query->selectRaw(match ($aggregate) {
526-
'count' => "sum({$this->wrap('count')})",
527-
'min' => "min({$this->wrap('min')})",
528-
'max' => "max({$this->wrap('max')})",
529-
'sum' => "sum({$this->wrap('sum')})",
530-
'avg' => "avg({$this->wrap('avg')})",
531-
}." as {$this->wrap($aggregate)}");
526+
'count' => "sum({$this->wrap('count')})",
527+
'min' => "min({$this->wrap('min')})",
528+
'max' => "max({$this->wrap('max')})",
529+
'sum' => "sum({$this->wrap('sum')})",
530+
'avg' => "avg({$this->wrap('avg')})",
531+
}." as {$this->wrap($aggregate)}");
532532
}
533533

534534
$query->fromSub(function (Builder $query) use ($type, $aggregates, $interval) {
@@ -542,12 +542,12 @@ public function aggregate(
542542

543543
foreach ($aggregates as $aggregate) {
544544
$query->selectRaw(match ($aggregate) {
545-
'count' => 'count(*)',
546-
'min' => "min({$this->wrap('value')})",
547-
'max' => "max({$this->wrap('value')})",
548-
'sum' => "sum({$this->wrap('value')})",
549-
'avg' => "avg({$this->wrap('value')})",
550-
}." as {$this->wrap($aggregate)}");
545+
'count' => 'count(*)',
546+
'min' => "min({$this->wrap('value')})",
547+
'max' => "max({$this->wrap('value')})",
548+
'sum' => "sum({$this->wrap('value')})",
549+
'avg' => "avg({$this->wrap('value')})",
550+
}." as {$this->wrap($aggregate)}");
551551
}
552552

553553
$query
@@ -565,12 +565,12 @@ public function aggregate(
565565
foreach ($aggregates as $aggregate) {
566566
if ($aggregate === $currentAggregate) {
567567
$query->selectRaw(match ($aggregate) {
568-
'count' => "sum({$this->wrap('value')})",
569-
'min' => "min({$this->wrap('value')})",
570-
'max' => "max({$this->wrap('value')})",
571-
'sum' => "sum({$this->wrap('value')})",
572-
'avg' => "avg({$this->wrap('value')})",
573-
}." as {$this->wrap($aggregate)}");
568+
'count' => "sum({$this->wrap('value')})",
569+
'min' => "min({$this->wrap('value')})",
570+
'max' => "max({$this->wrap('value')})",
571+
'sum' => "sum({$this->wrap('value')})",
572+
'avg' => "avg({$this->wrap('value')})",
573+
}." as {$this->wrap($aggregate)}");
574574
} else {
575575
$query->selectRaw("null as {$this->wrap($aggregate)}");
576576
}
@@ -638,12 +638,12 @@ public function aggregateTypes(
638638

639639
foreach ($types as $type) {
640640
$query->selectRaw(match ($aggregate) {
641-
'count' => "sum({$this->wrap($type)})",
642-
'min' => "min({$this->wrap($type)})",
643-
'max' => "max({$this->wrap($type)})",
644-
'sum' => "sum({$this->wrap($type)})",
645-
'avg' => "avg({$this->wrap($type)})",
646-
}." as {$this->wrap($type)}");
641+
'count' => "sum({$this->wrap($type)})",
642+
'min' => "min({$this->wrap($type)})",
643+
'max' => "max({$this->wrap($type)})",
644+
'sum' => "sum({$this->wrap($type)})",
645+
'avg' => "avg({$this->wrap($type)})",
646+
}." as {$this->wrap($type)}");
647647
}
648648

649649
$query->fromSub(function (Builder $query) use ($types, $aggregate, $interval) {
@@ -657,12 +657,12 @@ public function aggregateTypes(
657657

658658
foreach ($types as $type) {
659659
$query->selectRaw(match ($aggregate) {
660-
'count' => "count(case when ({$this->wrap('type')} = ?) then true else null end)",
661-
'min' => "min(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
662-
'max' => "max(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
663-
'sum' => "sum(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
664-
'avg' => "avg(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
665-
}." as {$this->wrap($type)}", [$type]);
660+
'count' => "count(case when ({$this->wrap('type')} = ?) then true else null end)",
661+
'min' => "min(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
662+
'max' => "max(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
663+
'sum' => "sum(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
664+
'avg' => "avg(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
665+
}." as {$this->wrap($type)}", [$type]);
666666
}
667667

668668
$query
@@ -678,12 +678,12 @@ public function aggregateTypes(
678678

679679
foreach ($types as $type) {
680680
$query->selectRaw(match ($aggregate) {
681-
'count' => "sum(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
682-
'min' => "min(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
683-
'max' => "max(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
684-
'sum' => "sum(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
685-
'avg' => "avg(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
686-
}." as {$this->wrap($type)}", [$type]);
681+
'count' => "sum(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
682+
'min' => "min(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
683+
'max' => "max(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
684+
'sum' => "sum(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
685+
'avg' => "avg(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
686+
}." as {$this->wrap($type)}", [$type]);
687687
}
688688

689689
$query
@@ -723,22 +723,22 @@ public function aggregateTotal(
723723
return $this->connection()->query()
724724
->when(is_array($types), fn ($query) => $query->addSelect('type'))
725725
->selectRaw(match ($aggregate) {
726-
'count' => "sum({$this->wrap('count')})",
727-
'min' => "min({$this->wrap('min')})",
728-
'max' => "max({$this->wrap('max')})",
729-
'sum' => "sum({$this->wrap('sum')})",
730-
'avg' => "avg({$this->wrap('avg')})",
731-
}." as {$this->wrap($aggregate)}")
726+
'count' => "sum({$this->wrap('count')})",
727+
'min' => "min({$this->wrap('min')})",
728+
'max' => "max({$this->wrap('max')})",
729+
'sum' => "sum({$this->wrap('sum')})",
730+
'avg' => "avg({$this->wrap('avg')})",
731+
}." as {$this->wrap($aggregate)}")
732732
->fromSub(fn (Builder $query) => $query
733-
// Buckets
733+
// Buckets
734734
->addSelect('type')
735735
->selectRaw(match ($aggregate) {
736-
'count' => "sum({$this->wrap('value')})",
737-
'min' => "min({$this->wrap('value')})",
738-
'max' => "max({$this->wrap('value')})",
739-
'sum' => "sum({$this->wrap('value')})",
740-
'avg' => "avg({$this->wrap('value')})",
741-
}." as {$this->wrap($aggregate)}")
736+
'count' => "sum({$this->wrap('value')})",
737+
'min' => "min({$this->wrap('value')})",
738+
'max' => "max({$this->wrap('value')})",
739+
'sum' => "sum({$this->wrap('value')})",
740+
'avg' => "avg({$this->wrap('value')})",
741+
}." as {$this->wrap($aggregate)}")
742742
->from('datum_aggregates')
743743
->where('period', $interval->value)
744744
->when(

0 commit comments

Comments
 (0)