-
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
135 changed files
with
1,826 additions
and
1,265 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
Binary file not shown.
Binary file not shown.
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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,50 @@ | ||
<?php | ||
|
||
namespace App\Services; | ||
|
||
use App\Models\Log; | ||
use Carbon\Carbon; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
class Trend | ||
{ | ||
/** | ||
* Create a new class instance. | ||
*/ | ||
public function __construct() | ||
{ | ||
// | ||
} | ||
|
||
public static function usageTrend(int $userId, int $period = 6) | ||
{ | ||
$months = []; | ||
for ($i = 0; $i < 6; $i++) { | ||
$months[] = now()->subMonths($period - 1)->addMonths($i)->format('Y-m'); | ||
} | ||
|
||
// Fetch the logs for the last 6 months, grouped by month | ||
$logsByMonth = Log::where('created_at', '>=', now()->subMonths($period)) | ||
->select( | ||
DB::raw("strftime('%Y', created_at) as year"), | ||
DB::raw("strftime('%m', created_at) as month"), | ||
DB::raw('COUNT(*) as total_logs'), | ||
DB::raw("SUM(CASE WHEN (select user_id from api_keys where logs.api_key_id = api_keys.id) = {$userId} THEN 1 ELSE 0 END) as user_logs") | ||
) | ||
->groupBy('year', 'month') | ||
->orderBy('year', 'desc') | ||
->get()->keyBy(function ($log) { | ||
return sprintf('%04d-%02d', $log->year, $log->month); | ||
}); | ||
|
||
// Calculate the usage rate per month | ||
return collect($months)->map(function ($month) use ($logsByMonth) { | ||
$log = $logsByMonth->get($month); | ||
|
||
return [ | ||
'month' => Carbon::createFromFormat('Y-m', $month)->format('F Y'), | ||
'usageRate' => round($log ? ($log->user_logs / $log->total_logs) * 100 : 0, 2), | ||
]; | ||
}); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -85,4 +85,4 @@ | |
}, | ||
"minimum-stability": "stable", | ||
"prefer-stable": true | ||
} | ||
} |
Oops, something went wrong.