Show front percent for switches with no fronter

Closes #113.
This commit is contained in:
Ske 2019-07-15 21:51:41 +02:00
parent 05f7ead62e
commit b6ba24d171
2 changed files with 25 additions and 5 deletions

View File

@ -141,7 +141,7 @@ namespace PluralKit.Bot {
.Build(); .Build();
} }
public async Task<Embed> CreateFrontPercentEmbed(IDictionary<PKMember, Duration> frontpercent, ZonedDateTime startingFrom) public async Task<Embed> CreateFrontPercentEmbed(SwitchStore.PerMemberSwitchDuration frontpercent, ZonedDateTime startingFrom)
{ {
var totalDuration = SystemClock.Instance.GetCurrentInstant() - startingFrom.ToInstant(); var totalDuration = SystemClock.Instance.GetCurrentInstant() - startingFrom.ToInstant();
@ -151,11 +151,17 @@ namespace PluralKit.Bot {
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"
var membersOrdered = frontpercent.OrderByDescending(pair => pair.Value).Take(maxEntriesToDisplay).ToList(); // We convert to a list of pairs so we can add the no-fronter value
// Dictionary doesn't allow for null keys so we instead have a pair with a null key ;)
var pairs = frontpercent.MemberSwitchDurations.ToList();
if (frontpercent.NoFronterDuration != Duration.Zero)
pairs.Add(new KeyValuePair<PKMember, Duration>(null, frontpercent.NoFronterDuration));
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 / totalDuration;
eb.AddField(pair.Key.Name, $"{frac*100:F0}% ({Formats.DurationFormat.Format(pair.Value)})"); eb.AddField(pair.Key?.Name ?? "*(no fronter)*", $"{frac*100:F0}% ({Formats.DurationFormat.Format(pair.Value)})");
} }
if (membersOrdered.Count > maxEntriesToDisplay) if (membersOrdered.Count > maxEntriesToDisplay)

View File

@ -314,11 +314,19 @@ namespace PluralKit {
return outList; return outList;
} }
public async Task<IDictionary<PKMember, Duration>> GetPerMemberSwitchDuration(PKSystem system, Instant periodStart, public struct PerMemberSwitchDuration
{
public Dictionary<PKMember, Duration> MemberSwitchDurations;
public Duration NoFronterDuration;
}
public async Task<PerMemberSwitchDuration> GetPerMemberSwitchDuration(PKSystem system, Instant periodStart,
Instant periodEnd) Instant periodEnd)
{ {
var dict = new Dictionary<PKMember, Duration>(); var dict = new Dictionary<PKMember, Duration>();
var noFronterDuration = Duration.Zero;
// 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
foreach (var sw in await GetTruncatedSwitchList(system, periodStart, periodEnd)) foreach (var sw in await GetTruncatedSwitchList(system, periodStart, periodEnd))
@ -328,9 +336,15 @@ namespace PluralKit {
if (!dict.ContainsKey(member)) dict.Add(member, sw.TimespanWithinRange); if (!dict.ContainsKey(member)) dict.Add(member, sw.TimespanWithinRange);
else dict[member] += sw.TimespanWithinRange; else dict[member] += sw.TimespanWithinRange;
} }
if (sw.Members.Count == 0) noFronterDuration += sw.TimespanWithinRange;
} }
return dict; return new PerMemberSwitchDuration
{
MemberSwitchDurations = dict,
NoFronterDuration = noFronterDuration
};
} }
} }
} }