mirror of
https://github.com/aaru-dps/Aaru.Server.git
synced 2025-12-16 11:14:27 +00:00
Add Archives page with pie chart and grid for archive formats statistics
This commit is contained in:
25
Aaru.Server/Components/Pages/Statistics/Archives.razor
Normal file
25
Aaru.Server/Components/Pages/Statistics/Archives.razor
Normal file
@@ -0,0 +1,25 @@
|
||||
@rendermode InteractiveServer
|
||||
|
||||
@inject Microsoft.EntityFrameworkCore.IDbContextFactory<DbContext> DbContextFactory
|
||||
|
||||
<PageTitle>Aaru: Statistics</PageTitle>
|
||||
|
||||
@*
|
||||
TODO: Group by datagrid
|
||||
*@
|
||||
|
||||
<h1>
|
||||
<p class="text-center" style="color: deeppink;">All archive formats found...</p>
|
||||
</h1>
|
||||
<PieChart @ref="_formatsChart" style="padding: 40px" TItem="long"/>
|
||||
<Grid Data="@ArchiveFormats" FixedHeader="true" PageSize="int.MaxValue" TItem="Archive" Responsive="true"
|
||||
AllowSorting="true" Class="table table-hover table-bordered table-striped table-dark" style="width: 100%;">
|
||||
<GridColumns>
|
||||
<GridColumn TItem="Archive" HeaderText="Filter" PropertyName="@nameof(Archive.Name)">
|
||||
@context.Name
|
||||
</GridColumn>
|
||||
<GridColumn TItem="Archive" HeaderText="Times found" PropertyName="@nameof(Archive.Count)">
|
||||
@context.Count
|
||||
</GridColumn>
|
||||
</GridColumns>
|
||||
</Grid>
|
||||
76
Aaru.Server/Components/Pages/Statistics/Archives.razor.cs
Normal file
76
Aaru.Server/Components/Pages/Statistics/Archives.razor.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using Aaru.Server.Database.Models;
|
||||
using BlazorBootstrap;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using DbContext = Aaru.Server.Database.DbContext;
|
||||
|
||||
namespace Aaru.Server.Components.Pages.Statistics;
|
||||
|
||||
public partial class Archives
|
||||
{
|
||||
PieChart _formatsChart;
|
||||
List<double?> _formatsCounts = [];
|
||||
List<string> _formatsLabels = [];
|
||||
bool _isAlreadyInitialized;
|
||||
List<Archive> ArchiveFormats { get; set; } = [];
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
await using DbContext ctx = await DbContextFactory.CreateDbContextAsync();
|
||||
|
||||
ArchiveFormats = await ctx.Archives.OrderBy(static format => format.Name).ToListAsync();
|
||||
|
||||
await base.OnInitializedAsync();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||||
{
|
||||
if(_isAlreadyInitialized) return;
|
||||
|
||||
_isAlreadyInitialized = true;
|
||||
await using DbContext ctx = await DbContextFactory.CreateDbContextAsync();
|
||||
|
||||
_formatsLabels = await ctx.Archives.OrderByDescending(static o => o.Count)
|
||||
.Take(10)
|
||||
.Select(static v => v.Name)
|
||||
.ToListAsync();
|
||||
|
||||
_formatsCounts = await ctx.Archives.OrderByDescending(static o => o.Count)
|
||||
.Take(10)
|
||||
.Select(static x => (double?)x.Count)
|
||||
.ToListAsync();
|
||||
|
||||
if(_formatsLabels.Count >= 10)
|
||||
{
|
||||
_formatsLabels[9] = "Other";
|
||||
|
||||
_formatsCounts[9] = ctx.Archives.Sum(static o => o.Count) - _formatsCounts.Take(9).Sum();
|
||||
}
|
||||
|
||||
PieChartOptions pieChartOptions = new()
|
||||
{
|
||||
Responsive = true
|
||||
};
|
||||
|
||||
pieChartOptions.Plugins.Title.Text = $"Top {_formatsLabels.Count} archive formats found";
|
||||
pieChartOptions.Plugins.Title.Display = true;
|
||||
|
||||
var chartData = new ChartData
|
||||
{
|
||||
Labels = _formatsLabels,
|
||||
Datasets =
|
||||
[
|
||||
new PieChartDataset
|
||||
{
|
||||
Data = _formatsCounts,
|
||||
BackgroundColor = Common.BackgroundColors,
|
||||
BorderColor = Common.BorderColors,
|
||||
BorderWidth = [1]
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
await _formatsChart.InitializeAsync(chartData, pieChartOptions);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user