Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StandUpStreak - implement internal counter #345

Open
wants to merge 21 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/HonzaBotner.Database/HonzaBotnerDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ protected override void OnModelCreating(ModelBuilder builder)
public DbSet<RoleBinding> RoleBindings { get; set; }
public DbSet<Warning> Warnings { get; set; }
public DbSet<Reminder> Reminders { get; set; }
public DbSet<StandUpStreak> StandUpStreaks { get; set; }

LucyAnne98 marked this conversation as resolved.
Show resolved Hide resolved
}
13 changes: 13 additions & 0 deletions src/HonzaBotner.Database/StandUpStreak.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace HonzaBotner.Database;

public class StandUpStreak
{
public int Id { get; set; }
public ulong UserId { get; set; }
public int Streak { get; set; }
public int LongestStreak { get; set; }
public int Freezes { get; set; }
public DateTime LastDayOfStreak { get; set; }
}
14 changes: 13 additions & 1 deletion src/HonzaBotner.Discord.Services/Jobs/StandUpJobProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using HonzaBotner.Discord.Services.Helpers;
using HonzaBotner.Discord.Services.Options;
using HonzaBotner.Scheduler.Contract;
using HonzaBotner.Services.Contract;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

Expand All @@ -22,15 +23,19 @@ public class StandUpJobProvider : IJob

private readonly CommonCommandOptions _commonOptions;

private readonly IStandUpStreakService _streakService;

public StandUpJobProvider(
ILogger<StandUpJobProvider> logger,
DiscordWrapper discord,
IOptions<CommonCommandOptions> commonOptions
IOptions<CommonCommandOptions> commonOptions,
IStandUpStreakService streakService
)
{
_logger = logger;
_discord = discord;
_commonOptions = commonOptions.Value;
_streakService = streakService;
}

/// <summary>
Expand Down Expand Up @@ -81,6 +86,7 @@ await channel.GetMessagesBeforeAsync(messageList.Last().Id)

foreach (DiscordMessage msg in messageList.Where(msg => msg.Timestamp.Date == yesterday))
{
bool streakMaintained = false;
foreach (Match match in Regex.Matches(msg.Content))
{
string state = match.Groups["State"].ToString();
Expand All @@ -89,12 +95,18 @@ await channel.GetMessagesBeforeAsync(messageList.Last().Id)
if (OkList.Any(s => state.Contains(s)))
{
ok.Increment(priority);
streakMaintained = true;
}
else
{
fail.Increment(priority);
}
}

if (streakMaintained)
{
await _streakService.UpdateStreak(msg.Author.Id);
LucyAnne98 marked this conversation as resolved.
Show resolved Hide resolved
}
}

await channel.SendMessageAsync($@"
Expand Down
13 changes: 13 additions & 0 deletions src/HonzaBotner.Services.Contract/Dto/StandUpStreak.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace HonzaBotner.Services.Contract.Dto;

public class StandUpStreak{

public int Id { get; set; }
public ulong UserId { get; set; }
public int Streak { get; set; }
public int LongestStreak { get; set; }
public int Freezes { get; set; }
public DateTime LastDayOfStreak { get; set; }
};
13 changes: 13 additions & 0 deletions src/HonzaBotner.Services.Contract/IStandUpStreakService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Threading.Tasks;
using HonzaBotner.Services.Contract.Dto;
namespace HonzaBotner.Services.Contract;

public interface IStandUpStreakService
{
Task<StandUpStreak?> GetStreak(ulong userId);

Task UpdateStreak(ulong userId);

Task<bool> IsValidStreak(ulong userId);
}
1 change: 1 addition & 0 deletions src/HonzaBotner.Services/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static IServiceCollection AddBotnerServices(this IServiceCollection servi
serviceCollection.AddScoped<IRoleBindingsService, RoleBindingsService>();
serviceCollection.AddScoped<IWarningService, WarningService>();
serviceCollection.AddScoped<IRemindersService, RemindersService>();
serviceCollection.AddScoped<IStandUpStreakService, StandUpStreakService>();

return serviceCollection;
}
Expand Down
124 changes: 124 additions & 0 deletions src/HonzaBotner.Services/StandUpStreakService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System;
using System.Threading.Tasks;
using HonzaBotner.Database;
using HonzaBotner.Services.Contract;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using StandUpStreak = HonzaBotner.Services.Contract.Dto.StandUpStreak;

namespace HonzaBotner.Services;

public class StandUpStreakService : IStandUpStreakService
{
private readonly HonzaBotnerDbContext _dbContext;
private readonly ILogger<StandUpStreakService> _logger;
private const int DaysToAcquireFreeze = 6;

public StandUpStreakService(HonzaBotnerDbContext dbContext, ILogger<StandUpStreakService> logger)
{
_dbContext = dbContext;
_logger = logger;
}

public async Task<StandUpStreak?> GetStreak(ulong userId)
{
Database.StandUpStreak? standUpStreak = await _dbContext.StandUpStreaks
.FirstOrDefaultAsync(streak => streak.UserId == userId);

return standUpStreak == null ? null : GetDto(standUpStreak);
}

public async Task UpdateStreak(ulong userId)
LucyAnne98 marked this conversation as resolved.
Show resolved Hide resolved
{
Database.StandUpStreak? streak = await _dbContext.StandUpStreaks
.FirstOrDefaultAsync(streak => streak.UserId == userId);

// Create new streak for a new user
if (streak is null)
{
Database.StandUpStreak newStreak = new()
{
UserId = userId,
Freezes = 0,
LastDayOfStreak = DateTime.Today.AddDays(-1),
Streak = 1,
LongestStreak = 1
};

await _dbContext.StandUpStreaks.AddAsync(newStreak);

try
{
await _dbContext.SaveChangesAsync();
}
catch (Exception e)
{
_logger.LogError(e, "Couldn't add streak {@Streak}", streak);
}

return;
}

int days = (DateTime.Today.AddDays(-2) - streak.LastDayOfStreak).Days;

if (days == -1) //Streak was already restored today
{
return;
}

if (days > streak.Freezes) //Streak broken
{
streak.Freezes = 0;
streak.LastDayOfStreak = DateTime.Today.AddDays(-1);
streak.Streak = 1;
}
else //streak restored
LucyAnne98 marked this conversation as resolved.
Show resolved Hide resolved
{
streak.Freezes -= days;
streak.Streak++;
streak.LastDayOfStreak = DateTime.Today.AddDays(-1);

if (streak.Streak > streak.LongestStreak)
{
streak.LongestStreak = streak.Streak;
}

if (streak.Streak % DaysToAcquireFreeze == 0) // freeze acquired
{
streak.Freezes++;
}
}

try
{
_dbContext.StandUpStreaks.Update(streak);
await _dbContext.SaveChangesAsync();
}
catch (Exception e)
{
_logger.LogError(e, "Couldn't update streak {@Streak}", streak);
}
}

public async Task<bool> IsValidStreak(ulong userId)
{
StandUpStreak? streak = await GetStreak(userId);

if (streak is null)
return false;
int days = (DateTime.Today.AddDays(-2) - streak.LastDayOfStreak).Days;

return days <= streak.Freezes;
}

private static StandUpStreak GetDto(Database.StandUpStreak standUpStreak) =>
new StandUpStreak()
{
Id = standUpStreak.Id,
UserId = standUpStreak.UserId,
Streak = standUpStreak.Streak,
LongestStreak = standUpStreak.LongestStreak,
Freezes = standUpStreak.Freezes,
LastDayOfStreak = standUpStreak.LastDayOfStreak
};
}