| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- <?php
- namespace ModulesGarden\ProxmoxAddon\App\Services\Cloud;
- use ModulesGarden\ProxmoxAddon\App\Models\VirtualNetwork;
- use ModulesGarden\ProxmoxAddon\App\Models\VmIpAddress;
- use ModulesGarden\ProxmoxAddon\App\Models\VmModel;
- use ModulesGarden\ProxmoxAddon\App\Services\Utility;
- use ModulesGarden\ProxmoxAddon\Core\UI\Traits\WhmcsParams;
- use ModulesGarden\ProxmoxAddon\App\Enum\Cloud\ConfigurableOption;
- class ResourceManager
- {
- use WhmcsParams;
- use ProductService;
- protected $notVmIds = [];
- /**
- * @var Resource[]
- */
- protected $items = [];
- public function notVmIds(array $ids)
- {
- $this->notVmIds = $ids;
- return $this;
- }
- /**
- * @return Resource
- */
- public function sockets()
- {
- //cache
- if ($this->items[__FUNCTION__])
- {
- return $this->items[__FUNCTION__];
- }
- $resurce = new Resource(__FUNCTION__);
- $resurce->setUnit('int');
- if ($this->isWhmcsConfigOption(ConfigurableOption::SOCKETS))
- {
- $resurce->setTotal((int)$this->getWhmcsConfigOption(ConfigurableOption::SOCKETS));
- }
- else
- {
- $resurce->setTotal((int)$this->configuration()->getSockets());
- }
- $resurce->setMin($this->configuration()->serverSockets->min);
- $resurce->setMax($this->configuration()->serverSockets->max);
- $query = VmModel::ofHostingId($this->getWhmcsParamByKey('serviceid'));
- if ($this->notVmIds)
- {
- $query->notIdIn($this->notVmIds);
- }
- $resurce->setUsed($query->sum(__FUNCTION__));
- return $this->items[__FUNCTION__] = $resurce;
- }
- public function cores()
- {
- //cache
- if ($this->items[__FUNCTION__])
- {
- return $this->items[__FUNCTION__];
- }
- $resurce = new Resource(__FUNCTION__);
- $resurce->setUnit('int');
- if ($this->isWhmcsConfigOption(ConfigurableOption::CORES))
- {
- $resurce->setTotal((int)$this->getWhmcsConfigOption(ConfigurableOption::CORES));
- }
- else
- {
- $resurce->setTotal((int)$this->configuration()->getCores());
- }
- $resurce->setMin($this->configuration()->serverCores->min);
- $resurce->setMax($this->configuration()->serverCores->max);
- $query = VmModel::ofHostingId($this->getWhmcsParamByKey('serviceid'));
- if ($this->notVmIds)
- {
- $query->notIdIn($this->notVmIds);
- }
- $resurce->setUsed($query->sum(__FUNCTION__));
- return $this->items[__FUNCTION__] = $resurce;
- }
- public function vcpus()
- {
- //cache
- if ($this->items[__FUNCTION__])
- {
- return $this->items[__FUNCTION__];
- }
- $resurce = new Resource(__FUNCTION__);
- $resurce->setUnit('int');
- if ($this->isWhmcsConfigOption(ConfigurableOption::VCPUS))
- {
- $resurce->setTotal((int)$this->getWhmcsConfigOption(ConfigurableOption::VCPUS));
- }
- else
- {
- $resurce->setTotal((int)$this->configuration()->getVcpus());
- }
- $resurce->setMin($this->configuration()->serverVcpus->min);
- $resurce->setMax($this->configuration()->serverVcpus->max);
- $query = VmModel::ofHostingId($this->getWhmcsParamByKey('serviceid'));
- if ($this->notVmIds)
- {
- $query->notIdIn($this->notVmIds);
- }
- $resurce->setUsed($query->sum(__FUNCTION__));
- return $this->items[__FUNCTION__] = $resurce;
- }
- public function cpulimit()
- {
- //cache
- if ($this->items[__FUNCTION__])
- {
- return $this->items[__FUNCTION__];
- }
- $resurce = new Resource(__FUNCTION__);
- $resurce->setUnit('int');
- if ($this->isWhmcsConfigOption(ConfigurableOption::CPU_LIMIT))
- {
- $resurce->setTotal(round($this->getWhmcsConfigOption(ConfigurableOption::CPU_LIMIT),2));
- }
- else
- {
- $resurce->setTotal(round($this->configuration()->getCpulimit(),2));
- }
- $resurce->setMin($this->configuration()->serverCpulimit->min);
- $resurce->setMax($this->configuration()->serverCpulimit->max);
- $query = VmModel::ofHostingId($this->getWhmcsParamByKey('serviceid'));
- if ($this->notVmIds)
- {
- $query->notIdIn($this->notVmIds);
- }
- $resurce->setUsed($query->sum(__FUNCTION__));
- return $this->items[__FUNCTION__] = $resurce;
- }
- public function virtualNetworks()
- {
- //cache
- if ($this->items[__FUNCTION__])
- {
- return $this->items[__FUNCTION__];
- }
- $resurce = new Resource(__FUNCTION__);
- $resurce->setUnit('int');
- if ($this->isWhmcsConfigOption(ConfigurableOption::VIRTUAL_NETWORKS))
- {
- $resurce->setTotal((int)$this->getWhmcsConfigOption(ConfigurableOption::VIRTUAL_NETWORKS));
- }
- else
- {
- $resurce->setTotal((int)$this->configuration()->getVirtualNetworks());
- }
- $resurce->setMin(0);
- $resurce->setMax($resurce->getTotal());
- $query = VirtualNetwork::ofHostingId($this->getWhmcsParamByKey('serviceid'));
- $resurce->setUsed($query->count());
- return $this->items[__FUNCTION__] = $resurce;
- }
- /**
- * @return Resource|Resource
- * @todo singel query
- */
- public function disk()
- {
- //cache
- if ($this->items[__FUNCTION__])
- {
- return $this->items[__FUNCTION__];
- }
- $resurce = new Resource(__FUNCTION__);
- $resurce->setUnit("gb");
- if ($this->isWhmcsConfigOption(ConfigurableOption::STORAGE))
- {
- $resurce->setUnit($this->configuration()->getStorageUnit());
- $resurce->setTotal((int)$this->getWhmcsConfigOption(ConfigurableOption::STORAGE));
- }
- else
- {
- $resurce->setTotal((int)$this->configuration()->getStorageSize());
- }
- $resurce->setMin((int)$this->configuration()->serverDiskSize->min);
- $resurce->setMax((int)$this->configuration()->serverDiskSize->max);
- $query = VmModel::ofHostingId($this->getWhmcsParamByKey('serviceid'));
- $query2 = VmModel::ofHostingId($this->getWhmcsParamByKey('serviceid'));
- if ($this->notVmIds)
- {
- $query->notIdIn($this->notVmIds);
- $query2->notIdIn($this->notVmIds);
- }
- $resurce->setUsed($query->sum('disk') + $query2->sum('disks'));
- return $this->items[__FUNCTION__] = $resurce;
- }
- /**
- * @return Resource|Resource
- */
- public function memory()
- {
- //cache
- if ($this->items[__FUNCTION__])
- {
- return $this->items[__FUNCTION__];
- }
- $resurce = new Resource(__FUNCTION__);
- $resurce->setUnit("mb");
- if ($this->isWhmcsConfigOption(ConfigurableOption::MEMORY))
- {
- $resurce->setUnit($this->configuration()->getMemoryUnit());
- $total = (int)$this->getWhmcsConfigOption(ConfigurableOption::MEMORY);
- Utility::unitFormat($total,$this->configuration()->getMemoryUnit(),"mb");
- $resurce->setTotal($total);
- }
- else
- {
- $resurce->setTotal((int)$this->configuration()->getMemory());
- }
- $resurce->setMin($this->configuration()->serverMemory->min);
- $resurce->setMax($this->configuration()->serverMemory->max);
- $query = VmModel::ofHostingId($this->getWhmcsParamByKey('serviceid'));
- if ($this->notVmIds)
- {
- $query->notIdIn($this->notVmIds);
- }
- $resurce->setUsed($query->sum(__FUNCTION__));
- return $this->items[__FUNCTION__] = $resurce;
- }
- /**
- * @return Resource|Resource
- */
- public function swap()
- {
- //cache
- if ($this->items[__FUNCTION__])
- {
- return $this->items[__FUNCTION__];
- }
- $resurce = new Resource(__FUNCTION__);
- $resurce->setUnit("mb");
- if ($this->isWhmcsConfigOption(ConfigurableOption::SWAP))
- {
- $resurce->setUnit($this->configuration()->getSwapUnit());
- $resurce->setTotal((int)$this->getWhmcsConfigOption(ConfigurableOption::SWAP));
- }
- else
- {
- $resurce->setTotal((int)$this->configuration()->getSwap());
- }
- $resurce->setMin($this->configuration()->serverSwap->min);
- $resurce->setMax($this->configuration()->serverSwap->max);
- $query = VmModel::ofHostingId($this->getWhmcsParamByKey('serviceid'));
- if ($this->notVmIds)
- {
- $query->notIdIn($this->notVmIds);
- }
- $resurce->setUsed($query->sum(__FUNCTION__));
- return $this->items[__FUNCTION__] = $resurce;
- }
- public function cpuunits()
- {
- //cache
- if ($this->items[__FUNCTION__])
- {
- return $this->items[__FUNCTION__];
- }
- $resurce = new Resource(__FUNCTION__);
- $resurce->setUnit("int");
- if ($this->isWhmcsConfigOption(ConfigurableOption::CPU_UNITS))
- {
- $resurce->setTotal((int)$this->getWhmcsConfigOption(ConfigurableOption::CPU_UNITS));
- }
- else
- {
- $resurce->setTotal((int)$this->configuration()->getCpuunits());
- }
- $resurce->setMin($this->configuration()->serverCpuunit->min);
- $resurce->setMax($this->configuration()->serverCpuunit->max);
- $query = VmModel::ofHostingId($this->getWhmcsParamByKey('serviceid'));
- if ($this->notVmIds)
- {
- $query->notIdIn($this->notVmIds);
- }
- $resurce->setUsed($query->sum(__FUNCTION__));
- return $this->items[__FUNCTION__] = $resurce;
- }
- public function ipv4()
- {
- //cache
- if ($this->items[__FUNCTION__])
- {
- return $this->items[__FUNCTION__];
- }
- $resurce = new Resource(__FUNCTION__);
- $resurce->setUnit("int");
- if ($this->isWhmcsConfigOption(ConfigurableOption::IPV4))
- {
- $resurce->setTotal((int)$this->getWhmcsConfigOption(ConfigurableOption::IPV4));
- }
- else
- {
- $resurce->setTotal((int)$this->configuration()->getIpv4());
- }
- $resurce->setMin($this->configuration()->serverIpv4->min);
- $resurce->setMax($this->configuration()->serverIpv4->max);
- $query = VmIpAddress::ofHostingId($this->getWhmcsParamByKey('serviceid'));
- $query->ofIp4();
- if ($this->notVmIds)
- {
- $query->notIdIn($this->notVmIds);
- }
- $resurce->setUsed($query->count());
- return $this->items[__FUNCTION__] = $resurce;
- }
- public function ipv6()
- {
- //cache
- if ($this->items[__FUNCTION__])
- {
- return $this->items[__FUNCTION__];
- }
- $resurce = new Resource(__FUNCTION__);
- $resurce->setUnit("int");
- if ($this->isWhmcsConfigOption(ConfigurableOption::IPV6))
- {
- $resurce->setTotal((int)$this->getWhmcsConfigOption(ConfigurableOption::IPV6));
- }
- else
- {
- $resurce->setTotal((int)$this->configuration()->getIpv6());
- }
- $resurce->setMin($this->configuration()->serverIpv6->min);
- $resurce->setMax($this->configuration()->serverIpv6->max);
- $query = VmIpAddress::ofHostingId($this->getWhmcsParamByKey('serviceid'));
- $query->ofIp6();
- if ($this->notVmIds)
- {
- $query->notIdIn($this->notVmIds);
- }
- $resurce->setUsed($query->count());
- return $this->items[__FUNCTION__] = $resurce;
- }
- public function cpuPriority(){
- //cache
- if ($this->items[__FUNCTION__])
- {
- return $this->items[__FUNCTION__];
- }
- $resurce = new Resource(__FUNCTION__);
- $resurce->setUnit("int");
- $resurce->setMin(1);
- $resurce->setMax(5);
- $resurce->setTotal(5);
- for($i=1; $i<=5; $i++){
- $cpuunits = $this->configuration()->get('cpuunitsPriority'.$i);
- $cpulimit = round( $this->configuration()->get('cpulimitPriority'.$i),2);
- if($this->cpulimit()->freeTotal() < $cpulimit){
- $resurce->setUsed($i);
- }
- if($this->cpuunits()->freeTotal() < $cpuunits){
- $resurce->setUsed($i);
- }
- }
- return $this->items[__FUNCTION__] = $resurce;
- }
- }
|