ResourceManager.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\App\Services\Cloud;
  3. use ModulesGarden\ProxmoxAddon\App\Models\VirtualNetwork;
  4. use ModulesGarden\ProxmoxAddon\App\Models\VmIpAddress;
  5. use ModulesGarden\ProxmoxAddon\App\Models\VmModel;
  6. use ModulesGarden\ProxmoxAddon\App\Services\Utility;
  7. use ModulesGarden\ProxmoxAddon\Core\UI\Traits\WhmcsParams;
  8. use ModulesGarden\ProxmoxAddon\App\Enum\Cloud\ConfigurableOption;
  9. class ResourceManager
  10. {
  11. use WhmcsParams;
  12. use ProductService;
  13. protected $notVmIds = [];
  14. /**
  15. * @var Resource[]
  16. */
  17. protected $items = [];
  18. public function notVmIds(array $ids)
  19. {
  20. $this->notVmIds = $ids;
  21. return $this;
  22. }
  23. /**
  24. * @return Resource
  25. */
  26. public function sockets()
  27. {
  28. //cache
  29. if ($this->items[__FUNCTION__])
  30. {
  31. return $this->items[__FUNCTION__];
  32. }
  33. $resurce = new Resource(__FUNCTION__);
  34. $resurce->setUnit('int');
  35. if ($this->isWhmcsConfigOption(ConfigurableOption::SOCKETS))
  36. {
  37. $resurce->setTotal((int)$this->getWhmcsConfigOption(ConfigurableOption::SOCKETS));
  38. }
  39. else
  40. {
  41. $resurce->setTotal((int)$this->configuration()->getSockets());
  42. }
  43. $resurce->setMin($this->configuration()->serverSockets->min);
  44. $resurce->setMax($this->configuration()->serverSockets->max);
  45. $query = VmModel::ofHostingId($this->getWhmcsParamByKey('serviceid'));
  46. if ($this->notVmIds)
  47. {
  48. $query->notIdIn($this->notVmIds);
  49. }
  50. $resurce->setUsed($query->sum(__FUNCTION__));
  51. return $this->items[__FUNCTION__] = $resurce;
  52. }
  53. public function cores()
  54. {
  55. //cache
  56. if ($this->items[__FUNCTION__])
  57. {
  58. return $this->items[__FUNCTION__];
  59. }
  60. $resurce = new Resource(__FUNCTION__);
  61. $resurce->setUnit('int');
  62. if ($this->isWhmcsConfigOption(ConfigurableOption::CORES))
  63. {
  64. $resurce->setTotal((int)$this->getWhmcsConfigOption(ConfigurableOption::CORES));
  65. }
  66. else
  67. {
  68. $resurce->setTotal((int)$this->configuration()->getCores());
  69. }
  70. $resurce->setMin($this->configuration()->serverCores->min);
  71. $resurce->setMax($this->configuration()->serverCores->max);
  72. $query = VmModel::ofHostingId($this->getWhmcsParamByKey('serviceid'));
  73. if ($this->notVmIds)
  74. {
  75. $query->notIdIn($this->notVmIds);
  76. }
  77. $resurce->setUsed($query->sum(__FUNCTION__));
  78. return $this->items[__FUNCTION__] = $resurce;
  79. }
  80. public function vcpus()
  81. {
  82. //cache
  83. if ($this->items[__FUNCTION__])
  84. {
  85. return $this->items[__FUNCTION__];
  86. }
  87. $resurce = new Resource(__FUNCTION__);
  88. $resurce->setUnit('int');
  89. if ($this->isWhmcsConfigOption(ConfigurableOption::VCPUS))
  90. {
  91. $resurce->setTotal((int)$this->getWhmcsConfigOption(ConfigurableOption::VCPUS));
  92. }
  93. else
  94. {
  95. $resurce->setTotal((int)$this->configuration()->getVcpus());
  96. }
  97. $resurce->setMin($this->configuration()->serverVcpus->min);
  98. $resurce->setMax($this->configuration()->serverVcpus->max);
  99. $query = VmModel::ofHostingId($this->getWhmcsParamByKey('serviceid'));
  100. if ($this->notVmIds)
  101. {
  102. $query->notIdIn($this->notVmIds);
  103. }
  104. $resurce->setUsed($query->sum(__FUNCTION__));
  105. return $this->items[__FUNCTION__] = $resurce;
  106. }
  107. public function cpulimit()
  108. {
  109. //cache
  110. if ($this->items[__FUNCTION__])
  111. {
  112. return $this->items[__FUNCTION__];
  113. }
  114. $resurce = new Resource(__FUNCTION__);
  115. $resurce->setUnit('int');
  116. if ($this->isWhmcsConfigOption(ConfigurableOption::CPU_LIMIT))
  117. {
  118. $resurce->setTotal(round($this->getWhmcsConfigOption(ConfigurableOption::CPU_LIMIT),2));
  119. }
  120. else
  121. {
  122. $resurce->setTotal(round($this->configuration()->getCpulimit(),2));
  123. }
  124. $resurce->setMin($this->configuration()->serverCpulimit->min);
  125. $resurce->setMax($this->configuration()->serverCpulimit->max);
  126. $query = VmModel::ofHostingId($this->getWhmcsParamByKey('serviceid'));
  127. if ($this->notVmIds)
  128. {
  129. $query->notIdIn($this->notVmIds);
  130. }
  131. $resurce->setUsed($query->sum(__FUNCTION__));
  132. return $this->items[__FUNCTION__] = $resurce;
  133. }
  134. public function virtualNetworks()
  135. {
  136. //cache
  137. if ($this->items[__FUNCTION__])
  138. {
  139. return $this->items[__FUNCTION__];
  140. }
  141. $resurce = new Resource(__FUNCTION__);
  142. $resurce->setUnit('int');
  143. if ($this->isWhmcsConfigOption(ConfigurableOption::VIRTUAL_NETWORKS))
  144. {
  145. $resurce->setTotal((int)$this->getWhmcsConfigOption(ConfigurableOption::VIRTUAL_NETWORKS));
  146. }
  147. else
  148. {
  149. $resurce->setTotal((int)$this->configuration()->getVirtualNetworks());
  150. }
  151. $resurce->setMin(0);
  152. $resurce->setMax($resurce->getTotal());
  153. $query = VirtualNetwork::ofHostingId($this->getWhmcsParamByKey('serviceid'));
  154. $resurce->setUsed($query->count());
  155. return $this->items[__FUNCTION__] = $resurce;
  156. }
  157. /**
  158. * @return Resource|Resource
  159. * @todo singel query
  160. */
  161. public function disk()
  162. {
  163. //cache
  164. if ($this->items[__FUNCTION__])
  165. {
  166. return $this->items[__FUNCTION__];
  167. }
  168. $resurce = new Resource(__FUNCTION__);
  169. $resurce->setUnit("gb");
  170. if ($this->isWhmcsConfigOption(ConfigurableOption::STORAGE))
  171. {
  172. $resurce->setUnit($this->configuration()->getStorageUnit());
  173. $resurce->setTotal((int)$this->getWhmcsConfigOption(ConfigurableOption::STORAGE));
  174. }
  175. else
  176. {
  177. $resurce->setTotal((int)$this->configuration()->getStorageSize());
  178. }
  179. $resurce->setMin((int)$this->configuration()->serverDiskSize->min);
  180. $resurce->setMax((int)$this->configuration()->serverDiskSize->max);
  181. $query = VmModel::ofHostingId($this->getWhmcsParamByKey('serviceid'));
  182. $query2 = VmModel::ofHostingId($this->getWhmcsParamByKey('serviceid'));
  183. if ($this->notVmIds)
  184. {
  185. $query->notIdIn($this->notVmIds);
  186. $query2->notIdIn($this->notVmIds);
  187. }
  188. $resurce->setUsed($query->sum('disk') + $query2->sum('disks'));
  189. return $this->items[__FUNCTION__] = $resurce;
  190. }
  191. /**
  192. * @return Resource|Resource
  193. */
  194. public function memory()
  195. {
  196. //cache
  197. if ($this->items[__FUNCTION__])
  198. {
  199. return $this->items[__FUNCTION__];
  200. }
  201. $resurce = new Resource(__FUNCTION__);
  202. $resurce->setUnit("mb");
  203. if ($this->isWhmcsConfigOption(ConfigurableOption::MEMORY))
  204. {
  205. $resurce->setUnit($this->configuration()->getMemoryUnit());
  206. $total = (int)$this->getWhmcsConfigOption(ConfigurableOption::MEMORY);
  207. Utility::unitFormat($total,$this->configuration()->getMemoryUnit(),"mb");
  208. $resurce->setTotal($total);
  209. }
  210. else
  211. {
  212. $resurce->setTotal((int)$this->configuration()->getMemory());
  213. }
  214. $resurce->setMin($this->configuration()->serverMemory->min);
  215. $resurce->setMax($this->configuration()->serverMemory->max);
  216. $query = VmModel::ofHostingId($this->getWhmcsParamByKey('serviceid'));
  217. if ($this->notVmIds)
  218. {
  219. $query->notIdIn($this->notVmIds);
  220. }
  221. $resurce->setUsed($query->sum(__FUNCTION__));
  222. return $this->items[__FUNCTION__] = $resurce;
  223. }
  224. /**
  225. * @return Resource|Resource
  226. */
  227. public function swap()
  228. {
  229. //cache
  230. if ($this->items[__FUNCTION__])
  231. {
  232. return $this->items[__FUNCTION__];
  233. }
  234. $resurce = new Resource(__FUNCTION__);
  235. $resurce->setUnit("mb");
  236. if ($this->isWhmcsConfigOption(ConfigurableOption::SWAP))
  237. {
  238. $resurce->setUnit($this->configuration()->getSwapUnit());
  239. $resurce->setTotal((int)$this->getWhmcsConfigOption(ConfigurableOption::SWAP));
  240. }
  241. else
  242. {
  243. $resurce->setTotal((int)$this->configuration()->getSwap());
  244. }
  245. $resurce->setMin($this->configuration()->serverSwap->min);
  246. $resurce->setMax($this->configuration()->serverSwap->max);
  247. $query = VmModel::ofHostingId($this->getWhmcsParamByKey('serviceid'));
  248. if ($this->notVmIds)
  249. {
  250. $query->notIdIn($this->notVmIds);
  251. }
  252. $resurce->setUsed($query->sum(__FUNCTION__));
  253. return $this->items[__FUNCTION__] = $resurce;
  254. }
  255. public function cpuunits()
  256. {
  257. //cache
  258. if ($this->items[__FUNCTION__])
  259. {
  260. return $this->items[__FUNCTION__];
  261. }
  262. $resurce = new Resource(__FUNCTION__);
  263. $resurce->setUnit("int");
  264. if ($this->isWhmcsConfigOption(ConfigurableOption::CPU_UNITS))
  265. {
  266. $resurce->setTotal((int)$this->getWhmcsConfigOption(ConfigurableOption::CPU_UNITS));
  267. }
  268. else
  269. {
  270. $resurce->setTotal((int)$this->configuration()->getCpuunits());
  271. }
  272. $resurce->setMin($this->configuration()->serverCpuunit->min);
  273. $resurce->setMax($this->configuration()->serverCpuunit->max);
  274. $query = VmModel::ofHostingId($this->getWhmcsParamByKey('serviceid'));
  275. if ($this->notVmIds)
  276. {
  277. $query->notIdIn($this->notVmIds);
  278. }
  279. $resurce->setUsed($query->sum(__FUNCTION__));
  280. return $this->items[__FUNCTION__] = $resurce;
  281. }
  282. public function ipv4()
  283. {
  284. //cache
  285. if ($this->items[__FUNCTION__])
  286. {
  287. return $this->items[__FUNCTION__];
  288. }
  289. $resurce = new Resource(__FUNCTION__);
  290. $resurce->setUnit("int");
  291. if ($this->isWhmcsConfigOption(ConfigurableOption::IPV4))
  292. {
  293. $resurce->setTotal((int)$this->getWhmcsConfigOption(ConfigurableOption::IPV4));
  294. }
  295. else
  296. {
  297. $resurce->setTotal((int)$this->configuration()->getIpv4());
  298. }
  299. $resurce->setMin($this->configuration()->serverIpv4->min);
  300. $resurce->setMax($this->configuration()->serverIpv4->max);
  301. $query = VmIpAddress::ofHostingId($this->getWhmcsParamByKey('serviceid'));
  302. $query->ofIp4();
  303. if ($this->notVmIds)
  304. {
  305. $query->notIdIn($this->notVmIds);
  306. }
  307. $resurce->setUsed($query->count());
  308. return $this->items[__FUNCTION__] = $resurce;
  309. }
  310. public function ipv6()
  311. {
  312. //cache
  313. if ($this->items[__FUNCTION__])
  314. {
  315. return $this->items[__FUNCTION__];
  316. }
  317. $resurce = new Resource(__FUNCTION__);
  318. $resurce->setUnit("int");
  319. if ($this->isWhmcsConfigOption(ConfigurableOption::IPV6))
  320. {
  321. $resurce->setTotal((int)$this->getWhmcsConfigOption(ConfigurableOption::IPV6));
  322. }
  323. else
  324. {
  325. $resurce->setTotal((int)$this->configuration()->getIpv6());
  326. }
  327. $resurce->setMin($this->configuration()->serverIpv6->min);
  328. $resurce->setMax($this->configuration()->serverIpv6->max);
  329. $query = VmIpAddress::ofHostingId($this->getWhmcsParamByKey('serviceid'));
  330. $query->ofIp6();
  331. if ($this->notVmIds)
  332. {
  333. $query->notIdIn($this->notVmIds);
  334. }
  335. $resurce->setUsed($query->count());
  336. return $this->items[__FUNCTION__] = $resurce;
  337. }
  338. public function cpuPriority(){
  339. //cache
  340. if ($this->items[__FUNCTION__])
  341. {
  342. return $this->items[__FUNCTION__];
  343. }
  344. $resurce = new Resource(__FUNCTION__);
  345. $resurce->setUnit("int");
  346. $resurce->setMin(1);
  347. $resurce->setMax(5);
  348. $resurce->setTotal(5);
  349. for($i=1; $i<=5; $i++){
  350. $cpuunits = $this->configuration()->get('cpuunitsPriority'.$i);
  351. $cpulimit = round( $this->configuration()->get('cpulimitPriority'.$i),2);
  352. if($this->cpulimit()->freeTotal() < $cpulimit){
  353. $resurce->setUsed($i);
  354. }
  355. if($this->cpuunits()->freeTotal() < $cpuunits){
  356. $resurce->setUsed($i);
  357. }
  358. }
  359. return $this->items[__FUNCTION__] = $resurce;
  360. }
  361. }