remove dead dashboard api key stats internals
This commit is contained in:
@@ -404,33 +404,6 @@ func (h *DashboardHandler) GetGroupStats(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAPIKeyUsageTrend handles getting API key usage trend data
|
|
||||||
// GET /api/v1/admin/dashboard/api-keys-trend
|
|
||||||
// Query params: start_date, end_date (YYYY-MM-DD), granularity (day/hour), limit (default 5)
|
|
||||||
func (h *DashboardHandler) GetAPIKeyUsageTrend(c *gin.Context) {
|
|
||||||
startTime, endTime := parseTimeRange(c)
|
|
||||||
granularity := c.DefaultQuery("granularity", "day")
|
|
||||||
limitStr := c.DefaultQuery("limit", "5")
|
|
||||||
limit, err := strconv.Atoi(limitStr)
|
|
||||||
if err != nil || limit <= 0 {
|
|
||||||
limit = 5
|
|
||||||
}
|
|
||||||
|
|
||||||
trend, hit, err := h.getAPIKeyUsageTrendCached(c.Request.Context(), startTime, endTime, granularity, limit)
|
|
||||||
if err != nil {
|
|
||||||
response.Error(c, 500, "Failed to get API key usage trend")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
c.Header("X-Snapshot-Cache", cacheStatusValue(hit))
|
|
||||||
|
|
||||||
response.Success(c, gin.H{
|
|
||||||
"trend": trend,
|
|
||||||
"start_date": startTime.Format("2006-01-02"),
|
|
||||||
"end_date": endTime.Add(-24 * time.Hour).Format("2006-01-02"),
|
|
||||||
"granularity": granularity,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetUserUsageTrend handles getting user usage trend data
|
// GetUserUsageTrend handles getting user usage trend data
|
||||||
// GET /api/v1/admin/dashboard/users-trend
|
// GET /api/v1/admin/dashboard/users-trend
|
||||||
// Query params: start_date, end_date (YYYY-MM-DD), granularity (day/hour), limit (default 12)
|
// Query params: start_date, end_date (YYYY-MM-DD), granularity (day/hour), limit (default 12)
|
||||||
@@ -465,7 +438,6 @@ type BatchUsersUsageRequest struct {
|
|||||||
|
|
||||||
var dashboardUsersRankingCache = newSnapshotCache(5 * time.Minute)
|
var dashboardUsersRankingCache = newSnapshotCache(5 * time.Minute)
|
||||||
var dashboardBatchUsersUsageCache = newSnapshotCache(30 * time.Second)
|
var dashboardBatchUsersUsageCache = newSnapshotCache(30 * time.Second)
|
||||||
var dashboardBatchAPIKeysUsageCache = newSnapshotCache(30 * time.Second)
|
|
||||||
|
|
||||||
func parseRankingLimit(raw string) int {
|
func parseRankingLimit(raw string) int {
|
||||||
limit, err := strconv.Atoi(strings.TrimSpace(raw))
|
limit, err := strconv.Atoi(strings.TrimSpace(raw))
|
||||||
@@ -558,50 +530,6 @@ func (h *DashboardHandler) GetBatchUsersUsage(c *gin.Context) {
|
|||||||
response.Success(c, payload)
|
response.Success(c, payload)
|
||||||
}
|
}
|
||||||
|
|
||||||
// BatchAPIKeysUsageRequest represents the request body for batch api key usage stats
|
|
||||||
type BatchAPIKeysUsageRequest struct {
|
|
||||||
APIKeyIDs []int64 `json:"api_key_ids" binding:"required"`
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetBatchAPIKeysUsage handles getting usage stats for multiple API keys
|
|
||||||
// POST /api/v1/admin/dashboard/api-keys-usage
|
|
||||||
func (h *DashboardHandler) GetBatchAPIKeysUsage(c *gin.Context) {
|
|
||||||
var req BatchAPIKeysUsageRequest
|
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
|
||||||
response.BadRequest(c, "Invalid request: "+err.Error())
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
apiKeyIDs := normalizeInt64IDList(req.APIKeyIDs)
|
|
||||||
if len(apiKeyIDs) == 0 {
|
|
||||||
response.Success(c, gin.H{"stats": map[string]any{}})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
keyRaw, _ := json.Marshal(struct {
|
|
||||||
APIKeyIDs []int64 `json:"api_key_ids"`
|
|
||||||
}{
|
|
||||||
APIKeyIDs: apiKeyIDs,
|
|
||||||
})
|
|
||||||
cacheKey := string(keyRaw)
|
|
||||||
if cached, ok := dashboardBatchAPIKeysUsageCache.Get(cacheKey); ok {
|
|
||||||
c.Header("X-Snapshot-Cache", "hit")
|
|
||||||
response.Success(c, cached.Payload)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
stats, err := h.dashboardService.GetBatchAPIKeyUsageStats(c.Request.Context(), apiKeyIDs, time.Time{}, time.Time{})
|
|
||||||
if err != nil {
|
|
||||||
response.Error(c, 500, "Failed to get API key usage stats")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
payload := gin.H{"stats": stats}
|
|
||||||
dashboardBatchAPIKeysUsageCache.Set(cacheKey, payload)
|
|
||||||
c.Header("X-Snapshot-Cache", "miss")
|
|
||||||
response.Success(c, payload)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetUserBreakdown handles getting per-user usage breakdown within a dimension.
|
// GetUserBreakdown handles getting per-user usage breakdown within a dimension.
|
||||||
// GET /api/v1/admin/dashboard/user-breakdown
|
// GET /api/v1/admin/dashboard/user-breakdown
|
||||||
// Query params: start_date, end_date, group_id, model, endpoint, endpoint_type, limit
|
// Query params: start_date, end_date, group_id, model, endpoint, endpoint_type, limit
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ var (
|
|||||||
dashboardModelStatsCache = newSnapshotCache(30 * time.Second)
|
dashboardModelStatsCache = newSnapshotCache(30 * time.Second)
|
||||||
dashboardGroupStatsCache = newSnapshotCache(30 * time.Second)
|
dashboardGroupStatsCache = newSnapshotCache(30 * time.Second)
|
||||||
dashboardUsersTrendCache = newSnapshotCache(30 * time.Second)
|
dashboardUsersTrendCache = newSnapshotCache(30 * time.Second)
|
||||||
dashboardAPIKeysTrendCache = newSnapshotCache(30 * time.Second)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type dashboardTrendCacheKey struct {
|
type dashboardTrendCacheKey struct {
|
||||||
@@ -168,23 +167,6 @@ func (h *DashboardHandler) getGroupStatsCached(
|
|||||||
return stats, hit, err
|
return stats, hit, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *DashboardHandler) getAPIKeyUsageTrendCached(ctx context.Context, startTime, endTime time.Time, granularity string, limit int) ([]usagestats.APIKeyUsageTrendPoint, bool, error) {
|
|
||||||
key := mustMarshalDashboardCacheKey(dashboardEntityTrendCacheKey{
|
|
||||||
StartTime: startTime.UTC().Format(time.RFC3339),
|
|
||||||
EndTime: endTime.UTC().Format(time.RFC3339),
|
|
||||||
Granularity: granularity,
|
|
||||||
Limit: limit,
|
|
||||||
})
|
|
||||||
entry, hit, err := dashboardAPIKeysTrendCache.GetOrLoad(key, func() (any, error) {
|
|
||||||
return h.dashboardService.GetAPIKeyUsageTrend(ctx, startTime, endTime, granularity, limit)
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, hit, err
|
|
||||||
}
|
|
||||||
trend, err := snapshotPayloadAs[[]usagestats.APIKeyUsageTrendPoint](entry.Payload)
|
|
||||||
return trend, hit, err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *DashboardHandler) getUserUsageTrendCached(ctx context.Context, startTime, endTime time.Time, granularity string, limit int) ([]usagestats.UserUsageTrendPoint, bool, error) {
|
func (h *DashboardHandler) getUserUsageTrendCached(ctx context.Context, startTime, endTime time.Time, granularity string, limit int) ([]usagestats.UserUsageTrendPoint, bool, error) {
|
||||||
key := mustMarshalDashboardCacheKey(dashboardEntityTrendCacheKey{
|
key := mustMarshalDashboardCacheKey(dashboardEntityTrendCacheKey{
|
||||||
StartTime: startTime.UTC().Format(time.RFC3339),
|
StartTime: startTime.UTC().Format(time.RFC3339),
|
||||||
|
|||||||
@@ -51,7 +51,6 @@ type UsageLogRepository interface {
|
|||||||
GetGroupStatsWithFilters(ctx context.Context, startTime, endTime time.Time, userID, apiKeyID, accountID, groupID int64, requestType *int16, stream *bool, billingType *int8) ([]usagestats.GroupStat, error)
|
GetGroupStatsWithFilters(ctx context.Context, startTime, endTime time.Time, userID, apiKeyID, accountID, groupID int64, requestType *int16, stream *bool, billingType *int8) ([]usagestats.GroupStat, error)
|
||||||
GetUserBreakdownStats(ctx context.Context, startTime, endTime time.Time, dim usagestats.UserBreakdownDimension, limit int) ([]usagestats.UserBreakdownItem, error)
|
GetUserBreakdownStats(ctx context.Context, startTime, endTime time.Time, dim usagestats.UserBreakdownDimension, limit int) ([]usagestats.UserBreakdownItem, error)
|
||||||
GetAllGroupUsageSummary(ctx context.Context, todayStart time.Time) ([]usagestats.GroupUsageSummary, error)
|
GetAllGroupUsageSummary(ctx context.Context, todayStart time.Time) ([]usagestats.GroupUsageSummary, error)
|
||||||
GetAPIKeyUsageTrend(ctx context.Context, startTime, endTime time.Time, granularity string, limit int) ([]usagestats.APIKeyUsageTrendPoint, error)
|
|
||||||
GetUserUsageTrend(ctx context.Context, startTime, endTime time.Time, granularity string, limit int) ([]usagestats.UserUsageTrendPoint, error)
|
GetUserUsageTrend(ctx context.Context, startTime, endTime time.Time, granularity string, limit int) ([]usagestats.UserUsageTrendPoint, error)
|
||||||
GetUserSpendingRanking(ctx context.Context, startTime, endTime time.Time, limit int) (*usagestats.UserSpendingRankingResponse, error)
|
GetUserSpendingRanking(ctx context.Context, startTime, endTime time.Time, limit int) (*usagestats.UserSpendingRankingResponse, error)
|
||||||
GetBatchUserUsageStats(ctx context.Context, userIDs []int64, startTime, endTime time.Time) (map[int64]*usagestats.BatchUserUsageStats, error)
|
GetBatchUserUsageStats(ctx context.Context, userIDs []int64, startTime, endTime time.Time) (map[int64]*usagestats.BatchUserUsageStats, error)
|
||||||
|
|||||||
@@ -341,14 +341,6 @@ func parseStatsUpdatedAt(raw string) time.Time {
|
|||||||
return parsed.UTC()
|
return parsed.UTC()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DashboardService) GetAPIKeyUsageTrend(ctx context.Context, startTime, endTime time.Time, granularity string, limit int) ([]usagestats.APIKeyUsageTrendPoint, error) {
|
|
||||||
trend, err := s.usageRepo.GetAPIKeyUsageTrend(ctx, startTime, endTime, granularity, limit)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("get api key usage trend: %w", err)
|
|
||||||
}
|
|
||||||
return trend, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *DashboardService) GetUserUsageTrend(ctx context.Context, startTime, endTime time.Time, granularity string, limit int) ([]usagestats.UserUsageTrendPoint, error) {
|
func (s *DashboardService) GetUserUsageTrend(ctx context.Context, startTime, endTime time.Time, granularity string, limit int) ([]usagestats.UserUsageTrendPoint, error) {
|
||||||
trend, err := s.usageRepo.GetUserUsageTrend(ctx, startTime, endTime, granularity, limit)
|
trend, err := s.usageRepo.GetUserUsageTrend(ctx, startTime, endTime, granularity, limit)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -380,11 +372,3 @@ func (s *DashboardService) GetBatchUserUsageStats(ctx context.Context, userIDs [
|
|||||||
}
|
}
|
||||||
return stats, nil
|
return stats, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *DashboardService) GetBatchAPIKeyUsageStats(ctx context.Context, apiKeyIDs []int64, startTime, endTime time.Time) (map[int64]*usagestats.BatchAPIKeyUsageStats, error) {
|
|
||||||
stats, err := s.usageRepo.GetBatchAPIKeyUsageStats(ctx, apiKeyIDs, startTime, endTime)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("get batch api key usage stats: %w", err)
|
|
||||||
}
|
|
||||||
return stats, nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ describe('useTableLoader', () => {
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.useFakeTimers()
|
vi.useFakeTimers()
|
||||||
vi.clearAllMocks()
|
vi.clearAllMocks()
|
||||||
|
delete window.__APP_CONFIG__
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { afterEach, describe, expect, it } from 'vitest'
|
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
|
||||||
|
|
||||||
import {
|
import {
|
||||||
DEFAULT_TABLE_PAGE_SIZE,
|
DEFAULT_TABLE_PAGE_SIZE,
|
||||||
@@ -9,6 +9,10 @@ import {
|
|||||||
} from '@/utils/tablePreferences'
|
} from '@/utils/tablePreferences'
|
||||||
|
|
||||||
describe('tablePreferences', () => {
|
describe('tablePreferences', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
delete window.__APP_CONFIG__
|
||||||
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
delete window.__APP_CONFIG__
|
delete window.__APP_CONFIG__
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user