L2TopologyView.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php declare(strict_types = 0);
  2. /*
  3. ** L2 Topology - GET controller.
  4. ** Renders either the live snapshot or any stored snapshot chosen by ?ts=<epoch>.
  5. */
  6. namespace Modules\WecomCenter\Actions;
  7. use CController;
  8. use CControllerResponseData;
  9. use CWebUser;
  10. use Modules\WecomCenter\Lib\L2Store;
  11. class L2TopologyView extends CController {
  12. protected function init() {
  13. $this->disableCsrfValidation();
  14. }
  15. protected function checkInput() {
  16. return true;
  17. }
  18. protected function checkPermissions() {
  19. return CWebUser::getType() >= USER_TYPE_ZABBIX_USER;
  20. }
  21. protected function doAction() {
  22. L2Store::ensureSchema();
  23. $cfg = L2Store::getConfig();
  24. $ts_raw = $_REQUEST['ts'] ?? '';
  25. $ts = ($ts_raw === '' || $ts_raw === null) ? null : (int) $ts_raw;
  26. $requested_ts = $ts;
  27. $snap = L2Store::snapshotAt($ts);
  28. $json = $snap['json'] ?? null;
  29. $used_ts = $snap['ts'];
  30. // A stale bookmark / hand-edited ?ts= must not leave an empty page.
  31. // If that exact snapshot is gone, fall back to the live latest view.
  32. if ($ts !== null && ($json === null || $json === '')) {
  33. $ts = null;
  34. $snap = L2Store::snapshotAt(null);
  35. $json = $snap['json'] ?? null;
  36. $used_ts = $snap['ts'];
  37. }
  38. // The selector must cover the full retention window, not only the
  39. // latest 500 rows. At 5 minutes / 7 days this is ~2,016 points.
  40. $history_days = max(1, min(30, (int) ($cfg['history_days'] ?? 7)));
  41. $history_interval = max(5, (int) ($cfg['history_interval_min'] ?? 5));
  42. $history_limit = (int) ceil($history_days * 1440 / $history_interval) + 30;
  43. $history_limit = max(500, min(10000, $history_limit));
  44. $this->setResponse(new CControllerResponseData([
  45. 'title' => _('L2 Topology'),
  46. 'snap_json' => ($json !== null && $json !== '') ? $json : 'null',
  47. 'cfg_json' => json_encode($cfg, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
  48. 'history' => L2Store::listHistory($history_limit),
  49. 'used_ts' => $used_ts,
  50. 'requested_ts' => $ts,
  51. 'is_live' => ($ts === null),
  52. 'cfg_refresh'=> (int) $cfg['refresh_seconds']
  53. ]));
  54. }
  55. }