<?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\ProxmoxAddon\App\UI\NodeDetail\Pages;

use ModulesGarden\ProxmoxAddon as main;
use ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\AdminArea;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Forms\Fields\Select;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Graphs\Line;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Graphs\Models\DataSet;

class ServerLoadGraph extends Line implements AdminArea
{

    use LoadAPIData;
    protected $id = 'serverLoadGraph';
    protected $name = 'serverLoadGraph';
    protected $graphSettingsEnabled = true;
    protected $graphSettingsKey = 'cpuGraphSettings';

    public function initContent()
    {

        $selectScope = new Select('timeframe');
        $selectScope->setAvailableValues([
            'hour' => 'Hour',
            'day'  => 'Day',
            'week' => 'Week',
            'year' => 'Year',
        ]);
        $selectScope->setDefaultValue('week');
        $this->addSettingField($selectScope);
        //Tooltip
        $this->updateChartOption('tooltips', [
            'callbacks' => [
                'label' => 'mgTooltipServerLoad'
            ]
        ]);
    }

    public function prepareAjaxData()
    {
        if ($this->configChartsSettings->timeframe)
        {
            $this->timeframe = $this->configChartsSettings->timeframe;
        }
        $rrdata = main\Core\Helper\DatabaseCache::loadData(
            $this->graphSettingsKey .$this->timeframe. '_cacheData', [$this, 'loadApiData'], 30, true);

        $labels     = [];
        $dataSets   = [
            'loadavg' => []
        ];
        $dateFormat = in_array($this->timeframe, ['hour', 'day']) ? "H:i:s" : "Y-m-d";
        foreach ($rrdata as $rrd)
        {
            $labels[] = date($dateFormat, $rrd['time']);
            $dataSets['loadavg'][]    = (float)$rrd['loadavg'];
        }
        //Labels
        $this->setLabels($labels);
        //Server Load
        $lang    = main\Core\ServiceLocator::call('lang');
        $dataSet = new DataSet();
        $dataSet->setTitle($lang->absoluteT('Server Load'))
            ->setData($dataSets['loadavg'])
            ->setConfigurationDataSet([
                "backgroundColor" => 'rgba(174, 198, 57, 0.79)',
                "borderColor"     => 'rgba(174, 198, 57, 1)',
            ]);
        $this->addDataSet($dataSet);
    }
}
