type = $type;
$this->filter = $filter;
(empty($randomId) === true) ? $this->randomId = uniqid() : $this->randomId = $randomId;
$this->refresh = $refresh;
$this->width = $width;
$this->height = $height;
$this->search = $search;
}
/**
* Run.
*
* @return void
*/
public function run()
{
ui_require_css_file('heatmap');
$settings = [
'type' => 'POST',
'dataType' => 'html',
'url' => ui_get_full_url(
'ajax.php',
false,
false,
false
),
'data' => [
'page' => 'operation/heatmap',
'method' => 'showHeatmap',
'randomId' => $this->randomId,
'type' => $this->type,
'filter' => $this->filter,
'refresh' => $this->refresh,
'search' => $this->search,
],
];
echo '
';
?>
';
}
/**
* Setter for filter
*
* @param array $filter Filter.
*
* @return void
*/
public function setFilter(array $filter)
{
$this->filter = $filter;
}
/**
* Setter for type
*
* @param integer $type Type.
*
* @return void
*/
public function setType(int $type)
{
$this->type = $type;
}
/**
* Setter for refresh
*
* @param integer $refresh Refresh.
*
* @return void
*/
public function setRefresh(int $refresh)
{
$this->refresh = $refresh;
}
/**
* Getter for randomId
*
* @return string
*/
public function getRandomId()
{
return $this->randomId;
}
/**
* Get all agents
*
* @return array
*/
protected function getAllAgents()
{
$filter['disabled'] = 0;
if (empty($this->search) === false) {
$filter['search'] = ' AND alias LIKE "%'.$this->search.'%"';
}
if (empty($this->filter) === false) {
$filter['id_grupo'] = current($this->filter);
}
// All agents.
$result = agents_get_agents(
$filter,
[
'id_agente as id',
'alias',
'id_grupo',
'normal_count',
'warning_count',
'critical_count',
'unknown_count',
'notinit_count',
'total_count',
'fired_count',
],
'AR',
[
'field' => 'id_grupo,id_agente',
'order' => 'ASC',
]
);
$agents = [];
// Agent status.
foreach ($result as $key => $agent) {
if ($agent['total_count'] === 0 || $agent['total_count'] === $agent['notinit_count']) {
$status = 'notinit';
} else if ($agent['critical_count'] > 0) {
$status = 'critical';
} else if ($agent['warning_count'] > 0) {
$status = 'warning';
} else if ($agent['unknown_count'] > 0) {
$status = 'unknown';
} else {
$status = 'normal';
}
$agents[$key] = $agent;
$agents[$key]['status'] = $status;
}
// -------------------Agent generator--------------------
$a = 1;
$agents = [];
$total = 1010;
while ($a <= $total) {
$agents[$a]['id'] = $a;
$agents[$a]['status'] = $this->statusColour(rand(4, 0));
$agents[$a]['id_grupo'] = ceil($a / 20);
$a++;
}
// -------------------------------------------
return $agents;
}
/**
* GetDataJson
*
* @return json
*/
public function getDataJson()
{
$return = $this->getAllAgents();
echo json_encode($return);
return '';
}
/**
* Get class by status
*
* @param integer $status Status.
*
* @return string
*/
protected function statusColour(int $status)
{
switch ($status) {
case AGENT_STATUS_CRITICAL:
$return = 'critical';
break;
case AGENT_STATUS_WARNING:
$return = 'warning';
break;
case AGENT_STATUS_UNKNOWN:
$return = 'unknown';
break;
case AGENT_STATUS_NOT_INIT:
$return = 'notinit';
break;
case AGENT_STATUS_NORMAL:
default:
$return = 'normal';
break;
}
return $return;
}
/**
* Get max. number of y-axis
*
* @param integer $total Total.
* @param float $relation Aspect relation.
*
* @return integer
*/
protected function getYAxis(int $total, float $relation)
{
$yAxis = sqrt(($total / $relation));
return $yAxis;
}
/**
* Checks if target method is available to be called using AJAX.
*
* @param string $method Target method.
*
* @return boolean True allowed, false not.
*/
public function ajaxMethod(string $method):bool
{
return in_array($method, $this->AJAXMethods);
}
/**
* ShowHeatmap
*
* @return void
*/
public function showHeatmap()
{
switch ($this->type) {
case 0:
default:
$result = $this->getAllAgents();
break;
}
$scale = ($this->width / $this->height);
$Yaxis = $this->getYAxis(count($result), $scale);
$Xaxis = (int) ceil($Yaxis * $scale);
$Yaxis = ceil($Yaxis);
$viewBox = sprintf(
'0 0 %d %d',
$Xaxis,
$Yaxis
);
echo '
';
// Dialog.
echo '
';
}
}