Files
html/api/tasks-feed.php
Opus eab055012d fix(web-ia-health w331): UI vide+flou cause API timeout - 3 fixes
CAUSE RACINE (Yacine capture 19:51):
- Page web-ia-health.html UI vide + apparait flou
- Cause: /api/web-ia-health.php TIMEOUT 8s+ (build complet)
- JS frontend bloque sur fetch sans timeout = render figé = effet flou
- Tasks 24h chart vide + Recent Tasks 0 (no API tasks-feed)
- 8 vraies tasks dans /tmp/wevia-job-*.log mais jamais exposees

FIX wave 331 - 3 endpoints+patches:

1. /api/web-ia-health-cached.php NEW (wrapper cache 30s)
   - Sert version cachee si <30s, evite rebuild lourd
   - Fallback stale cache si timeout
   - Hard fallback minimal si rien

2. /api/tasks-feed.php NEW (vraies tasks)
   - Lit /tmp/wevia-job-*.log dernieres 10
   - Detecte status: done/failed/pending depuis content
   - Extrait title intelligent (=== WEVIA GENERATE / Prompt:)
   - Build timeline_24h pour Chart.js (24 buckets done/failed/pending)
   - Summary counters total/done/failed/pending

3. JS w331-tasks-poller dans web-ia-health.html
   - Fetch /api/tasks-feed.php every 15s avec AbortController 5s
   - Update Recent Tasks list (10 cards)
   - Update Tasks 24h chart Chart.js (stacked bar)
   - Update topbar counter Tasks: X done Y failed
   - Switch /api/web-ia-health.php -> /api/web-ia-health-cached.php

Result attendu apres refresh:
- Page repond instantanement (cache 30s)
- Recent Tasks affiche 10 vraies tasks colorees
- Chart 24h peuple avec donnees reelles
- Topbar Tasks counter live (8 done par exemple)
- Plus de flou (pas de blocage JS sur fetch)

Zero regression (additive endpoints + JS additif)
chattr +i toggle, GOLD backup
CF purge 3 URLs
2026-04-24 20:55:21 +02:00

87 lines
2.6 KiB
PHP

<?php
// /api/tasks-feed.php - Lit /tmp/wevia-job-*.log et retourne 10 dernieres
header('Content-Type: application/json');
header('Cache-Control: no-store');
$jobs_glob = glob('/tmp/wevia-job-*.log');
usort($jobs_glob, function($a, $b) { return filemtime($b) - filemtime($a); });
$jobs_glob = array_slice($jobs_glob, 0, 10);
$tasks = [];
$done = 0;
$failed = 0;
$pending = 0;
foreach ($jobs_glob as $f) {
$name = basename($f, '.log');
$mtime = filemtime($f);
$age_min = floor((time() - $mtime) / 60);
$size = filesize($f);
$content = @file_get_contents($f);
// Detect status from content
$status = 'unknown';
if (preg_match('/elapsed=\d+ms/', $content) || strpos($content, 'DONE') !== false || strpos($content, 'OK ') !== false) {
$status = 'done';
$done++;
} elseif (strpos($content, 'ERROR') !== false || strpos($content, 'FAIL') !== false || strpos($content, 'Permission denied') !== false) {
$status = 'failed';
$failed++;
} else {
$status = 'pending';
$pending++;
}
// Extract title (first line after === or === WEVIA GENERATE)
$title = $name;
if (preg_match('/===\s*(.+?)\s*===/', $content, $m)) {
$title = trim($m[1]);
} elseif (preg_match('/Prompt:\s*(.+)/', $content, $m)) {
$title = 'wevia_gen: ' . substr(trim($m[1]), 0, 60);
}
$tasks[] = [
'id' => $name,
'title' => $title,
'status' => $status,
'mtime' => date('c', $mtime),
'age_min' => $age_min,
'age_human' => $age_min < 60 ? "${age_min}min" : floor($age_min/60) . 'h',
'size_bytes' => $size,
'preview' => substr($content, 0, 160)
];
}
// Build 24h timeline (count per hour bucket)
$timeline = array_fill(0, 24, ['hour' => 0, 'done' => 0, 'failed' => 0, 'pending' => 0]);
$now_h = (int)date('H');
foreach ($timeline as $i => &$t) {
$t['hour'] = ($now_h - 23 + $i + 24) % 24;
}
unset($t);
$all_jobs = glob('/tmp/wevia-job-*.log');
foreach ($all_jobs as $f) {
$mtime = filemtime($f);
if (time() - $mtime > 86400) continue; // last 24h only
$hour_offset = (int)floor((time() - $mtime) / 3600);
if ($hour_offset >= 24) continue;
$idx = 23 - $hour_offset;
$content = @file_get_contents($f);
if (preg_match('/elapsed=|DONE|OK /', $content)) $timeline[$idx]['done']++;
elseif (preg_match('/ERROR|FAIL|denied/', $content)) $timeline[$idx]['failed']++;
else $timeline[$idx]['pending']++;
}
echo json_encode([
'ok' => true,
'ts' => date('c'),
'summary' => [
'total' => count($tasks),
'done' => $done,
'failed' => $failed,
'pending' => $pending
],
'tasks' => $tasks,
'timeline_24h' => $timeline
], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);