-
Notifications
You must be signed in to change notification settings - Fork 45
/
ScrapeStateProvider.cs
67 lines (56 loc) · 2.75 KB
/
ScrapeStateProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace CompatBot.Database.Providers;
internal static class ScrapeStateProvider
{
private static readonly TimeSpan CheckInterval = TimeSpan.FromDays(365);
public static bool IsFresh(long timestamp)
=> IsFresh(new DateTime(timestamp, DateTimeKind.Utc));
public static bool IsFresh(DateTime timestamp)
=> timestamp.Add(CheckInterval) > DateTime.UtcNow;
public static bool IsFresh(string locale, string? containerId = null)
{
var id = GetId(locale, containerId);
using var db = new ThumbnailDb();
var timestamp = string.IsNullOrEmpty(id) ? db.State.OrderBy(s => s.Timestamp).FirstOrDefault() : db.State.FirstOrDefault(s => s.Locale == id);
if (timestamp is { Timestamp: long checkDate and > 0 })
return IsFresh(new DateTime(checkDate, DateTimeKind.Utc));
return false;
}
public static bool IsFresh(string locale, DateTime dataTimestamp)
{
using var db = new ThumbnailDb();
var timestamp = string.IsNullOrEmpty(locale) ? db.State.OrderBy(s => s.Timestamp).FirstOrDefault() : db.State.FirstOrDefault(s => s.Locale == locale);
if (timestamp is { Timestamp: long checkDate and > 0 })
return new DateTime(checkDate, DateTimeKind.Utc) > dataTimestamp;
return false;
}
public static async Task SetLastRunTimestampAsync(string locale, string? containerId = null)
{
if (string.IsNullOrEmpty(locale))
throw new ArgumentException("Locale is mandatory", nameof(locale));
var id = GetId(locale, containerId);
await using var db = new ThumbnailDb();
var timestamp = db.State.FirstOrDefault(s => s.Locale == id);
if (timestamp == null)
await db.State.AddAsync(new State {Locale = id, Timestamp = DateTime.UtcNow.Ticks}).ConfigureAwait(false);
else
timestamp.Timestamp = DateTime.UtcNow.Ticks;
await db.SaveChangesAsync().ConfigureAwait(false);
}
public static async Task CleanAsync(CancellationToken cancellationToken)
{
await using var db = new ThumbnailDb();
var latestTimestamp = db.State.OrderByDescending(s => s.Timestamp).FirstOrDefault()?.Timestamp;
if (!latestTimestamp.HasValue)
return;
var cutOff = new DateTime(latestTimestamp.Value, DateTimeKind.Utc).Add(-CheckInterval);
var oldItems = db.State.Where(s => s.Timestamp < cutOff.Ticks);
db.State.RemoveRange(oldItems);
await db.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
}
private static string GetId(string locale, string? containerId)
=> string.IsNullOrEmpty(containerId) ? locale : $"{locale} - {containerId}";
}