/// <summary> /// Populates a new <see cref="KaguyaStatistics" /> with fresh information, /// all from the database. /// </summary> /// <returns></returns> private static async Task <KaguyaStatistics> GetFreshStats() { int kaguyaUsers = await DatabaseQueries.GetCountAsync <User>(); int guilds = 0; int guildUsers = 0; int textchannels = 0; int voicechannels = 0; foreach (SocketGuild guild in ConfigProperties.Client.Guilds) { guilds++; guildUsers += guild.MemberCount; textchannels += guild.TextChannels.Count; voicechannels += guild.VoiceChannels.Count; } int shardCount = ConfigProperties.Client.Shards.Count; int commandCount = await DatabaseQueries.GetCountAsync <CommandHistory>(); int commandCount24Hours = await DatabaseQueries.GetCountAsync <CommandHistory>(x => x.Timestamp >= DateTime.Now.AddHours(-24)); int fish = await DatabaseQueries.GetCountAsync <Fish>(); int points = DatabaseQueries.GetTotalCurrency(); int gambles = await DatabaseQueries.GetCountAsync <GambleHistory>(); DateTime timestamp = DateTime.Now; double ram = (double)GC.GetTotalMemory(false) / 1000000; int latency = ConfigProperties.Client.Latency; string version = ConfigProperties.Version; long uptimeSeconds = (long)(DateTime.Now - Process.GetCurrentProcess().StartTime).TotalSeconds; var stats = new KaguyaStatistics { KaguyaUsers = kaguyaUsers, Guilds = guilds, GuildUsers = guildUsers, Shards = shardCount, Commands = commandCount, CommandsLast24Hours = commandCount24Hours, Fish = fish, Points = points, Gambles = gambles, TimeStamp = timestamp, TextChannels = textchannels, VoiceChannels = voicechannels, RamUsageMegabytes = ram, LatencyMilliseconds = latency, UptimeSeconds = uptimeSeconds, Version = version }; return(stats); }
public static async Task Initialize() { // Run this on init, then every 1 minute. KaguyaStatistics freshStats = await GetFreshStats(); MemoryCache.SetStats(freshStats); await DatabaseQueries.InsertAsync(freshStats); await ConsoleLogger.LogAsync("Fresh stats populated in memory cache!", LogLvl.INFO); var timer = new Timer(60000); // 1 Minute timer.Enabled = true; timer.AutoReset = true; timer.Elapsed += async(s, e) => { KaguyaStatistics stats = await GetFreshStats(); MemoryCache.SetStats(stats); await DatabaseQueries.InsertAsync(stats); }; }
// Should only ever be used by KaguyaProjectV2.KaguyaBot.Core.Handlers.KaguyaStatsFactory // Is run on program startup by said class. public static void SetStats(KaguyaStatistics stats) => MostRecentStats = stats;
public async Task Command() { DiscordShardedClient client = Client; SocketUser owner = client.GetUser(ConfigProperties.BotConfig.BotOwnerId); KaguyaStatistics stats = MemoryCache.MostRecentStats; int totalGuilds = stats.Guilds; int totalTextChannels = stats.TextChannels; int totalVoiceChannels = stats.VoiceChannels; Dictionary <string, int> mostPopCommand = MemoryCache.MostPopularCommandCache; string mostPopCommandName = mostPopCommand?.Keys.First(); string mostPopCommandCount = mostPopCommand?.Values.First().ToString("N0"); string mostPopCommandText; if (mostPopCommandName == null || String.IsNullOrWhiteSpace(mostPopCommandCount)) { mostPopCommandText = "Data not loaded into cache yet."; } else { mostPopCommandText = $"{mostPopCommandName} with {int.Parse(mostPopCommandCount.Replace(",", "")):N0} uses."; } var fields = new List <EmbedFieldBuilder> { new EmbedFieldBuilder { Name = "Author", Value = $"User: `{owner}`\n" + $"Id: `{owner.Id}`" }, new EmbedFieldBuilder { Name = "Command Stats", Value = $"Commands Run (Last 24 Hours): `{stats.CommandsLast24Hours:N0}`\n" + $"Commands Run (All-time): `{stats.Commands:N0}`\n" + $"Most Popular Command: `{mostPopCommandText}`" }, new EmbedFieldBuilder { Name = "Global Stats", Value = $"Uptime: `{(DateTime.Now - DateTime.Now.AddSeconds(-stats.UptimeSeconds)).Humanize(4, minUnit: TimeUnit.Second)}`\n" + $"Guilds: `{totalGuilds:N0}`\n" + $"Text Channels: `{totalTextChannels:N0}`\n" + $"Voice Channels: `{totalVoiceChannels:N0}`\n" + $"Users: `{stats.GuildUsers:N0}`\n" + $"RAM Usage: `{stats.RamUsageMegabytes:N2} Megabytes`\n" + $"Current Version: `{stats.Version}`" }, new EmbedFieldBuilder { Name = "Kaguya User Stats", Value = $"Unique Interactions (Users): `{stats.KaguyaUsers:N0}`\n" + $"Total Points in Circulation: `{stats.Points:N0}`\n" + $"Total Gambles: `{stats.Gambles:N0}`" } }; var embed = new KaguyaEmbedBuilder { Title = "Kaguya Statistics", Fields = fields }; embed.SetColor(EmbedColor.GOLD); await ReplyAsync(embed : embed.Build()); }
public async Task <string> Get() { KaguyaStatistics data = MemoryCache.MostRecentStats; return(JsonConvert.SerializeObject(data, Formatting.Indented)); }