Skip to content

Commit

Permalink
Fix/information on windows (#6)
Browse files Browse the repository at this point in the history
* Separate the first statistic stuff

* Update the method of getting the bot start time

* Add missing module to active modules

* Fix a load of stuff

* Update CHANGELOG

* Update strings
  • Loading branch information
Sella-GH authored Mar 17, 2024
1 parent ab6ea40 commit ef35074
Show file tree
Hide file tree
Showing 14 changed files with 229 additions and 231 deletions.
7 changes: 4 additions & 3 deletions AzzyBot/Customization/Core/StringsCore.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
"EmbedAzzyStats15MinLoadTitle": "15-Min-Load",
"EmbedAzzyStats15MinLoadDesc": "**%VALUE%**",
"EmbedAzzyStatsRamUsageTitle": "RAM usage",
"EmbedAzzyStatsRamUsageDesc": "Used: **%USED%** GB\nAzzyBot: **%BOT%** GB\nTotal: **%TOTAL%** GB",
"EmbedAzzyStatsRamUsageDesc": "Used: **%USED%** GB\nTotal: **%TOTAL%** GB",
"EmbedAzzyStatsRamUsageAzzyTitle": "AzzyBot RAM usage",
"EmbedAzzyStatsRamUsageAzzyDesc": "**%BOT%** GB",
"EmbedAzzyStatsDiskUsageTitle": "Disk usage",
"EmbedAzzyStatsDiskUsageDesc": "Used: **%USED%** GB\nTotal: **%TOTAL%** GB",
"EmbedAzzyStatsNetworkUsageTitle": "Network \"%NAME%\"",
"EmbedAzzyStatsNetworkUsageDesc": "Receive: **%RECEIVE%** KB/s\nTransmit: **%TRANSMIT%** KB/s",
"EmbedAzzyStatsNotAvailableTitle": "Command Not Available",
"EmbedAzzyStatsNotAvailableDesc": "This command is not available on non-Linux systems!",
"EmbedAzzyStatsMoreStats": "More stats are only available on linux systems.",
"EmbedAzzyInfoTitle": "Azzy Information",
"EmbedAzzyInfoBotUptime": "Bot Uptime",
"EmbedAzzyInfoBotName": "Name",
Expand Down
124 changes: 124 additions & 0 deletions AzzyBot/Modules/Core/CoreAzzyStatsGeneral.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using AzzyBot.ExceptionHandling;
using AzzyBot.Settings;
using AzzyBot.Strings.Core;
using Microsoft.Extensions.Logging;

namespace AzzyBot.Modules.Core;

/// <summary>
/// Contains methods for getting general information of the bot.
/// </summary>
internal static class CoreAzzyStatsGeneral
{
/// <summary>
/// Gets the disk usage of the server.
/// </summary>
/// <returns>A string representing the disk usage in GB.</returns>
internal static string GetDiskUsage()
{
try
{
string diskUsage = string.Empty;

foreach (DriveInfo drive in DriveInfo.GetDrives())
{
if (drive.IsReady && drive.Name == "/")
{
double totalSizeGB = drive.TotalSize / (1024.0 * 1024.0 * 1024.0);
double freeSpaceGB = drive.TotalFreeSpace / (1024.0 * 1024.0 * 1024.0);
double usedSpaceGB = totalSizeGB - freeSpaceGB;

return CoreStringBuilder.GetEmbedAzzyStatsDiskUsageDesc(Math.Round(usedSpaceGB, 2), Math.Round(totalSizeGB, 2));
}
}

return diskUsage;
}
catch (DriveNotFoundException)
{
ExceptionHandler.LogMessage(LogLevel.Error, "Main drive not found");
throw;
}
}

internal static long GetSystemUptime()
{
TimeSpan uptime = new(Environment.TickCount64);
DateTime dateTime = DateTime.Now.AddMinutes(-uptime.Minutes);

return CoreMisc.ConvertToUnixTime(dateTime);
}

internal static string GetActivatedModules()
{
string text = "- Core";

if (BaseSettings.ActivateAzuraCast)
text += "\n- AzuraCast";

if (BaseSettings.ActivateClubManagement)
text += "\n- ClubManagement";

if (BaseSettings.ActivateMusicStreaming)
text += "\n- MusicStreaming";

return text;
}

internal static string GetBotName => Assembly.GetExecutingAssembly().GetName().Name ?? "Bot name not found";

/// <summary>
/// Gets the memory usage only of the bot itself.
/// </summary>
/// <returns>The memory usage of the bot in GB.</returns>
internal static double GetBotMemoryUsage()
{
Process? process = Process.GetCurrentProcess();
double usedMemoryGB = process.WorkingSet64 / (1024.0 * 1024.0 * 1024.0);

process.Dispose();

return Math.Round(usedMemoryGB, 2);
}

internal static string GetBotUptime()
{
using Process azzy = Process.GetCurrentProcess();

return $"<t:{CoreMisc.ConvertToUnixTime(azzy.StartTime)}>";
}

internal static string GetBotEnvironment => (Program.GetDiscordClientId == 1217214768159653978) ? "Development" : "Production";
internal static string GetBotVersion => Assembly.GetExecutingAssembly().GetName().Version?.ToString(3) ?? "Azzy version not found";

internal static async Task<string> GetBotCommitAsync()
{
if (CoreModule.CommitLock is null)
return "Commit not found";

string commit = await CoreModule.CommitLock.GetFileContentAsync();
if (string.IsNullOrWhiteSpace(commit))
commit = "Commit not found";

return commit;
}

internal static async Task<string> GetBotCompileDateAsync()
{
if (CoreModule.BuildTimeLock is null)
return "CompileDate not found";

if (!DateTime.TryParse(await CoreModule.BuildTimeLock.GetFileContentAsync(), out DateTime dateTime))
return "CompileDate not found";

return $"<t:{CoreMisc.ConvertToUnixTime(dateTime)}>";
}

internal static string GetDotNetVersion => Environment.Version.ToString() ?? ".NET version not found";
internal static string GetDSharpNetVersion => Program.GetDiscordClientVersion;
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using AzzyBot.ExceptionHandling;
using AzzyBot.Modules.Core.Structs;
using AzzyBot.Settings;
using AzzyBot.Strings.Core;
using Microsoft.Extensions.Logging;

namespace AzzyBot.Modules.Core;

/// <summary>
/// Contains methods for getting the CPU usage of the server.
/// Contains methods for getting stats of the server on linux OS.
/// </summary>
internal static class ServerCpuUsage
internal static class CoreAzzyStatsLinux
{
/// <summary>
/// Gets the CPU usage of each core on the server.
Expand Down Expand Up @@ -158,13 +154,7 @@ internal static async Task<CpuLoadStruct> GetCpuLoadAsync()
throw;
}
}
}

/// <summary>
/// Contains methods for getting the memory usage of the server.
/// </summary>
internal static class ServerMemoryUsage
{
/// <summary>
/// Gets the total and used memory of the server.
/// </summary>
Expand Down Expand Up @@ -223,70 +213,7 @@ static long ParseValue(string line)
throw;
}
}
}

/// <summary>
/// Contains methods for getting the memory usage only of the bot itself.
/// </summary>
internal static class BotMemoryUsage
{
/// <summary>
/// Gets the memory usage only of the bot itself.
/// </summary>
/// <returns>The memory usage of the bot in GB.</returns>
internal static double GetMemoryUsage()
{
Process? process = Process.GetCurrentProcess();
double usedMemoryGB = process.WorkingSet64 / (1024.0 * 1024.0 * 1024.0);

process.Dispose();

return Math.Round(usedMemoryGB, 2);
}
}

/// <summary>
/// Contains methods for getting the disk usage of the server.
/// </summary>
internal static class ServerDiskUsage
{
/// <summary>
/// Gets the disk usage of the server.
/// </summary>
/// <returns>A string representing the disk usage in GB.</returns>
internal static string GetDiskUsage()
{
try
{
string diskUsage = string.Empty;

foreach (DriveInfo drive in DriveInfo.GetDrives())
{
if (drive.IsReady && drive.Name == "/")
{
double totalSizeGB = drive.TotalSize / (1024.0 * 1024.0 * 1024.0);
double freeSpaceGB = drive.TotalFreeSpace / (1024.0 * 1024.0 * 1024.0);
double usedSpaceGB = totalSizeGB - freeSpaceGB;

return CoreStringBuilder.GetEmbedAzzyStatsDiskUsageDesc(Math.Round(usedSpaceGB, 2), Math.Round(totalSizeGB, 2));
}
}

return diskUsage;
}
catch (DriveNotFoundException)
{
ExceptionHandler.LogMessage(LogLevel.Error, "Main drive not found");
throw;
}
}
}

/// <summary>
/// Contains methods for getting the network usage of the server.
/// </summary>
internal static class ServerNetworkUsage
{
/// <summary>
/// Gets the network usage of the server.
/// </summary>
Expand Down Expand Up @@ -358,13 +285,7 @@ private static async Task<Dictionary<string, NetworkStatsStruct>> ReadNetworkSta
throw;
}
}
}

/// <summary>
/// Contains methods for getting information about the server.
/// </summary>
internal static class ServerInfo
{
/// <summary>
/// Gets the system uptime of the server.
/// </summary>
Expand Down Expand Up @@ -396,64 +317,3 @@ internal static async Task<long> GetSystemUptimeAsync()
}
}
}

/// <summary>
/// Contains methods for getting information about the bot.
/// </summary>
internal static class BotInfo
{
private static DateTime StartTime = DateTime.MinValue;

/// <summary>
/// Sets the start time of the bot.
/// </summary>
internal static void SetStartTime()
{
if (StartTime == DateTime.MinValue)
StartTime = DateTime.Now;
}

internal static string GetActivatedModules()
{
string text = "- Core";

if (BaseSettings.ActivateAzuraCast)
text += "\n- AzuraCast";

if (BaseSettings.ActivateClubManagement)
text += "\n- ClubManagement";

return text;
}

internal static string GetBotName => Assembly.GetExecutingAssembly().GetName().Name ?? "Bot name not found";
internal static string GetBotUptime => (CoreMisc.CheckIfLinuxOs()) ? $"<t:{CoreMisc.ConvertToUnixTime(StartTime).ToString(CultureInfo.InvariantCulture)}>" : "This feature is not available on Windows";
internal static string GetBotEnvironment => (Program.GetDiscordClientId == 1169381408939192361) ? "Development" : "Production";
internal static string GetBotVersion => Assembly.GetExecutingAssembly().GetName().Version?.ToString(3) ?? "Azzy version not found";

internal static async Task<string> GetBotCommitAsync()
{
if (CoreModule.CommitLock is null)
return "Commit not found";

string commit = await CoreModule.CommitLock.GetFileContentAsync();
if (string.IsNullOrWhiteSpace(commit))
commit = "Commit not found";

return commit;
}

internal static async Task<string> GetBotCompileDateAsync()
{
if (CoreModule.BuildTimeLock is null)
return "CompileDate not found";

if (!DateTime.TryParse(await CoreModule.BuildTimeLock.GetFileContentAsync(), out DateTime dateTime))
return "CompileDate not found";

return $"<t:{CoreMisc.ConvertToUnixTime(dateTime)}>";
}

internal static string GetDotNetVersion => Environment.Version.ToString() ?? ".NET version not found";
internal static string GetDSharpNetVersion => Program.GetDiscordClientVersion;
}
6 changes: 0 additions & 6 deletions AzzyBot/Modules/Core/CoreCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,6 @@ internal static async Task CorePingAzzyCommandAsync(InteractionContext ctx)
ExceptionHandler.LogMessage(LogLevel.Debug, "CorePingAzzyCommand requested");
await ctx.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWithSource);

if (!CoreMisc.CheckIfLinuxOs())
{
await ctx.EditResponseAsync(new DiscordWebhookBuilder().AddEmbed(CoreEmbedBuilder.BuildAzzyStatsNotAvailableEmbed(ctx.Client.CurrentUser.Username, ctx.Client.CurrentUser.AvatarUrl)));
return;
}

await ctx.EditResponseAsync(new DiscordWebhookBuilder().AddEmbed(await CoreEmbedBuilder.BuildAzzyStatsEmbedAsync(ctx.Client.CurrentUser.Username, ctx.Client.CurrentUser.AvatarUrl, ctx.Client.Ping)));
}
}
Expand Down
Loading

0 comments on commit ef35074

Please sign in to comment.