fix(api-explorer): API 사용 통계 데이터 소스 변경
- api_request_logs 테이블 사용으로 변경 (실제 API 호출 기록) - 기존 admin_api_histories는 API Explorer 테스트 기록용으로 유지 - ApiRequestLog 모델 추가 - URL에서 엔드포인트 경로 추출 (REGEXP_REPLACE 사용) - DB facade 사용으로 Eloquent accessor 충돌 방지 변경 전: 테스트 호출 2건만 표시 변경 후: 실제 API 호출 857건+ 표시 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
68
app/Models/DevTools/ApiRequestLog.php
Normal file
68
app/Models/DevTools/ApiRequestLog.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\DevTools;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* API 요청 로그 모델 (API 서버에서 기록된 실제 API 호출)
|
||||
*
|
||||
* @property int $id
|
||||
* @property string $method
|
||||
* @property string $url
|
||||
* @property string|null $route_name
|
||||
* @property array|null $request_headers
|
||||
* @property array|null $request_body
|
||||
* @property array|null $request_query
|
||||
* @property int $response_status
|
||||
* @property string|null $response_body
|
||||
* @property int $duration_ms
|
||||
* @property string|null $ip_address
|
||||
* @property string|null $user_agent
|
||||
* @property int|null $user_id
|
||||
* @property int|null $tenant_id
|
||||
* @property string|null $group_id
|
||||
* @property \Carbon\Carbon $created_at
|
||||
*/
|
||||
class ApiRequestLog extends Model
|
||||
{
|
||||
public $timestamps = false;
|
||||
|
||||
protected $table = 'api_request_logs';
|
||||
|
||||
protected $fillable = [
|
||||
'method',
|
||||
'url',
|
||||
'route_name',
|
||||
'request_headers',
|
||||
'request_body',
|
||||
'request_query',
|
||||
'response_status',
|
||||
'response_body',
|
||||
'duration_ms',
|
||||
'ip_address',
|
||||
'user_agent',
|
||||
'user_id',
|
||||
'tenant_id',
|
||||
'group_id',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'request_headers' => 'array',
|
||||
'request_body' => 'array',
|
||||
'request_query' => 'array',
|
||||
'response_status' => 'integer',
|
||||
'duration_ms' => 'integer',
|
||||
'user_id' => 'integer',
|
||||
'tenant_id' => 'integer',
|
||||
'created_at' => 'datetime',
|
||||
];
|
||||
|
||||
/**
|
||||
* URL에서 엔드포인트 경로 추출 (쿼리스트링 제외)
|
||||
*/
|
||||
public function getEndpointAttribute(): string
|
||||
{
|
||||
return parse_url($this->url, PHP_URL_PATH) ?? $this->url;
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@
|
||||
namespace App\Services\ApiExplorer;
|
||||
|
||||
use App\Models\DevTools\ApiDeprecation;
|
||||
use App\Models\DevTools\ApiHistory;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
@@ -23,13 +22,18 @@ public function __construct(
|
||||
*/
|
||||
public function getUsageStats(): Collection
|
||||
{
|
||||
return ApiHistory::select('endpoint', 'method')
|
||||
// URL에서 경로만 추출 (도메인 및 쿼리스트링 제거)
|
||||
// 예: https://api.sam.kr/api/v1/users?page=1 → /api/v1/users
|
||||
// Note: DB facade 사용 (Eloquent accessor 충돌 방지)
|
||||
return DB::table('api_request_logs')
|
||||
->selectRaw("REGEXP_REPLACE(SUBSTRING_INDEX(url, '?', 1), '^https?://[^/]+', '') as endpoint")
|
||||
->addSelect('method')
|
||||
->selectRaw('COUNT(*) as call_count')
|
||||
->selectRaw('MAX(created_at) as last_called_at')
|
||||
->selectRaw('AVG(duration_ms) as avg_duration_ms')
|
||||
->selectRaw('SUM(CASE WHEN response_status >= 200 AND response_status < 300 THEN 1 ELSE 0 END) as success_count')
|
||||
->selectRaw('SUM(CASE WHEN response_status >= 400 THEN 1 ELSE 0 END) as error_count')
|
||||
->groupBy('endpoint', 'method')
|
||||
->groupByRaw("REGEXP_REPLACE(SUBSTRING_INDEX(url, '?', 1), '^https?://[^/]+', ''), method")
|
||||
->orderByDesc('call_count')
|
||||
->get();
|
||||
}
|
||||
@@ -224,8 +228,10 @@ public function getStaleApis(int $days = 30): Collection
|
||||
{
|
||||
$cutoffDate = now()->subDays($days);
|
||||
|
||||
// 최근 호출된 API
|
||||
$recentlyUsed = ApiHistory::select('endpoint', 'method')
|
||||
// 최근 호출된 API (DB facade 사용 - Eloquent accessor 충돌 방지)
|
||||
$recentlyUsed = DB::table('api_request_logs')
|
||||
->selectRaw("REGEXP_REPLACE(SUBSTRING_INDEX(url, '?', 1), '^https?://[^/]+', '') as endpoint")
|
||||
->addSelect('method')
|
||||
->where('created_at', '>=', $cutoffDate)
|
||||
->distinct()
|
||||
->get()
|
||||
@@ -246,9 +252,10 @@ public function getStaleApis(int $days = 30): Collection
|
||||
*/
|
||||
public function getDailyTrend(int $days = 30): Collection
|
||||
{
|
||||
return ApiHistory::select(DB::raw('DATE(created_at) as date'))
|
||||
return DB::table('api_request_logs')
|
||||
->select(DB::raw('DATE(created_at) as date'))
|
||||
->selectRaw('COUNT(*) as call_count')
|
||||
->selectRaw('COUNT(DISTINCT CONCAT(endpoint, "|", method)) as unique_endpoints')
|
||||
->selectRaw("COUNT(DISTINCT CONCAT(REGEXP_REPLACE(SUBSTRING_INDEX(url, '?', 1), '^https?://[^/]+', ''), '|', method)) as unique_endpoints")
|
||||
->where('created_at', '>=', now()->subDays($days))
|
||||
->groupBy('date')
|
||||
->orderBy('date')
|
||||
|
||||
Reference in New Issue
Block a user