26ff8d9b4f
Set up repository with legacy .NET Framework 4.8 source (OLD/), new .NET 10 Blazor solution (NEW/), OpenSpec specifications, documentation, and project configuration.
159 lines
6.1 KiB
C#
Executable File
159 lines
6.1 KiB
C#
Executable File
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
|
|
{
|
|
/// <summary>
|
|
/// Data update entry management functionality for data update processor
|
|
/// </summary>
|
|
public partial class UpdateProcessor
|
|
{
|
|
/// <summary>
|
|
/// SQL to close any open update entries (identified by number records = -2)
|
|
/// </summary>
|
|
private const string SQL_CLOSE_OPEN_UPDATE_ENTRIES = @"
|
|
UPDATE dbo.DataUpdate
|
|
SET EndDT = GETDATE(), WasSuccessful = 0, NumberRecords = -1
|
|
WHERE NumberRecords = -2";
|
|
|
|
/// <summary>
|
|
/// Closes any open data update entries
|
|
/// </summary>
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// SQL to purge data update records
|
|
/// </summary>
|
|
private const string SQL_PURGE_UPDATE_ENTRIES = @"
|
|
DELETE FROM dbo.DataUpdate
|
|
WHERE StartDT < DATEADD(DAY, @maxAge * -1, GETDATE())";
|
|
|
|
/// <summary>
|
|
/// Purges any data update entries older than given max age
|
|
/// </summary>
|
|
/// <param name="maxAge">Maximum entry age (in days)</param>
|
|
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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// SQL to insert data update record
|
|
/// </summary>
|
|
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);";
|
|
|
|
/// <summary>
|
|
/// Logs the data update entry at the start of the update
|
|
/// </summary>
|
|
/// <param name="connection">SQL connection to execute commands on</param>
|
|
/// <param name="dataUpdate">Data update entry to log</param>
|
|
public static void LogDataUpdateStart(SqlConnection connection, DataUpdate dataUpdate)
|
|
{
|
|
try
|
|
{
|
|
//Update the record
|
|
DataUpdate inserted = connection.QueryFirst<DataUpdate>(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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// SQL to update data update record with results
|
|
/// </summary>
|
|
private const string SQL_LOG_DATA_UPDATE_END = @"
|
|
UPDATE dbo.DataUpdate
|
|
SET EndDT = GETDATE(), WasSuccessful = @wasSuccessful, NumberRecords = @numberRecords
|
|
OUTPUT INSERTED.*
|
|
WHERE ID = @id";
|
|
|
|
/// <summary>
|
|
/// Logs the data update entry at the end of the update
|
|
/// </summary>
|
|
/// <param name="connection">SQL connection to execute commands on</param>
|
|
/// <param name="dataUpdate">Data update entry to log</param>
|
|
public static void LogDataUpdateEnd(SqlConnection connection, DataUpdate dataUpdate)
|
|
{
|
|
try
|
|
{
|
|
//Update the record
|
|
DataUpdate updated = connection.QueryFirst<DataUpdate>(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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets set of last successful data updates for all tables
|
|
/// </summary>
|
|
/// <param name="connection">SQL connection to execute commands on</param>
|
|
/// <returns>Set of last successful data updates for all tables</returns>
|
|
private static List<LastDataUpdate> GetLastDataUpdates(SqlConnection connection)
|
|
{
|
|
return connection.Query<LastDataUpdate>("SELECT * FROM dbo.LastDataUpdates").ToList();
|
|
}
|
|
}
|
|
}
|