Clamp frontpercent range to actual switch range

This commit is contained in:
Ske 2019-07-16 21:18:46 +02:00
parent 003f64abbd
commit d5da3bf004
2 changed files with 23 additions and 10 deletions

View File

@ -148,11 +148,10 @@ namespace PluralKit.Bot {
public async Task<Embed> CreateFrontPercentEmbed(SwitchStore.PerMemberSwitchDuration frontpercent, ZonedDateTime startingFrom) public async Task<Embed> CreateFrontPercentEmbed(SwitchStore.PerMemberSwitchDuration frontpercent, ZonedDateTime startingFrom)
{ {
var totalDuration = SystemClock.Instance.GetCurrentInstant() - startingFrom.ToInstant(); var actualPeriod = frontpercent.RangeEnd - frontpercent.RangeStart;
var eb = new EmbedBuilder() var eb = new EmbedBuilder()
.WithColor(Color.Blue) .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" 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(); var membersOrdered = pairs.OrderByDescending(pair => pair.Value).Take(maxEntriesToDisplay).ToList();
foreach (var pair in membersOrdered) 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)})"); eb.AddField(pair.Key?.Name ?? "*(no fronter)*", $"{frac*100:F0}% ({Formats.DurationFormat.Format(pair.Value)})");
} }

View File

@ -261,7 +261,8 @@ namespace PluralKit {
public struct SwitchListEntry public struct SwitchListEntry
{ {
public ICollection<PKMember> Members; public ICollection<PKMember> Members;
public Duration TimespanWithinRange; public Instant TimespanStart;
public Instant TimespanEnd;
} }
public async Task<IEnumerable<SwitchListEntry>> GetTruncatedSwitchList(PKSystem system, Instant periodStart, Instant periodEnd) public async Task<IEnumerable<SwitchListEntry>> GetTruncatedSwitchList(PKSystem system, Instant periodStart, Instant periodEnd)
@ -304,7 +305,8 @@ namespace PluralKit {
outList.Add(new SwitchListEntry outList.Add(new SwitchListEntry
{ {
Members = (await GetSwitchMemberIds(switchInRange)).Select(id => memberObjects[id]).ToList(), Members = (await GetSwitchMemberIds(switchInRange)).Select(id => memberObjects[id]).ToList(),
TimespanWithinRange = span TimespanStart = switchStartClamped,
TimespanEnd = endTime
}); });
// next switch's end is this switch's start // next switch's end is this switch's start
@ -318,6 +320,8 @@ namespace PluralKit {
{ {
public Dictionary<PKMember, Duration> MemberSwitchDurations; public Dictionary<PKMember, Duration> MemberSwitchDurations;
public Duration NoFronterDuration; public Duration NoFronterDuration;
public Instant RangeStart;
public Instant RangeEnd;
} }
public async Task<PerMemberSwitchDuration> GetPerMemberSwitchDuration(PKSystem system, Instant periodStart, public async Task<PerMemberSwitchDuration> GetPerMemberSwitchDuration(PKSystem system, Instant periodStart,
@ -329,21 +333,31 @@ namespace PluralKit {
// Sum up all switch durations for each member // 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 // 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)) foreach (var sw in await GetTruncatedSwitchList(system, periodStart, periodEnd))
{ {
var span = sw.TimespanEnd - sw.TimespanStart;
foreach (var member in sw.Members) foreach (var member in sw.Members)
{ {
if (!dict.ContainsKey(member)) dict.Add(member, sw.TimespanWithinRange); if (!dict.ContainsKey(member)) dict.Add(member, span);
else dict[member] += sw.TimespanWithinRange; 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 return new PerMemberSwitchDuration
{ {
MemberSwitchDurations = dict, MemberSwitchDurations = dict,
NoFronterDuration = noFronterDuration NoFronterDuration = noFronterDuration,
RangeStart = actualStart,
RangeEnd = actualEnd
}; };
} }
} }