<?php
/* * ********************************************************************
 * ProxmoxAddon product developed. (Aug 14, 2018)
 * *
 *
 *  CREATED BY MODULESGARDEN       ->       http://modulesgarden.com
 *  CONTACT                        ->       contact@modulesgarden.com
 *
 *
 * This software is furnished under a license and may be used and copied
 * only  in  accordance  with  the  terms  of such  license and with the
 * inclusion of the above copyright notice.  This software  or any other
 * copies thereof may not be provided or otherwise made available to any
 * other person.  No title to and  ownership of the  software is  hereby
 * transferred.
 *
 *
 * ******************************************************************** */

namespace ModulesGarden\ProxmoxAddon\App\UI\NodeDetail\Pages;


use MGProvision\Proxmox\v2\models\Node;
use ModulesGarden\ProxmoxAddon\App\Libs\Format;
use ModulesGarden\ProxmoxAddon\App\Services\BaseService;
use ModulesGarden\ProxmoxAddon\Core\UI\Builder\BaseContainer;
use ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\AdminArea;
use ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\AjaxElementInterface;
use ModulesGarden\ProxmoxAddon\Core\UI\ResponseTemplates\RawDataJsonResponse;

class Summary extends BaseContainer implements AdminArea, AjaxElementInterface
{

    use BaseService;

    protected $id = 'mg-summary';
    protected $name = 'mg-summary-name';
    protected $title = 'mg-summary-title';

    protected $vueComponent = true;
    protected $defaultVueComponentName = 'mg-details';

    public function initContent()
    {

    }

    public function returnAjaxData()
    {
        $data = [];
        $this->setServerId($this->getRequestValue('serverId'))
            ->getApi()
            ->setInstance();
        $node = new Node($this->getRequestValue('id'));
        //Summary
        $status                      = $node->getStatus();
        $status['memory']['percent'] = round($status['memory']['used'] / $status['memory']['total'] * 100);
        $status['swap']['percent']   = round($status['swap']['used'] / $status['swap']['total'] * 100);
        $status['rootfs']['percent'] = round($status['rootfs']['used'] / $status['rootfs']['total'] * 100);
        $status['rootfs']['free']    = $status['rootfs']['total'] - $status['rootfs']['used'];
        foreach ($status['memory'] as $k => &$v)
        {
            if ($k == 'percent')
            {
                break;
            }
            $v = Format::convertBytes($v);
        }
        foreach ($status['rootfs'] as $k => &$v)
        {
            if ($k == 'percent')
            {
                break;
            }
            $v = Format::convertBytes($v);
        }
        foreach ($status['swap'] as $k => &$v)
        {
            if ($k == 'percent')
            {
                break;
            }
            $v = Format::convertBytes($v);
        }
        $status['uptime'] = Format::uptime($status['uptime']);
        $status['cpu']    = $status['cpu'] ? round($status['cpu'] * 100, 2) . " %" : "0 %";
        $data['status']   = $status;
        $data['node']     = $node->getNode();
        return (new RawDataJsonResponse(['data' => $data]));
    }
}