@@ -165,83 +165,83 @@ public async Task<DashboardResponse> GetDashboardDataAsync(
165165 var today = DateTime . Now . Date ;
166166 var now = DateTime . Now ;
167167
168- // 并行查询提高性能
169- var totalApiKeysTask = _context . ApiKeys
168+ // 顺序查询,避免EF Core并发问题
169+ var totalApiKeys = await _context . ApiKeys
170170 . Where ( x => x . UserId == currentUserId )
171171 . CountAsync ( cancellationToken ) ;
172- var activeApiKeysTask =
173- _context . ApiKeys
174- . Where ( x => x . UserId == currentUserId )
175- . CountAsync ( x => x . IsEnabled && ( x . ExpiresAt == null || x . ExpiresAt > DateTime . Now ) ,
176- cancellationToken ) ;
177-
178- int totalAccountsTask = 0 ;
179- int activeAccountsTask = 0 ;
180- int rateLimitedAccountsTask = 0 ;
172+ var activeApiKeys = await _context . ApiKeys
173+ . Where ( x => x . UserId == currentUserId )
174+ . CountAsync ( x => x . IsEnabled && ( x . ExpiresAt == null || x . ExpiresAt > DateTime . Now ) , cancellationToken ) ;
175+
176+ int totalAccounts = 0 ;
177+ int activeAccounts = 0 ;
178+ int rateLimitedAccounts = 0 ;
181179 if ( admin )
182180 {
183- totalAccountsTask = await _context . Accounts
181+ totalAccounts = await _context . Accounts
184182 . CountAsync ( cancellationToken ) ;
185- activeAccountsTask =
186- await _context . Accounts . CountAsync ( x => x . IsEnabled && x . Status == "active" , cancellationToken ) ;
187- rateLimitedAccountsTask =
188- await _context . Accounts . CountAsync ( x => x . Status == "rate_limited" , cancellationToken ) ;
183+ activeAccounts = await _context . Accounts
184+ . CountAsync ( x => x . IsEnabled && x . Status == "active" , cancellationToken ) ;
185+ rateLimitedAccounts = await _context . Accounts
186+ . CountAsync ( x => x . Status == "rate_limited" , cancellationToken ) ;
189187 }
190188
191- var todayRequestsTask =
192- _context . RequestLogs
193- . Where ( x=> x . UserId == currentUserId )
194- . Where ( x => x . RequestDate == today ) . LongCountAsync ( cancellationToken ) ;
195- var totalRequestsTask = _context . RequestLogs
196- . Where ( x=> x . UserId == currentUserId ) . LongCountAsync ( cancellationToken ) ;
197- var todayInputTokensTask = _context . RequestLogs
198- . Where ( x=> x . UserId == currentUserId ) . Where ( x => x . RequestDate == today )
189+ var todayRequests = await _context . RequestLogs
190+ . Where ( x => x . UserId == currentUserId )
191+ . Where ( x => x . RequestDate == today )
192+ . LongCountAsync ( cancellationToken ) ;
193+ var totalRequests = await _context . RequestLogs
194+ . Where ( x => x . UserId == currentUserId )
195+ . LongCountAsync ( cancellationToken ) ;
196+ var todayInputTokens = await _context . RequestLogs
197+ . Where ( x => x . UserId == currentUserId )
198+ . Where ( x => x . RequestDate == today )
199199 . SumAsync ( x => ( long ) x . InputTokens , cancellationToken ) ;
200- var todayOutputTokensTask = _context . RequestLogs
201- . Where ( x=> x . UserId == currentUserId ) . Where ( x => x . RequestDate == today )
200+ var todayOutputTokens = await _context . RequestLogs
201+ . Where ( x => x . UserId == currentUserId )
202+ . Where ( x => x . RequestDate == today )
202203 . SumAsync ( x => ( long ) x . OutputTokens , cancellationToken ) ;
203- var todayCacheCreateTokensTask = _context . RequestLogs
204- . Where ( x=> x . UserId == currentUserId ) . Where ( x => x . RequestDate == today )
204+ var todayCacheCreateTokens = await _context . RequestLogs
205+ . Where ( x => x . UserId == currentUserId )
206+ . Where ( x => x . RequestDate == today )
205207 . SumAsync ( x => ( long ) x . CacheCreateTokens , cancellationToken ) ;
206- var todayCacheReadTokensTask = _context . RequestLogs
207- . Where ( x=> x . UserId == currentUserId ) . Where ( x => x . RequestDate == today )
208+ var todayCacheReadTokens = await _context . RequestLogs
209+ . Where ( x => x . UserId == currentUserId )
210+ . Where ( x => x . RequestDate == today )
211+ . SumAsync ( x => ( long ) x . CacheReadTokens , cancellationToken ) ;
212+ var totalInputTokens = await _context . RequestLogs
213+ . Where ( x => x . UserId == currentUserId )
214+ . SumAsync ( x => ( long ) x . InputTokens , cancellationToken ) ;
215+ var totalOutputTokens = await _context . RequestLogs
216+ . Where ( x => x . UserId == currentUserId )
217+ . SumAsync ( x => ( long ) x . OutputTokens , cancellationToken ) ;
218+ var totalCacheCreateTokens = await _context . RequestLogs
219+ . Where ( x => x . UserId == currentUserId )
220+ . SumAsync ( x => ( long ) x . CacheCreateTokens , cancellationToken ) ;
221+ var totalCacheReadTokens = await _context . RequestLogs
222+ . Where ( x => x . UserId == currentUserId )
208223 . SumAsync ( x => ( long ) x . CacheReadTokens , cancellationToken ) ;
209- var totalInputTokensTask = _context . RequestLogs
210- . Where ( x=> x . UserId == currentUserId ) . SumAsync ( x => ( long ) x . InputTokens , cancellationToken ) ;
211- var totalOutputTokensTask = _context . RequestLogs
212- . Where ( x=> x . UserId == currentUserId ) . SumAsync ( x => ( long ) x . OutputTokens , cancellationToken ) ;
213- var totalCacheCreateTokensTask =
214- _context . RequestLogs
215- . Where ( x=> x . UserId == currentUserId ) . SumAsync ( x => ( long ) x . CacheCreateTokens , cancellationToken ) ;
216- var totalCacheReadTokensTask = _context . RequestLogs . SumAsync ( x => ( long ) x . CacheReadTokens , cancellationToken ) ;
217-
218- await Task . WhenAll (
219- totalApiKeysTask , activeApiKeysTask ,
220- todayRequestsTask , totalRequestsTask , todayInputTokensTask , todayOutputTokensTask ,
221- todayCacheCreateTokensTask , todayCacheReadTokensTask , totalInputTokensTask ,
222- totalOutputTokensTask , totalCacheCreateTokensTask , totalCacheReadTokensTask
223- ) ;
224224
225225 // 计算实时RPM和TPM
226226 var ( rpm , tpm , isHistorical ) = await CalculateRealtimeMetricsAsync ( currentUserId , 5 , cancellationToken ) ;
227227
228228 return new DashboardResponse
229229 {
230- TotalApiKeys = await totalApiKeysTask ,
231- ActiveApiKeys = await activeApiKeysTask ,
232- TotalAccounts = totalAccountsTask ,
233- ActiveAccounts = activeAccountsTask ,
234- RateLimitedAccounts = rateLimitedAccountsTask ,
235- TodayRequests = await todayRequestsTask ,
236- TotalRequests = await totalRequestsTask ,
237- TodayInputTokens = await todayInputTokensTask ,
238- TodayOutputTokens = await todayOutputTokensTask ,
239- TodayCacheCreateTokens = await todayCacheCreateTokensTask ,
240- TodayCacheReadTokens = await todayCacheReadTokensTask ,
241- TotalInputTokens = await totalInputTokensTask ,
242- TotalOutputTokens = await totalOutputTokensTask ,
243- TotalCacheCreateTokens = await totalCacheCreateTokensTask ,
244- TotalCacheReadTokens = await totalCacheReadTokensTask ,
230+ TotalApiKeys = totalApiKeys ,
231+ ActiveApiKeys = activeApiKeys ,
232+ TotalAccounts = totalAccounts ,
233+ ActiveAccounts = activeAccounts ,
234+ RateLimitedAccounts = rateLimitedAccounts ,
235+ TodayRequests = todayRequests ,
236+ TotalRequests = totalRequests ,
237+ TodayInputTokens = todayInputTokens ,
238+ TodayOutputTokens = todayOutputTokens ,
239+ TodayCacheCreateTokens = todayCacheCreateTokens ,
240+ TodayCacheReadTokens = todayCacheReadTokens ,
241+ TotalInputTokens = totalInputTokens ,
242+ TotalOutputTokens = totalOutputTokens ,
243+ TotalCacheCreateTokens = totalCacheCreateTokens ,
244+ TotalCacheReadTokens = totalCacheReadTokens ,
245245 RealtimeRPM = rpm ,
246246 RealtimeTPM = tpm ,
247247 MetricsWindow = 5 ,
0 commit comments