Skip to content

Commit

Permalink
Add model export task
Browse files Browse the repository at this point in the history
  • Loading branch information
yorickdewid committed Jan 3, 2024
1 parent cb84167 commit dcb360b
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/FunderMaps.Worker/TaskRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ public async Task StartAsync(CancellationToken cancellationToken)
break;
}

case "modelexport":
{
logger.LogInformation("Running model export task");

using var scope = serviceScopeFactory.CreateScope();
await ActivatorUtilities.CreateInstance<ModelExportTask>(scope.ServiceProvider).RunAsync(cancellationToken);
break;
}

case "productexport":
{
logger.LogInformation("Running product export task");
Expand Down
63 changes: 63 additions & 0 deletions src/FunderMaps.Worker/Tasks/ModelExportTask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using FunderMaps.Core;
using FunderMaps.Core.Helpers;
using FunderMaps.Core.Interfaces;
using FunderMaps.Core.Interfaces.Repositories;
using FunderMaps.Core.Types.Products;
using Microsoft.Extensions.Logging;

namespace FunderMaps.Worker.Tasks;

/// <summary>
/// Construct new instance.
/// </summary>
internal sealed class ModelExportTask(
IBlobStorageService blobStorageService,
IAnalysisRepository analysisRepository,
ILogger<ModelExportTask> logger) : ITaskService
{
// TODO: Move into service/helper.
/// <summary>
/// Write CSV file.
/// </summary>
/// <param name="filePath">File path.</param>
/// <param name="productCalls">Product calls.</param>
/// <param name="cancellationToken">Cancellation token.</param>
private static async Task WriteCsvAsync(string filePath, IAsyncEnumerable<AnalysisProduct> productCalls, CancellationToken cancellationToken = default)
{
using var writer = new StreamWriter(filePath);

var csvConfig = new CsvHelper.Configuration.CsvConfiguration(System.Globalization.CultureInfo.InvariantCulture)
{
HasHeaderRecord = true,
};

using var csv = new CsvHelper.CsvWriter(writer, csvConfig);
await csv.WriteRecordsAsync(productCalls, cancellationToken);
}

/// <summary>
/// Triggered when the application host is ready to start the service.
/// </summary>
public async Task RunAsync(CancellationToken cancellationToken)
{
try
{
string filePath = $"model_export.csv";

await WriteCsvAsync(filePath, analysisRepository.ListAllAsync(Navigation.All), cancellationToken);

DateTime currentDate = DateTime.Now;
string dateString = currentDate.ToString("yyyy-MM-dd");

logger.LogInformation("Uploading model export");

await blobStorageService.StoreFileAsync($"model/export_{dateString}.csv", filePath);
}
finally
{
var currentDirectory = Directory.GetCurrentDirectory();

FileHelper.DeleteFilesWithExtension(currentDirectory, "csv");
}
}
}
1 change: 0 additions & 1 deletion src/FunderMaps.Worker/Tasks/ProductExportTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace FunderMaps.Worker.Tasks;
/// <summary>
/// Construct new instance.
/// </summary>
// [SingleShotTask("product-export")]
internal sealed class ProductExportTask(
IBlobStorageService blobStorageService,
ITelemetryRepository telemetryRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,9 @@ public async Task<bool> GetRiskIndexAsync(string id)
return true;
}

public IAsyncEnumerable<AnalysisProduct> ListAllAsync(Navigation navigation)
=> memory.Values.ToAsyncEnumerable();

public async Task<bool> RegisterProductMatch(string buildingId, string id, string product, Guid tenantId)
{
await Task.CompletedTask;
Expand Down

0 comments on commit dcb360b

Please sign in to comment.