Initial commit: Modular Deploymentcenter platform
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace Deploymentcenter\Modules\Watchdog;
|
||||
|
||||
use PDO;
|
||||
|
||||
class MonitorRepo
|
||||
{
|
||||
private PDO $db;
|
||||
|
||||
public function __construct(PDO $db)
|
||||
{
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
public function getAllMonitors(): array
|
||||
{
|
||||
$stmt = $this->db->query('SELECT * FROM watchdog_monitors ORDER BY group_key ASC, source ASC');
|
||||
return $stmt->fetchAll() ?: [];
|
||||
}
|
||||
|
||||
public function getMonitor(string $source, string $instance = 'default'): ?array
|
||||
{
|
||||
$stmt = $this->db->prepare('SELECT * FROM watchdog_monitors WHERE source = :s AND instance = :i');
|
||||
$stmt->execute([':s' => $source, ':i' => $instance]);
|
||||
$row = $stmt->fetch();
|
||||
return $row ?: null;
|
||||
}
|
||||
|
||||
public function upsertHeartbeat(
|
||||
string $source,
|
||||
string $instance,
|
||||
string $type,
|
||||
int $intervalSec,
|
||||
$metrics,
|
||||
string $status,
|
||||
?string $message,
|
||||
?string $groupKey = null,
|
||||
?string $os = null
|
||||
): array {
|
||||
$nowUtc = date('Y-m-d H:i:s');
|
||||
$metricsJson = is_array($metrics) || is_object($metrics) ? json_encode($metrics, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : null;
|
||||
$state = ($status === 'ok') ? 'up' : (($status === 'warning') ? 'warning' : 'down');
|
||||
|
||||
$stmt = $this->db->prepare('
|
||||
INSERT INTO watchdog_monitors (
|
||||
source, instance, type, state, expected_interval_sec, last_seen_utc,
|
||||
last_status, last_message, metrics_json, group_key, os, created_utc, updated_utc
|
||||
) VALUES (
|
||||
:source, :instance, :type, :state, :interval, :now,
|
||||
:last_status, :message, :metrics, :group_key, :os, :now, :now
|
||||
)
|
||||
ON DUPLICATE KEY UPDATE
|
||||
state = VALUES(state),
|
||||
expected_interval_sec = VALUES(expected_interval_sec),
|
||||
last_seen_utc = VALUES(last_seen_utc),
|
||||
last_status = VALUES(last_status),
|
||||
last_message = VALUES(last_message),
|
||||
metrics_json = VALUES(metrics_json),
|
||||
group_key = COALESCE(VALUES(group_key), group_key),
|
||||
os = COALESCE(VALUES(os), os),
|
||||
updated_utc = VALUES(updated_utc)
|
||||
');
|
||||
|
||||
$stmt->execute([
|
||||
':source' => $source,
|
||||
':instance' => $instance,
|
||||
':type' => $type,
|
||||
':state' => $state,
|
||||
':interval' => $intervalSec,
|
||||
':now' => $nowUtc,
|
||||
':last_status' => $status,
|
||||
':message' => $message,
|
||||
':metrics' => $metricsJson,
|
||||
':group_key' => $groupKey,
|
||||
':os' => $os,
|
||||
]);
|
||||
|
||||
return $this->getMonitor($source, $instance);
|
||||
}
|
||||
|
||||
public function updateState(int $id, string $state, ?string $reason = null): bool
|
||||
{
|
||||
$stmt = $this->db->prepare('UPDATE watchdog_monitors SET state = :state, metric_state_reason = :reason, updated_utc = NOW() WHERE id = :id');
|
||||
return $stmt->execute([':state' => $state, ':reason' => $reason, ':id' => $id]);
|
||||
}
|
||||
|
||||
public function setMaintenance(string $source, string $instance, ?string $untilUtc): bool
|
||||
{
|
||||
$state = $untilUtc ? 'maintenance' : 'up';
|
||||
$stmt = $this->db->prepare('UPDATE watchdog_monitors SET state = :state, suppress_until_utc = :until, updated_utc = NOW() WHERE source = :s AND instance = :i');
|
||||
return $stmt->execute([':state' => $state, ':until' => $untilUtc, ':s' => $source, ':i' => $instance]);
|
||||
}
|
||||
|
||||
public function deleteMonitor(string $source, string $instance = 'default'): bool
|
||||
{
|
||||
$stmt = $this->db->prepare('DELETE FROM watchdog_monitors WHERE source = :s AND instance = :i');
|
||||
return $stmt->execute([':s' => $source, ':i' => $instance]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user