Procházet zdrojové kódy

move parsing logic into a class

Henry Whitaker před 2 roky
rodič
revize
55726f1657
2 změnil soubory, kde provedl 73 přidání a 41 odebrání
  1. 69 0
      api/functions/UptimeKumaMetrics.php
  2. 4 41
      api/homepage/uptime_kuma.php

+ 69 - 0
api/functions/UptimeKumaMetrics.php

@@ -0,0 +1,69 @@
+<?php
+
+class UptimeKumaMetrics
+{
+    protected string $raw;
+    private array $monitors = [];
+    private array $latencies = [];
+
+    public function __construct(string $raw)
+    {
+        $this->raw = $raw;
+    }
+
+    public function process(): self
+    {
+        $processed = explode(PHP_EOL, $this->raw);
+
+        $monitors = array_filter($processed, function (string $item) {
+            return str_starts_with($item, 'monitor_status');
+        });
+        $latencies = array_filter($processed, function (string $item) {
+            return str_starts_with($item, 'monitor_response_time');
+        });
+        
+        $monitors = array_map(function (string $item) {
+            try {
+                return $this->parseMonitorStatus($item);
+            } catch (Exception $e) {
+                // do nothing when monitor is disabled
+            }
+        }, $monitors);
+        $this->monitors = array_values(array_filter($monitors));
+
+        return $this;
+    }
+
+    public function getMonitors(): array
+    {
+        return $this->monitors;
+    }
+
+    private function parseMonitorStatus(string $status): array
+    {  
+        if (substr($status, -1) === '2') {
+			throw new Exception("monitor diasbled");
+		}
+
+		$up = (substr($status, -1)) == '0' ? false : true;
+		$status = substr($status, 15);
+		$status = substr($status, 0, -4);
+		$status = explode(',', $status);
+		$data = [
+			'name' => $this->getStringBetweenQuotes($status[0]),
+			'url' => $this->getStringBetweenQuotes($status[2]),
+			'type' => $this->getStringBetweenQuotes($status[1]),
+			'status' => $up,
+		];
+
+		return $data;
+    }
+
+    private function getStringBetweenQuotes(string $input): string
+	{
+		if (preg_match('/"(.*?)"/', $input, $match) == 1) {
+			return $match[1];
+		}
+		return '';
+	} 
+}

+ 4 - 41
api/homepage/uptime_kuma.php

@@ -88,19 +88,10 @@ trait UptimeKumaHomepageItem
 
 			$body = $response->getBody()->getContents();
 
-			$body = explode(PHP_EOL, $body);
-			$body = array_filter($body, function (string $item) {
-				return str_starts_with($item, 'monitor_status');
-			});
-			$body = array_map(function (string $item) {
-				try {
-					return $this->parseUptimeKumaStatus($item);
-				} catch (Exception $e) {
-					// do nothing when monitor is disabled
-				}
-			}, $body);
+			$metrics = (new UptimeKumaMetrics($body))->process();
+
 			$api = [
-				'data' => array_values(array_filter($body)),
+				'data' => $metrics->getMonitors(),
 				'options' => [
 					'title' => $this->config['homepageUptimeKumaHeader'],
 					'titleToggle' => $this->config['homepageUptimeKumaHeaderToggle'],
@@ -130,32 +121,4 @@ trait UptimeKumaHomepageItem
 
 		return static::$kumaClient;
 	}
-
-	private function parseUptimeKumaStatus(string $status): array
-	{
-		if (substr($status, -1) === '2') {
-			throw new Exception("monitor diasbled");
-		}
-
-		$up = (substr($status, -1)) == '0' ? false : true;
-		$status = substr($status, 15);
-		$status = substr($status, 0, -4);
-		$status = explode(',', $status);
-		$data = [
-			'name' => $this->getStringBetweenQuotes($status[0]),
-			'url' => $this->getStringBetweenQuotes($status[2]),
-			'type' => $this->getStringBetweenQuotes($status[1]),
-			'status' => $up,
-		];
-
-		return $data;
-	}
-
-	private function getStringBetweenQuotes(string $input): string
-	{
-		if (preg_match('/"(.*?)"/', $input, $match) == 1) {
-			return $match[1];
-		}
-		return '';
-	} 
-}
+}