private void SequentialSend(StringBuilder sbEvent, Stopwatch timerTokenRefresh, APIMethods api, DbCommand command) { using (var reader = command.ExecuteReader()) { while (reader.Read()) { // conversion to DTO if (VentaDTO.TryFromDBRecord(reader, out VentaDTO venta)) { var ventaStr = venta.ToString(); Log(ventaStr, false); sbEvent.Append(ventaStr); var result = api.PostVenta(venta); if (!result) { var msg = $"ERROR while sending IdVenta {reader["IdentificadorVenta"]}"; Log(msg, false); sbEvent.AppendLine(" - ERROR"); LogToEventViewer(new Exception(msg)); throw new Exception(msg); } else { sbEvent.AppendLine(" - OK"); } RefreshAPIToken(timerTokenRefresh, api); } else { var msg = $"ERROR while building DTO for IdVenta record with Id: {reader["IdentificadorVenta"]}"; Log(msg); var ex = new Exception(msg); LogToEventViewer(ex); throw ex; } } } }
private void SendVenta2API(Stopwatch timerTokenRefresh, APIMethods api, DbDataReader reader) { // conversion to DTO if (VentaDTO.TryFromDBRecord(reader, out VentaDTO venta)) { // convertir a JSON (WebAPI will receive this) Debug.WriteLine(venta.ToString()); var result = api.PostVenta(venta); if (!result) { var msg = $"ERROR enviando IdVenta {reader["IdentificadorVenta"]}"; Debug.WriteLine(msg); throw new Exception(msg); } if (timerTokenRefresh.Elapsed.TotalMinutes > 4) { //refresh token Debug.WriteLine("Refreshing api token"); api.SetToken(); if (api.TokenData == null) { throw new System.Exception($"Could not refresh token from WebAPI, endpoint root was {_config.APIEndpoint}"); } else { timerTokenRefresh.Restart(); } } } else { var msg = $"ERROR de conversion a DTO en IdVenta {reader["IdentificadorVenta"]}"; Debug.WriteLine(msg); throw new Exception(msg); } }
private void BatchSend(StringBuilder sbEvent, Stopwatch timer, APIMethods api, DbCommand command) { List <VentaDTO> ventaList = new List <VentaDTO>(); using (var reader = command.ExecuteReader()) { while (reader.Read()) { VentaDTO.TryFromDBRecord(reader, out VentaDTO venta); ventaList.Add(venta); } } int ventaCount = ventaList.Count; const double pageSize = 10; double pageTop = Math.Ceiling(ventaCount / pageSize); for (int page = 0; page < pageTop; page++) { List <VentaDTO> ventaListPage = SendPage(api, ventaList, pageSize, pageTop, page); sbEvent.AppendLine($"Page {page + 1}: records: {JsonConvert.SerializeObject(ventaListPage)}"); RefreshAPIToken(timer, api); } }
public void TestSelectData() { int total = 0; try { this.ConnectionOpen(false); var command = _connection.CreateCommand(); command.CommandType = System.Data.CommandType.Text; command.CommandText = _countCommand; var twoDaysAgo = DateTime.Now.Subtract(TimeSpan.FromDays(2)); SqlParameter param = new SqlParameter { ParameterName = "@fromDate", Value = twoDaysAgo }; command.Parameters.Add(param); total = (int)command.ExecuteScalar(); // total number of recorsd for pagin command = _connection.CreateCommand(); command.CommandType = System.Data.CommandType.Text; command.CommandText = _commandSelect; int daysToRepeat = 150; SqlParameter weeks = new SqlParameter { ParameterName = "@daysToRepeat", Value = daysToRepeat * -1 }; command.Parameters.Add(weeks); using (var reader = command.ExecuteReader()) { while (reader.Read()) { var value = reader["IdentificadorVenta"]; Debug.WriteLine($"{reader["IdentificadorVenta"]}: CodProducto:{reader["CodProducto"]}; IdLinea:{reader["IdLinea"]}"); // conversion to DTO if (VentaDTO.TryFromDBRecord(reader, out VentaDTO venta)) { // convertir a JSON (WebAPI will receive this) var ventaJson = JsonConvert.SerializeObject(venta); Debug.WriteLine($"DTO en json: {ventaJson}"); } else { Debug.WriteLine($"ERROR de conversion a DTO en IdVenta {reader["IdentificadorVenta"]}"); } } } } finally { if (_connection.State == System.Data.ConnectionState.Open) { _connection.Close(); } _connection.Dispose(); } }
public async Task FullTestInBatch() { var configCS = ConfigurationManager.ConnectionStrings["CS_FarmaticDB"]; if (null == configCS) { throw new Exception("No app configuration found"); } if (!int.TryParse(ConfigurationManager.AppSettings["DaysToResend"], out int daysResend)) { throw new Exception($"DaysToResend config was not found or invalid, it must be a positive int"); } ConfigAndSetStartDateToRetrieve( configCS, daysResend, out Stopwatch timerTokenRefresh, out APIMethods api, out GetLastResult _lastExportDate); DbCommand command = PrepareDbCommand(ref daysResend, _lastExportDate); //using (var reader = command.ExecuteReader(System.Data.CommandBehavior.SequentialAccess)) // for long streams Stopwatch timer = new Stopwatch(); timer.Start(); List <VentaDTO> ventaList = new List <VentaDTO>(); using (var reader = await command.ExecuteReaderAsync()) { while (reader.Read()) { VentaDTO.TryFromDBRecord(reader, out VentaDTO venta); ventaList.Add(venta); } } int ventaCount = ventaList.Count; const double pageSize = 10; double pageTop = Math.Ceiling(ventaCount / pageSize); for (int page = 0; page < pageTop; page++) { List <VentaDTO> ventaListPage = null; if (page == pageTop - 1) { if (int.TryParse((ventaList.Count - (pageSize * (pageTop - 1))).ToString(), out int lastPageRecords)) { ventaListPage = ventaList .GetRange(int.Parse((page * pageSize).ToString()), lastPageRecords); } else { throw new Exception("ERROR: Paging last page error on type conversion"); } } else { ventaListPage = ventaList .GetRange(int.Parse((page * pageSize).ToString()), int.Parse(pageSize.ToString())); } var ventaListJson = new VentaDTOBatch { Ventas = ventaListPage }; SendVentaPage2API(timer, api, ventaListJson); } timer.Stop(); Debug.WriteLine($"Processing time: {timer.Elapsed.TotalSeconds} secs"); }