File size: 2,359 Bytes
88b6846
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { NextResponse } from 'next/server';
import { promises as fs } from 'fs';
import path from 'path';
import { getMetadataPath, ensureDir, getDataDir } from '@/lib/dataPath';
import { getCleanupConfig } from '@/lib/cleanup';

export async function GET() {
    try {
        const metadataDir = getMetadataPath();
        await ensureDir(metadataDir);
        const metadataPath = path.join(metadataDir, 'dataset_info.json');

        let metadata: Record<string, unknown> = { speakers: {}, last_updated: null };
        try {
            const content = await fs.readFile(metadataPath, 'utf-8');
            metadata = JSON.parse(content);
        } catch {
            // File might not exist yet
        }

        // Flatten statistics
        let recordedSentences = 0;
        const speakers = metadata.speakers as Record<string, { datasets?: Record<string, { recordings?: number }> }>;

        for (const speakerId in speakers) {
            const speaker = speakers[speakerId];
            if (speaker?.datasets) {
                for (const datasetName in speaker.datasets) {
                    const dataset = speaker.datasets[datasetName];
                    recordedSentences += dataset?.recordings || 0;
                }
            }
        }

        // Get cleanup configuration for display
        const cleanupConfig = getCleanupConfig();

        return NextResponse.json({
            lastUpdated: metadata.last_updated || null,
            total_recordings: Number(metadata.total_recordings) || 0,
            total_duration: Number(metadata.total_duration) || 0,
            recent_recordings: Array.isArray(metadata.recent_recordings) ? metadata.recent_recordings : [],
            speakers: Object.keys(speakers).length,
            recordedSentences,
            // Include storage info for transparency
            storage: {
                dataDirectory: getDataDir(),
                autoCleanupHours: cleanupConfig.maxFileAgeHours,
                isHuggingFaceSpaces: getDataDir() === '/data'
            },
            details: metadata
        });
    } catch (error) {
        console.error('Error getting stats:', error);
        return NextResponse.json({
            error: 'Internal Server Error',
            details: error instanceof Error ? error.message : 'Unknown error'
        }, { status: 500 });
    }
}