57 lines
1.8 KiB
C#
57 lines
1.8 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().ToList(); // Materialize the query
|
|
var data = GetData(updates, dateTime);
|
|
|
|
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
|
|
|
|
new PdfBuilder(filePath).AddHeader("Card Updates") // More descriptive header
|
|
.AddPieChart("Number of Times Card Updated", data) // Corrected caption
|
|
.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)
|
|
{
|
|
var result = new List<(string Caption, double Value)>();
|
|
|
|
var distinctCards = updates.Where(u => u.LastUpdate.Date == date.Date) // Filter by date
|
|
.GroupBy(u => u.CardID)
|
|
.Select(g => new { CardID = g.Key, Count = g.Count() })
|
|
.ToList();
|
|
|
|
|
|
foreach (var cardData in distinctCards)
|
|
{
|
|
result.Add(($"Card {cardData.CardID}", cardData.Count));
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
}
|