From 952d15f952cfb61c9b944ff706fda5e8aa15eb5c Mon Sep 17 00:00:00 2001 From: da3dsoul Date: Wed, 25 Oct 2023 09:59:58 -0400 Subject: [PATCH] Some Small Cleanup to Make Profiling Easier --- .../Filters/Legacy/LegacyFilterConverter.cs | 104 ++++++++++-------- 1 file changed, 56 insertions(+), 48 deletions(-) diff --git a/Shoko.Server/Filters/Legacy/LegacyFilterConverter.cs b/Shoko.Server/Filters/Legacy/LegacyFilterConverter.cs index 4b95a1100..e413cdbc5 100644 --- a/Shoko.Server/Filters/Legacy/LegacyFilterConverter.cs +++ b/Shoko.Server/Filters/Legacy/LegacyFilterConverter.cs @@ -103,12 +103,66 @@ public CL_GroupFilter ToClient(FilterPreset filter) public Dictionary ToClient(IReadOnlyList filters) { var result = new Dictionary(); - var userFilters = filters.Where(a => (a?.Expression?.UserDependent ?? false) || (a?.SortingExpression?.UserDependent ?? false)).ToList(); + var userFilters = filters.Where(a => a?.Expression?.UserDependent ?? false).ToList(); var otherFilters = filters.Except(userFilters).ToList(); // batch evaluate each list, then build the mappings + if (userFilters.Count > 0) SetUserFilters(userFilters, result); + if (otherFilters.Count > 0) SetOtherFilters(otherFilters, result); + + return result; + } + + private void SetOtherFilters(List otherFilters, Dictionary result) + { + + var results = _evaluator.BatchEvaluateFilters(otherFilters, null, true); + var models = results.Select(kv => + { + var filter = kv.Key; + var groupIds = new Dictionary>(); + var seriesIds = new Dictionary>(); + var groupIdSet = kv.Value.Select(a => a.Key).ToHashSet(); + var seriesIdSet = kv.Value.SelectMany(a => a).ToHashSet(); + foreach (var user in RepoFactory.JMMUser.GetAll()) + { + groupIds[user.JMMUserID] = groupIdSet.Where(a => user.AllowedGroup(RepoFactory.AnimeGroup.GetByID(a))).ToHashSet(); + seriesIds[user.JMMUserID] = seriesIdSet.Where(a => user.AllowedSeries(RepoFactory.AnimeSeries.GetByID(a))).ToHashSet(); + } + + LegacyConditionConverter.TryConvertToConditions(filter, out var conditions, out var baseCondition); + conditions?.ForEach(condition => condition.GroupFilterID = filter.FilterPresetID); + return (Filter: filter, new CL_GroupFilter + { + GroupFilterID = filter.FilterPresetID, + GroupFilterName = filter.Name, + ApplyToSeries = filter.ApplyAtSeriesLevel ? 1 : 0, + Locked = filter.Locked ? 1 : 0, + FilterType = (int)filter.FilterType, + ParentGroupFilterID = filter.ParentFilterPresetID, + InvisibleInClients = filter.Hidden ? 1 : 0, + BaseCondition = (int)baseCondition, + FilterConditions = conditions, + SortingCriteria = LegacyConditionConverter.GetSortingCriteria(filter), + Groups = groupIds, + Series = seriesIds, + Childs = filter.FilterPresetID == 0 + ? new HashSet() + : RepoFactory.FilterPreset.GetByParentID(filter.FilterPresetID).Select(a => a.FilterPresetID).ToHashSet() + }); + }); + + foreach (var (filter, model) in models) + { + result[filter] = model; + } + } + + private void SetUserFilters(List userFilters, Dictionary result) + { + var userResults = RepoFactory.JMMUser.GetAll() - .SelectMany(user => _evaluator.BatchEvaluateFilters(userFilters, user.JMMUserID).Select(a => (a.Key, user.JMMUserID, a.Value))) + .SelectMany(user => _evaluator.BatchEvaluateFilters(userFilters, user.JMMUserID, true).Select(a => (a.Key, user.JMMUserID, a.Value))) .GroupBy(a => a.Key, a => (a.JMMUserID, a.Value)); var userModels = userResults.Select(group => { @@ -146,51 +200,5 @@ public Dictionary ToClient(IReadOnlyList 0) - { - var results = _evaluator.BatchEvaluateFilters(otherFilters, null); - var models = results.Select(kv => - { - var filter = kv.Key; - var groupIds = new Dictionary>(); - var seriesIds = new Dictionary>(); - var groupIdSet = kv.Value.Select(a => a.Key).ToHashSet(); - var seriesIdSet = kv.Value.SelectMany(a => a).ToHashSet(); - foreach (var user in RepoFactory.JMMUser.GetAll()) - { - groupIds[user.JMMUserID] = groupIdSet.Where(a => user.AllowedGroup(RepoFactory.AnimeGroup.GetByID(a))).ToHashSet(); - seriesIds[user.JMMUserID] = seriesIdSet.Where(a => user.AllowedSeries(RepoFactory.AnimeSeries.GetByID(a))).ToHashSet(); - } - - LegacyConditionConverter.TryConvertToConditions(filter, out var conditions, out var baseCondition); - conditions?.ForEach(condition => condition.GroupFilterID = filter.FilterPresetID); - return (Filter: filter, new CL_GroupFilter - { - GroupFilterID = filter.FilterPresetID, - GroupFilterName = filter.Name, - ApplyToSeries = filter.ApplyAtSeriesLevel ? 1 : 0, - Locked = filter.Locked ? 1 : 0, - FilterType = (int)filter.FilterType, - ParentGroupFilterID = filter.ParentFilterPresetID, - InvisibleInClients = filter.Hidden ? 1 : 0, - BaseCondition = (int)baseCondition, - FilterConditions = conditions, - SortingCriteria = LegacyConditionConverter.GetSortingCriteria(filter), - Groups = groupIds, - Series = seriesIds, - Childs = filter.FilterPresetID == 0 - ? new HashSet() - : RepoFactory.FilterPreset.GetByParentID(filter.FilterPresetID).Select(a => a.FilterPresetID).ToHashSet() - }); - }); - - foreach (var (filter, model) in models) - { - result[filter] = model; - } - } - - return result; } }