From 1cb4bc928768bfa468d778914d65237bc122b43c Mon Sep 17 00:00:00 2001 From: spiral Date: Sun, 1 Aug 2021 15:22:23 -0400 Subject: [PATCH] Expose git version in 'pk;stats' and '/v1/meta' --- .dockerignore | 1 + .gitignore | 1 + Dockerfile | 1 + .../Controllers/v1/MetaController.cs | 1 + PluralKit.API/Program.cs | 9 ++++--- PluralKit.Bot/Commands/Misc.cs | 7 ++++-- PluralKit.Bot/Init.cs | 3 +++ PluralKit.Core/PluralKit.Core.csproj | 12 ++++++++++ PluralKit.Core/Services/BuildInfoService.cs | 24 +++++++++++++++++++ 9 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 PluralKit.Core/Services/BuildInfoService.cs diff --git a/.dockerignore b/.dockerignore index ebc82ce6..ab9de4ed 100644 --- a/.dockerignore +++ b/.dockerignore @@ -6,6 +6,7 @@ !Myriad/ !PluralKit.sln !nuget.config +!.git # Re-exclude host build artifact directories **/bin diff --git a/.gitignore b/.gitignore index 4da63524..33fbce6f 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,4 @@ pluralkit.*.conf # Generated logs/ +.version diff --git a/Dockerfile b/Dockerfile index e92181fe..f9343108 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,6 +8,7 @@ COPY PluralKit.API/PluralKit.API.csproj /app/PluralKit.API/ COPY PluralKit.Bot/PluralKit.Bot.csproj /app/PluralKit.Bot/ COPY PluralKit.Core/PluralKit.Core.csproj /app/PluralKit.Core/ COPY PluralKit.Tests/PluralKit.Tests.csproj /app/PluralKit.Tests/ +COPY .git/ /app/.git RUN dotnet restore PluralKit.sln # Copy the rest of the code and build diff --git a/PluralKit.API/Controllers/v1/MetaController.cs b/PluralKit.API/Controllers/v1/MetaController.cs index 4dd2a52e..bad8b396 100644 --- a/PluralKit.API/Controllers/v1/MetaController.cs +++ b/PluralKit.API/Controllers/v1/MetaController.cs @@ -32,6 +32,7 @@ namespace PluralKit.API var o = new JObject(); o.Add("shards", shards.ToJSON()); + o.Add("version", BuildInfoService.Version); return Ok(o); } diff --git a/PluralKit.API/Program.cs b/PluralKit.API/Program.cs index 7f7b7133..67860041 100644 --- a/PluralKit.API/Program.cs +++ b/PluralKit.API/Program.cs @@ -1,4 +1,6 @@ -using Autofac.Extensions.DependencyInjection; +using System.Threading.Tasks; + +using Autofac.Extensions.DependencyInjection; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; @@ -12,10 +14,11 @@ namespace PluralKit.API { public class Program { - public static void Main(string[] args) + public static async Task Main(string[] args) { InitUtils.InitStatic(); - CreateHostBuilder(args).Build().Run(); + await BuildInfoService.LoadVersion(); + await CreateHostBuilder(args).Build().RunAsync(); } public static IHostBuilder CreateHostBuilder(string[] args) => diff --git a/PluralKit.Bot/Commands/Misc.cs b/PluralKit.Bot/Commands/Misc.cs index 7e6ba494..24881b6d 100644 --- a/PluralKit.Bot/Commands/Misc.cs +++ b/PluralKit.Bot/Commands/Misc.cs @@ -92,7 +92,8 @@ namespace PluralKit.Bot { var process = Process.GetCurrentProcess(); var memoryUsage = process.WorkingSet64; - var shardUptime = SystemClock.Instance.GetCurrentInstant() - shardInfo.LastConnectionTime; + var now = SystemClock.Instance.GetCurrentInstant(); + var shardUptime = now - shardInfo.LastConnectionTime; var embed = new EmbedBuilder(); if (messagesReceived != null) embed.Field(new("Messages processed",$"{messagesReceived.OneMinuteRate * 60:F1}/m ({messagesReceived.FifteenMinuteRate * 60:F1}/m over 15m)", true)); @@ -105,7 +106,9 @@ namespace PluralKit.Bot { .Field(new("CPU usage", $"{_cpu.LastCpuMeasure:P1}", true)) .Field(new("Memory usage", $"{memoryUsage / 1024 / 1024} MiB", true)) .Field(new("Latency", $"API: {apiLatency.TotalMilliseconds:F0} ms, shard: {shardInfo.ShardLatency.Milliseconds} ms", true)) - .Field(new("Total numbers", $"{totalSystems:N0} systems, {totalMembers:N0} members, {totalGroups:N0} groups, {totalSwitches:N0} switches, {totalMessages:N0} messages")); + .Field(new("Total numbers", $"{totalSystems:N0} systems, {totalMembers:N0} members, {totalGroups:N0} groups, {totalSwitches:N0} switches, {totalMessages:N0} messages")) + .Timestamp(now.ToDateTimeOffset().ToString("O")) + .Footer(new($"PluralKit {BuildInfoService.Version} • https://github.com/xSke/PluralKit"));; await ctx.Rest.EditMessage(msg.ChannelId, msg.Id, new MessageEditRequest {Content = "", Embed = embed.Build()}); } diff --git a/PluralKit.Bot/Init.cs b/PluralKit.Bot/Init.cs index 7ccd9fe0..a93ed6a3 100644 --- a/PluralKit.Bot/Init.cs +++ b/PluralKit.Bot/Init.cs @@ -30,6 +30,9 @@ namespace PluralKit.Bot return RunWrapper(services, async ct => { + // init version service + await BuildInfoService.LoadVersion(); + var logger = services.Resolve().ForContext(); // Initialize Sentry SDK, and make sure it gets dropped at the end diff --git a/PluralKit.Core/PluralKit.Core.csproj b/PluralKit.Core/PluralKit.Core.csproj index 47e2d6e9..e14b1fa4 100644 --- a/PluralKit.Core/PluralKit.Core.csproj +++ b/PluralKit.Core/PluralKit.Core.csproj @@ -48,4 +48,16 @@ + + + + + + + + + diff --git a/PluralKit.Core/Services/BuildInfoService.cs b/PluralKit.Core/Services/BuildInfoService.cs new file mode 100644 index 00000000..ee32c8b0 --- /dev/null +++ b/PluralKit.Core/Services/BuildInfoService.cs @@ -0,0 +1,24 @@ +using System.IO; +using System.Reflection; +using System.Threading.Tasks; + +namespace PluralKit.Core +{ + public static class BuildInfoService + { + public static string Version { get; private set; } + + public static async Task LoadVersion() + { + using (var stream = typeof(BuildInfoService).Assembly.GetManifestResourceStream("version")) + { + // if this happens, something broke + if (stream == null) Version = "(unknown version) "; + else using (var reader = new StreamReader(stream)) Version = await reader.ReadToEndAsync(); + } + + // cheap hack to remove newline + Version = Version.Remove(Version.Length - 1); + } + } +} \ No newline at end of file