<?php
namespace App\Dashboard\Widget;

class ServerLoadLogs extends \App\Dashboard\Widget\Charts\ServerStats
{

	/**
	 * Generate the options form
	 * @return Object
	 */
	protected function populateLocalOptionsForm(\Engine\Form\Container\GeneralAbstract $form)
	{
		return $form;
	}

	/**
	 * Get data from DB and return.
	 * @param String $selectDate
	 * @return Array
	 */
	protected function getServerStatsData($selectDate)
	{
		$calledClass = get_called_class();
		$className = stripClassNamespace($calledClass);
		
		// load lang
		$loaderLang = \App\Loader_Lang::getInstance();
		$loaderLang->loadAppFile("_customLoad/dashboard/widgets/_general.lang.php");
		$loaderLang->loadAppFile("_customLoad/dashboard/widgets/{$className}.lang.php");
		$_LANG = $loaderLang->getAll();

		$output = [];

		global $DBTServerStats;

		$db = \DB::getInstance();

		$startTimeToday = \Engine\DateTime::createFromIsoDatetime($selectDate." 00:00:00")->formatTime();
		$currentTimeToday = \Engine\DateTime::createFromIsoDatetime($selectDate." 23:59:59")->formatTime();

		$query = "SELECT `timestamp`, `serverLoad` AS value FROM `$DBTServerStats` WHERE timestamp >=? AND  timestamp <=? ORDER BY timestamp ASC";
		$serverStatsReport = $db->Execute($query, [$startTimeToday, $currentTimeToday]);
		$rows = $serverStatsReport->GetRows();

		// check sql
		if (!count($rows)) {
			$output['errorMessage']=$_LANG['dashboard']['widgetsList'][__NAMESPACE__."\\ServerLoadLogs"]['msgs']['noDBData'];
			return $output;
		}

		$output['values'] = $rows;

		return $output;
	}

}
