ServerLoadGraph.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /* * ********************************************************************
  3. * ProxmoxAddon product developed. (Oct 1, 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\NodeDetail\Pages;
  20. use ModulesGarden\ProxmoxAddon as main;
  21. use ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\AdminArea;
  22. use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Forms\Fields\Select;
  23. use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Graphs\Line;
  24. use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Graphs\Models\DataSet;
  25. class ServerLoadGraph extends Line implements AdminArea
  26. {
  27. use LoadAPIData;
  28. protected $id = 'serverLoadGraph';
  29. protected $name = 'serverLoadGraph';
  30. protected $graphSettingsEnabled = true;
  31. protected $graphSettingsKey = 'cpuGraphSettings';
  32. public function initContent()
  33. {
  34. $selectScope = new Select('timeframe');
  35. $selectScope->setAvailableValues([
  36. 'hour' => 'Hour',
  37. 'day' => 'Day',
  38. 'week' => 'Week',
  39. 'year' => 'Year',
  40. ]);
  41. $selectScope->setDefaultValue('week');
  42. $this->addSettingField($selectScope);
  43. //Tooltip
  44. $this->updateChartOption('tooltips', [
  45. 'callbacks' => [
  46. 'label' => 'mgTooltipServerLoad'
  47. ]
  48. ]);
  49. }
  50. public function prepareAjaxData()
  51. {
  52. if ($this->configChartsSettings->timeframe)
  53. {
  54. $this->timeframe = $this->configChartsSettings->timeframe;
  55. }
  56. $rrdata = main\Core\Helper\DatabaseCache::loadData(
  57. $this->graphSettingsKey .$this->timeframe. '_cacheData', [$this, 'loadApiData'], 30, true);
  58. $labels = [];
  59. $dataSets = [
  60. 'loadavg' => []
  61. ];
  62. $dateFormat = in_array($this->timeframe, ['hour', 'day']) ? "H:i:s" : "Y-m-d";
  63. foreach ($rrdata as $rrd)
  64. {
  65. $labels[] = date($dateFormat, $rrd['time']);
  66. $dataSets['loadavg'][] = (float)$rrd['loadavg'];
  67. }
  68. //Labels
  69. $this->setLabels($labels);
  70. //Server Load
  71. $lang = main\Core\ServiceLocator::call('lang');
  72. $dataSet = new DataSet();
  73. $dataSet->setTitle($lang->absoluteT('Server Load'))
  74. ->setData($dataSets['loadavg'])
  75. ->setConfigurationDataSet([
  76. "backgroundColor" => 'rgba(174, 198, 57, 0.79)',
  77. "borderColor" => 'rgba(174, 198, 57, 1)',
  78. ]);
  79. $this->addDataSet($dataSet);
  80. }
  81. }