MemoryGraph.php 4.2 KB

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