NetworkGraph.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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\ProxmoxAddon\App\UI\NodeDetail\Pages;
  20. use ModulesGarden\ProxmoxAddon as main;
  21. use ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\AdminArea;
  22. use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Forms\Fields\Select;
  23. use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Graphs\Line;
  24. use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Graphs\Models\DataSet;
  25. class NetworkGraph extends Line implements AdminArea
  26. {
  27. use LoadAPIData;
  28. protected $id = 'networkGraph';
  29. protected $name = 'networkGraph';
  30. protected $graphSettingsEnabled = true;
  31. protected $graphSettingsKey = 'cpuGraphSettings';
  32. public function initContent()
  33. {
  34. $selectScope = new Select('timeframe');
  35. $selectScope->setAvailableValues([
  36. 'hour' => 'Hour',
  37. 'day' => 'Day',
  38. 'week' => 'Week',
  39. 'year' => 'Year',
  40. ]);
  41. $selectScope->setDefaultValue('week');
  42. $this->addSettingField($selectScope);
  43. $this->updateChartScale('yAxes', [
  44. [
  45. 'scaleLabel' => [
  46. 'display' => true,
  47. 'labelString' => 'Bytes/s'
  48. ],
  49. 'ticks' => [
  50. 'callback' => 'mgBytesToSize'
  51. ],
  52. ]]);
  53. $this->updateChartOption('tooltips', [
  54. 'callbacks' => [
  55. 'label' => 'mgTooltipCallbackForNet'
  56. ]
  57. ]);
  58. }
  59. public function prepareAjaxData()
  60. {
  61. if ($this->configChartsSettings->timeframe)
  62. {
  63. $this->timeframe = $this->configChartsSettings->timeframe;
  64. }
  65. $rrdata = main\Core\Helper\DatabaseCache::loadData(
  66. $this->graphSettingsKey .$this->timeframe. '_cacheData', [$this, 'loadApiData'], 30, true);
  67. $labels = [];
  68. $dataSets = [
  69. 'netin' => [],
  70. 'netout' => [],
  71. ];
  72. $dateFormat = in_array($this->timeframe, ['hour', 'day']) ? "H:i:s" : "Y-m-d";
  73. foreach ($rrdata as $rrd)
  74. {
  75. $labels[] = date($dateFormat, $rrd['time']);
  76. $dataSets['netin'][] = (float)$rrd['netin'] ;
  77. $dataSets['netout'][] = (float)$rrd['netout'];
  78. }
  79. //Labels
  80. $this->setLabels($labels);
  81. //Net In
  82. $lang = main\Core\ServiceLocator::call('lang');
  83. $dataSet = new DataSet();
  84. $dataSet->setTitle($lang->absoluteT('Net In'))
  85. ->setData($dataSets['netin'])
  86. ->setConfigurationDataSet([
  87. "backgroundColor" => 'rgba(174, 198, 57, 0.79)',
  88. "borderColor" => 'rgba(174, 198, 57, 1)',
  89. ]);
  90. $this->addDataSet($dataSet);
  91. //Net Out
  92. $dataSet = new DataSet();
  93. $dataSet->setTitle($lang->absoluteT('Net Out'))
  94. ->setData($dataSets['netout'])
  95. ->setConfigurationDataSet([
  96. "backgroundColor" => 'rgba(39, 133, 134, 0.91)',
  97. "borderColor" => 'rgba(39, 133, 134, 1)',
  98. ]);
  99. $this->addDataSet($dataSet);
  100. }
  101. }