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 ''; $groups = []; $contX = 0; $contY = 0; foreach ($result as $value) { echo ''; $contX++; if ($contX >= $Xaxis) { $contY++; $contX = 0; } if (empty($groups[$value['id_grupo']]) === true) { $groups[$value['id_grupo']] = 1; } else { $groups[$value['id_grupo']] += 1; } } ?> '; echo ''; echo ''; foreach ($groups as $group) { if (($x_back + $group) <= $Xaxis) { $x_position = ($x_back + $group); $y_position = $y_back; $points = sprintf( '%d,%d %d,%d %d,%d %d,%d', $x_position, $y_back, $x_back, $y_back, $x_back, ($y_position + 1), $x_position, ($y_position + 1) ); echo ''; $x_back = $x_position; if ($x_position === $Xaxis) { $points = sprintf( '%d,%d %d,%d', $x_position, $y_back, $x_position, ($y_back + 1) ); echo ''; $y_back ++; $x_back = 0; } } else { $round = (int) floor(($x_back + $group) / $Xaxis); $y_position = ($round + $y_back); // Top of the first line. $points = sprintf( '%d,%d %d,%d %d,%d', $x_back, ($y_back + 1), $x_back, $y_back, $Xaxis, $y_back ); echo ''; if ($round === 1) { // One line. $x_position = (($x_back + $group) - $Xaxis); // Bottom of last line. $points = sprintf( '%d,%d %d,%d', 0, ($y_position + 1), $x_position, ($y_position + 1) ); echo ''; } else { // Two or more lines. $x_position = (($x_back + $group) - ($Xaxis * $round)); if ($x_position === 0) { $x_position = $Xaxis; } // Bottom of last line. $points = sprintf( '%d,%d %d,%d', 0, ($y_position + 1), $x_position, ($y_position + 1) ); echo ''; } if ($x_position === $Xaxis) { $x_position = 0; } $x_back = $x_position; $y_back = $y_position; } } echo ''; // Dialog. echo ''; } }