44 lines
1.4 KiB
C#
44 lines
1.4 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using LDBproject.Entities;
|
|
using LDBproject.Repositories;
|
|
using LDBproject.Reports;
|
|
|
|
internal class ChartReport
|
|
{
|
|
private readonly IUpdateRep _updR;
|
|
private readonly ILogger<ChartReport> _logger;
|
|
|
|
public ChartReport(IUpdateRep updR, ILogger<ChartReport> logger)
|
|
{
|
|
_updR = updR ?? throw new ArgumentNullException(nameof(updR));
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
}
|
|
|
|
public bool CreateChart(string filePath, DateTime dateTime)
|
|
{
|
|
try
|
|
{
|
|
var updates = _updR.GetUpdateList(dateFrom: dateTime.Date, dateTo: dateTime.Date.AddDays(1)).ToList(); // Materialize the query
|
|
var data = GetData(updates, dateTime);
|
|
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
|
new PdfBuilder(filePath).AddHeader("Card Updates")
|
|
.AddPieChart("Number of Times Card Updated", data)
|
|
.Build();
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError(ex, "Error creating chart.");
|
|
return false;
|
|
}
|
|
}
|
|
private List<(string Caption, double Value)> GetData(List<UpdateC> updates, DateTime date)
|
|
{
|
|
return updates.GroupBy(x => x.CardID)
|
|
.Select(group => (
|
|
Caption: $"Card n_{group.Key}",
|
|
Value: (double)group.Count()
|
|
)).ToList();
|
|
}
|
|
}
|