Skip to content

Commit

Permalink
Some Small Cleanup to Make Profiling Easier
Browse files Browse the repository at this point in the history
  • Loading branch information
da3dsoul committed Oct 25, 2023
1 parent 3eb1255 commit 952d15f
Showing 1 changed file with 56 additions and 48 deletions.
104 changes: 56 additions & 48 deletions Shoko.Server/Filters/Legacy/LegacyFilterConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,66 @@ public CL_GroupFilter ToClient(FilterPreset filter)
public Dictionary<FilterPreset, CL_GroupFilter> ToClient(IReadOnlyList<FilterPreset> filters)
{
var result = new Dictionary<FilterPreset, CL_GroupFilter>();
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<FilterPreset> otherFilters, Dictionary<FilterPreset, CL_GroupFilter> result)
{

var results = _evaluator.BatchEvaluateFilters(otherFilters, null, true);
var models = results.Select(kv =>
{
var filter = kv.Key;
var groupIds = new Dictionary<int, HashSet<int>>();
var seriesIds = new Dictionary<int, HashSet<int>>();
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<int>()
: RepoFactory.FilterPreset.GetByParentID(filter.FilterPresetID).Select(a => a.FilterPresetID).ToHashSet()
});
});

foreach (var (filter, model) in models)
{
result[filter] = model;
}
}

private void SetUserFilters(List<FilterPreset> userFilters, Dictionary<FilterPreset, CL_GroupFilter> 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 =>
{
Expand Down Expand Up @@ -146,51 +200,5 @@ public Dictionary<FilterPreset, CL_GroupFilter> ToClient(IReadOnlyList<FilterPre
{
result[filter] = model;
}

if (otherFilters.Count > 0)
{
var results = _evaluator.BatchEvaluateFilters(otherFilters, null);
var models = results.Select(kv =>
{
var filter = kv.Key;
var groupIds = new Dictionary<int, HashSet<int>>();
var seriesIds = new Dictionary<int, HashSet<int>>();
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<int>()
: RepoFactory.FilterPreset.GetByParentID(filter.FilterPresetID).Select(a => a.FilterPresetID).ToHashSet()
});
});

foreach (var (filter, model) in models)
{
result[filter] = model;
}
}

return result;
}
}

0 comments on commit 952d15f

Please sign in to comment.