NetworkGraph.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 NetworkGraph extends Line implements ClientArea, AdminArea
  29. {
  30. use GraphData;
  31. protected $id = 'networkGraph';
  32. protected $graphSettingsEnabled = true;
  33. protected $graphSettingsKey = 'networkGraphSettings';
  34. protected $displayScaleLabel = true;
  35. protected $graphHeight = null;
  36. protected $graphWidth = null;
  37. public function initContent()
  38. {
  39. $this->initIds('networkGraph');
  40. $this->setRawTitle( di('lang')->translate('networkGraph'));
  41. $selectScope = new Select('timeframe');
  42. $selectScope->setAvailableValues($this->chartOptions());
  43. $selectScope->setDefaultValue('week');
  44. $this->addSettingField($selectScope);
  45. $this->updateChartScale('yAxes', [
  46. [
  47. 'scaleLabel' => [
  48. 'display' => $this->displayScaleLabel,
  49. 'labelString' => sl('lang')->tr('Bytes/s')
  50. ],
  51. 'ticks' => [
  52. 'callback' => 'mgBytesToSize',
  53. 'beginAtZero' => true,
  54. 'fontSize' => 10
  55. ],
  56. ]]);
  57. $this->updateChartOption('tooltips', [
  58. 'callbacks' => [
  59. 'label' => 'mgTooltipCallbackForNet'
  60. ]
  61. ]);
  62. }
  63. public function prepareAjaxData()
  64. {
  65. session_write_close();
  66. if ($this->configChartsSettings->timeframe)
  67. {
  68. $this->timeframe = $this->configChartsSettings->timeframe;
  69. }
  70. $rrdata = DatabaseCache::loadData(
  71. $this->graphSettingsKey.$this->timeframe .$this->getWhmcsParamByKey('serviceid'). '_cacheData', [$this, 'chartData'], 30, true);
  72. $labels = [];
  73. $dataSets = [
  74. 'netin' => [],
  75. 'netout' => [],
  76. ];
  77. $dateFormat = in_array($this->timeframe, ['hour', 'day']) ? "H:i:s" : "Y-m-d";
  78. foreach ($this->convertData($rrdata) as $rrd)
  79. {
  80. $labels[] = date($dateFormat, $rrd['time']);
  81. $dataSets['netin'][] = isset($rrd['netin']) ? (float)$rrd['netin'] : 0;
  82. $dataSets['netout'][] = isset($rrd['netout']) ? (float)$rrd['netout'] : 0;
  83. }
  84. //Labels
  85. $this->setLabels($labels);
  86. //net in
  87. $lang = sl('lang');
  88. $dataSet = new DataSet();
  89. $dataSet->setTitle($lang->tr('Net In'))
  90. ->setData($dataSets['netin'])
  91. ->setConfigurationDataSet([
  92. "backgroundColor" => 'rgba(174, 198, 57, 0.79)',
  93. "borderColor" => 'rgba(174, 198, 57, 1)',
  94. ]);
  95. $this->addDataSet($dataSet);
  96. //net out
  97. $dataSet = new DataSet();
  98. $dataSet->setTitle($lang->tr('Net Out'))
  99. ->setData($dataSets['netout'])
  100. ->setConfigurationDataSet([
  101. "backgroundColor" => 'rgba(39, 133, 134, 0.91)',
  102. "borderColor" => 'rgba(39, 133, 134, 1)',
  103. ]);
  104. $this->addDataSet($dataSet);
  105. }
  106. }