CpuGraph.php 3.9 KB

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