Skip to content

Commit

Permalink
#2664 Added metrics so I can keep track of done API requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Wotuu committed Jan 20, 2025
1 parent 57558bb commit 363bc6f
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Http\Middleware;
namespace App\Http\Middleware\Api;

use App\Service\User\UserServiceInterface;
use Closure;
Expand Down
34 changes: 34 additions & 0 deletions app/Http/Middleware/Api/ApiMetrics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Http\Middleware\Api;

use App\Models\Metrics\Metric;
use App\Service\Metric\MetricServiceInterface;
use Auth;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class ApiMetrics
{
public function __construct(private readonly MetricServiceInterface $metricService)
{
}

/**
* Handle an incoming request.
*/
public function handle(Request $request, Closure $next): Response
{
if (!app()->runningUnitTests()) {
$this->metricService->storeMetricByModel(
Auth::user(),
Metric::CATEGORY_API_CALL,
$request->path(),
1
);
}

return $next($request);
}
}
5 changes: 3 additions & 2 deletions app/Models/Metrics/Metric.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
3 changes: 2 additions & 1 deletion app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'));
}

Expand Down
6 changes: 3 additions & 3 deletions app/Service/Metric/MetricService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion app/Service/Metric/MetricServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 363bc6f

Please sign in to comment.