From d5da3bf0041de2d681ede4399f1ef85686967253 Mon Sep 17 00:00:00 2001 From: Ske Date: Tue, 16 Jul 2019 21:18:46 +0200 Subject: [PATCH] Clamp frontpercent range to actual switch range --- PluralKit.Bot/Services/EmbedService.cs | 7 +++---- PluralKit.Core/Stores.cs | 26 ++++++++++++++++++++------ 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/PluralKit.Bot/Services/EmbedService.cs b/PluralKit.Bot/Services/EmbedService.cs index 5d2b6699..46c98ef3 100644 --- a/PluralKit.Bot/Services/EmbedService.cs +++ b/PluralKit.Bot/Services/EmbedService.cs @@ -148,11 +148,10 @@ namespace PluralKit.Bot { public async Task CreateFrontPercentEmbed(SwitchStore.PerMemberSwitchDuration frontpercent, ZonedDateTime startingFrom) { - var totalDuration = SystemClock.Instance.GetCurrentInstant() - startingFrom.ToInstant(); - + var actualPeriod = frontpercent.RangeEnd - frontpercent.RangeStart; var eb = new EmbedBuilder() .WithColor(Color.Blue) - .WithFooter($"Since {Formats.ZonedDateTimeFormat.Format(startingFrom)} ({Formats.DurationFormat.Format(totalDuration)} ago)"); + .WithFooter($"Since {Formats.ZonedDateTimeFormat.Format(startingFrom)} ({Formats.DurationFormat.Format(actualPeriod)} ago)"); var maxEntriesToDisplay = 24; // max 25 fields allowed in embed - reserve 1 for "others" @@ -165,7 +164,7 @@ namespace PluralKit.Bot { var membersOrdered = pairs.OrderByDescending(pair => pair.Value).Take(maxEntriesToDisplay).ToList(); foreach (var pair in membersOrdered) { - var frac = pair.Value / totalDuration; + var frac = pair.Value / actualPeriod; eb.AddField(pair.Key?.Name ?? "*(no fronter)*", $"{frac*100:F0}% ({Formats.DurationFormat.Format(pair.Value)})"); } diff --git a/PluralKit.Core/Stores.cs b/PluralKit.Core/Stores.cs index 1192f489..7433ecd8 100644 --- a/PluralKit.Core/Stores.cs +++ b/PluralKit.Core/Stores.cs @@ -261,7 +261,8 @@ namespace PluralKit { public struct SwitchListEntry { public ICollection Members; - public Duration TimespanWithinRange; + public Instant TimespanStart; + public Instant TimespanEnd; } public async Task> GetTruncatedSwitchList(PKSystem system, Instant periodStart, Instant periodEnd) @@ -304,7 +305,8 @@ namespace PluralKit { outList.Add(new SwitchListEntry { Members = (await GetSwitchMemberIds(switchInRange)).Select(id => memberObjects[id]).ToList(), - TimespanWithinRange = span + TimespanStart = switchStartClamped, + TimespanEnd = endTime }); // next switch's end is this switch's start @@ -318,6 +320,8 @@ namespace PluralKit { { public Dictionary MemberSwitchDurations; public Duration NoFronterDuration; + public Instant RangeStart; + public Instant RangeEnd; } public async Task GetPerMemberSwitchDuration(PKSystem system, Instant periodStart, @@ -329,21 +333,31 @@ namespace PluralKit { // Sum up all switch durations for each member // switches with multiple members will result in the duration to add up to more than the actual period range + + var actualStart = periodEnd; // will be "pulled" down + var actualEnd = periodStart; // will be "pulled" up + foreach (var sw in await GetTruncatedSwitchList(system, periodStart, periodEnd)) { + var span = sw.TimespanEnd - sw.TimespanStart; foreach (var member in sw.Members) { - if (!dict.ContainsKey(member)) dict.Add(member, sw.TimespanWithinRange); - else dict[member] += sw.TimespanWithinRange; + if (!dict.ContainsKey(member)) dict.Add(member, span); + else dict[member] += span; } - if (sw.Members.Count == 0) noFronterDuration += sw.TimespanWithinRange; + if (sw.Members.Count == 0) noFronterDuration += span; + + if (sw.TimespanStart < actualStart) actualStart = sw.TimespanStart; + if (sw.TimespanEnd < actualStart) actualStart = sw.TimespanEnd; } return new PerMemberSwitchDuration { MemberSwitchDurations = dict, - NoFronterDuration = noFronterDuration + NoFronterDuration = noFronterDuration, + RangeStart = actualStart, + RangeEnd = actualEnd }; } }