CpuGraph.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 CpuGraph extends Line implements AdminArea
  26. {
  27. use LoadAPIData;
  28. protected $id = 'cpuGraph';
  29. protected $name = 'cpuGraph';
  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. //yAxes
  44. $this->updateChartScale('yAxes', [
  45. [
  46. 'scaleLabel' => [
  47. 'display' => true,
  48. 'labelString' => '%'
  49. ],
  50. 'ticks' => [
  51. 'beginAtZero' => true
  52. ],
  53. ]]);
  54. //Tooltip
  55. $this->updateChartOption('tooltips', [
  56. 'callbacks' => [
  57. 'label' => 'mgTooltipCpu'
  58. ]
  59. ]);
  60. }
  61. public function prepareAjaxData()
  62. {
  63. if ($this->configChartsSettings->timeframe)
  64. {
  65. $this->timeframe = $this->configChartsSettings->timeframe;
  66. }
  67. $rrdata = main\Core\Helper\DatabaseCache::loadData(
  68. $this->graphSettingsKey .$this->timeframe. '_cacheData', [$this, 'loadApiData'], 30, true);
  69. $labels = [];
  70. $dataSets = [
  71. 'cpu' => [],
  72. 'iowait' => [],
  73. ];
  74. $dateFormat = in_array($this->timeframe, ['hour', 'day']) ? "H:i:s" : "Y-m-d";
  75. foreach ($rrdata as $rrd)
  76. {
  77. $labels[] = date($dateFormat, $rrd['time']);
  78. $dataSets['cpu'][] = (float)$rrd['cpu'] * 100;
  79. $dataSets['iowait'][] = (float)$rrd['iowait'] * 100;
  80. }
  81. //Labels
  82. $this->setLabels($labels);
  83. //CPU Usage
  84. $lang = main\Core\ServiceLocator::call('lang');
  85. $dataSet = new DataSet();
  86. $dataSet->setTitle($lang->absoluteT('CPU Usage'))
  87. ->setData($dataSets['cpu'])
  88. ->setConfigurationDataSet([
  89. "backgroundColor" => "rgba(174, 198, 57, 0.79)",
  90. "borderColor" => "rgba(174, 198, 57, 1)"
  91. ]);
  92. $this->addDataSet($dataSet);
  93. //IO Delay
  94. $dataSet = new DataSet();
  95. $dataSet->setTitle($lang->absoluteT('IO Delay'))
  96. ->setData($dataSets['iowait'])
  97. ->setConfigurationDataSet([
  98. "backgroundColor" => 'rgba(39, 133, 134, 0.91)',
  99. "borderColor" => 'rgba(39, 133, 134, 1)',
  100. ]);
  101. $this->addDataSet($dataSet);
  102. }
  103. }