mirror of
https://github.com/barelyprofessional/KfChatDotNet.git
synced 2026-04-30 03:22:04 -04:00
Only add a row to the view counts table if the view count has changed or the stream ID changed
This commit is contained in:
@@ -3,6 +3,7 @@ using System.Net.Http.Json;
|
||||
using System.Text.Json;
|
||||
using KfChatDotNetBot.Models.DbModels;
|
||||
using KfChatDotNetBot.Settings;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using NLog;
|
||||
|
||||
namespace KfChatDotNetBot.Services;
|
||||
@@ -57,18 +58,21 @@ public class TwitchGraphQl(string? proxy = null, CancellationToken cancellationT
|
||||
|
||||
if (stream.IsLive)
|
||||
{
|
||||
_logger.Info("Updating DB with fresh view count");
|
||||
await using var db = new ApplicationDbContext();
|
||||
await db.TwitchViewCounts.AddAsync(new TwitchViewCountDbModel
|
||||
var mostRecentViewCount = await db.TwitchViewCounts.OrderBy(x => x.Id).LastOrDefaultAsync(ct);
|
||||
if (mostRecentViewCount == null || mostRecentViewCount.Viewers != stream.ViewerCount!.Value || mostRecentViewCount.Topic != stream.Id)
|
||||
{
|
||||
Topic = stream.Id!,
|
||||
Viewers = stream.ViewerCount!.Value,
|
||||
Time = DateTimeOffset.UtcNow,
|
||||
ServerTime = 0
|
||||
}, ct);
|
||||
await db.SaveChangesAsync(ct);
|
||||
_logger.Info("Updating DB with fresh view count");
|
||||
await db.TwitchViewCounts.AddAsync(new TwitchViewCountDbModel
|
||||
{
|
||||
Topic = stream.Id!,
|
||||
Viewers = stream.ViewerCount!.Value,
|
||||
Time = DateTimeOffset.UtcNow,
|
||||
ServerTime = 0
|
||||
}, ct);
|
||||
await db.SaveChangesAsync(ct);
|
||||
}
|
||||
}
|
||||
|
||||
var persistedLive = settings[BuiltIn.Keys.TwitchGraphQlPersistedCurrentlyLive].ToBoolean();
|
||||
if (stream.IsLive == persistedLive) continue;
|
||||
OnStreamStateUpdated?.Invoke(this, settings[BuiltIn.Keys.TwitchBossmanJackUsername].Value!, stream.IsLive);
|
||||
|
||||
Reference in New Issue
Block a user