using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using Dapper; using DataModel.Models; using DataModel.Process; using WorkerService.Models; namespace WorkerService.Process { /// /// Data update entry management functionality for data update processor /// public partial class UpdateProcessor { /// /// SQL to close any open update entries (identified by number records = -2) /// private const string SQL_CLOSE_OPEN_UPDATE_ENTRIES = @" UPDATE dbo.DataUpdate SET EndDT = GETDATE(), WasSuccessful = 0, NumberRecords = -1 WHERE NumberRecords = -2"; /// /// Closes any open data update entries /// private static void CloseOpenUpdateEntries() { try { using (SqlConnection connection = LotFinderDB.GetConnection()) { //Close any open update entries connection.Execute(SQL_CLOSE_OPEN_UPDATE_ENTRIES); } } catch (Exception error) { //Log but do not forward error logger.Error("CloseOpenUpdateEntries: failed to close open data update entries: {0}.", error.Message); } } /// /// SQL to purge data update records /// private const string SQL_PURGE_UPDATE_ENTRIES = @" DELETE FROM dbo.DataUpdate WHERE StartDT < DATEADD(DAY, @maxAge * -1, GETDATE())"; /// /// Purges any data update entries older than given max age /// /// Maximum entry age (in days) private static void PurgeUpdateEntries(int maxAge) { try { using (SqlConnection connection = LotFinderDB.GetConnection()) { //Purge the records connection.Execute(SQL_PURGE_UPDATE_ENTRIES, new { maxAge }); } } catch (Exception error) { //Log but do not forward error logger.Error("PurgeUpdateEntries: failed to purge data update entries older than {0} days: {1}.", maxAge, error.Message); } } /// /// SQL to insert data update record /// private const string SQL_LOG_DATA_UPDATE_START = @" INSERT INTO dbo.DataUpdate(SourceSystem, SourceData, TableName, StartDT, EndDT, UpdateType, WasSuccessful, NumberRecords) OUTPUT INSERTED.* VALUES(@sourceSystem, @sourceData, @tableName, GETDATE(), '1970-01-01', @updateType, 0, -2);"; /// /// Logs the data update entry at the start of the update /// /// SQL connection to execute commands on /// Data update entry to log public static void LogDataUpdateStart(SqlConnection connection, DataUpdate dataUpdate) { try { //Update the record DataUpdate inserted = connection.QueryFirst(SQL_LOG_DATA_UPDATE_START, new { sourceSystem = dataUpdate.SourceSystem, sourceData = dataUpdate.SourceData, tableName = dataUpdate.TableName, updateType = dataUpdate.UpdateType }); //Copy output values to model dataUpdate.ID = inserted.ID; dataUpdate.StartDT = inserted.StartDT; } catch (Exception error) { //Log but do not forward error logger.Error("LogDataUpdateStart: failed to log starting data update entry: {0}.", error.Message); } } /// /// SQL to update data update record with results /// private const string SQL_LOG_DATA_UPDATE_END = @" UPDATE dbo.DataUpdate SET EndDT = GETDATE(), WasSuccessful = @wasSuccessful, NumberRecords = @numberRecords OUTPUT INSERTED.* WHERE ID = @id"; /// /// Logs the data update entry at the end of the update /// /// SQL connection to execute commands on /// Data update entry to log public static void LogDataUpdateEnd(SqlConnection connection, DataUpdate dataUpdate) { try { //Update the record DataUpdate updated = connection.QueryFirst(SQL_LOG_DATA_UPDATE_END, new { id = dataUpdate.ID, wasSuccessful = dataUpdate.WasSuccessful, numberRecords = dataUpdate.NumberRecords }); //Copy output values to model dataUpdate.EndDT = updated.EndDT; } catch (Exception error) { //Log but do not forward error logger.Error("LogDataUpdateEnd: failed to log ending data update entry: {0}.", error.Message); } } /// /// Gets set of last successful data updates for all tables /// /// SQL connection to execute commands on /// Set of last successful data updates for all tables private static List GetLastDataUpdates(SqlConnection connection) { return connection.Query("SELECT * FROM dbo.LastDataUpdates").ToList(); } } }