Start counting messages per quarter starting 1 january 2021
This commit is contained in:
parent
7e792bd782
commit
29c0def713
3 changed files with 95 additions and 15 deletions
|
@ -1,5 +1,7 @@
|
|||
using System;
|
||||
using System.Globalization;
|
||||
using System.Threading.Tasks;
|
||||
using System.Timers;
|
||||
using Discord.WebSocket;
|
||||
using Geekbot.Core.Database;
|
||||
using Geekbot.Core.Database.Models;
|
||||
|
@ -13,11 +15,28 @@ namespace Geekbot.Bot.Handlers
|
|||
{
|
||||
private readonly IGeekbotLogger _logger;
|
||||
private readonly DatabaseContext _database;
|
||||
private string _season;
|
||||
// ToDo: Clean up in 2021
|
||||
private bool _seasonsStarted;
|
||||
|
||||
public StatsHandler(IGeekbotLogger logger, DatabaseContext database)
|
||||
{
|
||||
_logger = logger;
|
||||
_database = database;
|
||||
_season = GetSeason();
|
||||
_seasonsStarted = DateTime.Now.Year == 2021;
|
||||
|
||||
var timer = new Timer()
|
||||
{
|
||||
Enabled = true,
|
||||
AutoReset = true,
|
||||
Interval = TimeSpan.FromHours(1).TotalMilliseconds
|
||||
};
|
||||
timer.Elapsed += (sender, args) =>
|
||||
{
|
||||
_season = GetSeason();
|
||||
_seasonsStarted = DateTime.Now.Year == 2021;
|
||||
};
|
||||
}
|
||||
|
||||
public async Task UpdateStats(SocketMessage message)
|
||||
|
@ -32,23 +51,12 @@ namespace Geekbot.Bot.Handlers
|
|||
}
|
||||
|
||||
var channel = (SocketGuildChannel) message.Channel;
|
||||
|
||||
var rowId = await _database.Database.ExecuteSqlRawAsync(
|
||||
"UPDATE \"Messages\" SET \"MessageCount\" = \"MessageCount\" + 1 WHERE \"GuildId\" = {0} AND \"UserId\" = {1}",
|
||||
channel.Guild.Id.AsLong(),
|
||||
message.Author.Id.AsLong()
|
||||
);
|
||||
|
||||
if (rowId == 0)
|
||||
await UpdateTotalTable(message, channel);
|
||||
if (_seasonsStarted)
|
||||
{
|
||||
await _database.Messages.AddAsync(new MessagesModel
|
||||
{
|
||||
UserId = message.Author.Id.AsLong(),
|
||||
GuildId = channel.Guild.Id.AsLong(),
|
||||
MessageCount = 1
|
||||
});
|
||||
await _database.SaveChangesAsync();
|
||||
await UpdateSeasonsTable(message, channel);
|
||||
}
|
||||
|
||||
|
||||
if (message.Author.IsBot) return;
|
||||
_logger.Information(LogSource.Message, message.Content, SimpleConextConverter.ConvertSocketMessage(message));
|
||||
|
@ -58,5 +66,55 @@ namespace Geekbot.Bot.Handlers
|
|||
_logger.Error(LogSource.Message, "Could not process message stats", e);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task UpdateTotalTable(SocketMessage message, SocketGuildChannel channel)
|
||||
{
|
||||
var rowId = await _database.Database.ExecuteSqlRawAsync(
|
||||
"UPDATE \"Messages\" SET \"MessageCount\" = \"MessageCount\" + 1 WHERE \"GuildId\" = {0} AND \"UserId\" = {1}",
|
||||
channel.Guild.Id.AsLong(),
|
||||
message.Author.Id.AsLong()
|
||||
);
|
||||
|
||||
if (rowId == 0)
|
||||
{
|
||||
await _database.Messages.AddAsync(new MessagesModel
|
||||
{
|
||||
UserId = message.Author.Id.AsLong(),
|
||||
GuildId = channel.Guild.Id.AsLong(),
|
||||
MessageCount = 1
|
||||
});
|
||||
await _database.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private async Task UpdateSeasonsTable(SocketMessage message, SocketGuildChannel channel)
|
||||
{
|
||||
var rowId = await _database.Database.ExecuteSqlRawAsync(
|
||||
"UPDATE \"MessagesSeasons\" SET \"MessageCount\" = \"MessageCount\" + 1 WHERE \"GuildId\" = {0} AND \"UserId\" = {1} AND \"Season\" = {2}",
|
||||
channel.Guild.Id.AsLong(),
|
||||
message.Author.Id.AsLong(),
|
||||
_season
|
||||
);
|
||||
|
||||
if (rowId == 0)
|
||||
{
|
||||
await _database.MessagesSeasons.AddAsync(new MessageSeasonsModel()
|
||||
{
|
||||
UserId = message.Author.Id.AsLong(),
|
||||
GuildId = channel.Guild.Id.AsLong(),
|
||||
Season = _season,
|
||||
MessageCount = 1
|
||||
});
|
||||
await _database.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetSeason()
|
||||
{
|
||||
var now = DateTime.Now;
|
||||
var year = (now.Year - 2000).ToString(CultureInfo.InvariantCulture);
|
||||
var quarter = Math.Ceiling(now.Month / 3.0).ToString(CultureInfo.InvariantCulture);
|
||||
return $"{year}Q{quarter}";
|
||||
}
|
||||
}
|
||||
}
|
|
@ -11,6 +11,7 @@ namespace Geekbot.Core.Database
|
|||
public DbSet<KarmaModel> Karma { get; set; }
|
||||
public DbSet<ShipsModel> Ships { get; set; }
|
||||
public DbSet<RollsModel> Rolls { get; set; }
|
||||
public DbSet<MessageSeasonsModel> MessagesSeasons { get; set; }
|
||||
public DbSet<MessagesModel> Messages { get; set; }
|
||||
public DbSet<SlapsModel> Slaps { get; set; }
|
||||
public DbSet<GlobalsModel> Globals { get; set; }
|
||||
|
|
21
src/Core/Database/Models/MessageSeasonsModel.cs
Normal file
21
src/Core/Database/Models/MessageSeasonsModel.cs
Normal file
|
@ -0,0 +1,21 @@
|
|||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Geekbot.Core.Database.Models
|
||||
{
|
||||
public class MessageSeasonsModel
|
||||
{
|
||||
[Key]
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public long GuildId { get; set; }
|
||||
|
||||
[Required]
|
||||
public long UserId { get; set; }
|
||||
|
||||
[Required]
|
||||
public string Season { get; set; }
|
||||
|
||||
public int MessageCount { get; set; }
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue