From eba72a955b91db4120189ce0d46545593a024f2b Mon Sep 17 00:00:00 2001 From: Mirko Bellabarba Date: Thu, 19 Mar 2020 10:57:13 +0100 Subject: [PATCH] wip #49 * refactored the code * moved oracle integration into core --- .../Elfo.Wardein.Abstractions.csproj | 5 +- Elfo.Wardein.Core/Elfo.Wardein.Core.csproj | 7 +- .../DB/Oracle/Abstractions/IOracleService.cs | 22 +++ .../DB/Oracle/DapperOracleService.cs | 47 ++++-- .../Oracle/OracleConnectionConfiguration.cs | 77 ++++++++- .../DB/Oracle/OracleHelper.cs | 136 +++++++++++++++ .../FyleSystem}/IOHelper.cs | 0 .../OracleConnectionExtensionMethods.cs | 20 +++ .../Elfo.Wardein.Integrations.Tests.csproj | 1 + .../GetDataFromSourceTest.cs | 11 +- .../Elfo.Wardein.Integrations.csproj | 10 +- .../Oracle.Integration/Extensions.cs | 5 +- .../Oracle.Integration/OracleIntegration.cs | 155 ++---------------- .../HeartBeat/HeartBeatWatcher.cs | 29 ++-- 14 files changed, 334 insertions(+), 191 deletions(-) create mode 100644 Elfo.Wardein.Core/Helpers/ExternalResources/DB/Oracle/Abstractions/IOracleService.cs rename Elfo.Wardein.Integrations/Oracle.Integration/IOracleService.cs => Elfo.Wardein.Core/Helpers/ExternalResources/DB/Oracle/DapperOracleService.cs (52%) rename Elfo.Wardein.Integrations/Oracle.Integration/OracleIntegrationConfiguration.cs => Elfo.Wardein.Core/Helpers/ExternalResources/DB/Oracle/OracleConnectionConfiguration.cs (66%) create mode 100644 Elfo.Wardein.Core/Helpers/ExternalResources/DB/Oracle/OracleHelper.cs rename Elfo.Wardein.Core/Helpers/{ => ExternalResources/FyleSystem}/IOHelper.cs (100%) create mode 100644 Elfo.Wardein.Core/Helpers/OracleConnectionExtensionMethods.cs diff --git a/Elfo.Wardein.Abstractions/Elfo.Wardein.Abstractions.csproj b/Elfo.Wardein.Abstractions/Elfo.Wardein.Abstractions.csproj index a20524d..7cd486c 100644 --- a/Elfo.Wardein.Abstractions/Elfo.Wardein.Abstractions.csproj +++ b/Elfo.Wardein.Abstractions/Elfo.Wardein.Abstractions.csproj @@ -1,7 +1,10 @@ - netcoreapp3.1 + netstandard2.1 + latest + Elfo.Firmenich.Wardein.Abstractions + Elfo.Firmenich.Wardein.Abstractions diff --git a/Elfo.Wardein.Core/Elfo.Wardein.Core.csproj b/Elfo.Wardein.Core/Elfo.Wardein.Core.csproj index adf2b11..87f4c04 100644 --- a/Elfo.Wardein.Core/Elfo.Wardein.Core.csproj +++ b/Elfo.Wardein.Core/Elfo.Wardein.Core.csproj @@ -1,7 +1,10 @@ - netcoreapp3.1 + netstandard2.1 + latest + Elfo.Firmenich.Wardein.Core + Elfo.Firmenich.Wardein.Core 1.1.0 @@ -20,12 +23,14 @@ + + diff --git a/Elfo.Wardein.Core/Helpers/ExternalResources/DB/Oracle/Abstractions/IOracleService.cs b/Elfo.Wardein.Core/Helpers/ExternalResources/DB/Oracle/Abstractions/IOracleService.cs new file mode 100644 index 0000000..d5c88ed --- /dev/null +++ b/Elfo.Wardein.Core/Helpers/ExternalResources/DB/Oracle/Abstractions/IOracleService.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Threading.Tasks; + +namespace Elfo.Wardein.Core.Helpers +{ + public interface IOracleService + { + IEnumerable Query(IDbConnection connection, string query, + IDictionary parameters, TimeSpan? timeout = null); + + int Execute(IDbConnection connection, string command, + IDictionary parameters, TimeSpan? timeout = null); + + Task> QueryAsync(IDbConnection connection, string query, + IDictionary parameters, TimeSpan? timeout = null); + + Task ExecuteAsync(IDbConnection connection, string command, + IDictionary parameters, TimeSpan? timeout = null); + } +} diff --git a/Elfo.Wardein.Integrations/Oracle.Integration/IOracleService.cs b/Elfo.Wardein.Core/Helpers/ExternalResources/DB/Oracle/DapperOracleService.cs similarity index 52% rename from Elfo.Wardein.Integrations/Oracle.Integration/IOracleService.cs rename to Elfo.Wardein.Core/Helpers/ExternalResources/DB/Oracle/DapperOracleService.cs index c1774e2..e069d88 100644 --- a/Elfo.Wardein.Integrations/Oracle.Integration/IOracleService.cs +++ b/Elfo.Wardein.Core/Helpers/ExternalResources/DB/Oracle/DapperOracleService.cs @@ -1,24 +1,47 @@ -using System; +using Dapper; +using System; using System.Collections.Generic; using System.Data; using System.Threading.Tasks; -using Dapper; -namespace Elfo.Wardein.Integrations.Oracle.Integration +namespace Elfo.Wardein.Core.Helpers { - public interface IOracleService + public class DapperOracleService : IOracleService { - Task> QueryAsync(IDbConnection connection, string query, - IDictionary parameters, TimeSpan? timeout = null); + public IEnumerable Query(IDbConnection connection, string query, + IDictionary parameters, TimeSpan? timeout = null) + { + var queryParameters = new DynamicParameters(); + if (parameters != null) + { + foreach (var parameter in parameters) + { + queryParameters.Add(parameter.Key, parameter.Value); + } + } - Task ExecuteAsync(IDbConnection connection, string command, - IDictionary parameters, TimeSpan? timeout = null); - } + return connection.Query(query, queryParameters, + commandTimeout: (int?)timeout?.TotalSeconds); + } + + public int Execute(IDbConnection connection, string command, + IDictionary parameters, TimeSpan? timeout = null) + { + var commandParameters = new DynamicParameters(); + if (parameters != null) + { + foreach (var parameter in parameters) + { + commandParameters.Add(parameter.Key, parameter.Value); + } + } + + return connection.Execute(command, parameters, + commandTimeout: (int?)timeout?.TotalSeconds); + } - public class DapperOracleService : IOracleService - { public async Task> QueryAsync(IDbConnection connection, string query, - IDictionary parameters, TimeSpan? timeout = null) + IDictionary parameters, TimeSpan? timeout = null) { var queryParameters = new DynamicParameters(); if (parameters != null) diff --git a/Elfo.Wardein.Integrations/Oracle.Integration/OracleIntegrationConfiguration.cs b/Elfo.Wardein.Core/Helpers/ExternalResources/DB/Oracle/OracleConnectionConfiguration.cs similarity index 66% rename from Elfo.Wardein.Integrations/Oracle.Integration/OracleIntegrationConfiguration.cs rename to Elfo.Wardein.Core/Helpers/ExternalResources/DB/Oracle/OracleConnectionConfiguration.cs index 53ed9c7..7e85930 100644 --- a/Elfo.Wardein.Integrations/Oracle.Integration/OracleIntegrationConfiguration.cs +++ b/Elfo.Wardein.Core/Helpers/ExternalResources/DB/Oracle/OracleConnectionConfiguration.cs @@ -1,13 +1,14 @@ -using System; +using Oracle.ManagedDataAccess.Client; +using System; using System.Collections.Generic; using System.Data; -using Oracle.ManagedDataAccess.Client; +using System.Text; -namespace Elfo.Wardein.Integrations.Oracle.Integration +namespace Elfo.Wardein.Core.Helpers { - public class OracleIntegrationConfiguration + public class OracleConnectionConfiguration { - public OracleIntegrationConfiguration(string connectionString) + public OracleConnectionConfiguration(string connectionString) { ConnectionString = connectionString; @@ -18,7 +19,8 @@ public OracleIntegrationConfiguration(string connectionString) { var oracleConnectionStringBuilder = new OracleConnectionStringBuilder(connectionString); DatabaseType = oracleConnectionStringBuilder.DataSource; - } catch (Exception ex) + } + catch (Exception ex) { throw new ArgumentException("Oracle connection string is invalid.", nameof(connectionString), ex); } @@ -40,16 +42,21 @@ public OracleIntegrationConfiguration(string connectionString) public IDictionary CommandParameters { get; protected set; } public TimeSpan? CommandTimeout { get; protected set; } + public string ClientId { get; set; } + public string ClientInfo { get; set; } + public string ModuleName { get; set; } + public string DateLanguage { get; set; } + public static Builder Create(string connectionString) => new Builder(connectionString); public class Builder { - protected readonly OracleIntegrationConfiguration Configuration; + protected readonly OracleConnectionConfiguration Configuration; public Builder(string connectionString) { - Configuration = new OracleIntegrationConfiguration(connectionString); + Configuration = new OracleConnectionConfiguration(connectionString); } public Builder WithCommand(string command, IDictionary parameters = null) @@ -126,7 +133,59 @@ public Builder WithOracleServiceProvider(Func oracleServiceProvi return this; } - public OracleIntegrationConfiguration Build() => Configuration; + public Builder WithClientId(string clientId) + { + if (string.IsNullOrWhiteSpace(clientId)) + { + throw new ArgumentNullException(nameof(clientId), + "ClientId can not be empty"); + } + + Configuration.ClientId = clientId; + + return this; + } + + public Builder WithClientInfo(string clientInfo) + { + if (string.IsNullOrWhiteSpace(clientInfo)) + { + throw new ArgumentNullException(nameof(clientInfo), + "ClientInfo can not be empty."); + } + + Configuration.ClientId = clientInfo; + + return this; + } + + public Builder WithModuleName(string moduleName) + { + if (string.IsNullOrWhiteSpace(moduleName)) + { + throw new ArgumentNullException(nameof(moduleName), + "ModuleName can not be empty."); + } + + Configuration.ClientId = moduleName; + + return this; + } + + public Builder WithDateLanguage(string dateLanguage) + { + if (string.IsNullOrWhiteSpace(dateLanguage)) + { + throw new ArgumentNullException(nameof(dateLanguage), + "DateLanguage can not be empty."); + } + + Configuration.ClientId = dateLanguage; + + return this; + } + + public OracleConnectionConfiguration Build() => Configuration; } } } diff --git a/Elfo.Wardein.Core/Helpers/ExternalResources/DB/Oracle/OracleHelper.cs b/Elfo.Wardein.Core/Helpers/ExternalResources/DB/Oracle/OracleHelper.cs new file mode 100644 index 0000000..d495a6f --- /dev/null +++ b/Elfo.Wardein.Core/Helpers/ExternalResources/DB/Oracle/OracleHelper.cs @@ -0,0 +1,136 @@ +using Oracle.ManagedDataAccess.Client; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +namespace Elfo.Wardein.Core.Helpers +{ + public class OracleHelper + { + private readonly OracleConnectionConfiguration oracleConfiguration; + + public OracleHelper(OracleConnectionConfiguration oracleConfiguration) + { + this.oracleConfiguration = oracleConfiguration; + } + + public IEnumerable Query(string query, IDictionary parameters = null) + { + try + { + var result = default(IEnumerable); + using (var connection = new OracleConnection(oracleConfiguration.ConnectionString)) + { + connection.Open(); + using (var transaction = connection.BeginTransaction()) + { + connection.SetSessionInfoForTransaction(oracleConfiguration); + + var queryToExecute = string.IsNullOrWhiteSpace(query) ? oracleConfiguration.Query : query; + var queryParameters = parameters ?? oracleConfiguration.QueryParameters; + + result = oracleConfiguration.OracleServiceProvider().Query(connection, queryToExecute, + queryParameters, oracleConfiguration.QueryTimeout); + transaction.Commit(); + } + } + return result; + } + catch (Exception exception) + { + throw exception; + } + } + + public int Execute(string command, IDictionary parameters = null) + { + + try + { + int result = 0; + + using (var connection = new OracleConnection(oracleConfiguration.ConnectionString)) + { + connection.Open(); + + using (var transaction = connection.BeginTransaction()) + { + connection.SetSessionInfoForTransaction(oracleConfiguration); + + var commandToExecute = string.IsNullOrWhiteSpace(command) ? oracleConfiguration.Command : command; + var commandParameters = parameters ?? oracleConfiguration.CommandParameters; + + result = oracleConfiguration.OracleServiceProvider().Execute(connection, commandToExecute, + commandParameters, oracleConfiguration.CommandTimeout); + transaction.Commit(); + } + } + return result; + } + catch (Exception exception) + { + throw exception; + } + } + + public async Task> QueryAsync(string query, IDictionary parameters = null) + { + try + { + var result = default(IEnumerable); + using (var connection = new OracleConnection(oracleConfiguration.ConnectionString)) + { + connection.Open(); + using (var transaction = connection.BeginTransaction()) + { + connection.SetSessionInfoForTransaction(oracleConfiguration); + + var queryToExecute = string.IsNullOrWhiteSpace(query) ? oracleConfiguration.Query : query; + var queryParameters = parameters ?? oracleConfiguration.QueryParameters; + + result = await oracleConfiguration.OracleServiceProvider().QueryAsync(connection, queryToExecute, + queryParameters, oracleConfiguration.QueryTimeout); + transaction.Commit(); + } + } + return result; + } + catch (Exception exception) + { + throw exception; + } + } + + public async Task ExecuteAsync(string command, IDictionary parameters = null) + { + + try + { + int result = 0; + + using (var connection = new OracleConnection(oracleConfiguration.ConnectionString)) + { + connection.Open(); + + using (var transaction = connection.BeginTransaction()) + { + connection.SetSessionInfoForTransaction(oracleConfiguration); + + var commandToExecute = string.IsNullOrWhiteSpace(command) ? oracleConfiguration.Command : command; + var commandParameters = parameters ?? oracleConfiguration.CommandParameters; + + result = await oracleConfiguration.OracleServiceProvider().ExecuteAsync(connection, commandToExecute, + commandParameters, oracleConfiguration.CommandTimeout); + transaction.Commit(); + } + } + return result; + } + catch (Exception exception) + { + throw exception; + } + } + } +} diff --git a/Elfo.Wardein.Core/Helpers/IOHelper.cs b/Elfo.Wardein.Core/Helpers/ExternalResources/FyleSystem/IOHelper.cs similarity index 100% rename from Elfo.Wardein.Core/Helpers/IOHelper.cs rename to Elfo.Wardein.Core/Helpers/ExternalResources/FyleSystem/IOHelper.cs diff --git a/Elfo.Wardein.Core/Helpers/OracleConnectionExtensionMethods.cs b/Elfo.Wardein.Core/Helpers/OracleConnectionExtensionMethods.cs new file mode 100644 index 0000000..b5369b8 --- /dev/null +++ b/Elfo.Wardein.Core/Helpers/OracleConnectionExtensionMethods.cs @@ -0,0 +1,20 @@ +using Oracle.ManagedDataAccess.Client; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Elfo.Wardein.Core.Helpers +{ + public static class OracleConnectionExtensionMethods + { + public static void SetSessionInfoForTransaction(this OracleConnection connection, OracleConnectionConfiguration oracleConfiguration) + { + connection.ClientId = oracleConfiguration.ClientId; + connection.ClientInfo = oracleConfiguration.ClientInfo; + connection.ModuleName = oracleConfiguration.ModuleName; + var sessionInfo = connection.GetSessionInfo(); + sessionInfo.DateLanguage = oracleConfiguration.DateLanguage; + connection.SetSessionInfo(sessionInfo); + } + } +} diff --git a/Elfo.Wardein.Integrations.Tests/Elfo.Wardein.Integrations.Tests.csproj b/Elfo.Wardein.Integrations.Tests/Elfo.Wardein.Integrations.Tests.csproj index d25a10a..818d61a 100644 --- a/Elfo.Wardein.Integrations.Tests/Elfo.Wardein.Integrations.Tests.csproj +++ b/Elfo.Wardein.Integrations.Tests/Elfo.Wardein.Integrations.Tests.csproj @@ -16,6 +16,7 @@ + diff --git a/Elfo.Wardein.Integrations.Tests/GetDataFromSourceTest.cs b/Elfo.Wardein.Integrations.Tests/GetDataFromSourceTest.cs index cdb8739..e881b1f 100644 --- a/Elfo.Wardein.Integrations.Tests/GetDataFromSourceTest.cs +++ b/Elfo.Wardein.Integrations.Tests/GetDataFromSourceTest.cs @@ -1,4 +1,5 @@ -using Elfo.Wardein.Integrations.Oracle.Integration; +using Elfo.Wardein.Core.Helpers; +using Elfo.Wardein.Integrations.Oracle.Integration; using Microsoft.Extensions.Configuration; using Microsoft.VisualStudio.TestTools.UnitTesting; using Oracle.ManagedDataAccess.Client; @@ -26,7 +27,7 @@ public void Initialize() [TestCategory("ManualTest")] public async Task ConnectionAreOk() { - OracleIntegrationConfiguration config = new OracleIntegrationConfiguration(connectionString); + OracleConnectionConfiguration config = new OracleConnectionConfiguration(connectionString); OracleIntegration connection = new OracleIntegration(config); @@ -39,7 +40,7 @@ public async Task ConnectionAreOk() [TestCategory("ManualTest")] public async Task InsertIsOk() { - OracleIntegrationConfiguration config = new OracleIntegrationConfiguration(connectionString); + OracleConnectionConfiguration config = new OracleConnectionConfiguration(connectionString); OracleIntegration connection = new OracleIntegration(config); @@ -52,7 +53,7 @@ public async Task InsertIsOk() [TestCategory("ManualTest")] public async Task SelectAreOk() { - OracleIntegrationConfiguration config = new OracleIntegrationConfiguration(connectionString); + OracleConnectionConfiguration config = new OracleConnectionConfiguration(connectionString); OracleIntegration connection = new OracleIntegration(config); @@ -65,7 +66,7 @@ public async Task SelectAreOk() [TestCategory("ManualTest")] public async Task UpdateIsOk() { - OracleIntegrationConfiguration config = new OracleIntegrationConfiguration(connectionString); + OracleConnectionConfiguration config = new OracleConnectionConfiguration(connectionString); OracleIntegration connection = new OracleIntegration(config); diff --git a/Elfo.Wardein.Integrations/Elfo.Wardein.Integrations.csproj b/Elfo.Wardein.Integrations/Elfo.Wardein.Integrations.csproj index 8ad942e..0ddc043 100644 --- a/Elfo.Wardein.Integrations/Elfo.Wardein.Integrations.csproj +++ b/Elfo.Wardein.Integrations/Elfo.Wardein.Integrations.csproj @@ -1,10 +1,10 @@ - + netstandard2.1 latest - Elfo.Firmenich.Wardein.Portal.Integrations - Elfo.Firmenich.Wardein.Portal.Integrations + Elfo.Firmenich.Wardein.Integrations + Elfo.Firmenich.Wardein.Integrations false false @@ -17,4 +17,8 @@ + + + + diff --git a/Elfo.Wardein.Integrations/Oracle.Integration/Extensions.cs b/Elfo.Wardein.Integrations/Oracle.Integration/Extensions.cs index 52d4510..aef554d 100644 --- a/Elfo.Wardein.Integrations/Oracle.Integration/Extensions.cs +++ b/Elfo.Wardein.Integrations/Oracle.Integration/Extensions.cs @@ -1,4 +1,5 @@ using System; +using Elfo.Wardein.Core.Helpers; using Elfo.Wardein.Integrations.Oracle.Integration; using Warden.Core; using Warden.Integrations; @@ -10,7 +11,7 @@ public static class Extensions public static WardenConfiguration.Builder IntegrateWithOracle( this WardenConfiguration.Builder builder, string connectionString, - Action configurator = null) + Action configurator = null) { builder.AddIntegration(OracleIntegration.Create(connectionString, configurator)); @@ -19,7 +20,7 @@ public static WardenConfiguration.Builder IntegrateWithOracle( public static WardenConfiguration.Builder IntegrateWithOracle( this WardenConfiguration.Builder builder, - OracleIntegrationConfiguration configuration) + OracleConnectionConfiguration configuration) { builder.AddIntegration(OracleIntegration.Create(configuration)); diff --git a/Elfo.Wardein.Integrations/Oracle.Integration/OracleIntegration.cs b/Elfo.Wardein.Integrations/Oracle.Integration/OracleIntegration.cs index 9875379..07065c9 100644 --- a/Elfo.Wardein.Integrations/Oracle.Integration/OracleIntegration.cs +++ b/Elfo.Wardein.Integrations/Oracle.Integration/OracleIntegration.cs @@ -2,54 +2,30 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using Oracle.ManagedDataAccess.Client; using Warden; using Warden.Integrations; using Warden.Watchers; +using Oracle.ManagedDataAccess.Client; +using Elfo.Wardein.Core.Helpers; namespace Elfo.Wardein.Integrations.Oracle.Integration { public class OracleIntegration : IIntegration { - private readonly OracleIntegrationConfiguration configuration; - private readonly IOracleService oracleService; + private readonly OracleConnectionConfiguration configuration; + private readonly OracleHelper oracleHelper; - public OracleIntegration(OracleIntegrationConfiguration configuration) + public OracleIntegration(OracleConnectionConfiguration configuration) { this.configuration = configuration; - oracleService = configuration.OracleServiceProvider(); - } - private void GetSessionInfoForTransaction(OracleConnection connection) - { - connection.ClientId = "HULK"; - connection.ClientInfo = "WEBCLIENT_COREDB"; - connection.ModuleName = "ExecuteAsync"; - var sessionInfo = connection.GetSessionInfo(); - sessionInfo.DateLanguage = "AMERICAN"; - connection.SetSessionInfo(sessionInfo); + oracleHelper = new OracleHelper(configuration); } public async Task> QueryAsync(string query, IDictionary parameters = null) { try { - var result = default(IEnumerable); - using (var connection = new OracleConnection(configuration.ConnectionString)) - { - connection.Open(); - using (var transaction = connection.BeginTransaction()) - { - GetSessionInfoForTransaction(connection); - - var queryToExecute = string.IsNullOrWhiteSpace(query) ? configuration.Query : query; - var queryParameters = parameters ?? configuration.QueryParameters; - - result = await oracleService.QueryAsync(connection, queryToExecute, - queryParameters, configuration.QueryTimeout); - transaction.Commit(); - } - } - return result; + return await oracleHelper.QueryAsync(query, parameters); } catch (OracleException exception) { throw new IntegrationException("There was a SQL error while trying to execute the query.", exception); @@ -60,32 +36,12 @@ public async Task> QueryAsync(string query, IDictionary ExecuteAsync(string command, IDictionary parameters = null) { try { - int result = 0; - - using (var connection = new OracleConnection(configuration.ConnectionString)) - { - connection.Open(); - - using (var transaction = connection.BeginTransaction()) - { - GetSessionInfoForTransaction(connection); - - var commandToExecute = string.IsNullOrWhiteSpace(command) ? configuration.Command : command; - var commandParameters = parameters ?? configuration.CommandParameters; - - result = await oracleService.ExecuteAsync(connection, commandToExecute, - commandParameters, configuration.CommandTimeout); - transaction.Commit(); - } - } - return result; + return await oracleHelper.ExecuteAsync(command, parameters); } catch (OracleException exception) { @@ -97,105 +53,16 @@ public async Task ExecuteAsync(string command, IDictionary } } - public async Task SaveIterationAsync(IWardenIteration iteration) - { - var wardenIterationCommand = "insert into WardenIterations values" + - "(@wardenName, @ordinal, @startedAt, @completedAt, @executionTime, @isValid);" + - "select cast(scope_identity() as bigint)"; - var wardenIterationParameters = new Dictionary - { - ["wardenName"] = iteration.WardenName, - ["ordinal"] = iteration.Ordinal, - ["startedAt"] = iteration.StartedAt, - ["completedAt"] = iteration.CompletedAt, - ["executionTime"] = iteration.ExecutionTime, - ["isValid"] = iteration.IsValid, - }; - var iterationResultIds = await QueryAsync(wardenIterationCommand, wardenIterationParameters); - var iterationId = iterationResultIds.FirstOrDefault(); - if (iterationId <= 0) - return; - await SaveWardenCheckResultsAsync(iteration.Results, iterationId); - } - - private async Task SaveWardenCheckResultsAsync(IEnumerable results, long iterationId) - { - foreach (var result in results) - { - var wardenCheckResultCommand = "insert into WardenCheckResults values (@wardenIteration_Id, @isValid, " + - "@startedAt, @completedAt, @executionTime);select cast(scope_identity() as bigint)"; - var wardenCheckResultParameters = new Dictionary - { - ["wardenIteration_Id"] = iterationId, - ["isValid"] = result.IsValid, - ["startedAt"] = result.StartedAt, - ["completedAt"] = result.CompletedAt, - ["executionTime"] = result.ExecutionTime - }; - var wardenCheckResultIds = await QueryAsync(wardenCheckResultCommand, wardenCheckResultParameters); - var wardenCheckResultId = wardenCheckResultIds.FirstOrDefault(); - if (wardenCheckResultId <= 0) - return; - await SaveWatcherCheckResultAsync(result.WatcherCheckResult, wardenCheckResultId); - await SaveExceptionAsync(result.Exception, wardenCheckResultId); - } - } - - private async Task SaveWatcherCheckResultAsync(IWatcherCheckResult result, long wardenCheckResultId) - { - if (result == null) - return; - - var watcherCheckResultCommand = "insert into WatcherCheckResults values " + - "(@wardenCheckResult_Id, @watcherName, @watcherType, @description, @isValid)"; - var watcherCheckResultParameters = new Dictionary - { - ["wardenCheckResult_Id"] = wardenCheckResultId, - ["watcherName"] = result.WatcherName, - ["watcherType"] = result.WatcherType.ToString().Split('.').Last(), - ["description"] = result.Description, - ["isValid"] = result.IsValid - }; - await ExecuteAsync(watcherCheckResultCommand, watcherCheckResultParameters); - } - - private async Task SaveExceptionAsync(Exception exception, long wardenCheckResultId, long? parentExceptionId = null) - { - if (exception == null) - return null; - - var exceptionCommand = "insert into Exceptions values (@wardenCheckResult_Id, @parentException_Id, " + - "@message, @source, @stackTrace);select cast(scope_identity() as bigint)"; - var exceptionParameters = new Dictionary - { - ["wardenCheckResult_Id"] = wardenCheckResultId, - ["parentException_Id"] = parentExceptionId, - ["message"] = exception.Message, - ["source"] = exception.Source, - ["stackTrace"] = exception.StackTrace - }; - var exceptionIds = await QueryAsync(exceptionCommand, exceptionParameters); - var exceptionId = exceptionIds.FirstOrDefault(); - if (exceptionId <= 0) - return null; - if (exception.InnerException == null) - return exceptionId; - - await SaveExceptionAsync(exception.InnerException, wardenCheckResultId, exceptionId); - - return exceptionId; - } - public static OracleIntegration Create(string connectionString, - Action configurator) + Action configurator) { - var config = new OracleIntegrationConfiguration.Builder(connectionString); + var config = new OracleConnectionConfiguration.Builder(connectionString); configurator?.Invoke(config); return Create(config.Build()); } - public static OracleIntegration Create(OracleIntegrationConfiguration configuration) + public static OracleIntegration Create(OracleConnectionConfiguration configuration) => new OracleIntegration(configuration); } diff --git a/Elfo.Wardein.Watchers/HeartBeat/HeartBeatWatcher.cs b/Elfo.Wardein.Watchers/HeartBeat/HeartBeatWatcher.cs index 2ccfc5e..6bb1181 100644 --- a/Elfo.Wardein.Watchers/HeartBeat/HeartBeatWatcher.cs +++ b/Elfo.Wardein.Watchers/HeartBeat/HeartBeatWatcher.cs @@ -36,25 +36,26 @@ public override async Task ExecuteWatcherActionAsync() public async Task UpdateHeartBeatByAppName(string appName) { - var configuration = new ConfigurationBuilder() - .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) - .Build(); + //var configuration = new ConfigurationBuilder() + // .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) + // .Build(); - var connectionString = configuration["ConnectionStrings:Db"]; + //var connectionString = configuration["ConnectionStrings:Db"]; - OracleIntegrationConfiguration config = new OracleIntegrationConfiguration(connectionString); + //OracleIntegrationConfiguration config = new OracleIntegrationConfiguration(connectionString); - OracleIntegration connection = new OracleIntegration(config); - // TODO: Check SQL Injection - var updateDateParameter = new Dictionary - { - ["DT_LAST_HB"] = new OracleParameter("DT_LAST_HB", OracleDbType.Date).Value = DateTime.UtcNow, - ["APPL_HOSTNAME"] = new OracleParameter("APPL_HOSTNAME", OracleDbType.Varchar2).Value = appName - }; + //OracleIntegration connection = new OracleIntegration(config); + //// TODO: Check SQL Injection + //var updateDateParameter = new Dictionary + //{ + // ["DT_LAST_HB"] = new OracleParameter("DT_LAST_HB", OracleDbType.Date).Value = DateTime.UtcNow, + // ["APPL_HOSTNAME"] = new OracleParameter("APPL_HOSTNAME", OracleDbType.Varchar2).Value = appName + //}; - string query = "UPDATE WRD_CNFG SET DT_LAST_HB = :DT_LAST_HB WHERE APPL_HOSTNAME = :APPL_HOSTNAME"; + //string query = "UPDATE WRD_CNFG SET DT_LAST_HB = :DT_LAST_HB WHERE APPL_HOSTNAME = :APPL_HOSTNAME"; - return await connection.ExecuteAsync(query, updateDateParameter); + //return await connection.ExecuteAsync(query, updateDateParameter); + return 0; } } }