Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» Fix styling
Browse files Browse the repository at this point in the history
  • Loading branch information
rabrowne85 authored and github-actions[bot] committed Jan 26, 2024
1 parent c303dd1 commit 61f8758
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 94 deletions.
12 changes: 1 addition & 11 deletions src/Enums/Period.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ public function maxDataPoints(): int

/**
* Function period.
*
* @return int
*/
public function period(): int
{
Expand All @@ -81,8 +79,6 @@ public function period(): int

/**
* Function currentBucket.
*
* @return int
*/
public function currentBucket(): int
{
Expand All @@ -98,9 +94,6 @@ public function currentBucket(): int

/**
* Function getBucketForTimestamp.
*
* @param $timestamp
* @return int
*/
public function getBucketForTimestamp($timestamp): int
{
Expand All @@ -116,8 +109,6 @@ public function getBucketForTimestamp($timestamp): int

/**
* Function getDateTimeFormat.
*
* @return string
*/
public function getDateTimeFormat(): string
{
Expand All @@ -131,8 +122,6 @@ public function getDateTimeFormat(): string

/**
* Function getBuckets.
*
* @return array
*/
public function getBuckets(): array
{
Expand All @@ -148,6 +137,7 @@ public function getBuckets(): array
self::HALFYEAR => CarbonPeriodImmutable::create($now->startOfMonth(), '-1 month', $this->maxDataPoints()),
self::YEAR => CarbonPeriodImmutable::create($now->startOfMonth(), '-1 month', $this->maxDataPoints()),
};

return collect($period->toArray())->reverse()->values()->all();
}
}
166 changes: 83 additions & 83 deletions src/Storage/DatabaseStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public function store(Collection $items): void
->insert(
$this->requiresManualKeyHash()
? $chunk->map(fn ($entry) => [
...($attributes = $entry->attributes()),
'key_hash' => md5($attributes['key']),
])->all()
...($attributes = $entry->attributes()),
'key_hash' => md5($attributes['key']),
])->all()
: $chunk->map->attributes()->all()
)
);
Expand Down Expand Up @@ -108,9 +108,9 @@ public function store(Collection $items): void
->upsert(
$this->requiresManualKeyHash()
? $chunk->map(fn ($entry) => [
...($attributes = $entry->attributes()),
'key_hash' => md5($attributes['key']),
])->all()
...($attributes = $entry->attributes()),
'key_hash' => md5($attributes['key']),
])->all()
: $chunk->map->attributes()->all(), // @phpstan-ignore method.notFound
['type', 'key_hash'],
['timestamp', 'value']
Expand All @@ -132,7 +132,7 @@ protected function connection(): Connection
*/
protected function requiresManualKeyHash(): bool
{
return $this->connection()->getDriverName() === 'sqlite';
return 'sqlite' === $this->connection()->getDriverName();
}

/**
Expand Down Expand Up @@ -410,7 +410,7 @@ public function trim(): void
*/
public function purge(?array $types = null): void
{
if ($types === null) {
if (null === $types) {
$this->connection()->table('datum_values')->truncate();
$this->connection()->table('datum_entries')->truncate();
$this->connection()->table('datum_aggregates')->truncate();
Expand Down Expand Up @@ -466,7 +466,7 @@ public function graph(array $types, string $aggregate, Period $interval): Collec
$structure = collect($types)->mapWithKeys(fn ($type) => [$type => $padding]);

return $this->connection()->table('datum_aggregates') // @phpstan-ignore return.type
->select(['bucket', 'type', 'key', 'value'])
->select(['bucket', 'type', 'key', 'value'])
->whereIn('type', $types)
->where('aggregate', $aggregate)
->where('period', $period)
Expand Down Expand Up @@ -529,12 +529,12 @@ public function aggregate(

foreach ($aggregates as $aggregate) {
$query->selectRaw(match ($aggregate) {
'count' => "sum({$this->wrap('count')})",
'min' => "min({$this->wrap('min')})",
'max' => "max({$this->wrap('max')})",
'sum' => "sum({$this->wrap('sum')})",
'avg' => "avg({$this->wrap('avg')})",
}." as {$this->wrap($aggregate)}");
'count' => "sum({$this->wrap('count')})",
'min' => "min({$this->wrap('min')})",
'max' => "max({$this->wrap('max')})",
'sum' => "sum({$this->wrap('sum')})",
'avg' => "avg({$this->wrap('avg')})",
}." as {$this->wrap($aggregate)}");
}

$query->fromSub(function (Builder $query) use ($type, $aggregates, $interval) {
Expand All @@ -548,12 +548,12 @@ public function aggregate(

foreach ($aggregates as $aggregate) {
$query->selectRaw(match ($aggregate) {
'count' => 'count(*)',
'min' => "min({$this->wrap('value')})",
'max' => "max({$this->wrap('value')})",
'sum' => "sum({$this->wrap('value')})",
'avg' => "avg({$this->wrap('value')})",
}." as {$this->wrap($aggregate)}");
'count' => 'count(*)',
'min' => "min({$this->wrap('value')})",
'max' => "max({$this->wrap('value')})",
'sum' => "sum({$this->wrap('value')})",
'avg' => "avg({$this->wrap('value')})",
}." as {$this->wrap($aggregate)}");
}

$query
Expand All @@ -571,12 +571,12 @@ public function aggregate(
foreach ($aggregates as $aggregate) {
if ($aggregate === $currentAggregate) {
$query->selectRaw(match ($aggregate) {
'count' => "sum({$this->wrap('value')})",
'min' => "min({$this->wrap('value')})",
'max' => "max({$this->wrap('value')})",
'sum' => "sum({$this->wrap('value')})",
'avg' => "avg({$this->wrap('value')})",
}." as {$this->wrap($aggregate)}");
'count' => "sum({$this->wrap('value')})",
'min' => "min({$this->wrap('value')})",
'max' => "max({$this->wrap('value')})",
'sum' => "sum({$this->wrap('value')})",
'avg' => "avg({$this->wrap('value')})",
}." as {$this->wrap($aggregate)}");
} else {
$query->selectRaw("null as {$this->wrap($aggregate)}");
}
Expand Down Expand Up @@ -644,12 +644,12 @@ public function aggregateTypes(

foreach ($types as $type) {
$query->selectRaw(match ($aggregate) {
'count' => "sum({$this->wrap($type)})",
'min' => "min({$this->wrap($type)})",
'max' => "max({$this->wrap($type)})",
'sum' => "sum({$this->wrap($type)})",
'avg' => "avg({$this->wrap($type)})",
}." as {$this->wrap($type)}");
'count' => "sum({$this->wrap($type)})",
'min' => "min({$this->wrap($type)})",
'max' => "max({$this->wrap($type)})",
'sum' => "sum({$this->wrap($type)})",
'avg' => "avg({$this->wrap($type)})",
}." as {$this->wrap($type)}");
}

$query->fromSub(function (Builder $query) use ($types, $aggregate, $interval) {
Expand All @@ -663,12 +663,12 @@ public function aggregateTypes(

foreach ($types as $type) {
$query->selectRaw(match ($aggregate) {
'count' => "count(case when ({$this->wrap('type')} = ?) then true else null end)",
'min' => "min(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
'max' => "max(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
'sum' => "sum(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
'avg' => "avg(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
}." as {$this->wrap($type)}", [$type]);
'count' => "count(case when ({$this->wrap('type')} = ?) then true else null end)",
'min' => "min(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
'max' => "max(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
'sum' => "sum(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
'avg' => "avg(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
}." as {$this->wrap($type)}", [$type]);
}

$query
Expand All @@ -684,12 +684,12 @@ public function aggregateTypes(

foreach ($types as $type) {
$query->selectRaw(match ($aggregate) {
'count' => "sum(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
'min' => "min(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
'max' => "max(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
'sum' => "sum(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
'avg' => "avg(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
}." as {$this->wrap($type)}", [$type]);
'count' => "sum(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
'min' => "min(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
'max' => "max(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
'sum' => "sum(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
'avg' => "avg(case when ({$this->wrap('type')} = ?) then {$this->wrap('value')} else null end)",
}." as {$this->wrap($type)}", [$type]);
}

$query
Expand Down Expand Up @@ -734,52 +734,52 @@ public function aggregateTotal(
return $this->connection()->query()
->when(is_array($types), fn ($query) => $query->addSelect('type'))
->selectRaw(match ($aggregate) {
'count' => "sum({$this->wrap('count')})",
'min' => "min({$this->wrap('min')})",
'max' => "max({$this->wrap('max')})",
'sum' => "sum({$this->wrap('sum')})",
'avg' => "avg({$this->wrap('avg')})",
}." as {$this->wrap($aggregate)}")
'count' => "sum({$this->wrap('count')})",
'min' => "min({$this->wrap('min')})",
'max' => "max({$this->wrap('max')})",
'sum' => "sum({$this->wrap('sum')})",
'avg' => "avg({$this->wrap('avg')})",
}." as {$this->wrap($aggregate)}")
->fromSub(fn (Builder $query) => $query
// Tail
->addSelect('type')
// Tail
->addSelect('type')
->selectRaw(match ($aggregate) {
'count' => 'count(*)',
'min' => "min({$this->wrap('value')})",
'max' => "max({$this->wrap('value')})",
'sum' => "sum({$this->wrap('value')})",
'avg' => "avg({$this->wrap('value')})",
}." as {$this->wrap($aggregate)}")
->from('datum_entries')
->when(
is_array($types),
fn ($query) => $query->whereIn('type', $types),
fn ($query) => $query->where('type', $types)
)
->where('timestamp', '>=', $tailStart)
->where('timestamp', '<=', $tailEnd)
->groupBy('type')
// Buckets
->unionAll(fn (Builder $query) => $query
->select('type')
->selectRaw(match ($aggregate) {
'count' => 'count(*)',
'min' => "min({$this->wrap('value')})",
'max' => "max({$this->wrap('value')})",
'sum' => "sum({$this->wrap('value')})",
'avg' => "avg({$this->wrap('value')})",
}." as {$this->wrap($aggregate)}")
->from('datum_entries')
'count' => "sum({$this->wrap('value')})",
'min' => "min({$this->wrap('value')})",
'max' => "max({$this->wrap('value')})",
'sum' => "sum({$this->wrap('value')})",
'avg' => "avg({$this->wrap('value')})",
}." as {$this->wrap($aggregate)}")
->from('datum_aggregates')
->where('period', $period)
->when(
is_array($types),
fn ($query) => $query->whereIn('type', $types),
fn ($query) => $query->where('type', $types)
)
->where('timestamp', '>=', $tailStart)
->where('timestamp', '<=', $tailEnd)
->where('aggregate', $aggregate)
->where('bucket', '>=', $oldestBucket)
->groupBy('type')
// Buckets
->unionAll(fn (Builder $query) => $query
->select('type')
->selectRaw(match ($aggregate) {
'count' => "sum({$this->wrap('value')})",
'min' => "min({$this->wrap('value')})",
'max' => "max({$this->wrap('value')})",
'sum' => "sum({$this->wrap('value')})",
'avg' => "avg({$this->wrap('value')})",
}." as {$this->wrap($aggregate)}")
->from('datum_aggregates')
->where('period', $period)
->when(
is_array($types),
fn ($query) => $query->whereIn('type', $types),
fn ($query) => $query->where('type', $types)
)
->where('aggregate', $aggregate)
->where('bucket', '>=', $oldestBucket)
->groupBy('type')
), as: 'child'
), as: 'child'
)
->groupBy('type')
->when(
Expand Down

0 comments on commit 61f8758

Please sign in to comment.