-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
948 additions
and
191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace App\Providers; | ||
|
||
use App\Services\ActiveTimeService; | ||
use App\Services\Charts\Types\LineChartService; | ||
use App\Services\StudentService; | ||
use Illuminate\Support\ServiceProvider; | ||
|
||
class StudentServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Register services. | ||
*/ | ||
public function register(): void | ||
{ | ||
$this->app->singleton(StudentService::class, function ($app) { | ||
return new StudentService( | ||
$app->make(ActiveTimeService::class), | ||
$app->make(LineChartService::class) | ||
); | ||
}); | ||
} | ||
|
||
/** | ||
* Bootstrap services. | ||
*/ | ||
public function boot(): void | ||
{ | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace App\Services; | ||
|
||
use App\Models\ActiveTime; | ||
|
||
class ActiveTimeService | ||
{ | ||
public function fetchTotalTimeForUser(string $timeframe, mixed $userId = null): string | ||
{ | ||
$totalSecondsDuringTimeframe = ActiveTime::query() | ||
->when($userId, fn ($query) => $query->where('user_id', $userId)) | ||
->when(! $userId, fn ($query) => $query->whereHas('user', fn ($query) => $query->where('parent_id', request()->user()->id))) | ||
->filterByTimeframe($timeframe) | ||
->sum('total_seconds'); | ||
|
||
return $this->formatActiveTime($totalSecondsDuringTimeframe); | ||
} | ||
|
||
protected function formatActiveTime(int $seconds): string | ||
{ | ||
$days = intdiv($seconds, 86400); | ||
$hours = intdiv($seconds % 86400, 3600); | ||
$minutes = intdiv($seconds % 3600, 60); | ||
|
||
return $days.'d '.$hours.'h '.$minutes.'m'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace App\Services\Charts\Interfaces; | ||
|
||
use Illuminate\Support\Collection; | ||
|
||
interface TimeframeStrategy | ||
{ | ||
public function fetchData(string $userId): Collection; | ||
|
||
public function getPeriod(): array; | ||
|
||
public function transformData(Collection $data): array; | ||
|
||
public function buildChartData(array $data): array; | ||
} |
62 changes: 62 additions & 0 deletions
62
app/Services/Charts/TimeframeStrategies/MonthlyStrategy.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
namespace App\Services\Charts\TimeframeStrategies; | ||
|
||
use App\Models\ActiveTime; | ||
use App\Services\Charts\Interfaces\TimeframeStrategy; | ||
use Carbon\CarbonPeriod; | ||
use Illuminate\Support\Carbon; | ||
use Illuminate\Support\Collection; | ||
|
||
class MonthlyStrategy implements TimeframeStrategy | ||
{ | ||
public function fetchData(mixed $userId = null): Collection | ||
{ | ||
return ActiveTime::query() | ||
->when($userId, fn ($query) => $query->where('user_id', $userId)) | ||
->when(! $userId, fn ($query) => $query->whereHas('user', fn ($query) => $query->where('parent_id', request()->user()->id))) | ||
->whereBetween('created_at', [ | ||
now(request()->user()->timezone)->startOfMonth(), | ||
now(request()->user()->timezone)->endOfMonth(), | ||
]) | ||
->selectRaw('DAY(created_at) as day, SUM(total_seconds) as total_seconds') | ||
->groupBy('day') | ||
->orderBy('day') | ||
->get(); | ||
} | ||
|
||
public function getPeriod(): array | ||
{ | ||
$startOfMonth = now(request()->user()->timezone)->startOfMonth(); | ||
$endOfMonth = now(request()->user()->timezone)->endOfMonth(); | ||
|
||
$period = CarbonPeriod::create($startOfMonth, '1 day', $endOfMonth); | ||
$days = []; | ||
|
||
foreach ($period as $date) { | ||
$days[$date->format('j')] = 0; | ||
} | ||
|
||
return $days; | ||
} | ||
|
||
public function transformData($data): array | ||
{ | ||
$totalHoursPerDay = $data->reduce(function ($carry, $item) { | ||
$day = Carbon::createFromFormat('j', $item->day)->format('j'); | ||
$carry[$day] = (int) $item->total_seconds; | ||
|
||
return $carry; | ||
}, []); | ||
|
||
return array_replace($this->getPeriod(), $totalHoursPerDay); | ||
} | ||
|
||
public function buildChartData(array $data): array | ||
{ | ||
return [ | ||
'labels' => array_keys($data), | ||
'series' => array_values($data), | ||
]; | ||
} | ||
} |
Oops, something went wrong.