From 2da929ccbccf9691d891f3fc8bcd09bbebf2dc66 Mon Sep 17 00:00:00 2001 From: pfh59 <109206285+pfh59@users.noreply.github.com> Date: Wed, 4 Dec 2024 22:11:43 +0100 Subject: [PATCH] - Fix : #325 (#326) * - Fix : #325 - Enhance search functionality by adding strict mode parameter to search methods * Refactor logging in EveMapperSearch to use structured logging format * Enhance logging in EveMapperSearch to include exception details for operation cancellations --- .../Services/EveMapper/EveMapperSearch.cs | 130 +++++++++++++----- .../Services/EveMapper/IEveMapperSearch.cs | 6 +- 2 files changed, 101 insertions(+), 35 deletions(-) diff --git a/src/WHMapper/Services/EveMapper/EveMapperSearch.cs b/src/WHMapper/Services/EveMapper/EveMapperSearch.cs index e7c77f37..68d92ba5 100644 --- a/src/WHMapper/Services/EveMapper/EveMapperSearch.cs +++ b/src/WHMapper/Services/EveMapper/EveMapperSearch.cs @@ -27,7 +27,7 @@ public EveMapperSearch(ILogger logger, ISDEService sdeServices, _eveAPIServices = eveAPIServices; } - private async Task?> Search(string value, CancellationToken cancellationToken) + private async Task?> Search(string value,bool strict, CancellationToken cancellationToken) { try { @@ -56,7 +56,7 @@ public EveMapperSearch(ILogger logger, ISDEService sdeServices, if (typeof(T) == typeof(CharactereEntity)) { BlockingCollection eveEntityResults = new BlockingCollection(); - SearchCharacterResults? characterResults = await _eveAPIServices.SearchServices.SearchCharacter(value); + SearchCharacterResults? characterResults = await _eveAPIServices.SearchServices.SearchCharacter(value,strict); if (characterResults != null && characterResults.Characters != null) { await Parallel.ForEachAsync(characterResults.Characters, options, async (characterId, token) => @@ -75,7 +75,7 @@ await Parallel.ForEachAsync(characterResults.Characters, options, async (charact else if (typeof(T) == typeof(CorporationEntity)) { BlockingCollection eveEntityResults = new BlockingCollection(); - SearchCoporationResults? coporationResults = await _eveAPIServices.SearchServices.SearchCorporation(value); + SearchCoporationResults? coporationResults = await _eveAPIServices.SearchServices.SearchCorporation(value,strict); if (coporationResults != null && coporationResults.Corporations != null) { await Parallel.ForEachAsync(coporationResults.Corporations, options, async (corpoId, token) => @@ -93,7 +93,7 @@ await Parallel.ForEachAsync(coporationResults.Corporations, options, async (corp else if (typeof(T) == typeof(AllianceEntity)) { BlockingCollection eveEntityResults = new BlockingCollection(); - SearchAllianceResults? allianceResults = await _eveAPIServices.SearchServices.SearchAlliance(value); + SearchAllianceResults? allianceResults = await _eveAPIServices.SearchServices.SearchAlliance(value,strict); if (allianceResults != null && allianceResults.Alliances != null) { await Parallel.ForEachAsync(allianceResults.Alliances, options, async (allianceId, token) => @@ -111,7 +111,7 @@ await Parallel.ForEachAsync(allianceResults.Alliances, options, async (allianceI } else { - _logger.LogError($"Search {value} Unknow value"); + _logger.LogError("Search {Value} Unknow value", value); return null; } @@ -119,18 +119,18 @@ await Parallel.ForEachAsync(allianceResults.Alliances, options, async (allianceI return results; else { - _logger.LogDebug($"Search {value} not found"); + _logger.LogDebug("Search {Value} not found", value); return null; } } catch (OperationCanceledException) { - _logger.LogInformation($"Search {value} cancelled"); + _logger.LogInformation("Search {Value} cancelled", value); return null; } catch (Exception ex) { - _logger.LogError(ex, $"Search {value}"); + _logger.LogError(ex, "Search {Value}", value); return null; } } @@ -138,93 +138,157 @@ await Parallel.ForEachAsync(allianceResults.Alliances, options, async (allianceI { try { - return await Search(value, cancellationToken); + return await Search(value,true, cancellationToken); } - catch (OperationCanceledException) + catch (OperationCanceledException oce) { - _logger.LogInformation($"SearchSystem {value} cancelled"); + _logger.LogInformation(oce,"SearchSystem {Value} cancelled", value); return null; } catch (Exception ex) { - _logger.LogError(ex, $"SearchSystem {value}"); + _logger.LogError(ex, "SearchSystem {Value}", value); return null; } } + public async Task?> SearchCharactere(string value, CancellationToken cancellationToken) { try { - return await Search(value, cancellationToken); + IEnumerable? results = new BlockingCollection(); + IEnumerable? strictCharacterEntities = await Search(value, true, cancellationToken); + IEnumerable? characterEntities = await Search(value, false, cancellationToken); + + if (strictCharacterEntities != null) + { + results = results.Union(strictCharacterEntities.OrderBy(x => x.Name)); + } + + if (characterEntities != null) + { + results = results.UnionBy(characterEntities.OrderBy(x => x.Name), x => x.Id); + } + + return results; } - catch (OperationCanceledException) + catch (OperationCanceledException oce) { - _logger.LogInformation($"SearchCharactere {value} cancelled"); + _logger.LogInformation(oce,"SearchCharactere {Value} cancelled", value); return null; } catch (Exception ex) { - _logger.LogError(ex, $"SearchCharactere {value}"); + _logger.LogError(ex, "SearchCharactere {Value}", value); + return null; + } + + + } + + public async Task?> SearchCharactere(string value,bool strict, CancellationToken cancellationToken) + { + try + { + return await Search(value,strict, cancellationToken); + } + catch (OperationCanceledException oce) + { + _logger.LogInformation(oce,"SearchCharactere {Value} cancelled", value); + return null; + } + catch (Exception ex) + { + _logger.LogError(ex, "SearchCharactere {Value}", value); return null; } } - public async Task?> SearchCorporation(string value, CancellationToken cancellationToken) + public async Task?> SearchCorporation(string value,bool strict, CancellationToken cancellationToken) { try { - return await Search(value, cancellationToken); + return await Search(value,strict, cancellationToken); } - catch (OperationCanceledException) + catch (OperationCanceledException oce) { - _logger.LogInformation($"SearchCoorporation {value} cancelled"); + _logger.LogInformation(oce,"SearchCoorporation {Value} cancelled", value); return null; } catch (Exception ex) { - _logger.LogError(ex, $"SearchCoorporation {value}"); + _logger.LogError(ex, "SearchCoorporation {Value}", value); return null; } } - public async Task?> SearchAlliance(string value, CancellationToken cancellationToken) + public async Task?> SearchAlliance(string value,bool strict, CancellationToken cancellationToken) { try { - return await Search(value, cancellationToken); + return await Search(value,strict, cancellationToken); } - catch (OperationCanceledException) + catch (OperationCanceledException oce) { - _logger.LogInformation($"SearchAlliance {value} cancelled"); + _logger.LogInformation(oce,"SearchAlliance {Value} cancelled", value); return null; } catch (Exception ex) { - _logger.LogError(ex, $"SearchAlliance {value}"); + _logger.LogError(ex, "SearchAlliance {Value}", value); return null; } } public async Task?> SearchEveEntities(string value, CancellationToken cancellationToken) { IEnumerable? results = new BlockingCollection(); - IEnumerable? allianceEntities = await SearchAlliance(value, cancellationToken); - IEnumerable? coorporationEntities = await SearchCorporation(value, cancellationToken); - IEnumerable? characterEntities = await SearchCharactere(value, cancellationToken); + IEnumerable? results2 = new BlockingCollection(); + IEnumerable? strictAllianceEntities = await SearchAlliance(value,true, cancellationToken); + IEnumerable? strictCoorporationEntities = await SearchCorporation(value,true, cancellationToken); + IEnumerable? strictCharacterEntities = await SearchCharactere(value,true, cancellationToken); + + + if (strictAllianceEntities != null) + { + results = results.Union(strictAllianceEntities as IEnumerable); + } + + if (strictCoorporationEntities != null) + { + results = results.Union(strictCoorporationEntities as IEnumerable); + } + + if (strictCharacterEntities != null) + { + results = results.Union(strictCharacterEntities as IEnumerable); + } + + results = results.OrderByDescending(x => x.EntityType).ThenBy(x => x.Name); + + + + IEnumerable? allianceEntities = await SearchAlliance(value,false, cancellationToken); + IEnumerable? coorporationEntities = await SearchCorporation(value,false, cancellationToken); + IEnumerable? characterEntities = await SearchCharactere(value,false, cancellationToken); if (allianceEntities != null) { - results = results.Union(allianceEntities as IEnumerable); + results2 = results2.Union(allianceEntities as IEnumerable); } if (coorporationEntities != null) { - results = results.Union(coorporationEntities as IEnumerable); + results2 = results2.Union(coorporationEntities as IEnumerable); } if (characterEntities != null) { - results = results.Union(characterEntities as IEnumerable); + results2 = results2.Union(characterEntities as IEnumerable); } - return results.OrderByDescending(x => x.Name).ThenBy(x => x.EntityType); + results2 = results2.OrderByDescending(x => x.EntityType).ThenBy(x => x.Name); + + + + return results.UnionBy(results2, x => x.Id); } diff --git a/src/WHMapper/Services/EveMapper/IEveMapperSearch.cs b/src/WHMapper/Services/EveMapper/IEveMapperSearch.cs index 55a6a04a..2bb34eaa 100644 --- a/src/WHMapper/Services/EveMapper/IEveMapperSearch.cs +++ b/src/WHMapper/Services/EveMapper/IEveMapperSearch.cs @@ -9,9 +9,11 @@ public interface IEveMapperSearch public const int MIN_SEARCH_ENTITY_CHARACTERS = 5; Task?> SearchSystem(string value, CancellationToken cancellationToken); + Task?> SearchCharactere(string value, CancellationToken cancellationToken); - Task?> SearchCorporation(string value, CancellationToken cancellationToken); - Task?> SearchAlliance(string value, CancellationToken cancellationToken); + Task?> SearchCharactere(string value,bool strict, CancellationToken cancellationToken); + Task?> SearchCorporation(string value,bool strict, CancellationToken cancellationToken); + Task?> SearchAlliance(string value,bool strict, CancellationToken cancellationToken); Task?> SearchEveEntities(string value, CancellationToken cancellationToken); IEnumerable ValidateSearchType(string value); }