first commit from hostslim
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Concerns;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
trait ParsesDateRange
|
||||
{
|
||||
private function parseDateRange(Request $request): array
|
||||
{
|
||||
$range = $request->get('range', '7d');
|
||||
$to = Carbon::today();
|
||||
|
||||
$from = match ($range) {
|
||||
'today' => Carbon::today(),
|
||||
'7d' => Carbon::today()->subDays(6),
|
||||
'30d' => Carbon::today()->subDays(29),
|
||||
'90d' => Carbon::today()->subDays(89),
|
||||
'custom' => Carbon::parse($request->get('from', Carbon::today()->subDays(6)->toDateString())),
|
||||
default => Carbon::today()->subDays(6),
|
||||
};
|
||||
|
||||
if ($range === 'custom' && $request->has('to')) {
|
||||
$to = Carbon::parse($request->get('to'));
|
||||
}
|
||||
|
||||
if ($from->gt($to)) {
|
||||
[$from, $to] = [$to, $from];
|
||||
}
|
||||
|
||||
return [$from, $to];
|
||||
}
|
||||
|
||||
private function previousPeriod(Carbon $from, Carbon $to): array
|
||||
{
|
||||
$days = $from->diffInDays($to) + 1;
|
||||
|
||||
$prevTo = $from->copy()->subDay();
|
||||
$prevFrom = $prevTo->copy()->subDays($days - 1);
|
||||
|
||||
return [$prevFrom, $prevTo];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Concerns;
|
||||
|
||||
use App\Models\Segment;
|
||||
use App\Models\Website;
|
||||
use App\Services\AnalyticsQueryService;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
trait ParsesSegment
|
||||
{
|
||||
private function applySegment(Request $request, Website $website, AnalyticsQueryService $service): AnalyticsQueryService
|
||||
{
|
||||
$segmentId = $request->get('segment');
|
||||
if (! $segmentId) {
|
||||
return $service;
|
||||
}
|
||||
|
||||
$segment = $website->segments()->find($segmentId);
|
||||
if (! $segment) {
|
||||
return $service;
|
||||
}
|
||||
|
||||
return $service->withSegment($segment->matchingVisitorIds());
|
||||
}
|
||||
|
||||
private function activeSegment(Request $request, Website $website): ?Segment
|
||||
{
|
||||
$segmentId = $request->get('segment');
|
||||
if (! $segmentId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $website->segments()->find($segmentId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user