subDays(30); // 1. Average transcription duration (last 30 days, valid start & finish, min 120s) $transcriptionDurationAverage = Transcription::whereNotNull('transcription_start') ->whereNotNull('transcription_finish') ->where('transcription_start', '>=', $thirtyDaysAgo) ->whereRaw('TIMESTAMPDIFF(SECOND, transcription_start, transcription_finish) >= 120') ->whereRaw('transcription_start > "2025-03-26 23:59:59"') ->selectRaw('AVG(TIMESTAMPDIFF(SECOND, transcription_start, transcription_finish)) as avg_duration') ->value('avg_duration'); // 2. Average VOD processing time (last 30 days, downloaded & processed = true) $vodProcessingAverage = Video::where('data_downloaded', true) ->where('processed', true) ->where('created_at', '>=', $thirtyDaysAgo) ->whereNotNull('created_at') ->whereNotNull('updated_at') ->whereRaw('updated_at > "2025-03-31 23:59:59"') ->selectRaw('AVG(TIMESTAMPDIFF(SECOND, created_at, updated_at)) as avg_processing') ->value('avg_processing'); $averageExternalDate = DB::table('videos') ->where('data_downloaded', false) ->whereNotNull('external_date') ->selectRaw('AVG(UNIX_TIMESTAMP(external_date)) as avg_timestamp') ->value('avg_timestamp'); $now = Carbon::now()->timestamp; $vodDelayNow = $averageExternalDate ? ($now - $averageExternalDate) : null; // 3. Average clip count per day (only clips from the last 30 days) $clipsPerDay = Clip::where('created_at', '>=', $thirtyDaysAgo) ->selectRaw('DATE(created_at) as date, COUNT(*) as clip_count') ->whereRaw('created_at > "2025-03-28 23:59:59"') ->groupBy(DB::raw('DATE(created_at)')) ->pluck('clip_count'); $averageClipsPerDay = $clipsPerDay->avg(); // 4. Count of videos: downloaded = true & false $videoDownloadStats = Video::selectRaw(" SUM(CASE WHEN data_downloaded = 1 THEN 1 ELSE 0 END) as downloaded_true, SUM(CASE WHEN data_downloaded = 0 THEN 1 ELSE 0 END) as downloaded_false ")->first(); // 5. Count of videos with processed = true $processedVideoCount = Video::where('processed', true)->count(); // Return all the stats return response()->json([ 'transcription_duration_average' => round($transcriptionDurationAverage, 2), 'vod_processing_average' => round($vodDelayNow, 2), 'clip_average_per_day' => round($averageClipsPerDay, 0), 'video_downloaded_true' => $videoDownloadStats->downloaded_true, 'video_downloaded_false' => $videoDownloadStats->downloaded_false, 'processed_video_count' => $processedVideoCount, ]); } } ?>