GraphData.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Graph\Pages;
  3. use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
  4. use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
  5. use ModulesGarden\ProxmoxAddon\Core\Models\ModuleSettings\Model;
  6. use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;
  7. trait GraphData
  8. {
  9. use ProductService;
  10. use ApiService;
  11. protected $timeframe = "week";
  12. public function chartData()
  13. {
  14. $request = [
  15. "timeframe" => $this->timeframe,
  16. "cf" => "MAX",
  17. ];
  18. $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
  19. return $vm->rrdData($request);
  20. }
  21. protected function chartOptions()
  22. {
  23. $registrationDate = new \DateTime($this->getWhmcsParamByKey('model')->registrationDate->format("Y-m-d"));
  24. $options = ['hour' => sl("lang")->tr("Hour")];
  25. $dnow = new \DateTime();
  26. $dDiff = $registrationDate->diff($dnow);
  27. if ($dDiff->days >= 1)
  28. {
  29. $options['day'] = sl("lang")->tr("Day");
  30. }
  31. if ($dDiff->days >= 7)
  32. {
  33. $options['week'] = sl("lang")->tr("Week");
  34. }
  35. if ($dDiff->days >= 30)
  36. {
  37. $options['month'] = sl("lang")->tr("Month");
  38. }
  39. if ($dDiff->y >= 1)
  40. {
  41. $options['year'] = sl("lang")->tr("year");
  42. }
  43. return $options;
  44. }
  45. protected function loadSettings()
  46. {
  47. $this->configChartsSettings = json_decode(Model::where('setting', $this->graphSettingsKey)->first()->value);
  48. if ($this->configChartsSettings)
  49. {
  50. $this->setGraphFilterInfo(null, $this->configChartsSettings->start, $this->configChartsSettings->end);
  51. }
  52. return $this;
  53. }
  54. protected function convertData($data, $format = null)
  55. {
  56. return array_slice($data, 0, 24);
  57. $format = [];
  58. //Format data
  59. foreach($this->convertData($rrdata) as $record)
  60. {
  61. $time = date('Y-m-d H', $record['time']);
  62. $format[$time][] = $record;
  63. }
  64. //calculate average value
  65. foreach($format as $time => $records)
  66. {
  67. $sum = [];
  68. $count = count($records);
  69. foreach($records as $record)
  70. {
  71. foreach($records as $type => $value)
  72. {
  73. }
  74. $sum['time'] = strtotime($time);
  75. }
  76. }
  77. }
  78. }