Skip to content

Commit

Permalink
Test in prod
Browse files Browse the repository at this point in the history
  • Loading branch information
marchermans committed Nov 11, 2024
1 parent f270c1e commit 0fdbd12
Show file tree
Hide file tree
Showing 17 changed files with 255 additions and 272 deletions.
6 changes: 5 additions & 1 deletion LDTTeam Authentication.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AHttpStatusCode_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fafa340febeff45fd9c30d5ee754ad84435800_003Fad_003Ff8375340_003FHttpStatusCode_002Ecs/@EntryIndexedValue">ForceIncluded</s:String></wpf:ResourceDictionary>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ACommandGroup_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FSourcesCache_003F3ae3554df54242d29975c0721a8339ce4246ccd6ccfcb1a35521a8b168a4c_003FCommandGroup_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AHttpStatusCode_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fafa340febeff45fd9c30d5ee754ad84435800_003Fad_003Ff8375340_003FHttpStatusCode_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AIDiscordRestWebhookAPI_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FDecompilerCache_003Fdecompiler_003Fd01c2f8aabda453ca04afdae2b88df7f16600_003F80_003F57fe5bd9_003FIDiscordRestWebhookAPI_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ATokenExtensions_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FSourcesCache_003F71bd9d6425d1b3468328fb69fcb92d442b8dce7f54dc18cb7fb975298a187_003FTokenExtensions_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AUrlHelperExtensions_002Ecs_002Fl_003A_002E_002E_003F_002E_002E_003F_002Econfig_003FJetBrains_003FRider2024_002E2_003Fresharper_002Dhost_003FSourcesCache_003F63fcee389eb69c983ee8a43935cbf35c6c88371dd5b8cf525b525976e0c96d16_003FUrlHelperExtensions_002Ecs/@EntryIndexedValue">ForceIncluded</s:String></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace LDTTeam.Authentication.Modules.Api.Events
{
public interface IBackgroundEventsQueue
{
Task QueueBackgroundWorkItemAsync(Func<EventsService, IServiceScope, CancellationToken, Task> workItem);
Task QueueBackgroundWorkItemAsync(Func<EventsService, IServiceScope, CancellationToken, Task> workItem, CancellationToken token);

ValueTask<Func<EventsService, IServiceScope, CancellationToken, Task>> DequeueAsync(
CancellationToken cancellationToken);
Expand All @@ -33,14 +33,14 @@ public BackgroundEventsQueue(int capacity)
}

public async Task QueueBackgroundWorkItemAsync(
Func<EventsService, IServiceScope, CancellationToken, Task> workItem)
Func<EventsService, IServiceScope, CancellationToken, Task> workItem, CancellationToken token)
{
if (workItem == null)
{
throw new ArgumentNullException(nameof(workItem));
}

await _queue.Writer.WriteAsync(workItem);
await _queue.Writer.WriteAsync(workItem, token);
}

public async ValueTask<Func<EventsService, IServiceScope, CancellationToken, Task>> DequeueAsync(
Expand Down
3 changes: 2 additions & 1 deletion LDTTeam.Authentication.Modules.Api/IModule.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Threading;
using LDTTeam.Authentication.Modules.Api.Events;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Configuration;
Expand All @@ -20,7 +21,7 @@ public IServiceCollection ConfigureServices(IConfiguration configuration, IServi
return services;
}

public void EventsSubscription(IServiceProvider services, EventsService events)
public void EventsSubscription(IServiceProvider services, EventsService events, CancellationToken token)
{
}
}
Expand Down
13 changes: 7 additions & 6 deletions LDTTeam.Authentication.Modules.Api/Rewards/IConditionService.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace LDTTeam.Authentication.Modules.Api.Rewards
{
public interface IConditionService
{
public Task<bool?> CheckReward(string provider, string providerKey, string rewardId);
public Task<bool?> CheckReward(string provider, string providerKey, string rewardId, CancellationToken token);

public Task<Dictionary<string, List<string>>> GetRewardsForProvider(string provider);
public Task<Dictionary<string, List<string>>> GetRewardsForProvider(string provider, CancellationToken token);

public Task<Dictionary<string, bool>?> GetRewardsForUser(string provider, string providerKey);
public Task<Dictionary<string, bool>?> GetRewardsForUser(string provider, string providerKey, CancellationToken token);

public Task<Dictionary<string, bool>> GetRewardsForUser(string userId);
public Task<Dictionary<string, bool>> GetRewardsForUser(string userId, CancellationToken token);

public Task AddConditionToReward(string rewardId, string moduleName, string conditionName, string lambda);
public Task AddConditionToReward(string rewardId, string moduleName, string conditionName, string lambda, CancellationToken token);

public Task RemoveConditionFromReward(string rewardId, string moduleName, string conditionName);
public Task RemoveConditionFromReward(string rewardId, string moduleName, string conditionName, CancellationToken token);
}
}
58 changes: 26 additions & 32 deletions LDTTeam.Authentication.Server/Api/ApiController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using LDTTeam.Authentication.Modules.Api.Events;
using LDTTeam.Authentication.Modules.Api.Rewards;
Expand All @@ -16,57 +17,48 @@ namespace LDTTeam.Authentication.Server.Api
{
[ApiController]
[Route("Api")]
public class ApiController : ControllerBase
public class ApiController(
IBackgroundEventsQueue eventsQueue,
IConditionService conditionService,
DatabaseContext context)
: ControllerBase
{
private readonly IBackgroundEventsQueue _eventsQueue;
private readonly IConditionService _conditionService;
private readonly DatabaseContext _context;

public ApiController(IBackgroundEventsQueue eventsQueue, IConditionService conditionService,
DatabaseContext context)
{
_eventsQueue = eventsQueue;
_conditionService = conditionService;
_context = context;
}

[HttpGet("webhook/{provider}")]
[HttpPost("webhook/{provider}")]
public async Task<ActionResult> WebhookEndpoint(string provider)
public async Task<ActionResult> WebhookEndpoint(string provider, CancellationToken token)
{
if (provider == "all")
{
await _eventsQueue.QueueBackgroundWorkItemAsync(async (events, scope, _) =>
await eventsQueue.QueueBackgroundWorkItemAsync(async (events, scope, _) =>
{
await events._refreshContentEvent.InvokeAsync(scope, null);
await events._postRefreshContentEvent.InvokeAsync(scope);
});
}, token);
return Ok();
}

await _eventsQueue.QueueBackgroundWorkItemAsync(async (events, scope, _) =>
await eventsQueue.QueueBackgroundWorkItemAsync(async (events, scope, _) =>
{
await events._refreshContentEvent.InvokeAsync(scope, new List<string> {provider});
await events._refreshContentEvent.InvokeAsync(scope, [provider]);
await events._postRefreshContentEvent.InvokeAsync(scope);
});
}, token);
return Ok();
}

[HttpGet("{provider}/{providerKey}/{rewardId}")]
public async Task<ActionResult<bool>> Endpoint(string provider, string providerKey, string rewardId)
public async Task<ActionResult<bool>> Endpoint(string provider, string providerKey, string rewardId, CancellationToken token)
{
bool? check = await _conditionService.CheckReward(provider, providerKey, rewardId);
bool? check = await conditionService.CheckReward(provider, providerKey, rewardId, token);

if (check == null)
return NotFound();

try
{
EndpointMetric? metric = await _context.Metrics.FirstOrDefaultAsync(x =>
EndpointMetric? metric = await context.Metrics.FirstOrDefaultAsync(x =>
x.Provider.ToLower() == provider.ToLower() &&
x.RewardId.ToLower() == rewardId.ToLower() &&
x.Result == check
);
x.Result == check, cancellationToken: token);

if (metric == null)
{
Expand All @@ -78,11 +70,11 @@ public async Task<ActionResult<bool>> Endpoint(string provider, string providerK
Count = 0
};

await _context.AddAsync(metric);
await context.AddAsync(metric, token);
}
metric.Count++;

await _context.SaveChangesAsync();
await context.SaveChangesAsync(token);
}
catch (Exception e)
{
Expand All @@ -93,25 +85,27 @@ public async Task<ActionResult<bool>> Endpoint(string provider, string providerK
}

[HttpGet("/metrics")]
public async Task<ActionResult<List<EndpointMetric>>> Metrics()
public async Task<ActionResult<List<EndpointMetric>>> Metrics(CancellationToken token)
{
return await _context.Metrics
return await context.Metrics
.OrderBy(x => x.Id)
.ToListAsync();
.ToListAsync(cancellationToken: token);
}

[HttpGet("/metrics/{id:guid}")]
public async Task<ActionResult<List<HistoricalEndpointMetric>>> HistoricalMetrics(Guid id, bool all = false)
public async Task<ActionResult<List<HistoricalEndpointMetric>>> HistoricalMetrics(Guid id, bool? loadAll, CancellationToken token)
{
IQueryable<HistoricalEndpointMetric> query = _context.HistoricalMetrics
var all = loadAll ?? false;

IQueryable<HistoricalEndpointMetric> query = context.HistoricalMetrics
.Where(x => x.Metric.Id == id);

DateTimeOffset sevenDaysAgo = DateTimeOffset.Now.Subtract(TimeSpan.FromDays(7));

if (!all)
query = query.Where(x => x.DateTime > sevenDaysAgo);

return await query.ToListAsync();
return await query.ToListAsync(cancellationToken: token);
}
}
}
Loading

0 comments on commit 0fdbd12

Please sign in to comment.