L2TopologyView.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. // The single collection interval is also the history snapshot interval.
  42. $history_interval = max(5, (int) ($cfg['full_interval_min'] ?? $cfg['history_interval_min'] ?? 5));
  43. $history_limit = (int) ceil($history_days * 1440 / $history_interval) + 30;
  44. $history_limit = max(500, min(10000, $history_limit));
  45. $this->setResponse(new CControllerResponseData([
  46. 'title' => _('L2 Topology'),
  47. 'snap_json' => ($json !== null && $json !== '') ? $json : 'null',
  48. 'cfg_json' => json_encode($cfg, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
  49. 'history' => L2Store::listHistory($history_limit),
  50. 'used_ts' => $used_ts,
  51. 'requested_ts' => $ts,
  52. 'is_live' => ($ts === null),
  53. 'cfg_refresh'=> (int) $cfg['refresh_seconds']
  54. ]));
  55. }
  56. }