From cf59314b7989fc02fb39f3c5c2bd874bbee72f20 Mon Sep 17 00:00:00 2001 From: Christiano Donke Date: Tue, 10 Mar 2020 14:06:27 -0300 Subject: [PATCH 1/4] Updating packages --- BotMessageRouting/BotMessageRouting.csproj | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BotMessageRouting/BotMessageRouting.csproj b/BotMessageRouting/BotMessageRouting.csproj index 5d6a3a5..7d04309 100644 --- a/BotMessageRouting/BotMessageRouting.csproj +++ b/BotMessageRouting/BotMessageRouting.csproj @@ -14,8 +14,8 @@ - - + + From 5f9cd3c152f421573bdc0055d4de6026954e72f6 Mon Sep 17 00:00:00 2001 From: Christiano Donke Date: Thu, 12 Mar 2020 16:53:45 -0300 Subject: [PATCH 2/4] Implementing ReplaceAsync for Az Tables --- .../DataStore/Azure/AzureStorageHelper.cs | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/BotMessageRouting/MessageRouting/DataStore/Azure/AzureStorageHelper.cs b/BotMessageRouting/MessageRouting/DataStore/Azure/AzureStorageHelper.cs index 1a6c949..96e9bc6 100644 --- a/BotMessageRouting/MessageRouting/DataStore/Azure/AzureStorageHelper.cs +++ b/BotMessageRouting/MessageRouting/DataStore/Azure/AzureStorageHelper.cs @@ -38,6 +38,31 @@ public static CloudTable GetTable(string connectionString, string tableName) return cloudTableClient?.GetTableReference(tableName); } + /// + /// Tries to insert the given entry into the given table. + /// + /// TableEntity derivative. + /// The destination table. + /// The entry to insert into the table. + /// True, if the given entry was inserted successfully. False otherwise. + public static async Task ReplaceAsync(CloudTable cloudTable, T entryToInsert) where T : ITableEntity + { + TableOperation replaceOperation = TableOperation.InsertOrReplace(entryToInsert); + TableResult replaceResult = null; + + try + { + replaceResult = await cloudTable.ExecuteAsync(replaceOperation); + } + catch (Exception e) + { + System.Diagnostics.Debug.WriteLine($"Failed to replace the given entity into table '{cloudTable.Name}': {e.Message}"); + return false; + } + + return (replaceResult?.Result != null); + } + /// /// Tries to insert the given entry into the given table. /// From c45ba1ad29062034e5540709f62bba51ef107138 Mon Sep 17 00:00:00 2001 From: Christiano Donke Date: Fri, 13 Mar 2020 10:17:07 -0300 Subject: [PATCH 3/4] Implementing logging --- .../Azure/AzureTableRoutingDataStore.cs | 11 +++-- .../MessageRouting/Logging/ConsoleLogger.cs | 44 +++++++++++++++++++ .../MessageRouting/Logging/DebugLogger.cs | 10 +++++ .../MessageRouting/Logging/ILogger.cs | 15 +++++++ 4 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 BotMessageRouting/MessageRouting/Logging/ConsoleLogger.cs diff --git a/BotMessageRouting/MessageRouting/DataStore/Azure/AzureTableRoutingDataStore.cs b/BotMessageRouting/MessageRouting/DataStore/Azure/AzureTableRoutingDataStore.cs index 931efad..88f4d64 100644 --- a/BotMessageRouting/MessageRouting/DataStore/Azure/AzureTableRoutingDataStore.cs +++ b/BotMessageRouting/MessageRouting/DataStore/Azure/AzureTableRoutingDataStore.cs @@ -6,6 +6,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Threading.Tasks; +using Underscore.Bot.MessageRouting.Logging; using Underscore.Bot.MessageRouting.Models; using Underscore.Bot.MessageRouting.Models.Azure; @@ -24,7 +25,7 @@ public class AzureTableRoutingDataStore : IRoutingDataStore protected const string TableNameAggregationChannels = "AggregationChannels"; protected const string TableNameConnectionRequests = "ConnectionRequests"; protected const string TableNameConnections = "Connections"; - + protected readonly ILogger _logger; protected CloudTable _botInstancesTable; protected CloudTable _usersTable; protected CloudTable _aggregationChannelsTable; @@ -35,13 +36,15 @@ public class AzureTableRoutingDataStore : IRoutingDataStore /// Constructor. /// /// The connection string associated with an Azure Table Storage. - public AzureTableRoutingDataStore(string connectionString) + public AzureTableRoutingDataStore(string connectionString, ILogger logger) { if (string.IsNullOrEmpty(connectionString)) { throw new ArgumentNullException("The connection string cannot be null or empty"); } + _logger = logger; + _botInstancesTable = AzureStorageHelper.GetTable(connectionString, TableNameBotInstances); _usersTable = AzureStorageHelper.GetTable(connectionString, TableNameUsers); _aggregationChannelsTable = AzureStorageHelper.GetTable(connectionString, TableNameAggregationChannels); @@ -216,11 +219,11 @@ protected virtual async void MakeSureTablesExistAsync() try { await cloudTable.CreateIfNotExistsAsync(); - Debug.WriteLine($"Table '{cloudTable.Name}' created or did already exist"); + _logger.LogInformation($"Table '{cloudTable.Name}' created or did already exist"); } catch (StorageException e) { - Debug.WriteLine($"Failed to create table '{cloudTable.Name}' (perhaps it already exists): {e.Message}"); + _logger.LogError($"Failed to create table '{cloudTable.Name}' (perhaps it already exists): {e.Message}"); } } } diff --git a/BotMessageRouting/MessageRouting/Logging/ConsoleLogger.cs b/BotMessageRouting/MessageRouting/Logging/ConsoleLogger.cs new file mode 100644 index 0000000..bcc430c --- /dev/null +++ b/BotMessageRouting/MessageRouting/Logging/ConsoleLogger.cs @@ -0,0 +1,44 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Runtime.CompilerServices; +using System.Text; + +namespace Underscore.Bot.MessageRouting.Logging +{ + public class ConsoleLogger : Bot.MessageRouting.Logging.ILogger + { + private readonly Microsoft.Extensions.Logging.ILogger _logger; + + public ConsoleLogger(Microsoft.Extensions.Logging.ILogger logger) + { + _logger = logger; + } + + public void Log(string message, [CallerMemberName] string methodName = "") + { + _logger.LogDebug(BuildMessage(message, methodName)); + } + + public void LogError(string message, [CallerMemberName] string methodName = "") + { + _logger.LogError(BuildMessage(message, methodName)); + } + + public void LogInformation(string message, [CallerMemberName] string methodName = "") + { + _logger.LogInformation(BuildMessage(message, methodName)); + } + + private string BuildMessage(string message, string methodName) + { + var msg = $"{DateTime.Now}> "; + if (!string.IsNullOrWhiteSpace(methodName)) + msg += $"{methodName}: {message}"; + else + msg += message; + + return msg; + } + } +} diff --git a/BotMessageRouting/MessageRouting/Logging/DebugLogger.cs b/BotMessageRouting/MessageRouting/Logging/DebugLogger.cs index d79346e..a99f2ac 100644 --- a/BotMessageRouting/MessageRouting/Logging/DebugLogger.cs +++ b/BotMessageRouting/MessageRouting/Logging/DebugLogger.cs @@ -15,5 +15,15 @@ public void Log(string message, [CallerMemberName] string methodName = "") Debug.WriteLine($"{DateTime.Now}> {message}"); } + + public void LogError(string message, [CallerMemberName] string methodName = "") + { + Log(message, methodName); + } + + public void LogInformation(string message, [CallerMemberName] string methodName = "") + { + Log(message, methodName); + } } } \ No newline at end of file diff --git a/BotMessageRouting/MessageRouting/Logging/ILogger.cs b/BotMessageRouting/MessageRouting/Logging/ILogger.cs index d6334ea..e1b8ab1 100644 --- a/BotMessageRouting/MessageRouting/Logging/ILogger.cs +++ b/BotMessageRouting/MessageRouting/Logging/ILogger.cs @@ -10,5 +10,20 @@ public interface ILogger /// The message to log. /// Resolved by the [CallerMemberName] attribute. No value required. void Log(string message, [CallerMemberName] string methodName = ""); + + /// + /// Logs the given message. + /// + /// The message to log. + /// Resolved by the [CallerMemberName] attribute. No value required. + void LogInformation(string message, [CallerMemberName] string methodName = ""); + + /// + /// Logs the given message. + /// + /// The message to log. + /// Resolved by the [CallerMemberName] attribute. No value required. + void LogError(string message, [CallerMemberName] string methodName = ""); + } } \ No newline at end of file From 9df89d27d89d28a9f57c9f3bba45792473e4054b Mon Sep 17 00:00:00 2001 From: Christiano Donke Date: Fri, 13 Mar 2020 10:39:00 -0300 Subject: [PATCH 4/4] Algo performance --- .../MessageRouting/DataStore/RoutingDataManager.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/BotMessageRouting/MessageRouting/DataStore/RoutingDataManager.cs b/BotMessageRouting/MessageRouting/DataStore/RoutingDataManager.cs index 2205b4b..1b29d86 100644 --- a/BotMessageRouting/MessageRouting/DataStore/RoutingDataManager.cs +++ b/BotMessageRouting/MessageRouting/DataStore/RoutingDataManager.cs @@ -366,11 +366,11 @@ public virtual bool IsAssociatedWithAggregation(ConversationReference conversati return (conversationReference != null && aggregationParties != null - && aggregationParties.Count() > 0 + && aggregationParties.Count > 0 && aggregationParties.Where(aggregationChannel => - aggregationChannel.Conversation.Id == conversationReference.Conversation.Id - && aggregationChannel.ServiceUrl == conversationReference.ServiceUrl - && aggregationChannel.ChannelId == conversationReference.ChannelId).Count() > 0); + aggregationChannel.Conversation.Id.Equals(conversationReference.Conversation.Id, StringComparison.OrdinalIgnoreCase) + && aggregationChannel.ServiceUrl.Equals(conversationReference.ServiceUrl) + && aggregationChannel.ChannelId.Equals(conversationReference.ChannelId)).Any()); } ///