ClusterDataTable.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /* * ********************************************************************
  3. * WordPress Manager product developed. (Feb 5, 2018)
  4. * *
  5. *
  6. * CREATED BY MODULESGARDEN -> http://modulesgarden.com
  7. * CONTACT -> contact@modulesgarden.com
  8. *
  9. *
  10. * This software is furnished under a license and may be used and copied
  11. * only in accordance with the terms of such license and with the
  12. * inclusion of the above copyright notice. This software or any other
  13. * copies thereof may not be provided or otherwise made available to any
  14. * other person. No title to and ownership of the software is hereby
  15. * transferred.
  16. *
  17. *
  18. * ******************************************************************** */
  19. namespace ModulesGarden\ProxmoxAddon\App\UI\Cluster\Pages;
  20. use MGProvision\Proxmox\v2 as proxmox;
  21. use ModulesGarden\ProxmoxAddon as main;
  22. use ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\AdminArea;
  23. use ModulesGarden\ProxmoxAddon\Core\UI\Widget\DataTable\Column;
  24. use ModulesGarden\ProxmoxAddon\Core\UI\Widget\RawDataTable\RawDataTable;
  25. use \ModulesGarden\ProxmoxAddon\App\UI\Cluster\Buttons\UpdateButton;
  26. /**
  27. * Description of PluginInstalled
  28. *
  29. * @author Pawel Kopec <pawelk@modulesgardne.com>
  30. */
  31. class ClusterDataTable extends RawDataTable implements AdminArea
  32. {
  33. use main\App\Services\BaseService;
  34. protected $id = 'cluster';
  35. protected $name = 'clusterName';
  36. protected $title = 'clusterTitle';
  37. public function isRawTitle()
  38. {
  39. return false;
  40. }
  41. public function initContent()
  42. {
  43. //Update
  44. $this->addActionButton(new UpdateButton);
  45. $rd = new main\Core\UI\Widget\Buttons\ButtonRedirect();
  46. $rd->setRawUrl('addonmodules.php?module=proxmoxAddon&mg-page=home&mg-action=nodeDetail&serverId=' . $this->getRequestValue('id'))
  47. ->setIcon('lu-btn__icon lu-zmdi lu-zmdi-info-outline')
  48. ->setRedirectParams(['id' => ':id']);
  49. $this->addActionButton($rd);
  50. try
  51. {
  52. $this->setServerId($this->getRequestValue('id'))->getApi();
  53. }
  54. catch (\Exception $ex)
  55. {
  56. $this->setInternalAlertMessage($ex->getMessage())
  57. ->setInternalAlertMessageType('danger');
  58. }
  59. }
  60. public function replaceFieldUptime($key, $row)
  61. {
  62. return main\App\Libs\Format::uptime($row['status']['uptime']);
  63. }
  64. public function replaceFieldCpuUsage($key, $row)
  65. {
  66. $cpu = $row['status']['cpu'] ? round($row['status']['cpu'] * 100, 2) . " %" : "0 %";
  67. return $cpu . ' of ' . $row['status']['cpuinfo']['cpus'] . ' CPU(s)';
  68. }
  69. public function replaceFieldSwap($key, $row)
  70. {
  71. return main\App\Libs\Format::convertBytes($row['status']['swap']['used']) . ' / ' . main\App\Libs\Format::convertBytes($row['status']['swap']['total']);
  72. }
  73. public function replaceFieldMemory($key, $row)
  74. {
  75. return main\App\Libs\Format::convertBytes($row['status']['memory']['used']) . ' / ' . main\App\Libs\Format::convertBytes($row['status']['memory']['total']);
  76. }
  77. public function replaceFieldRootfs($key, $row)
  78. {
  79. return main\App\Libs\Format::convertBytes($row['status']['rootfs']['used']) . ' / ' . main\App\Libs\Format::convertBytes($row['status']['rootfs']['total']);
  80. }
  81. protected function loadHtml()
  82. {
  83. $this->addColumn((new Column('node'))->setSearchable(true, Column::TYPE_STRING)->setOrderable('ASC'))
  84. ->addColumn((new Column('uptime'))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
  85. ->addColumn((new Column('vmsLimit'))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
  86. ->addColumn((new Column('cpuLimit'))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
  87. ->addColumn((new Column('diskLimit'))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
  88. ->addColumn((new Column('ramLimit'))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
  89. ->addColumn((new Column('cpuUsage'))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
  90. ->addColumn((new Column('swap'))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
  91. ->addColumn((new Column('memory'))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
  92. ->addColumn((new Column('rootfs'))->setSearchable(true, Column::TYPE_STRING)->setOrderable());
  93. }
  94. protected function loadData()
  95. {
  96. $this->loadRequestObj()->setServerId($this->getRequestValue('id'))->getApi()->setInstance();
  97. $dataProv = new main\Core\UI\Widget\DataTable\DataProviders\Providers\ArrayDataProvider();
  98. $nodeRepository = new proxmox\repository\NodeRepository();
  99. $data = [];
  100. $loadBalancer = new main\App\Services\LoadBalancerService($this->getRequestValue('id'));
  101. $loadBalancer->setApi($this->getApi());
  102. foreach ($nodeRepository->fetch() as $node)
  103. {
  104. $loadBalancer->findByNode($node->getNode(),false);
  105. $resurces = $loadBalancer->getNodesResurces()[0];
  106. /* @var $node proxmox\models\Node */
  107. $data[] = [
  108. "id" => $node->getNode(),
  109. "node" => $node->getNode(),
  110. "status" => $node->getStatus(),
  111. "vmsLimit" => sprintf("%s / %s" ,(int)$resurces['vmsUsed'] , $resurces['vmsMax'] ? $resurces['vmsMax'] : '-' ) ,
  112. "cpuLimit" => sprintf("%s / %s" ,(int)$resurces['cpuUsed'] , $resurces['cpuMax'] ? $resurces['cpuMax'] : '-' ) ,
  113. "diskLimit" => sprintf("%s / %s" ,main\App\Libs\Format::convertBytes((int)$resurces['diskUsed'] ), main\App\Libs\Format::convertBytes($resurces['diskMax'] ) ) ,
  114. "ramLimit" => sprintf("%s / %s" ,main\App\Libs\Format::convertBytes((int)$resurces['ramUsed'] ), main\App\Libs\Format::convertBytes($resurces['ramMax'] )) ,
  115. ];
  116. }
  117. $dataProv->setDefaultSorting("node", 'ASC');
  118. $dataProv->setData((array) $data);
  119. $this->setDataProvider($dataProv);
  120. }
  121. }