diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index ed88b48d3..75c9c1669 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -2,7 +2,7 @@ namespace App\Http; -use App\Http\Middleware\ApiAuthentication; +use App\Http\Middleware\Api\ApiAuthentication; use App\Http\Middleware\DebugBarMessageLogger; use App\Http\Middleware\DebugInfoContextLogger; use App\Http\Middleware\EncryptCookies; diff --git a/app/Http/Middleware/ApiAuthentication.php b/app/Http/Middleware/Api/ApiAuthentication.php similarity index 95% rename from app/Http/Middleware/ApiAuthentication.php rename to app/Http/Middleware/Api/ApiAuthentication.php index 985f37d11..6a6440e34 100644 --- a/app/Http/Middleware/ApiAuthentication.php +++ b/app/Http/Middleware/Api/ApiAuthentication.php @@ -1,6 +1,6 @@ runningUnitTests()) { + $this->metricService->storeMetricByModel( + Auth::user(), + Metric::CATEGORY_API_CALL, + $request->path(), + 1 + ); + } + + return $next($request); + } +} diff --git a/app/Models/Metrics/Metric.php b/app/Models/Metrics/Metric.php index 5b68b85cf..9b133614c 100644 --- a/app/Models/Metrics/Metric.php +++ b/app/Models/Metrics/Metric.php @@ -27,13 +27,14 @@ class Metric extends Model use HasGenericModelRelation; public const CATEGORY_DUNGEON_ROUTE_MDT_COPY = 1; + public const CATEGORY_API_CALL = 10; public const ALL_CATEGORIES = [ self::CATEGORY_DUNGEON_ROUTE_MDT_COPY, + self::CATEGORY_API_CALL, ]; - public const TAG_MDT_COPY_VIEW = 'view'; - + public const TAG_MDT_COPY_VIEW = 'view'; public const TAG_MDT_COPY_EMBED = 'embed'; public const ALL_TAGS = [ diff --git a/app/Providers/RouteServiceProvider.php b/app/Providers/RouteServiceProvider.php index f192daa8d..7c88b34b4 100644 --- a/app/Providers/RouteServiceProvider.php +++ b/app/Providers/RouteServiceProvider.php @@ -2,6 +2,7 @@ namespace App\Providers; +use App\Http\Middleware\Api\ApiMetrics; use App\Models\Laratrust\Role; use App\Models\User; use Illuminate\Cache\RateLimiting\Limit; @@ -57,7 +58,7 @@ protected function mapWebRoutes(): void protected function mapApiRoutes(): void { Route::prefix('api') - ->middleware(['api', 'throttle:api-general']) + ->middleware(['api', 'throttle:api-general', ApiMetrics::class]) ->group(base_path('routes/api.php')); } diff --git a/app/Service/Metric/MetricService.php b/app/Service/Metric/MetricService.php index 701515641..7f25708c4 100644 --- a/app/Service/Metric/MetricService.php +++ b/app/Service/Metric/MetricService.php @@ -21,11 +21,11 @@ public function storeMetric(?int $modelId, ?string $modelClass, int $category, s ]); } - public function storeMetricByModel(Model $model, int $category, string $tag, int $value): Metric + public function storeMetricByModel(?Model $model, int $category, string $tag, int $value): Metric { return Metric::create([ - 'model_id' => $model->id, - 'model_class' => $model::class, + 'model_id' => $model?->id, + 'model_class' => $model !== null ? $model::class : null, 'category' => $category, 'tag' => $tag, 'value' => $value, diff --git a/app/Service/Metric/MetricServiceInterface.php b/app/Service/Metric/MetricServiceInterface.php index b860dffb9..1c70147fb 100644 --- a/app/Service/Metric/MetricServiceInterface.php +++ b/app/Service/Metric/MetricServiceInterface.php @@ -9,7 +9,7 @@ interface MetricServiceInterface { public function storeMetric(?int $modelId, ?string $modelClass, int $category, string $tag, int $value): Metric; - public function storeMetricByModel(Model $model, int $category, string $tag, int $value): Metric; + public function storeMetricByModel(?Model $model, int $category, string $tag, int $value): Metric; public function aggregateMetrics(): bool; }