<?php

/* * ********************************************************************
 * ProxmoxAddon product developed. (Oct 1, 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\Servers\ProxmoxCloudVps\App\UI\Graph\Pages;

use ModulesGarden\ProxmoxAddon\Core\Helper\DatabaseCache;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Forms\Fields\Select;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\Graphs\Models\DataSet;
use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\di;
use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;

class CpuSmallGraph extends  BaseSmallGraph
{
    protected $id = 'cpuSmallGraph';
    protected $name = 'cpuSmallGraph';

    public function initContent()
    {
        //yAxes
        $this->updateChartScale('yAxes', [
            [
                'scaleLabel' => [
                    'display'     => $this->displayScaleLabel,
                    'labelString' => sl('lang')->tr("CPU %")
                ],
                'ticks'      => [
                    'beginAtZero' => true,
                    'fontSize' => 10
                ],
            ]]);
        //Tooltip
        $this->updateChartOption('tooltips', [
            'callbacks' => [
                'label' => 'mgTooltipCpu'
            ]
        ]);
        //xAxes
        $this->updateChartScale('xAxes', [
            [
                'display'   => false
            ]]);

        $this->configChartsSettings->timeframe = 'day';
    }

    public function prepareAjaxData()
    {
        session_write_close();
        $labels     = [];
        $dataSets   = [
            'cpu'   => []
        ];
        foreach ($this->rrdDataQuery() as $rrd)
        {
            $labels[]           = $rrd->date;
            $dataSets['cpu'][]  = (isset($rrd->cpu) ? (float)$rrd->cpu : 0) * 100;
        }
        //Labels
        $this->setLabels($labels);
        //CPU Usage
        $lang    = sl('lang');
        $dataSet = new DataSet();
        $dataSet->setTitle($lang->tr('CPU Usage'))
            ->setData($dataSets['cpu'])
            ->setConfigurationDataSet([
                "backgroundColor" => "rgba(174, 198, 57, 0.79)",
                "borderColor"     => "rgba(174, 198, 57, 1)"
            ]);
        $this->addDataSet($dataSet);
    }


}
