Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions src/Actions/Dto/GetTrendResponseDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

declare(strict_types=1);

namespace BytesCommerce\ZabbixApi\Actions\Dto;

final readonly class GetTrendResponseDto
{
/**
* @param list<TrendItemDto> $items
*/
public function __construct(
public array $items = [],
) {
}

/**
* @param array<int, array<string, mixed>> $data
*/
public static function fromArray(array $data): self
{
$items = [];
foreach ($data as $item) {
if (!is_array($item)) {
continue;
}
$items[] = TrendItemDto::fromArray($item);
}

return new self($items);
}

public function isEmpty(): bool
{
return $this->items === [];
}

public function count(): int
{
return count($this->items);
}

/**
* @return list<float>
*/
public function getValues(): array
{
return array_map(static fn (TrendItemDto $item) => $item->valueAvg, $this->items);
}

/**
* @return list<float>
*/
public function getMinValues(): array
{
return array_map(static fn (TrendItemDto $item) => $item->valueMin, $this->items);
}

/**
* @return list<float>
*/
public function getMaxValues(): array
{
return array_map(static fn (TrendItemDto $item) => $item->valueMax, $this->items);
}

public function getAverageValue(): float
{
$values = $this->getValues();
if ($values === []) {
return 0.0;
}

return array_sum($values) / count($values);
}

public function getMinValue(): float
{
$values = $this->getMinValues();
if ($values === []) {
return 0.0;
}

return min($values);
}

public function getMaxValue(): float
{
$values = $this->getMaxValues();
if ($values === []) {
return 0.0;
}

return max($values);
}
}
45 changes: 45 additions & 0 deletions src/Actions/Dto/TrendItemDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace BytesCommerce\ZabbixApi\Actions\Dto;

final readonly class TrendItemDto
{
public function __construct(
public string $itemid,
public int $clock,
public int $num,
public float $valueMin,
public float $valueAvg,
public float $valueMax,
) {
}

/**
* @param array<string, mixed> $data
*/
public static function fromArray(array $data): self
{
$itemid = $data['itemid'] ?? '';
$clock = $data['clock'] ?? 0;
$num = $data['num'] ?? 0;
$valueMin = $data['value_min'] ?? 0;
$valueAvg = $data['value_avg'] ?? 0;
$valueMax = $data['value_max'] ?? 0;

return new self(
itemid: is_string($itemid) ? $itemid : '',
clock: is_int($clock) ? $clock : 0,
num: is_int($num) ? $num : 0,
valueMin: is_numeric($valueMin) ? (float) $valueMin : 0.0,
valueAvg: is_numeric($valueAvg) ? (float) $valueAvg : 0.0,
valueMax: is_numeric($valueMax) ? (float) $valueMax : 0.0,
);
}

public function getTimestamp(): \DateTimeImmutable
{
return (new \DateTimeImmutable())->setTimestamp($this->clock);
}
}
191 changes: 191 additions & 0 deletions src/Actions/Trend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
<?php

declare(strict_types=1);

namespace BytesCommerce\ZabbixApi\Actions;

use BytesCommerce\ZabbixApi\Actions\Dto\GetTrendResponseDto;
use BytesCommerce\ZabbixApi\Enums\OutputEnum;
use BytesCommerce\ZabbixApi\Enums\ZabbixAction;

final class Trend extends AbstractAction
{
public static function getActionPrefix(): string
{
return 'trend';
}

/**
* @param list<string> $itemIds
* @param array<string, mixed> $additionalParams
*/
public function get(
array $itemIds,
?int $timeFrom = null,
?int $timeTill = null,
?int $limit = null,
string $sortField = 'clock',
string $sortOrder = 'DESC',
bool $preserveKeys = false,
array $additionalParams = [],
): GetTrendResponseDto {
if ($itemIds === []) {
return new GetTrendResponseDto([]);
}

$params = [
'output' => OutputEnum::EXTEND->value,
'itemids' => $itemIds,
'sortfield' => $sortField,
'sortorder' => $sortOrder,
'preservekeys' => $preserveKeys,
...$additionalParams,
];

if ($timeFrom !== null) {
$params['time_from'] = $timeFrom;
}

if ($timeTill !== null) {
$params['time_till'] = $timeTill;
}

if ($limit !== null) {
$params['limit'] = $limit;
}

$result = $this->client->call(ZabbixAction::TREND_GET, $params);

/** @var array<int, array<string, mixed>> $trendData */
$trendData = is_array($result) ? $result : [];

return GetTrendResponseDto::fromArray($trendData);
}

/**
* @param list<string> $itemIds
*/
public function getLast24Hours(
array $itemIds,
?int $limit = null,
): GetTrendResponseDto {
$now = time();
$twentyFourHoursAgo = $now - 86400;

return $this->get(
itemIds: $itemIds,
timeFrom: $twentyFourHoursAgo,
timeTill: $now,
limit: $limit,
);
}

/**
* @param list<string> $itemIds
*/
public function getLast7Days(
array $itemIds,
?int $limit = null,
): GetTrendResponseDto {
$now = time();
$sevenDaysAgo = $now - (7 * 86400);

return $this->get(
itemIds: $itemIds,
timeFrom: $sevenDaysAgo,
timeTill: $now,
limit: $limit,
);
}

/**
* @param list<string> $itemIds
*/
public function getLast30Days(
array $itemIds,
?int $limit = null,
): GetTrendResponseDto {
$now = time();
$thirtyDaysAgo = $now - (30 * 86400);

return $this->get(
itemIds: $itemIds,
timeFrom: $thirtyDaysAgo,
timeTill: $now,
limit: $limit,
);
}

/**
* @param list<string> $itemIds
* @param array<string, mixed> $filter
*/
public function getWithFilter(
array $itemIds,
array $filter,
?int $timeFrom = null,
?int $timeTill = null,
?int $limit = null,
): GetTrendResponseDto {
if ($itemIds === []) {
return new GetTrendResponseDto([]);
}

$params = [
'output' => OutputEnum::EXTEND->value,
'itemids' => $itemIds,
'filter' => $filter,
'sortfield' => 'clock',
'sortorder' => 'DESC',
];

if ($timeFrom !== null) {
$params['time_from'] = $timeFrom;
}

if ($timeTill !== null) {
$params['time_till'] = $timeTill;
}

if ($limit !== null) {
$params['limit'] = $limit;
}

$result = $this->client->call(ZabbixAction::TREND_GET, $params);

/** @var array<int, array<string, mixed>> $trendData */
$trendData = is_array($result) ? $result : [];

return GetTrendResponseDto::fromArray($trendData);
}

/**
* @param list<string> $itemIds
*/
public function count(
array $itemIds,
?int $timeFrom = null,
?int $timeTill = null,
): int {
if ($itemIds === []) {
return 0;
}

$params = [
'countOutput' => true,
'itemids' => $itemIds,
];

if ($timeFrom !== null) {
$params['time_from'] = $timeFrom;
}

if ($timeTill !== null) {
$params['time_till'] = $timeTill;
}

$result = $this->client->call(ZabbixAction::TREND_GET, $params);

return is_numeric($result) ? (int) $result : 0;
}
}
1 change: 1 addition & 0 deletions src/Enums/ZabbixAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ enum ZabbixAction: string
case ITEM_DELETE = 'item.delete';
case ITEM_GET = 'item.get';
case ITEM_UPDATE = 'item.update';
case TREND_GET = 'trend.get';
case TRIGGER_CREATE = 'trigger.create';
case TRIGGER_DELETE = 'trigger.delete';
case TRIGGER_GET = 'trigger.get';
Expand Down
Loading