Skip to content

Commit

Permalink
Improve sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Sep 12, 2021
1 parent 81008ff commit c969e71
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 64 deletions.
3 changes: 3 additions & 0 deletions src/combination/dimensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { getInputIds } from './inputs.js'
// - can be used in `select`, `limit`, etc.
// - are checked for duplicates
// As opposed to non-combination identifiers: inputs.
// The order is significant as it defines:
// - The sorting order in reporters
// - The order when printing combinationName in previews and `dev`
export const COMBINATION_DIMENSIONS = [
{
dimension: 'task',
Expand Down
62 changes: 0 additions & 62 deletions src/combination/group.js

This file was deleted.

46 changes: 46 additions & 0 deletions src/combination/sort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import mapObj from 'map-obj'
import sortOn from 'sort-on'

import { getMean } from '../stats/sum.js'
import { groupBy } from '../utils/group.js'

import { COMBINATION_DIMENSIONS } from './dimensions.js'

// Sort combinations based on their `stats.mean`.
// Combinations with the same dimension are grouped together in the sorting
// order.
export const sortCombinations = function (result) {
const sortFunctions = COMBINATION_DIMENSIONS.map(({ idName }) =>
getSortFunction(idName, result.combinations),
)
const combinations = sortOn(result.combinations, sortFunctions)
return { ...result, combinations }
}

// Retrieve a function used to compare combinations for a specific dimension
const getSortFunction = function (idName, combinations) {
const meansOfMeans = mapObj(groupBy(combinations, idName), getMeanOfMeans)
return getCombinationOrder.bind(undefined, idName, meansOfMeans)
}

// Retrieve the mean of all `stat.mean` for a specific dimension and id.
// `undefined` means are omitted. Ids with all means undefined are sorted last.
const getMeanOfMeans = function (id, combinations) {
const means = combinations.map(getCombinationMean).filter(isDefined)
const meanOfMeans =
means.length === 0 ? Number.POSITIVE_INFINITY : getMean(means)
return [id, meanOfMeans]
}

const getCombinationMean = function ({ stats: { mean } }) {
return mean
}

const isDefined = function (mean) {
return mean !== undefined
}

const getCombinationOrder = function (idName, meansOfMeans, combination) {
const id = combination[idName]
return meansOfMeans[id]
}
4 changes: 2 additions & 2 deletions src/report/normalize/common.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { groupResultCombinations } from '../../combination/group.js'
import { sortCombinations } from '../../combination/sort.js'
import { addCombinationsDiff } from '../../history/compare/diff.js'
import { omitSystemProps } from '../../system/omit.js'
import { normalizeTimestamp } from '../../system/timestamp.js'
Expand Down Expand Up @@ -37,7 +37,7 @@ export const normalizeCombAllUnmerged = function (result, sinceResult) {
// Add report-specific properties to a result that are in `combinations`, are
// not reporter-specific and must be applied after the history is merged.
export const normalizeCombAllMerged = function (result) {
const resultA = groupResultCombinations(result)
const resultA = sortCombinations(result)
const resultB = omitSystemProps(resultA)
return resultB
}
Expand Down

0 comments on commit c969e71

Please sign in to comment.