LANCommander/LANCommander.Server/UI/Pages/Dashboard/Charts/TopGamesAverageSessionPlaytime.razor

80 lines
No EOL
2.2 KiB
Text

@using AntDesign.Charts
@inject PlaySessionService PlaySessionService
<Spin Spinning="Loading">
<div class="dashboard-chart">
<AntDesign.Charts.Column Data="Data" Config="Config" JsConfig="@JsConfig" />
</div>
</Spin>
@code {
ICollection<PlaySession> PlaySessions;
Settings Settings = SettingService.GetSettings();
object[] Data;
bool Loading = true;
string JsConfig = @"{
meta: {
playtime: {
alias: 'Average Session Playtime',
formatter: (v) => Math.floor(v / 3600) > 0 ? `${Math.floor(v / 3600)}h ${Math.floor((v % 3600) / 60)}m` : `${Math.floor((v % 3600) / 60)}m`
},
game: {
alias: 'Game'
}
},
label: {
visible: true,
type: 'outer-center',
style: {
fontSize: '1px'
}
}
}";
ColumnConfig Config = new ColumnConfig
{
XField = "game",
YField = "playtime",
ColorField = "game",
};
protected override async Task OnInitializedAsync()
{
Config.Theme = Settings.Theme == LANCommanderTheme.Dark ? "dark" : "light";
Dictionary<string, TimeSpan> playtimes = new Dictionary<string, TimeSpan>();
PlaySessions = await PlaySessionService
.AsNoTracking()
.Include(ps => ps.Game)
.Include(ps => ps.CreatedBy)
.Include(ps => ps.User)
.GetAsync(ps => ps.GameId.HasValue && ps.GameId.Value != Guid.Empty);
foreach (var gameSessions in PlaySessions.GroupBy(s => s.GameId))
{
var total = new TimeSpan();
foreach (var session in gameSessions.Where(gs => gs.Start != null && gs.End != null))
{
total = total.Add(session.End.Value.Subtract(session.Start.Value));
}
playtimes[gameSessions.First().Game.Title] = TimeSpan.FromSeconds(total.TotalSeconds / gameSessions.Count());
}
Data = playtimes.OrderByDescending(pt => pt.Value).Take(20).Select(pt => new
{
Game = pt.Key,
Playtime = (int)pt.Value.TotalSeconds
}).ToArray();
Loading = false;
StateHasChanged();
}
}