CpuGraph.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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\Servers\ProxmoxVps\App\UI\Graph\Pages;
  20. use ModulesGarden\ProxmoxAddon\Core\Helper\DatabaseCache;
  21. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\AdminArea;
  22. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\ClientArea;
  23. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Forms\Fields\Select;
  24. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Graphs\Line;
  25. use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\Graphs\Models\DataSet;
  26. use function ModulesGarden\Servers\ProxmoxVps\Core\Helper\sl;
  27. class CpuGraph extends Line implements ClientArea, AdminArea
  28. {
  29. use GraphData;
  30. protected $id = 'cpuGraph';
  31. protected $name = 'cpuGraph';
  32. protected $graphSettingsEnabled = true;
  33. protected $graphSettingsKey = 'cpuGraphSettings';
  34. public function initContent()
  35. {
  36. $selectScope = new Select('timeframe');
  37. $selectScope->setAvailableValues($this->chartOptions());
  38. $selectScope->setDefaultValue('week');
  39. $this->addSettingField($selectScope);
  40. //yAxes
  41. $this->updateChartScale('yAxes', [
  42. [
  43. 'scaleLabel' => [
  44. 'display' => true,
  45. 'labelString' => sl('lang')->tr("Percentage (%)")
  46. ],
  47. 'ticks' => [
  48. 'beginAtZero' => true
  49. ],
  50. ]]);
  51. //Tooltip
  52. $this->updateChartOption('tooltips', [
  53. 'callbacks' => [
  54. 'label' => 'mgTooltipCpu'
  55. ]
  56. ]);
  57. }
  58. public function prepareAjaxData()
  59. {
  60. if ($this->configChartsSettings->timeframe)
  61. {
  62. $this->timeframe = $this->configChartsSettings->timeframe;
  63. }
  64. $rrdata = DatabaseCache::loadData(
  65. $this->graphSettingsKey.$this->timeframe .$this->getWhmcsParamByKey('serviceid'). '_cacheData', [$this, 'chartData'], 30, true);
  66. $labels = [];
  67. $dataSets = [
  68. 'cpu' => []
  69. ];
  70. $dateFormat = in_array($this->timeframe, ['hour', 'day']) ? "H:i:s" : "Y-m-d";
  71. foreach ($rrdata as $rrd)
  72. {
  73. $labels[] = date($dateFormat, $rrd['time']);
  74. $dataSets['cpu'][] = (isset($rrd['cpu']) ? (float)$rrd['cpu'] : 0) * 100;
  75. }
  76. //Labels
  77. $this->setLabels($labels);
  78. //CPU Usage
  79. $lang = sl('lang');
  80. $dataSet = new DataSet();
  81. $dataSet->setTitle($lang->tr('CPU Usage'))
  82. ->setData($dataSets['cpu'])
  83. ->setConfigurationDataSet([
  84. "backgroundColor" => "rgba(174, 198, 57, 0.79)",
  85. "borderColor" => "rgba(174, 198, 57, 1)"
  86. ]);
  87. $this->addDataSet($dataSet);
  88. }
  89. }