diff --git a/tensorboard/webapp/runs/store/runs_selectors.ts b/tensorboard/webapp/runs/store/runs_selectors.ts index ce6f670bc6..23ceea336d 100644 --- a/tensorboard/webapp/runs/store/runs_selectors.ts +++ b/tensorboard/webapp/runs/store/runs_selectors.ts @@ -79,6 +79,22 @@ export const getRuns = createSelector( } ); +export const getRunsFromExperimentIds = (experimentIds: string[]) => + createSelector( + getDataState, + (state: RunsDataState): Array => { + return experimentIds.reduce((runs, experimentId) => { + (state.runIds[experimentId] || []) + .filter((id) => Boolean(state.runMetadata[id])) + .forEach((runId) => { + runs.push({...state.runMetadata[runId], experimentId}); + }); + + return runs; + }, [] as Array); + } + ); + /** * Returns Observable that emits runs list for an experiment. */ diff --git a/tensorboard/webapp/runs/store/runs_selectors_test.ts b/tensorboard/webapp/runs/store/runs_selectors_test.ts index c99189b187..5b5797544a 100644 --- a/tensorboard/webapp/runs/store/runs_selectors_test.ts +++ b/tensorboard/webapp/runs/store/runs_selectors_test.ts @@ -176,6 +176,57 @@ describe('runs_selectors', () => { }); }); + describe('#getRunsFromExperimentIds', () => { + it('returns runs', () => { + const state = buildStateFromRunsState( + buildRunsState({ + runIds: { + eid: ['run1'], + }, + runMetadata: { + run1: buildRun({id: 'run1'}), + }, + }) + ); + expect(selectors.getRunsFromExperimentIds(['eid'])(state)).toEqual([ + { + ...buildRun({ + id: 'run1', + }), + experimentId: 'eid', + }, + ]); + }); + + it('returns runs for the ones that has metadata', () => { + const state = buildStateFromRunsState( + buildRunsState({ + runIds: { + eid: ['run1', 'run2'], + }, + runMetadata: { + run1: buildRun({id: 'run1'}), + }, + }) + ); + expect(selectors.getRunsFromExperimentIds(['eid'])(state)).toEqual([ + { + ...buildRun({ + id: 'run1', + }), + experimentId: 'eid', + }, + ]); + }); + + it('returns empty list if experiment id does not exist', () => { + const state = buildStateFromRunsState(buildRunsState()); + expect( + selectors.getRunsFromExperimentIds(['i_do_not_exist'])(state) + ).toEqual([]); + }); + }); + describe('#getRunIdsForExperiment', () => { beforeEach(() => { // Clear the memoization.