analytics-debug.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Endpoint de diagnostic analytics — à supprimer en production.
  5. * GET /api/analytics-debug.php
  6. */
  7. use AuditExpress\Analytics;
  8. use AuditExpress\Http;
  9. ['root' => $root] = require __DIR__ . '/_bootstrap.php';
  10. $storageDir = $root . '/storage';
  11. $file = $storageDir . '/analytics.json';
  12. $checks = [
  13. 'root_exists' => is_dir($root),
  14. 'storage_dir_exists' => is_dir($storageDir),
  15. 'storage_writable' => is_writable($storageDir),
  16. 'file_exists' => is_file($file),
  17. 'file_writable' => is_file($file) ? is_writable($file) : (is_dir($storageDir) && is_writable($storageDir)),
  18. ];
  19. // Tenter un track test
  20. $before = null;
  21. $after = null;
  22. $trackOk = false;
  23. $trackError = null;
  24. try {
  25. $analytics = new Analytics($root);
  26. $before = $analytics->getStats();
  27. $analytics->track(Analytics::EVENT_DIA_INIT, '_debug_test');
  28. $after = $analytics->getStats();
  29. $trackOk = ($after['total'][Analytics::EVENT_DIA_INIT] ?? 0) > ($before['total'][Analytics::EVENT_DIA_INIT] ?? 0);
  30. } catch (Throwable $e) {
  31. $trackError = $e->getMessage();
  32. }
  33. Http::json([
  34. 'diagnostic' => [
  35. 'root' => $root,
  36. 'storage_dir' => $storageDir,
  37. 'file' => $file,
  38. 'checks' => $checks,
  39. 'track_test_ok' => $trackOk,
  40. 'track_error' => $trackError,
  41. 'before_dia_init' => $before['total'][Analytics::EVENT_DIA_INIT] ?? null,
  42. 'after_dia_init' => $after['total'][Analytics::EVENT_DIA_INIT] ?? null,
  43. ],
  44. 'last_updated' => $after['last_updated'] ?? null,
  45. ]);