Skip to content

Commit bcc33e2

Browse files
Fix stats key mismatch and optimize to single query
Co-authored-by: delicatacurtis <247246500+delicatacurtis@users.noreply.github.com>
1 parent 1a5a6ea commit bcc33e2

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

app/Livewire/DocumentTranscriptionComponent.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,12 @@ private function getStats(): array
185185

186186
if (!$teamId) {
187187
return [
188-
'total' => 0,
189-
'completed' => 0,
190-
'pending' => 0,
188+
'total_transcriptions' => 0,
189+
'completed_transcriptions' => 0,
190+
'pending_transcriptions' => 0,
191+
'failed_transcriptions' => 0,
192+
'total_corrections' => 0,
193+
'avg_confidence' => 0,
191194
];
192195
}
193196

app/Services/HandwritingRecognitionService.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)