MemoryGraph.php 3.8 KB

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