mais otimizações 3.0

This commit is contained in:
2025-06-18 22:17:06 -03:00
parent fd9e4324dd
commit afd6f0298c
5 changed files with 149 additions and 3 deletions

View File

@@ -2,6 +2,7 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OpenCand.Config;
using OpenCand.ETL.Repository;
using OpenCand.ETL.Services;
using OpenCand.Parser.Models;
using OpenCand.Parser.Services;
@@ -21,6 +22,8 @@ namespace OpenCand.Parser
private readonly DespesaReceitaService despesaReceitaService;
private readonly ViewRepository viewRepository;
private readonly string BasePath;
public ParserManager(
@@ -32,7 +35,8 @@ namespace OpenCand.Parser
CsvParserService<RedeSocialCSV> redeSocialParserService,
CsvParserService<DespesasCSV> despesaParserService,
CsvParserService<ReceitasCSV> receitaParserService,
DespesaReceitaService despesaReceitaService)
DespesaReceitaService despesaReceitaService,
ViewRepository viewRepository)
{
this.logger = logger;
this.csvSettings = csvSettings.Value;
@@ -43,6 +47,7 @@ namespace OpenCand.Parser
this.despesaParserService = despesaParserService;
this.receitaParserService = receitaParserService;
this.despesaReceitaService = despesaReceitaService;
this.viewRepository = viewRepository;
// Get the base path from either SampleFolder in csvSettings or the BasePath in configuration
BasePath = configuration.GetValue<string>("BasePath") ?? string.Empty;
@@ -73,6 +78,12 @@ namespace OpenCand.Parser
await ParseFolder(receitasDirectory, receitaParserService);
logger.LogInformation("ParseFullDataAsync - Full data parsing completed!");
logger.LogInformation("ParseFullDataAsync - Will refresh materialized views and re-run the analyzes.");
await viewRepository.RefreshMaterializedViews();
logger.LogInformation("ParseFullDataAsync - Materialized views refreshed successfully!");
}
private async Task ParseFolder<CsvObj>(string csvDirectory, CsvParserService<CsvObj> csvParserService)

View File

@@ -79,6 +79,7 @@ namespace OpenCand
services.AddTransient<RedeSocialRepository>();
services.AddTransient<PartidoRepository>();
services.AddTransient<CsvFixerService>();
services.AddTransient<ViewRepository>();
});
}
}

View File

@@ -0,0 +1,36 @@
using Dapper;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration;
using Npgsql;
using OpenCand.Core.Models;
using OpenCand.Repository;
namespace OpenCand.ETL.Repository
{
public class ViewRepository : BaseRepository
{
public ViewRepository(IConfiguration configuration) : base(configuration)
{ }
public async Task RefreshMaterializedViews()
{
using (var connection = new NpgsqlConnection(ConnectionString))
{
// Get all materialized view names
var materializedViews = await connection.QueryAsync<string>(
@"SELECT schemaname || '.' || matviewname as full_name
FROM pg_matviews
ORDER BY schemaname, matviewname");
foreach (var viewName in materializedViews)
{
// Refresh the materialized view
await connection.ExecuteAsync($"REFRESH MATERIALIZED VIEW {viewName}");
// Analyze the materialized view to update statistics
await connection.ExecuteAsync($"ANALYZE {viewName}");
}
}
}
}
}