Skip to content

Commit

Permalink
fix: moved overview graph query to data model as opposed to logs model
Browse files Browse the repository at this point in the history
  • Loading branch information
lewislarsen committed Jun 29, 2024
1 parent 7a1dd0c commit a64b313
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
10 changes: 5 additions & 5 deletions app/Models/BackupTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ class BackupTask extends Model
];

/**
* Get the count of logs per month for the last six months for a given user.
* Get the count of tasks per month for the last six months for a given user.
*
* @return array<string, int>
*/
public static function logsCountPerMonthForLastSixMonths(int $userId): array
{
$sixMonthsAgo = now()->subMonths(6);

return BackupTaskLog::query()
->join('backup_tasks', 'backup_tasks.id', '=', 'backup_task_logs.backup_task_id')
return BackupTaskData::query()
->join('backup_tasks', 'backup_tasks.id', '=', 'backup_task_data.backup_task_id')
->where('backup_tasks.user_id', $userId)
->where('backup_task_logs.created_at', '>=', $sixMonthsAgo)
->selectRaw('COUNT(*) as count, to_char(backup_task_logs.created_at, \'Mon YYYY\') as month')
->where('backup_task_data.created_at', '>=', $sixMonthsAgo)
->selectRaw('COUNT(*) as count, to_char(backup_task_data.created_at, \'Mon YYYY\') as month')
->groupBy('month')
->get()
->mapWithKeys(function ($item) {
Expand Down
5 changes: 3 additions & 2 deletions tests/Unit/BackupTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Jobs\RunFileBackupTaskJob;
use App\Mail\BackupTasks\OutputMail;
use App\Models\BackupTask;
use App\Models\BackupTaskData;
use App\Models\BackupTaskLog;
use App\Models\RemoteServer;
use App\Models\User;
Expand Down Expand Up @@ -466,8 +467,8 @@
]);

for ($i = 0; $i < 6; $i++) {
BackupTaskLog::create([
'output' => 'Test output',
BackupTaskData::create([
'duration' => 25,
'backup_task_id' => $backupTask->id,
'created_at' => now()->subMonths($i),
]);
Expand Down

0 comments on commit a64b313

Please sign in to comment.