From 72a75595f8dfc3206fce4b153f9b1ff1b4811012 Mon Sep 17 00:00:00 2001 From: Joseph Schick Date: Tue, 8 Nov 2022 19:26:28 -0600 Subject: [PATCH] Wired up filter updates to state filtering --- .../EventLog/EventLogAction.cs | 2 +- .../EventLog/EventLogReducers.cs | 9 +- .../FilterPane/FilterPaneAction.cs | 2 + .../FilterPane/FilterPaneEffects.cs | 33 +++++ .../FilterPane/FilterPaneReducers.cs | 8 +- .../Shared/Components/FilterRow.razor.cs | 118 ++++++++++++++++++ 6 files changed, 166 insertions(+), 6 deletions(-) create mode 100644 src/EventLogExpert.Store/FilterPane/FilterPaneEffects.cs diff --git a/src/EventLogExpert.Store/EventLog/EventLogAction.cs b/src/EventLogExpert.Store/EventLog/EventLogAction.cs index b9ee1dda..fe5dfecc 100644 --- a/src/EventLogExpert.Store/EventLog/EventLogAction.cs +++ b/src/EventLogExpert.Store/EventLog/EventLogAction.cs @@ -20,5 +20,5 @@ IReadOnlyList AllTaskNames public record ClearFilters : EventLogAction; - public record FilterEvents(List> Filter) : EventLogAction; + public record FilterEvents(IEnumerable> Filters) : EventLogAction; } diff --git a/src/EventLogExpert.Store/EventLog/EventLogReducers.cs b/src/EventLogExpert.Store/EventLog/EventLogReducers.cs index 5e15956c..fc478f27 100644 --- a/src/EventLogExpert.Store/EventLog/EventLogReducers.cs +++ b/src/EventLogExpert.Store/EventLog/EventLogReducers.cs @@ -13,10 +13,11 @@ public static EventLogState ReduceClearEvents(EventLogState state, EventLogActio new(state.ActiveLog, new List(), new List()); [ReducerMethod] - public static EventLogState ReduceFilterEvents(EventLogState state, EventLogAction.FilterEvents action) => - new(state.ActiveLog, - state.Events, - action.Filter.Count < 1 ? state.Events : state.Events.Where(ev => action.Filter.All(f => f(ev))).ToList()); + public static EventLogState ReduceFilterEvents(EventLogState state, EventLogAction.FilterEvents action) => new( + state.ActiveLog, + state.Events, + action.Filters.Any() is false ? state.Events : + state.Events.Where(ev => action.Filters.All(f => f(ev))).ToList()); [ReducerMethod(typeof(EventLogAction.ClearFilters))] public static EventLogState ReduceClearFilters(EventLogState state) => diff --git a/src/EventLogExpert.Store/FilterPane/FilterPaneAction.cs b/src/EventLogExpert.Store/FilterPane/FilterPaneAction.cs index 0435af64..1b331027 100644 --- a/src/EventLogExpert.Store/FilterPane/FilterPaneAction.cs +++ b/src/EventLogExpert.Store/FilterPane/FilterPaneAction.cs @@ -12,4 +12,6 @@ public record AddAvailableFilters(string FilterText); public record AddFilter; public record RemoveFilter(FilterModel FilterModel); + + public record ApplyFilters; } diff --git a/src/EventLogExpert.Store/FilterPane/FilterPaneEffects.cs b/src/EventLogExpert.Store/FilterPane/FilterPaneEffects.cs new file mode 100644 index 00000000..b56a3062 --- /dev/null +++ b/src/EventLogExpert.Store/FilterPane/FilterPaneEffects.cs @@ -0,0 +1,33 @@ +// // Copyright (c) Microsoft Corporation. +// // Licensed under the MIT License. + +using EventLogExpert.Library.Models; +using EventLogExpert.Store.EventLog; +using Fluxor; + +namespace EventLogExpert.Store.FilterPane; + +public class FilterPaneEffects +{ + private readonly IState _state; + + public FilterPaneEffects(IState state) => _state = state; + + [EffectMethod(typeof(FilterPaneAction.ApplyFilters))] + public Task HandleApplyFiltersAction(IDispatcher dispatcher) + { + List> filters = new(); + + foreach (var filter in _state.Value.CurrentFilters) + { + if (filter.Comparison is not null) + { + filters.Add(filter.Comparison); + } + } + + dispatcher.Dispatch(new EventLogAction.FilterEvents(filters)); + + return Task.CompletedTask; + } +} diff --git a/src/EventLogExpert.Store/FilterPane/FilterPaneReducers.cs b/src/EventLogExpert.Store/FilterPane/FilterPaneReducers.cs index d021e660..7bc7dac8 100644 --- a/src/EventLogExpert.Store/FilterPane/FilterPaneReducers.cs +++ b/src/EventLogExpert.Store/FilterPane/FilterPaneReducers.cs @@ -9,6 +9,10 @@ namespace EventLogExpert.Store.FilterPane; public class FilterPaneReducers { + private readonly IDispatcher _dispatcher; + + public FilterPaneReducers(IDispatcher dispatcher) => _dispatcher = dispatcher; + [ReducerMethod(typeof(FilterPaneAction.AddFilter))] public static FilterPaneState ReduceAddFilterAction(FilterPaneState state) { @@ -35,7 +39,7 @@ public static AvailableFilterState ReduceLoadEventsAction(AvailableFilterState s EventLogAction.LoadEvents action) => new(action.AllEventIds, action.AllProviderNames, action.AllTaskNames); [ReducerMethod] - public static FilterPaneState ReduceRemoveFilterAction(FilterPaneState state, FilterPaneAction.RemoveFilter action) + public FilterPaneState ReduceRemoveFilterAction(FilterPaneState state, FilterPaneAction.RemoveFilter action) { var updatedList = state.CurrentFilters.ToList(); var filter = updatedList.FirstOrDefault(filter => filter.Id == action.FilterModel.Id); @@ -47,6 +51,8 @@ public static FilterPaneState ReduceRemoveFilterAction(FilterPaneState state, Fi updatedList.Remove(filter); + _dispatcher.Dispatch(new FilterPaneAction.ApplyFilters()); + return new FilterPaneState(updatedList); } } diff --git a/src/EventLogExpert/Shared/Components/FilterRow.razor.cs b/src/EventLogExpert/Shared/Components/FilterRow.razor.cs index 24477b07..ca0936f7 100644 --- a/src/EventLogExpert/Shared/Components/FilterRow.razor.cs +++ b/src/EventLogExpert/Shared/Components/FilterRow.razor.cs @@ -36,9 +36,30 @@ private void SaveFilter() if (SetComparisonString()) { // Set the actual filter + SetComparison(); } Value.IsEditing = false; + + Dispatcher.Dispatch(new FilterPaneAction.ApplyFilters()); + } + + private void SetComparison() + { + switch (Value.FilterComparison) + { + case FilterComparison.Equals : + SetEqualsComparison(); + break; + case FilterComparison.Contains : + SetContainsComparison(); + break; + case FilterComparison.NotEqual : + SetNotEqualsComparison(); + break; + default : + return; + } } private bool SetComparisonString() @@ -98,4 +119,101 @@ private bool SetComparisonString() return true; } + + private void SetContainsComparison() + { + switch (Value.FilterType) + { + case FilterType.EventId : + // TODO: Clean this up, extension method? + Value.Comparison = x => x.Id.ToString().Contains(Value.FilterIntValue.ToString()!); + break; + case FilterType.Severity : + Value.Comparison = x => x.Level.ToString()!.Contains(Value.FilterSeverityValue.ToString()!); + break; + case FilterType.Provider : + Value.Comparison = x => + x.ProviderName.Contains(Value.FilterStringValue!, StringComparison.InvariantCultureIgnoreCase); + + break; + case FilterType.Task : + Value.Comparison = x => + x.TaskDisplayName.Contains(Value.FilterStringValue!, StringComparison.InvariantCultureIgnoreCase); + + break; + case FilterType.Description : + Value.Comparison = x => + x.Description.Contains(Value.FilterStringValue!, StringComparison.InvariantCultureIgnoreCase); + + break; + default : + return; + } + } + + private void SetEqualsComparison() + { + switch (Value.FilterType) + { + case FilterType.EventId : + Value.Comparison = x => x.Id == Value.FilterIntValue; + break; + case FilterType.Severity : + Value.Comparison = x => x.Level == Value.FilterSeverityValue; + break; + case FilterType.Provider : + Value.Comparison = x => string.Equals(x.ProviderName, + Value.FilterStringValue, + StringComparison.InvariantCultureIgnoreCase); + + break; + case FilterType.Task : + Value.Comparison = x => string.Equals(x.TaskDisplayName, + Value.FilterStringValue, + StringComparison.InvariantCultureIgnoreCase); + + break; + case FilterType.Description : + Value.Comparison = x => string.Equals(x.Description, + Value.FilterStringValue, + StringComparison.InvariantCultureIgnoreCase); + + break; + default : + return; + } + } + + private void SetNotEqualsComparison() + { + switch (Value.FilterType) + { + case FilterType.EventId : + Value.Comparison = x => x.Id != Value.FilterIntValue; + break; + case FilterType.Severity : + Value.Comparison = x => x.Level != Value.FilterSeverityValue; + break; + case FilterType.Provider : + Value.Comparison = x => !string.Equals(x.ProviderName, + Value.FilterStringValue, + StringComparison.InvariantCultureIgnoreCase); + + break; + case FilterType.Task : + Value.Comparison = x => !string.Equals(x.TaskDisplayName, + Value.FilterStringValue, + StringComparison.InvariantCultureIgnoreCase); + + break; + case FilterType.Description : + Value.Comparison = x => !string.Equals(x.Description, + Value.FilterStringValue, + StringComparison.InvariantCultureIgnoreCase); + + break; + default : + return; + } + } }