@@ -235,32 +235,29 @@ protected function learnFromCorrection(TranscriptionCorrection $correction): voi
235235 */
236236 public function getTeamStats (int $ teamId ): array
237237 {
238- // Get status counts in a single query
239- $ statusCounts = DocumentTranscription::where ('team_id ' , $ teamId )
240- ->selectRaw ('status, COUNT(*) as count ' )
241- ->groupBy ('status ' )
242- ->pluck ('count ' , 'status ' )
243- ->toArray ();
244-
245- $ totalTranscriptions = array_sum ($ statusCounts );
246-
247- // Get average confidence for completed transcriptions
248- $ avgConfidence = DocumentTranscription::where ('team_id ' , $ teamId )
249- ->where ('status ' , 'completed ' )
250- ->avg ('metadata->confidence ' ) ?? 0 ;
238+ // Get all statistics in a single optimized query
239+ $ stats = DocumentTranscription::where ('team_id ' , $ teamId )
240+ ->selectRaw ('
241+ COUNT(*) as total,
242+ SUM(CASE WHEN status = "completed" THEN 1 ELSE 0 END) as completed,
243+ SUM(CASE WHEN status = "pending" THEN 1 ELSE 0 END) as pending,
244+ SUM(CASE WHEN status = "failed" THEN 1 ELSE 0 END) as failed,
245+ AVG(CASE WHEN status = "completed" THEN JSON_EXTRACT(metadata, "$.confidence") ELSE NULL END) as avg_confidence
246+ ' )
247+ ->first ();
251248
252249 // Get total corrections count
253250 $ totalCorrections = TranscriptionCorrection::whereHas ('documentTranscription ' , function ($ query ) use ($ teamId ) {
254251 $ query ->where ('team_id ' , $ teamId );
255252 })->count ();
256253
257254 return [
258- 'total_transcriptions ' => $ totalTranscriptions ,
259- 'completed_transcriptions ' => $ statusCounts [ ' completed ' ] ?? 0 ,
260- 'pending_transcriptions ' => $ statusCounts [ ' pending ' ] ?? 0 ,
261- 'failed_transcriptions ' => $ statusCounts [ ' failed ' ] ?? 0 ,
255+ 'total_transcriptions ' => ( int ) ( $ stats -> total ?? 0 ) ,
256+ 'completed_transcriptions ' => ( int ) ( $ stats -> completed ?? 0 ) ,
257+ 'pending_transcriptions ' => ( int ) ( $ stats -> pending ?? 0 ) ,
258+ 'failed_transcriptions ' => ( int ) ( $ stats -> failed ?? 0 ) ,
262259 'total_corrections ' => $ totalCorrections ,
263- 'avg_confidence ' => round ($ avgConfidence , 2 ),
260+ 'avg_confidence ' => round (( float ) ( $ stats -> avg_confidence ?? 0 ) , 2 ),
264261 ];
265262 }
266263}
0 commit comments