Skip to content

Commit

Permalink
fix(rum-explorer): improve readability for filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
akalfas committed Jun 3, 2024
1 parent f19d8a7 commit b3c6640
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions tools/rum/cruncher.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,36 +385,40 @@ export class DataChunks {
* @param {string[]} skipped facets to skip
*/
filterBundles(bundles, filterSpec, skipped = []) {
const existenceFilter = ([facetName]) => this.facetFns[facetName];
const skipFilter = ([facetName]) => !skipped.includes(facetName);
const valuesExtractor = (attributeName, bundle, parent) => {
const existenceFilterFn = ([facetName]) => this.facetFns[facetName];
const skipFilterFn = ([facetName]) => !skipped.includes(facetName);
const valuesExtractorFn = (attributeName, bundle, parent) => {
const facetValue = parent.facetFns[attributeName](bundle);
return Array.isArray(facetValue) ? facetValue : [facetValue];
};
const combinerExtractor = (attributeName, parent) => parent.facetFns[attributeName].combiner || 'some';
const combinerExtractorFn = (attributeName, parent) => parent.facetFns[attributeName].combiner || 'some';
// eslint-disable-next-line max-len
return this.filtering(bundles, filterSpec, skipFilter, existenceFilter, valuesExtractor, combinerExtractor);
return this.applyFilter(bundles, filterSpec, skipFilterFn, existenceFilterFn, valuesExtractorFn, combinerExtractorFn);
}

/**
* @private
* @param {Bundle[]} bundles
* @param {Object<string, string[]>} filterSpec
* @param skipFilter function to skip filters
* @param existenceFilter function to filter out non-existing attributes
* @param valuesExtractor function to extract the probed values
* @param combinerExtractor function to extract the combiner
* @returns {*}
* @param {Bundle[]} bundles that will be filtered based on a filter specification.
* @param {Object<string, string[]>} filterSpec the filter specification.
* @param skipFilterFn function to skip filters. Useful for skipping unwanted facets,
* in general skipping attributes.
* @param existenceFilterFn function to filter out non-existing attributes.
* This is used to skip facets that have not been added. In general,
* this can be used to whitelist attributes names.
* @param valuesExtractorFn function to extract the probed values.
* @param combinerExtractorFn function to extract the combiner.
* @returns {Bundle[]} the filtered bundles.
*/
filtering(bundles, filterSpec, skipFilter, existenceFilter, valuesExtractor, combinerExtractor) {
const filterBy = Object.entries(filterSpec) // use the full filter spec
.filter(skipFilter) // except for skipped facets
.filter(([, desiredValues]) => desiredValues.length) // and filters that accept no values
.filter(existenceFilter); // and facets that don't exist
// eslint-disable-next-line max-len
applyFilter(bundles, filterSpec, skipFilterFn, existenceFilterFn, valuesExtractorFn, combinerExtractorFn) {
const filterBy = Object.entries(filterSpec)
.filter(skipFilterFn)
.filter(([, desiredValues]) => desiredValues.length)
.filter(existenceFilterFn);
return bundles.filter((bundle) => {
const matches = filterBy.map(([attributeName, desiredValues]) => {
const actualValues = valuesExtractor(attributeName, bundle, this);
const combiner = combinerExtractor(attributeName, this);
const actualValues = valuesExtractorFn(attributeName, bundle, this);
const combiner = combinerExtractorFn(attributeName, this);
return desiredValues[combiner]((value) => actualValues.includes(value));
});
return matches.every((match) => match);
Expand All @@ -429,13 +433,13 @@ export class DataChunks {
* @returns {boolean}
*/
hasConversion(aBundle, filterSpec, combiner) {
const existenceFilter = () => true;
const skipFilter = () => true;
const valuesExtractor = (attributeName, bundle) => bundle.events.map((e) => e[attributeName]);
const combinerExtractor = () => combiner || 'every';
const existenceFilterFn = () => true;
const skipFilterFn = () => true;
const valuesExtractorFn = (attributeName, bundle) => bundle.events.map((e) => e[attributeName]);
const combinerExtractorFn = () => combiner || 'every';

// eslint-disable-next-line max-len
return this.filtering([aBundle], filterSpec, skipFilter, existenceFilter, valuesExtractor, combinerExtractor).length > 0;
return this.applyFilter([aBundle], filterSpec, skipFilterFn, existenceFilterFn, valuesExtractorFn, combinerExtractorFn).length > 0;
}

filterBy(filterSpec) {
Expand Down

0 comments on commit b3c6640

Please sign in to comment.