Skip to content

Commit

Permalink
Fixes & Improvements: logs, dedupe, private methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Elanis committed Mar 1, 2025
1 parent ee28e8b commit 7f48807
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
<AssemblyVersion>1.0.0</AssemblyVersion>
<FileVersion>1.0.0</FileVersion>
<Version>1.0.0</Version>
<UserSecretsId>12b396a4-a415-424f-99fe-819d74368c2b</UserSecretsId>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Lumen.Modules.Sdk" Version="0.4.0" />
<PackageReference Include="Lumen.Modules.Sdk" Version="0.5.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.2" />
</ItemGroup>

Expand Down
55 changes: 33 additions & 22 deletions Lumen.Modules.Youtube.Module/YoutubeModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Microsoft.EntityFrameworkCore;

namespace Lumen.Modules.Youtube.Module {
public class YoutubeModule(LumenModuleRunsOnFlag runsOn, IEnumerable<ConfigEntry> configEntries, ILogger<LumenModuleBase> logger, IServiceProvider provider) : LumenModuleBase(runsOn, configEntries, logger) {
public class YoutubeModule(IEnumerable<ConfigEntry> configEntries, ILogger<LumenModuleBase> logger, IServiceProvider provider) : LumenModuleBase(configEntries, logger, provider) {
public const string PLAYLIST_ID = nameof(PLAYLIST_ID);
public const string API_KEY = nameof(API_KEY);

Expand All @@ -16,18 +16,25 @@ public override Task InitAsync(LumenModuleRunsOnFlag currentEnv) {
}

public override async Task RunAsync(LumenModuleRunsOnFlag currentEnv) {
var (amount, duration) = await YoutubeAPIHelper.ComputeWatchlistStatus(GetAPIKey(), GetPlaylistId());

var timespan = TimeSpan.FromSeconds(duration);
Console.WriteLine($"{DateTime.Now} - Stats: {amount} videos, {(int)timespan.TotalHours}:{timespan:mm\\:ss}");

switch (currentEnv) {
case LumenModuleRunsOnFlag.API:
await RunAPIAsync(amount, duration);
break;
case LumenModuleRunsOnFlag.UI:
await RunUIAsync(amount, duration);
break;
try {
logger.LogTrace($"[{nameof(YoutubeModule)}] Running tasks ...");
var (amount, duration) = await YoutubeAPIHelper.ComputeWatchlistStatus(GetAPIKey(), GetPlaylistId());

var timespan = TimeSpan.FromSeconds(duration);
logger.LogTrace($"[{nameof(YoutubeModule)}] {DateTime.Now} - Stats: {amount} videos, {(int)timespan.TotalHours}:{timespan:mm\\:ss}");

switch (currentEnv) {
case LumenModuleRunsOnFlag.API:
await RunAPIAsync(amount, duration);
break;
case LumenModuleRunsOnFlag.UI:
await RunUIAsync(amount, duration);
break;
}

logger.LogTrace($"[{nameof(YoutubeModule)}] Running tasks ... Done!");
} catch (Exception ex) {
logger.LogError(ex, $"[{nameof(YoutubeModule)}] Error when running tasks.");
}
}

Expand All @@ -49,26 +56,30 @@ private string GetAPIKey() {
return configEntry.ConfigValue;

Check warning on line 56 in Lumen.Modules.Youtube.Module/YoutubeModule.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 56 in Lumen.Modules.Youtube.Module/YoutubeModule.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference return.
}

public async Task RunUIAsync(int amount, int duration) {
private async Task RunUIAsync(int amount, int duration) {

Check warning on line 59 in Lumen.Modules.Youtube.Module/YoutubeModule.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
// TODO
}

public async Task RunAPIAsync(int amount, int duration) {
private async Task RunAPIAsync(int amount, int duration) {
using var scope = provider.CreateScope();
var context = provider.GetRequiredService<YoutubeContext>();

context.YoutubeWatchlist.Add(new YoutubeWatchlistPointInTime {
Time = DateTime.UtcNow,
AmountVideos = amount,
SecondsDuration = duration
});
await context.SaveChangesAsync();
var lastEntry = context.YoutubeWatchlist.OrderBy(x => x.Time).LastOrDefault();
if (lastEntry is null || lastEntry.AmountVideos != amount || lastEntry.SecondsDuration != duration) {
logger.LogTrace($"[{nameof(YoutubeModule)}] Saving current watchlist status.");
context.YoutubeWatchlist.Add(new YoutubeWatchlistPointInTime {
Time = DateTime.UtcNow,
AmountVideos = amount,
SecondsDuration = duration
});
await context.SaveChangesAsync();
}
}

public override bool ShouldRunNow(LumenModuleRunsOnFlag currentEnv) {
return currentEnv switch {
LumenModuleRunsOnFlag.UI => DateTime.UtcNow.Second == 0 && DateTime.UtcNow.Minute == 27,
LumenModuleRunsOnFlag.API => true, // DateTime.UtcNow.Second == 0 && DateTime.UtcNow.Minute % 5 == 0,
LumenModuleRunsOnFlag.API => DateTime.UtcNow.Second == 0 && DateTime.UtcNow.Minute % 5 == 0,
_ => false,
};
}
Expand Down

0 comments on commit 7f48807

Please sign in to comment.