Init
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
using FinlyticAssets.Models;
|
||||
using FinlyticAssets.Util;
|
||||
|
||||
namespace FinlyticAssets.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Provides methods for generating and maintaining the indexed asset reference file used for pre-filtering.
|
||||
/// </summary>
|
||||
public interface IAssetsIndexService
|
||||
{
|
||||
/// <summary>
|
||||
/// Recreates the index file containing basic asset identifiers (ISIN and Name) for all active, valid assets.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
public Task ReCreateIndexFileAsync(CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implements the <see cref="IAssetsIndexService"/> to maintain local asset index references.
|
||||
/// </summary>
|
||||
public class AssetsIndexService : IAssetsIndexService
|
||||
{
|
||||
private readonly ILogger<AssetsIndexService> _logger;
|
||||
private readonly IAssetsDbService _assetsDbService;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AssetsIndexService"/> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">The logger for documenting indexing events and errors.</param>
|
||||
/// <param name="assetsDbService">The database service to query the assets from.</param>
|
||||
public AssetsIndexService(ILogger<AssetsIndexService> logger, IAssetsDbService assetsDbService)
|
||||
{
|
||||
_logger = logger;
|
||||
_assetsDbService = assetsDbService;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task ReCreateIndexFileAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var assets = await _assetsDbService.GetAllValidAssetsAsync();
|
||||
if (assets == null || !assets.Any())
|
||||
{
|
||||
_logger.LogWarning("No valid assets found in the database to index.");
|
||||
return;
|
||||
}
|
||||
|
||||
var indexAssets = assets.Select(a => new AssetIndex(a.Isin, a.Name)).ToList();
|
||||
|
||||
var directoryPath = Volumes.IndexRelativePath;
|
||||
var filePath = Path.Combine(directoryPath, "index.json");
|
||||
|
||||
if (!Directory.Exists(directoryPath))
|
||||
{
|
||||
Directory.CreateDirectory(directoryPath);
|
||||
}
|
||||
|
||||
using (var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None, 4096,
|
||||
useAsync: true))
|
||||
{
|
||||
await JsonSerializer.SerializeAsync(fileStream, indexAssets, cancellationToken: cancellationToken);
|
||||
}
|
||||
|
||||
_logger.LogInformation("Successfully recreated asset index file with {Count} entries at {Path}",
|
||||
indexAssets.Count, filePath);
|
||||
}
|
||||
catch (IOException ex)
|
||||
{
|
||||
_logger.LogError(ex, "Disk I/O error occurred while writing the asset index file.");
|
||||
throw;
|
||||
}
|
||||
catch (JsonException ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to serialize the asset index data to JSON.");
|
||||
throw;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "An unexpected error occurred while recreating the asset index file.");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user