Spaces:
Configuration error

File size: 3,113 Bytes
023064b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
import { Request, Response } from 'express';
import { pipelineAppService } from '../../application/pipeline-app.service';
import { setupService } from '../../application/setup.service';
import { mediaAppService } from '../../application/media-app.service';
import { jobStore } from '../../infrastructure/persistence/job-store';
import { EDITABLE_ENV_KEYS } from '../../constants/config';
import { isLocalRequest } from '../../middleware/local-only';
import { toEditableEnvUpdates } from './api-helpers';
import { bundledStatus } from '../../agentic/media/bundled-media';
import { execSync } from 'child_process';
import * as fs from 'fs';
import * as os from 'os';

export const healthCheck = (req: Request, res: Response) => {
    const health = pipelineAppService.getDiagnostics();
    const includeDetails = isLocalRequest(req) || process.env.EXPOSE_HEALTH_DETAILS === '1';
    const publishedVideos = mediaAppService.listPublishedVideos(req);

    // Production health check: include system metrics
    const bundled = bundledStatus();
    let diskFreeGB = 0;
    try {
        if (process.platform === 'win32') {
            const out = execSync('powershell -Command "(Get-PSDrive -Name C).Free / 1GB"', { encoding: 'utf8' });
            diskFreeGB = Math.round(parseFloat(out.trim()) * 10) / 10;
        } else {
            const out = execSync('df -BG . | tail -1 | awk \'{print $4}\'', { encoding: 'utf8' });
            diskFreeGB = parseInt(out.trim().replace('G', ''), 10);
        }
    } catch { /* ignore */ }

    let ffmpegVersion = 'unknown';
    try {
        const v = execSync('ffmpeg -version 2>&1 | head -1', { encoding: 'utf8' });
        ffmpegVersion = v.trim().split(' ')[2] || 'unknown';
    } catch { /* ignore */ }

    res.json({
        status: health.overall,
        service: 'video-generator',
        version: process.env.npm_package_version || '5.0.0',
        publishedVideos: publishedVideos.length,
        jobsTracked: jobStore.all().length,
        system: {
            platform: process.platform,
            arch: process.arch,
            nodeVersion: process.version,
            uptimeSec: Math.round(process.uptime()),
            memoryUsedMB: Math.round(process.memoryUsage().heapUsed / 1024 / 1024),
            memoryTotalMB: Math.round(os.totalmem() / 1024 / 1024),
            diskFreeGB,
            ffmpegVersion,
        },
        offline: {
            available: bundled.images > 0 || bundled.videos > 0,
            bundledImages: bundled.images,
            bundledVideos: bundled.videos,
            bundledMusic: bundled.music,
        },
        ...(includeDetails ? { dependencies: health.checks, environment: health.environment } : {}),
    });
};

export const getStatus = (_req: Request, res: Response) => {
    res.json({ success: true, data: pipelineAppService.getSetupStatus() });
};

export const updateEnv = (req: Request, res: Response) => {
    const updated = setupService.updateEnvValues(
        toEditableEnvUpdates(req.body as Record<string, unknown>, EDITABLE_ENV_KEYS),
    );
    res.json({ success: true, data: updated });
};