|
| 1 | +package admin |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/Wei-Shaw/sub2api/internal/pkg/usagestats" |
| 10 | +) |
| 11 | + |
| 12 | +var ( |
| 13 | + dashboardTrendCache = newSnapshotCache(30 * time.Second) |
| 14 | + dashboardModelStatsCache = newSnapshotCache(30 * time.Second) |
| 15 | + dashboardGroupStatsCache = newSnapshotCache(30 * time.Second) |
| 16 | + dashboardUsersTrendCache = newSnapshotCache(30 * time.Second) |
| 17 | + dashboardAPIKeysTrendCache = newSnapshotCache(30 * time.Second) |
| 18 | +) |
| 19 | + |
| 20 | +type dashboardTrendCacheKey struct { |
| 21 | + StartTime string `json:"start_time"` |
| 22 | + EndTime string `json:"end_time"` |
| 23 | + Granularity string `json:"granularity"` |
| 24 | + UserID int64 `json:"user_id"` |
| 25 | + APIKeyID int64 `json:"api_key_id"` |
| 26 | + AccountID int64 `json:"account_id"` |
| 27 | + GroupID int64 `json:"group_id"` |
| 28 | + Model string `json:"model"` |
| 29 | + RequestType *int16 `json:"request_type"` |
| 30 | + Stream *bool `json:"stream"` |
| 31 | + BillingType *int8 `json:"billing_type"` |
| 32 | +} |
| 33 | + |
| 34 | +type dashboardModelGroupCacheKey struct { |
| 35 | + StartTime string `json:"start_time"` |
| 36 | + EndTime string `json:"end_time"` |
| 37 | + UserID int64 `json:"user_id"` |
| 38 | + APIKeyID int64 `json:"api_key_id"` |
| 39 | + AccountID int64 `json:"account_id"` |
| 40 | + GroupID int64 `json:"group_id"` |
| 41 | + RequestType *int16 `json:"request_type"` |
| 42 | + Stream *bool `json:"stream"` |
| 43 | + BillingType *int8 `json:"billing_type"` |
| 44 | +} |
| 45 | + |
| 46 | +type dashboardEntityTrendCacheKey struct { |
| 47 | + StartTime string `json:"start_time"` |
| 48 | + EndTime string `json:"end_time"` |
| 49 | + Granularity string `json:"granularity"` |
| 50 | + Limit int `json:"limit"` |
| 51 | +} |
| 52 | + |
| 53 | +func cacheStatusValue(hit bool) string { |
| 54 | + if hit { |
| 55 | + return "hit" |
| 56 | + } |
| 57 | + return "miss" |
| 58 | +} |
| 59 | + |
| 60 | +func mustMarshalDashboardCacheKey(value any) string { |
| 61 | + raw, err := json.Marshal(value) |
| 62 | + if err != nil { |
| 63 | + return "" |
| 64 | + } |
| 65 | + return string(raw) |
| 66 | +} |
| 67 | + |
| 68 | +func snapshotPayloadAs[T any](payload any) (T, error) { |
| 69 | + typed, ok := payload.(T) |
| 70 | + if !ok { |
| 71 | + var zero T |
| 72 | + return zero, fmt.Errorf("unexpected cache payload type %T", payload) |
| 73 | + } |
| 74 | + return typed, nil |
| 75 | +} |
| 76 | + |
| 77 | +func (h *DashboardHandler) getUsageTrendCached( |
| 78 | + ctx context.Context, |
| 79 | + startTime, endTime time.Time, |
| 80 | + granularity string, |
| 81 | + userID, apiKeyID, accountID, groupID int64, |
| 82 | + model string, |
| 83 | + requestType *int16, |
| 84 | + stream *bool, |
| 85 | + billingType *int8, |
| 86 | +) ([]usagestats.TrendDataPoint, bool, error) { |
| 87 | + key := mustMarshalDashboardCacheKey(dashboardTrendCacheKey{ |
| 88 | + StartTime: startTime.UTC().Format(time.RFC3339), |
| 89 | + EndTime: endTime.UTC().Format(time.RFC3339), |
| 90 | + Granularity: granularity, |
| 91 | + UserID: userID, |
| 92 | + APIKeyID: apiKeyID, |
| 93 | + AccountID: accountID, |
| 94 | + GroupID: groupID, |
| 95 | + Model: model, |
| 96 | + RequestType: requestType, |
| 97 | + Stream: stream, |
| 98 | + BillingType: billingType, |
| 99 | + }) |
| 100 | + entry, hit, err := dashboardTrendCache.GetOrLoad(key, func() (any, error) { |
| 101 | + return h.dashboardService.GetUsageTrendWithFilters(ctx, startTime, endTime, granularity, userID, apiKeyID, accountID, groupID, model, requestType, stream, billingType) |
| 102 | + }) |
| 103 | + if err != nil { |
| 104 | + return nil, hit, err |
| 105 | + } |
| 106 | + trend, err := snapshotPayloadAs[[]usagestats.TrendDataPoint](entry.Payload) |
| 107 | + return trend, hit, err |
| 108 | +} |
| 109 | + |
| 110 | +func (h *DashboardHandler) getModelStatsCached( |
| 111 | + ctx context.Context, |
| 112 | + startTime, endTime time.Time, |
| 113 | + userID, apiKeyID, accountID, groupID int64, |
| 114 | + requestType *int16, |
| 115 | + stream *bool, |
| 116 | + billingType *int8, |
| 117 | +) ([]usagestats.ModelStat, bool, error) { |
| 118 | + key := mustMarshalDashboardCacheKey(dashboardModelGroupCacheKey{ |
| 119 | + StartTime: startTime.UTC().Format(time.RFC3339), |
| 120 | + EndTime: endTime.UTC().Format(time.RFC3339), |
| 121 | + UserID: userID, |
| 122 | + APIKeyID: apiKeyID, |
| 123 | + AccountID: accountID, |
| 124 | + GroupID: groupID, |
| 125 | + RequestType: requestType, |
| 126 | + Stream: stream, |
| 127 | + BillingType: billingType, |
| 128 | + }) |
| 129 | + entry, hit, err := dashboardModelStatsCache.GetOrLoad(key, func() (any, error) { |
| 130 | + return h.dashboardService.GetModelStatsWithFilters(ctx, startTime, endTime, userID, apiKeyID, accountID, groupID, requestType, stream, billingType) |
| 131 | + }) |
| 132 | + if err != nil { |
| 133 | + return nil, hit, err |
| 134 | + } |
| 135 | + stats, err := snapshotPayloadAs[[]usagestats.ModelStat](entry.Payload) |
| 136 | + return stats, hit, err |
| 137 | +} |
| 138 | + |
| 139 | +func (h *DashboardHandler) getGroupStatsCached( |
| 140 | + ctx context.Context, |
| 141 | + startTime, endTime time.Time, |
| 142 | + userID, apiKeyID, accountID, groupID int64, |
| 143 | + requestType *int16, |
| 144 | + stream *bool, |
| 145 | + billingType *int8, |
| 146 | +) ([]usagestats.GroupStat, bool, error) { |
| 147 | + key := mustMarshalDashboardCacheKey(dashboardModelGroupCacheKey{ |
| 148 | + StartTime: startTime.UTC().Format(time.RFC3339), |
| 149 | + EndTime: endTime.UTC().Format(time.RFC3339), |
| 150 | + UserID: userID, |
| 151 | + APIKeyID: apiKeyID, |
| 152 | + AccountID: accountID, |
| 153 | + GroupID: groupID, |
| 154 | + RequestType: requestType, |
| 155 | + Stream: stream, |
| 156 | + BillingType: billingType, |
| 157 | + }) |
| 158 | + entry, hit, err := dashboardGroupStatsCache.GetOrLoad(key, func() (any, error) { |
| 159 | + return h.dashboardService.GetGroupStatsWithFilters(ctx, startTime, endTime, userID, apiKeyID, accountID, groupID, requestType, stream, billingType) |
| 160 | + }) |
| 161 | + if err != nil { |
| 162 | + return nil, hit, err |
| 163 | + } |
| 164 | + stats, err := snapshotPayloadAs[[]usagestats.GroupStat](entry.Payload) |
| 165 | + return stats, hit, err |
| 166 | +} |
| 167 | + |
| 168 | +func (h *DashboardHandler) getAPIKeyUsageTrendCached(ctx context.Context, startTime, endTime time.Time, granularity string, limit int) ([]usagestats.APIKeyUsageTrendPoint, bool, error) { |
| 169 | + key := mustMarshalDashboardCacheKey(dashboardEntityTrendCacheKey{ |
| 170 | + StartTime: startTime.UTC().Format(time.RFC3339), |
| 171 | + EndTime: endTime.UTC().Format(time.RFC3339), |
| 172 | + Granularity: granularity, |
| 173 | + Limit: limit, |
| 174 | + }) |
| 175 | + entry, hit, err := dashboardAPIKeysTrendCache.GetOrLoad(key, func() (any, error) { |
| 176 | + return h.dashboardService.GetAPIKeyUsageTrend(ctx, startTime, endTime, granularity, limit) |
| 177 | + }) |
| 178 | + if err != nil { |
| 179 | + return nil, hit, err |
| 180 | + } |
| 181 | + trend, err := snapshotPayloadAs[[]usagestats.APIKeyUsageTrendPoint](entry.Payload) |
| 182 | + return trend, hit, err |
| 183 | +} |
| 184 | + |
| 185 | +func (h *DashboardHandler) getUserUsageTrendCached(ctx context.Context, startTime, endTime time.Time, granularity string, limit int) ([]usagestats.UserUsageTrendPoint, bool, error) { |
| 186 | + key := mustMarshalDashboardCacheKey(dashboardEntityTrendCacheKey{ |
| 187 | + StartTime: startTime.UTC().Format(time.RFC3339), |
| 188 | + EndTime: endTime.UTC().Format(time.RFC3339), |
| 189 | + Granularity: granularity, |
| 190 | + Limit: limit, |
| 191 | + }) |
| 192 | + entry, hit, err := dashboardUsersTrendCache.GetOrLoad(key, func() (any, error) { |
| 193 | + return h.dashboardService.GetUserUsageTrend(ctx, startTime, endTime, granularity, limit) |
| 194 | + }) |
| 195 | + if err != nil { |
| 196 | + return nil, hit, err |
| 197 | + } |
| 198 | + trend, err := snapshotPayloadAs[[]usagestats.UserUsageTrendPoint](entry.Payload) |
| 199 | + return trend, hit, err |
| 200 | +} |
0 commit comments