Melhoria do processo de estimativa de dados faltantes para atender metodologia da CCEE. Adapatação de modelos para poder inserir dados nulos (faltantes) no banco de dados.
386 lines
17 KiB
C#
386 lines
17 KiB
C#
using System.Collections.Concurrent;
|
||
using System.Globalization;
|
||
using System.Text;
|
||
using System.Xml.Linq;
|
||
using Domain;
|
||
using Infrastructure;
|
||
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||
|
||
namespace Application
|
||
{
|
||
public class ProcessarMedicoesUseCase
|
||
{
|
||
private readonly IPostgresRepository _postgresRepository;
|
||
private readonly IAccessRepository _accessRepository;
|
||
private readonly HttpClient _httpClient;
|
||
private readonly RateLimiter _rateLimiter;
|
||
|
||
public ProcessarMedicoesUseCase(
|
||
IPostgresRepository postgresRepository,
|
||
IAccessRepository accessRepository,
|
||
HttpClient httpClient,
|
||
RateLimiter rateLimiter)
|
||
{
|
||
_postgresRepository = postgresRepository;
|
||
_accessRepository = accessRepository;
|
||
_httpClient = httpClient;
|
||
_rateLimiter = rateLimiter;
|
||
}
|
||
|
||
public async Task ExecuteAsync(DateTime dataIni, DateTime dataFim, string caminhoLog, CancellationToken ct)
|
||
{
|
||
var errosPersistentes = new ConcurrentBag<string>();
|
||
var perfis = (await _accessRepository.ObterPerfisAsync(ct)).ToList();
|
||
|
||
_httpClient.DefaultRequestHeaders.Add("SOAPAction", "listarMedidaCincoMinutos");
|
||
var endpoint = new Uri("https://servicos.ccee.org.br/ws/v2/MedidaCincoMinutosBSv2");
|
||
var datas = Enumerable.Range(0, (dataFim - dataIni).Days).Select(i => dataIni.AddDays(i));
|
||
|
||
await Parallel.ForEachAsync(perfis, async (perfil, ct) =>
|
||
{
|
||
Console.WriteLine($"{DateTime.Now}: Iniciado ponto {perfil.CodigoSCDE}");
|
||
if (perfil.Codigo5Minutos == "0" || perfil.Codigo5Minutos == string.Empty)
|
||
{
|
||
Console.WriteLine($"Pular {perfil.CodigoSCDE} - (cod 5 min pendente)");
|
||
errosPersistentes.Add($"{perfil.Codigo5Minutos};{perfil.CodigoSCDE}; cod_5min pendente");
|
||
return;
|
||
}
|
||
|
||
var existentes = await ObterMedicoesComRetry(perfil.CodigoSCDE, dataIni, dataFim, ct, errosPersistentes);
|
||
|
||
foreach (DateTime dia in datas)
|
||
{
|
||
await ProcessarDiaAsync(perfil, dia, existentes, endpoint, errosPersistentes, ct);
|
||
}
|
||
Console.WriteLine($"{DateTime.Now}: Finalizado ponto {perfil.CodigoSCDE}");
|
||
});
|
||
|
||
if (errosPersistentes.Count > 0)
|
||
{
|
||
File.WriteAllLines(caminhoLog, new[] { "Perfil;Ponto;Status;Message" }.Concat(errosPersistentes));
|
||
}
|
||
}
|
||
|
||
private async Task<IDictionary<(string, double, int), Medicao>> ObterMedicoesComRetry(
|
||
string codigoSCDE, DateTime dataIni, DateTime dataFim, CancellationToken ct, ConcurrentBag<string> errosPersistentes)
|
||
{
|
||
int tentativas = 0;
|
||
while (tentativas < 3)
|
||
{
|
||
try
|
||
{
|
||
return await _postgresRepository.ObterMedicoesAsync(codigoSCDE, dataIni, dataFim, ct);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
tentativas++;
|
||
if (tentativas >= 3)
|
||
{
|
||
errosPersistentes.Add($"{codigoSCDE};Erro;{ex.Message.Replace("\n", "-n-")}");
|
||
throw;
|
||
}
|
||
int backoff = (int)Math.Pow(2, tentativas) * 1000;
|
||
Console.WriteLine($"Erro ao acessar Postgres ({ex.Message}), tentativa {tentativas}. Aguardando {backoff / 1000}s...");
|
||
await Task.Delay(backoff, ct);
|
||
}
|
||
}
|
||
return new Dictionary<(string, double, int), Medicao>();
|
||
}
|
||
|
||
private async Task ProcessarDiaAsync(
|
||
Perfil perfil,
|
||
DateTime dia,
|
||
IDictionary<(string, double, int), Medicao> existentes,
|
||
Uri endpoint,
|
||
ConcurrentBag<string> errosPersistentes,
|
||
CancellationToken ct)
|
||
{
|
||
if (perfil.DataDeMigracao > dia)
|
||
{
|
||
Console.WriteLine($"Pular {perfil.CodigoSCDE} - {dia.ToShortDateString()} (antes da migra<72><61>o)");
|
||
errosPersistentes.Add($"{perfil.Codigo5Minutos};{perfil.CodigoSCDE};Fora da data de migra<72><61>o {perfil.DataDeMigracao} x {dia}");
|
||
return;
|
||
}
|
||
|
||
int tentativas = 0;
|
||
bool sucesso = false;
|
||
while (tentativas < 5 && !sucesso)
|
||
{
|
||
try
|
||
{
|
||
string payload = Xml_requisicao(dia, perfil.Codigo5Minutos, perfil.CodigoSCDE, 1);
|
||
var conteudo = new StringContent(payload, Encoding.UTF8, "application/xml");
|
||
|
||
await _rateLimiter.WaitAsync(ct);
|
||
using var response = await _httpClient.PostAsync(endpoint, conteudo, ct);
|
||
string resposta = await response.Content.ReadAsStringAsync();
|
||
|
||
if ((int)response.StatusCode >= 400)
|
||
{
|
||
try
|
||
{
|
||
SoapHelper.VerificarRespostaSOAP(resposta);
|
||
}
|
||
catch (SoapFaultException ex)
|
||
{
|
||
if (ex.ErrorCode == "2003")
|
||
{
|
||
var now = DateTime.UtcNow;
|
||
var delay = 60000 - (now.Second * 1000 + now.Millisecond);
|
||
Console.WriteLine($"!! Limite de requisi<73><69>es atingido. Aguardando at<61> {DateTime.Now.AddMilliseconds(delay)}");
|
||
await Task.Delay(delay, ct);
|
||
continue;
|
||
}
|
||
if (ex.ErrorCode == "4001" || ex.ErrorCode == "2001")
|
||
{
|
||
errosPersistentes.Add($"{perfil.Codigo5Minutos};{perfil.CodigoSCDE};SOAP Fault: {ex.ErrorCode};{ex.ErrorMessage.Replace("\n", "-n-")}");
|
||
break;
|
||
}
|
||
throw;
|
||
}
|
||
}
|
||
|
||
await ProcessarXMLAsync(resposta, dia, perfil.Codigo5Minutos, perfil.CodigoSCDE, existentes, ct, 1);
|
||
sucesso = true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
tentativas++;
|
||
if (tentativas >= 5)
|
||
{
|
||
errosPersistentes.Add($"{perfil.Codigo5Minutos};{perfil.CodigoSCDE};Erro;{ex.Message.Replace("\n", "-n-")}");
|
||
}
|
||
else
|
||
{
|
||
int backoff = (int)Math.Pow(2.4, tentativas) * 1000;
|
||
Console.WriteLine($"Erro na requisi<73><69>o ({ex.Message}), tentativa {tentativas}. Aguardando {backoff / 1000}s...");
|
||
await Task.Delay(backoff, ct);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
private async Task ProcessarXMLAsync(
|
||
string xml,
|
||
DateTime dia,
|
||
string perfil,
|
||
string ponto,
|
||
IDictionary<(string, double, int), Medicao> existentes,
|
||
CancellationToken ct,
|
||
int paginaAtual = 1,
|
||
List<XElement>? acumulador = null,
|
||
int totalPaginas = 1)
|
||
{
|
||
var doc = XDocument.Parse(xml);
|
||
XNamespace ns = "http://xmlns.energia.org.br/BO/v2";
|
||
|
||
int.TryParse(doc.Descendants().FirstOrDefault(e => e.Name.LocalName == "totalPaginas")?.Value, out totalPaginas);
|
||
int.TryParse(doc.Descendants().FirstOrDefault(e => e.Name.LocalName == "numero")?.Value, out paginaAtual);
|
||
|
||
var medidas = doc.Descendants(ns + "medida")
|
||
.Where(x => (string)x.Element(ns + "tipoEnergia") == "L");
|
||
|
||
acumulador ??= new List<XElement>();
|
||
acumulador.AddRange(medidas);
|
||
|
||
if (paginaAtual < totalPaginas)
|
||
{
|
||
// Requisita pr<70>xima p<>gina
|
||
string payload = Xml_requisicao(dia, perfil, ponto, paginaAtual + 1);
|
||
var conteudo = new StringContent(payload, Encoding.UTF8, "application/xml");
|
||
await _rateLimiter.WaitAsync(ct);
|
||
using var resp = await _httpClient.PostAsync("https://servicos.ccee.org.br/ws/v2/MedidaCincoMinutosBSv2", conteudo, ct);
|
||
string proxXml = await resp.Content.ReadAsStringAsync();
|
||
|
||
await ProcessarXMLAsync(proxXml, dia, perfil, ponto, existentes, ct, paginaAtual + 1, acumulador, totalPaginas);
|
||
return;
|
||
}
|
||
|
||
var medidasProcessadas = acumulador
|
||
.Select(m =>
|
||
{
|
||
string origem = m.Element(ns + "coletaMedicao")?.Element(ns + "tipo")?.Element(ns + "nome")?.Value ?? "";
|
||
string pontoMed = m.Element(ns + "medidor")?.Element(ns + "codigo")?.Value ?? "";
|
||
DateTime data = DateTime.Parse(m.Element(ns + "data")?.Value ?? "");
|
||
double diaNum = (data.ToOADate() - data.ToOADate() % 1);
|
||
int minuto = data.Hour * 60 + data.Minute;
|
||
if (minuto == 0) { minuto = 1440; diaNum--; }
|
||
|
||
double.TryParse(m.Element(ns + "energiaAtiva")?.Element(ns + "consumo")?.Element(ns + "valor")?.Value,
|
||
NumberStyles.Any, CultureInfo.InvariantCulture, out double ativa_c);
|
||
double.TryParse(m.Element(ns + "energiaAtiva")?.Element(ns + "geracao")?.Element(ns + "valor")?.Value,
|
||
NumberStyles.Any, CultureInfo.InvariantCulture, out double ativa_g);
|
||
double.TryParse(m.Element(ns + "energiaReativa")?.Element(ns + "consumo")?.Element(ns + "valor")?.Value,
|
||
NumberStyles.Any, CultureInfo.InvariantCulture, out double reat_c);
|
||
double.TryParse(m.Element(ns + "energiaReativa")?.Element(ns + "geracao")?.Element(ns + "valor")?.Value,
|
||
NumberStyles.Any, CultureInfo.InvariantCulture, out double reat_g);
|
||
|
||
return new Medicao(
|
||
pontoMed,
|
||
diaNum,
|
||
minuto,
|
||
origem,
|
||
ativa_c,
|
||
ativa_g,
|
||
reat_c,
|
||
reat_g
|
||
);
|
||
})
|
||
.GroupBy(x => new { x.Ponto, x.DiaNum, x.Minuto })
|
||
.Select(g =>
|
||
{
|
||
var logica = g.FirstOrDefault(x => x.Origem == "Inspe<70><65>o L<>gica");
|
||
return logica ?? g.First();
|
||
}).ToList();
|
||
|
||
var minutosEsperados = new[] { 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60 };
|
||
|
||
var medidasPorHora = medidasProcessadas
|
||
.GroupBy(m => new { m.Ponto, m.DiaNum, Hora = (m.Minuto - 5) / 60 })
|
||
.ToList();
|
||
|
||
var medidasComEstimativa = new List<Medicao>();
|
||
for (int hora = 0; hora < 24; hora++)
|
||
{
|
||
var grupoHora = medidasPorHora.Where(h => h.Key.Hora == hora).ToList();
|
||
var lista = grupoHora.SelectMany(g => g).OrderBy(m => m.Minuto).ToList();
|
||
var minutosPresentes = lista.Select(m => m.Minuto).ToHashSet();
|
||
var minutosEsperadosAbsolutos = minutosEsperados.Select(m => m + (60 * hora)).ToList();
|
||
var faltantes = minutosEsperadosAbsolutos.Except(minutosPresentes).OrderBy(m => m).ToList();
|
||
|
||
// Use apenas valores reais para interpola<6C><61>o
|
||
var reais = lista.ToDictionary(m => m.Minuto, m => m);
|
||
|
||
var estimadas = new List<Medicao>();
|
||
|
||
foreach (var faltante in faltantes)
|
||
{
|
||
if (faltantes.Count > 3)
|
||
{
|
||
// Se mais de 3 faltantes na hora, n<>o faz estimativa
|
||
var estimada = new Medicao(
|
||
ponto + "P",
|
||
(dia.ToOADate() - dia.ToOADate() % 1),
|
||
faltante,
|
||
"Faltante",
|
||
null,
|
||
null,
|
||
null,
|
||
null
|
||
);
|
||
estimadas.Add(estimada);
|
||
continue;
|
||
}
|
||
else
|
||
{
|
||
// Busca anterior real
|
||
var anterior = reais.Values.Where(m => m.Minuto < faltante).OrderByDescending(m => m.Minuto).FirstOrDefault();
|
||
// Busca posterior real
|
||
var posterior = reais.Values.Where(m => m.Minuto > faltante).OrderBy(m => m.Minuto).FirstOrDefault();
|
||
|
||
var ativaConsumo = Interpolar(anterior?.Minuto, anterior?.AtivaConsumo, posterior?.Minuto, posterior?.AtivaConsumo, faltante) ?? 0;
|
||
var ativaGeracao = Interpolar(anterior?.Minuto, anterior?.AtivaGeracao, posterior?.Minuto, posterior?.AtivaGeracao, faltante) ?? 0;
|
||
var reativaConsumo = Interpolar(anterior?.Minuto, anterior?.ReativaConsumo, posterior?.Minuto, posterior?.ReativaConsumo, faltante) ?? 0;
|
||
var reativaGeracao = Interpolar(anterior?.Minuto, anterior?.ReativaGeracao, posterior?.Minuto, posterior?.ReativaGeracao, faltante) ?? 0;
|
||
|
||
var estimada = new Medicao(
|
||
grupoHora.First().Key.Ponto,
|
||
grupoHora.First().Key.DiaNum,
|
||
faltante,
|
||
"Estimado",
|
||
ativaConsumo,
|
||
ativaGeracao,
|
||
reativaConsumo,
|
||
reativaGeracao
|
||
);
|
||
estimadas.Add(estimada);
|
||
}
|
||
}
|
||
|
||
// Adiciona todos (originais + estimados) ao resultado final
|
||
medidasComEstimativa.AddRange(lista);
|
||
medidasComEstimativa.AddRange(estimadas);
|
||
}
|
||
|
||
var novos = new List<Medicao>();
|
||
var alterados = new List<Medicao>();
|
||
|
||
foreach (var m in medidasComEstimativa)
|
||
{
|
||
var chave = (m.Ponto, m.DiaNum, m.Minuto);
|
||
|
||
if (!existentes.TryGetValue(chave, out var existente))
|
||
{
|
||
novos.Add(m);
|
||
}
|
||
else
|
||
{
|
||
if (existente.Origem != m.Origem ||
|
||
existente.AtivaConsumo != m.AtivaConsumo ||
|
||
existente.AtivaGeracao != m.AtivaGeracao ||
|
||
existente.ReativaConsumo != m.ReativaConsumo ||
|
||
existente.ReativaGeracao != m.ReativaGeracao)
|
||
{
|
||
alterados.Add(m);
|
||
}
|
||
}
|
||
}
|
||
|
||
if (novos.Any())
|
||
{
|
||
await _postgresRepository.InserirMedicoesAsync(novos, ct);
|
||
Console.WriteLine($"Inserido {novos.Count} registros. Ponto {ponto}. Dia {dia}");
|
||
}
|
||
|
||
if (alterados.Any())
|
||
{
|
||
await _postgresRepository.AtualizarMedicoesAsync(alterados, ct);
|
||
Console.WriteLine($"Atualizado {alterados.Count} registros. Ponto {ponto}. Dia {dia}");
|
||
}
|
||
}
|
||
private static string Xml_requisicao(DateTime data_req, string perfil, string cod_ponto, int pagina)
|
||
{
|
||
string cam_ent, tex_req, sdat_req;
|
||
cam_ent = @"X:\Back\Plataforma de Integra<72><61>o CCEE\RequestPaginate.txt";
|
||
cod_ponto += "P";
|
||
sdat_req = data_req.ToString("yyyy-MM-ddT00:00:00");
|
||
tex_req = File.ReadAllText(cam_ent);
|
||
tex_req = tex_req.Replace("DATAALTERADA", sdat_req);
|
||
tex_req = tex_req.Replace("PONTOMEDICAO", cod_ponto);
|
||
tex_req = tex_req.Replace("CODPERFIL", perfil);
|
||
tex_req = tex_req.Replace("PAGNUM", pagina.ToString());
|
||
return tex_req;
|
||
}
|
||
private static double? Interpolar(
|
||
double? xAnterior, double? yAnterior,
|
||
double? xPosterior, double? yPosterior,
|
||
double xProcurado)
|
||
{
|
||
if (xAnterior.HasValue && yAnterior.HasValue &&
|
||
(!xPosterior.HasValue || !yPosterior.HasValue))
|
||
{
|
||
return yAnterior.Value;
|
||
}
|
||
|
||
if (xPosterior.HasValue && yPosterior.HasValue &&
|
||
(!xAnterior.HasValue || !yAnterior.HasValue))
|
||
{
|
||
return yPosterior.Value;
|
||
}
|
||
|
||
if (xAnterior.HasValue && yAnterior.HasValue &&
|
||
xPosterior.HasValue && yPosterior.HasValue)
|
||
{
|
||
if (xPosterior.Value == xAnterior.Value)
|
||
throw new ArgumentException("xAnterior e xPosterior n<>o podem ser iguais (divis<69>o por zero).");
|
||
|
||
double yProcurado = yAnterior.Value +
|
||
((yPosterior.Value - yAnterior.Value) / (xPosterior.Value - xAnterior.Value)) *
|
||
(xProcurado - xAnterior.Value);
|
||
|
||
return yProcurado;
|
||
}
|
||
|
||
return null;
|
||
}
|
||
}
|
||
} |