Skip to content

Commit

Permalink
Filtering bug fixes and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronPlave committed Jan 6, 2025
1 parent f8bea69 commit dd79f3c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,17 @@
function onTypeSubfilterRemove(type: string, id: number) {
const typeSubfilters = dirtyFilter.type_subfilters || {};
const currentFilters = typeSubfilters[type];
const newFilters = currentFilters.filter(f => f.id !== id);
dirtyFilter = {
...dirtyFilter,
type_subfilters: {
...typeSubfilters,
[type]: currentFilters.filter(f => f.id !== id),
[type]: newFilters,
},
};
if (dirtyFilter.type_subfilters && newFilters.length === 0) {
delete dirtyFilter.type_subfilters[type];
}
dispatch('filterChange', { filter: dirtyFilter });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@
return (
(layer.filter.activity?.static_types?.length ?? 0) +
(layer.filter.activity?.dynamic_type_filters?.length ?? 0) +
(layer.filter.activity?.other_filters?.length ?? 0)
(layer.filter.activity?.other_filters?.length ?? 0) +
(layer.filter.activity?.type_subfilters ? Object.keys(layer.filter.activity?.type_subfilters).length : 0)
);
}
Expand Down
129 changes: 57 additions & 72 deletions src/utilities/timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1405,7 +1405,7 @@ export function applyActivityLayerFilter(
(!filter.dynamic_type_filters?.length &&
!filter.other_filters?.length &&
!filter.static_types?.length &&
!filter.type_subfilters?.length)
(!filter.type_subfilters || !Object.keys(filter.type_subfilters).length))
) {
return { directives, spans };
}
Expand All @@ -1417,7 +1417,7 @@ export function applyActivityLayerFilter(
},
{},
);
// TODO could be passed in to avoid recomputing this

const typeDefMap: Record<string, ActivityType> = (types || []).reduce(
(acc: Record<string, ActivityType>, cur: ActivityType) => {
acc[cur.name] = cur;
Expand All @@ -1426,81 +1426,65 @@ export function applyActivityLayerFilter(
{},
);

const anyTypeFiltersSpecified = !!(filter.static_types?.length || filter.dynamic_type_filters?.length);
const filteredDirectives = directives.filter(directive => {
let included = false;

// Check to see if directive is included in static list
if (filter.static_types?.length) {
included = !!staticTypeMap[directive.type];
}

// Check if necessary to see if directive is included in dynamic list
if ((!filter.static_types?.length || !included) && filter.dynamic_type_filters?.length) {
included = directiveOrSpanMatchesDynamicFilters(
directive,
filter.dynamic_type_filters,
typeDefMap,
defaultArgumentsMap,
);
}

// Apply other filters on top of the types
if (filter.other_filters?.length) {
included =
directiveOrSpanMatchesDynamicFilters(directive, filter.other_filters, typeDefMap, defaultArgumentsMap) &&
(anyTypeFiltersSpecified ? included : true);
}

// Apply type specific filters if found
if (included && filter.type_subfilters && filter.type_subfilters[directive.type]) {
included = directiveOrSpanMatchesDynamicFilters(
directive,
filter.type_subfilters[directive.type],
typeDefMap,
defaultArgumentsMap,
);
}
return included;
return applyFiltersToDirectiveOrSpan(directive, filter, staticTypeMap, typeDefMap, defaultArgumentsMap);
});

const filteredSpans = spans.filter(span => {
let included = false;
return applyFiltersToDirectiveOrSpan(span, filter, staticTypeMap, typeDefMap, defaultArgumentsMap);
});
return { directives: filteredDirectives, spans: filteredSpans };
}

// Check to see if span is included in static list
if (filter.static_types?.length) {
included = !!staticTypeMap[span.type];
}
export function applyFiltersToDirectiveOrSpan(
directiveOrSpan: ActivityDirective | Span,
filter: ActivityLayerFilter,
staticTypeMap: Record<string, boolean>,
typeDefMap: Record<string, ActivityType>,
defaultArgumentsMap: DefaultEffectiveArgumentsMap,
) {
const anyTypeFiltersSpecified = !!(filter.static_types?.length || filter.dynamic_type_filters?.length);
const anyMainFiltersSpecified = anyTypeFiltersSpecified || !!filter.other_filters?.length;
let included = !anyMainFiltersSpecified;

// Check if necessary to see if span is included in dynamic list
if ((!filter.static_types?.length || !included) && filter.dynamic_type_filters?.length) {
included = directiveOrSpanMatchesDynamicFilters(
span,
filter.dynamic_type_filters,
typeDefMap,
defaultArgumentsMap,
);
}
// Check to see if directive is included in static list
if (filter.static_types?.length) {
included = !!staticTypeMap[directiveOrSpan.type];
}

// Apply other filters on top of the types
if (filter.other_filters?.length) {
included =
directiveOrSpanMatchesDynamicFilters(span, filter.other_filters, typeDefMap, defaultArgumentsMap) &&
(anyTypeFiltersSpecified ? included : true);
}
// Check if necessary to see if directive is included in dynamic list
if ((!filter.static_types?.length || !included) && filter.dynamic_type_filters?.length) {
included = directiveOrSpanMatchesDynamicFilters(
directiveOrSpan,
filter.dynamic_type_filters,
typeDefMap,
defaultArgumentsMap,
);
}

// Apply other filters on top of the types
if (filter.other_filters?.length) {
included =
directiveOrSpanMatchesDynamicFilters(directiveOrSpan, filter.other_filters, typeDefMap, defaultArgumentsMap) &&
(anyTypeFiltersSpecified ? included : true);
}

// Apply type specific filters if found
if (included && filter.type_subfilters && filter.type_subfilters[span.type]) {
included = directiveOrSpanMatchesDynamicFilters(
span,
filter.type_subfilters[span.type],
// Apply type specific filters if found and if the type is already included or
// if no other filters were specified (case where all types are included by default)
if (
filter.type_subfilters &&
filter.type_subfilters[directiveOrSpan.type] &&
filter.type_subfilters[directiveOrSpan.type].length
) {
included =
directiveOrSpanMatchesDynamicFilters(
directiveOrSpan,
filter.type_subfilters[directiveOrSpan.type],
typeDefMap,
defaultArgumentsMap,
);
}
return included;
});
return { directives: filteredDirectives, spans: filteredSpans };
) && (anyMainFiltersSpecified ? included : true);
}
return included;
}

export function getMatchingTypesForActivityLayerFilter(filter: ActivityLayerFilter | undefined, types: ActivityType[]) {
Expand All @@ -1509,7 +1493,7 @@ export function getMatchingTypesForActivityLayerFilter(filter: ActivityLayerFilt
(!filter.dynamic_type_filters?.length &&
!filter.other_filters?.length &&
!filter.static_types?.length &&
!filter.type_subfilters?.length)
(!filter.type_subfilters || !Object.keys(filter.type_subfilters).length))
) {
return types;
}
Expand All @@ -1522,8 +1506,10 @@ export function getMatchingTypesForActivityLayerFilter(filter: ActivityLayerFilt
{},
);

const anyTypeFiltersSpecified = !!(filter.static_types?.length || filter.dynamic_type_filters?.length);

return types.filter(type => {
let included = false;
let included = !anyTypeFiltersSpecified;

// Check to see if type is included in static list
if (filter.static_types?.length) {
Expand All @@ -1534,7 +1520,6 @@ export function getMatchingTypesForActivityLayerFilter(filter: ActivityLayerFilt
if ((!filter.static_types?.length || !included) && filter.dynamic_type_filters?.length) {
included = typeMatchesDynamicFilters(type, filter.dynamic_type_filters);
}

return included;
});
}
Expand Down Expand Up @@ -1596,8 +1581,8 @@ export function typeMatchesDynamicFilters(
let matches = false;
if (curr.field === 'Type') {
matches = matchesDynamicFilter(type.name, curr.operator, curr.value);
} else if (curr.field === 'Subsystem' && typeof type.subsystem_tag?.id === 'number') {
matches = matchesDynamicFilter(type.subsystem_tag.id, curr.operator, curr.value);
} else if (curr.field === 'Subsystem') {
matches = matchesDynamicFilter(type.subsystem_tag?.id ?? -1, curr.operator, curr.value);
}
return acc && matches;
}, true);
Expand Down

0 comments on commit dd79f3c

Please sign in to comment.