From d0004e9fff7f1f843b5f4552a141fd8303c4f3e4 Mon Sep 17 00:00:00 2001 From: Marcel Baumgartner Date: Fri, 9 Jun 2023 15:01:51 +0200 Subject: [PATCH] Added uptime service --- Moonlight/App/Helpers/Formatter.cs | 12 ++++++++++++ .../App/Services/Background/UptimeService.cs | 11 +++++++++++ Moonlight/Program.cs | 2 ++ Moonlight/Shared/Views/Admin/Sys/Index.razor | 19 +++++++++++++++++++ 4 files changed, 44 insertions(+) create mode 100644 Moonlight/App/Services/Background/UptimeService.cs diff --git a/Moonlight/App/Helpers/Formatter.cs b/Moonlight/App/Helpers/Formatter.cs index 929a7bb9..31834ccd 100644 --- a/Moonlight/App/Helpers/Formatter.cs +++ b/Moonlight/App/Helpers/Formatter.cs @@ -17,6 +17,18 @@ public static class Formatter return $"{t.Hours}h {t.Minutes}m {t.Seconds}s"; } } + + public static string FormatUptime(TimeSpan t) + { + if (t.Days > 0) + { + return $"{t.Days}d {t.Hours}h {t.Minutes}m {t.Seconds}s"; + } + else + { + return $"{t.Hours}h {t.Minutes}m {t.Seconds}s"; + } + } private static double Round(this double d, int decimals) { diff --git a/Moonlight/App/Services/Background/UptimeService.cs b/Moonlight/App/Services/Background/UptimeService.cs new file mode 100644 index 00000000..c55ceaf7 --- /dev/null +++ b/Moonlight/App/Services/Background/UptimeService.cs @@ -0,0 +1,11 @@ +namespace Moonlight.App.Services.Background; + +public class UptimeService +{ + public DateTime StartTimestamp { get; private set; } + + public UptimeService() + { + StartTimestamp = DateTime.UtcNow; + } +} \ No newline at end of file diff --git a/Moonlight/Program.cs b/Moonlight/Program.cs index ab7aee4d..63298719 100644 --- a/Moonlight/Program.cs +++ b/Moonlight/Program.cs @@ -167,6 +167,7 @@ namespace Moonlight builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); + builder.Services.AddSingleton(); // Third party services builder.Services.AddBlazorTable(); @@ -202,6 +203,7 @@ namespace Moonlight _ = app.Services.GetRequiredService(); _ = app.Services.GetRequiredService(); _ = app.Services.GetRequiredService(); + _ = app.Services.GetRequiredService(); // Discord bot service //var discordBotService = app.Services.GetRequiredService(); diff --git a/Moonlight/Shared/Views/Admin/Sys/Index.razor b/Moonlight/Shared/Views/Admin/Sys/Index.razor index f740a7ed..6f54a54e 100644 --- a/Moonlight/Shared/Views/Admin/Sys/Index.razor +++ b/Moonlight/Shared/Views/Admin/Sys/Index.razor @@ -2,8 +2,10 @@ @using Moonlight.Shared.Components.Navigations @using Moonlight.App.Helpers +@using Moonlight.App.Services.Background @inject HostSystemHelper HostSystemHelper +@inject UptimeService UptimeService @@ -70,5 +72,22 @@ +
+
+
+ + Uptime + +
+
+ + Moonlight is since + + @(Formatter.FormatUptime(DateTime.UtcNow - UptimeService.StartTimestamp)) + + +
+
+
\ No newline at end of file