Skip to content

Commit

Permalink
Move maxSuggestionCount to UI prop
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack Ord committed Nov 8, 2023
1 parent d578824 commit 98a0d4c
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 11 deletions.
14 changes: 12 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions packages/core/src/context/experiment/experiment-reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,10 @@ export type ExperimentAction =
}
| {
type: 'updateSuggestionCount'
payload: string
payload: {
suggestionCount: string
maxSuggestionCount?: number
}
}
| {
type: 'copySuggestedToDataPoints'
Expand Down Expand Up @@ -183,8 +186,13 @@ export const experimentReducer = produce(
experimentSchema.shape.info.shape.description.parse(action.payload)
break
case 'updateSuggestionCount': {
const payloadVal = Number(action.payload)
const actualVal = payloadVal <= 10 ? payloadVal : 10
const payloadVal = Number(action.payload.suggestionCount)
const maxSuggestionCount = action.payload.maxSuggestionCount
let actualVal = payloadVal
if (maxSuggestionCount !== undefined) {
actualVal =
payloadVal <= maxSuggestionCount ? payloadVal : maxSuggestionCount
}
state.extras.experimentSuggestionCount = actualVal >= 1 ? actualVal : 1
break
}
Expand Down
24 changes: 22 additions & 2 deletions packages/core/src/context/experiment/reducers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,36 @@ describe('experiment reducer', () => {
})

describe('updateSuggestionCount', () => {
it('should change suggestion count (min 1, max 10)', () => {
it('should cap suggestion count to max', () => {
const actual = rootReducer(initState, {
type: 'updateSuggestionCount',
payload: '42',
payload: { suggestionCount: '42', maxSuggestionCount: 10 },
})
expect(actual.experiment.extras).toMatchObject({
experimentSuggestionCount: 10,
})
expect(actual.experiment.changedSinceLastEvaluation).toBeTruthy()
})
it('should not cap suggestion count to max when unset', () => {
const actual = rootReducer(initState, {
type: 'updateSuggestionCount',
payload: { suggestionCount: '42' },
})
expect(actual.experiment.extras).toMatchObject({
experimentSuggestionCount: 42,
})
expect(actual.experiment.changedSinceLastEvaluation).toBeTruthy()
})
it('should set suggestion count to min 1', () => {
const actual = rootReducer(initState, {
type: 'updateSuggestionCount',
payload: { suggestionCount: '0' },
})
expect(actual.experiment.extras).toMatchObject({
experimentSuggestionCount: 1,
})
expect(actual.experiment.changedSinceLastEvaluation).toBeTruthy()
})
})

describe('Constraints', () => {
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/context/experiment/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ export const dummyPayloads: Payloads = {
updateExperimentDescription: '',
updateConfiguration: initialState.experiment.optimizerConfig,
updateDataPoints: initialState.experiment.dataPoints,
updateSuggestionCount: '',
updateSuggestionCount: {
suggestionCount: '',
},
copySuggestedToDataPoints: [],
'experiment/toggleMultiObjective': undefined,
'experiment/setConstraintSum': 0,
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"dependencies": {
"@boostv/process-optimizer-frontend-core": "*",
"@boostv/process-optimizer-frontend-plots": "*",
"lodash": "^4.17.21",
"lodash.debounce": "^4.0.8",
"react-hook-form": "^7.33.0",
"remeda": "^1.12.0",
"tss-react": "^4.8.2"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ interface ResultDataProps {
warning?: string
padding?: number
allowIndividualSuggestionCopy?: boolean
maxSuggestionCount?: number
toggleUISize?: () => void
onMouseEnterExpand?: () => void
onMouseLeaveExpand?: () => void
Expand All @@ -54,6 +55,7 @@ export const ExperimentationGuide = (props: ResultDataProps) => {
padding,
loadingMode,
allowIndividualSuggestionCopy = true,
maxSuggestionCount,
toggleUISize,
onMouseEnterExpand,
onMouseLeaveExpand,
Expand Down Expand Up @@ -181,6 +183,7 @@ export const ExperimentationGuide = (props: ResultDataProps) => {
{!isInitializing && (
<Box width={160}>
<NextExperiments
maxSuggestionCount={maxSuggestionCount}
onSuggestionChange={suggestionCount =>
debouncedUpdate(suggestionCount)
}
Expand Down
10 changes: 8 additions & 2 deletions packages/ui/src/features/result-data/next-experiments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ import {
} from '@boostv/process-optimizer-frontend-core'

type Props = {
maxSuggestionCount?: number
onSuggestionChange: (suggestionCount: string) => void
}

export const NextExperiments: FC<Props> = ({ onSuggestionChange }) => {
export const NextExperiments: FC<Props> = ({
onSuggestionChange,
maxSuggestionCount,
}) => {
const isSuggestionCountEditable = useSelector(selectIsSuggestionCountEditable)
const suggestionCount = useSelector(selectCalculatedSuggestionCount)
const [suggestionCountUI, setSuggestionCountUI] = useState(suggestionCount)
Expand All @@ -32,7 +36,9 @@ export const NextExperiments: FC<Props> = ({ onSuggestionChange }) => {
type="number"
value={suggestionCountUI + ''}
name="numberOfSuggestions"
label="Suggestions (1-10)"
label={`Suggestions${
maxSuggestionCount !== undefined ? ` (1-${maxSuggestionCount})` : ''
}`}
size="small"
onChange={val => {
setSuggestionCountUI(Number(val.target.value))
Expand Down

0 comments on commit 98a0d4c

Please sign in to comment.