Skip to content

Commit

Permalink
Added retailer statistics export
Browse files Browse the repository at this point in the history
  • Loading branch information
davewalker5 committed Nov 30, 2023
1 parent 655a23a commit fcc35b0
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/MusicCatalogue.Api/Controllers/ExportController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ public class ExportController : Controller
private readonly IBackgroundQueue<ArtistStatisticsExportWorkItem> _artistStatisticsQueue;
private readonly IBackgroundQueue<GenreStatisticsExportWorkItem> _genreStatisticsQueue;
private readonly IBackgroundQueue<MonthlySpendExportWorkItem> _monthlySpendQueue;
private readonly IBackgroundQueue<RetailerStatisticsExportWorkItem> _retailerStatisticsQueue;

public ExportController(
IBackgroundQueue<CatalogueExportWorkItem> catalogueQueue,
IBackgroundQueue<ArtistStatisticsExportWorkItem> artistStatisticsQueue,
IBackgroundQueue<GenreStatisticsExportWorkItem> genreStatisticsQueue,
IBackgroundQueue<MonthlySpendExportWorkItem> monthlySpendQueue
)
IBackgroundQueue<MonthlySpendExportWorkItem> monthlySpendQueue,
IBackgroundQueue<RetailerStatisticsExportWorkItem> retailerStatisticsQueue)
{
_catalogueQueue = catalogueQueue;
_artistStatisticsQueue = artistStatisticsQueue;
_genreStatisticsQueue = genreStatisticsQueue;
_monthlySpendQueue = monthlySpendQueue;
_retailerStatisticsQueue = retailerStatisticsQueue;
}

[HttpPost]
Expand Down Expand Up @@ -76,5 +78,17 @@ public IActionResult ExportMonthySpendReport([FromBody] MonthlySpendExportWorkIt
_monthlySpendQueue.Enqueue(item);
return Accepted();
}

[HttpPost]
[Route("retailerstatistics")]
public IActionResult ExportRetailerStatisticsReport([FromBody] RetailerStatisticsExportWorkItem item)
{
// Set the job name used in the job status record
item.JobName = "Retailer Statistics Export";

// Queue the work item
_retailerStatisticsQueue.Enqueue(item);
return Accepted();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace MusicCatalogue.Api.Entities
{
public class RetailerStatisticsExportWorkItem : BackgroundWorkItem
{
public string FileName { get; set; } = "";
public bool WishList { get; set; }
}
}
4 changes: 4 additions & 0 deletions src/MusicCatalogue.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ public static void Main(string[] args)
builder.Services.AddSingleton<IBackgroundQueue<MonthlySpendExportWorkItem>, BackgroundQueue<MonthlySpendExportWorkItem>>();
builder.Services.AddHostedService<MonthlySpendExportService>();

// Add the retailer statistics report exporter hosted service
builder.Services.AddSingleton<IBackgroundQueue<RetailerStatisticsExportWorkItem>, BackgroundQueue<RetailerStatisticsExportWorkItem>>();
builder.Services.AddHostedService<RetailerStatisticsExportService>();

// Configure JWT
byte[] key = Encoding.ASCII.GetBytes(settings!.Secret);
builder.Services.AddAuthentication(x =>
Expand Down
47 changes: 47 additions & 0 deletions src/MusicCatalogue.Api/Services/RetailerStatisticsExportService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using Microsoft.Extensions.Options;
using MusicCatalogue.Api.Entities;
using MusicCatalogue.Api.Interfaces;
using MusicCatalogue.Entities.Config;
using MusicCatalogue.Entities.Interfaces;
using MusicCatalogue.Entities.Reporting;
using MusicCatalogue.Logic.DataExchange.Generic;
using System.Diagnostics.CodeAnalysis;

namespace MusicCatalogue.Api.Services
{
[ExcludeFromCodeCoverage]
public class RetailerStatisticsExportService : BackgroundQueueProcessor<RetailerStatisticsExportWorkItem>
{
private readonly MusicApplicationSettings _settings;
public RetailerStatisticsExportService(
ILogger<BackgroundQueueProcessor<RetailerStatisticsExportWorkItem>> logger,
IBackgroundQueue<RetailerStatisticsExportWorkItem> queue,
IServiceScopeFactory serviceScopeFactory,
IOptions<MusicApplicationSettings> settings)
: base(logger, queue, serviceScopeFactory)
{
_settings = settings.Value;
}

/// <summary>
/// Export the retailer statistics report
/// </summary>
/// <param name="item"></param>
/// <param name="factory"></param>
/// <returns></returns>
protected override async Task ProcessWorkItem(RetailerStatisticsExportWorkItem item, IMusicCatalogueFactory factory)
{
// Get the report data
MessageLogger.LogInformation("Retrieving the retailer statistics report for export");
var records = await factory.RetailerStatistics.GenerateReportAsync(item.WishList, 1, int.MaxValue);

// Construct the full path to the export file
var filePath = Path.Combine(_settings.ReportsExportPath, item.FileName);

// Export the report
var exporter = new CsvExporter<RetailerStatistics>();
exporter.Export(records, filePath, ',');
MessageLogger.LogInformation("Retailer statistics report export completed");
}
}
}

0 comments on commit fcc35b0

Please sign in to comment.