Skip to content

Commit

Permalink
refactor and tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
mattseddon committed Jun 5, 2023
1 parent d47a04d commit a400131
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
22 changes: 22 additions & 0 deletions extension/src/experiments/model/collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,28 @@ const collectExpState = (
return baseline
}

export const collectAddRemoveCommitsDetails = (
availableNbCommits: {
[branch: string]: number
},
getNbOfCommitsToShow: (branch: string) => number
): {
hasMoreCommits: { [branch: string]: boolean }
isShowingMoreCommits: { [branch: string]: boolean }
} => {
const hasMoreCommits: { [branch: string]: boolean } = {}
const isShowingMoreCommits: { [branch: string]: boolean } = {}

for (const [branch, availableCommits] of Object.entries(availableNbCommits)) {
const nbOfCommitsToShow = getNbOfCommitsToShow(branch)
hasMoreCommits[branch] = availableCommits > nbOfCommitsToShow
isShowingMoreCommits[branch] =
Math.min(nbOfCommitsToShow, availableCommits) > 1
}

return { hasMoreCommits, isShowingMoreCommits }
}

const getExecutor = (experiment: Experiment): Executor => {
if ([experiment.executor, experiment.id].includes(EXPERIMENT_WORKSPACE_ID)) {
return Executor.WORKSPACE
Expand Down
17 changes: 7 additions & 10 deletions extension/src/experiments/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Memento } from 'vscode'
import { SortDefinition, sortExperiments } from './sortBy'
import { FilterDefinition, filterExperiment, getFilterId } from './filterBy'
import {
collectAddRemoveCommitsDetails,
collectExperiments,
collectOrderedCommitsAndExperiments,
collectRunningInQueue,
Expand Down Expand Up @@ -128,17 +129,13 @@ export class ExperimentsModel extends ModelWithPersistence {
hasCheckpoints
} = collectExperiments(expShow, gitLog, dvcLiveOnly)

this.hasMoreCommits = {}
this.isShowingMoreCommits = {}
const { hasMoreCommits, isShowingMoreCommits } =
collectAddRemoveCommitsDetails(availableNbCommits, (branch: string) =>
this.getNbOfCommitsToShow(branch)
)

for (const [branch, availableCommits] of Object.entries(
availableNbCommits
)) {
const nbOfCommitsToShow = this.getNbOfCommitsToShow(branch)
this.hasMoreCommits[branch] = availableCommits > nbOfCommitsToShow
this.isShowingMoreCommits[branch] =
Math.min(nbOfCommitsToShow, availableCommits) > 1
}
this.hasMoreCommits = hasMoreCommits
this.isShowingMoreCommits = isShowingMoreCommits

commits.sort((a, b) => (b.Created || '').localeCompare(a.Created || ''))

Expand Down
8 changes: 4 additions & 4 deletions extension/src/test/suite/experiments/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ import * as ProcessExecution from '../../../process/execution'
import { DvcReader } from '../../../cli/dvc/reader'
import { DvcViewer } from '../../../cli/dvc/viewer'
import { DEFAULT_NB_ITEMS_PER_ROW } from '../../../plots/webview/contract'
import { GitReader } from '../../../cli/git/reader'
import { Toast } from '../../../vscode/toast'
import { Response } from '../../../vscode/response'

Expand Down Expand Up @@ -288,12 +287,13 @@ suite('Experiments Test Suite', () => {
}).timeout(WEBVIEW_TEST_TIMEOUT)

it('should set isShowingMoreCommits to false it is showing only the current commit', async () => {
stub(GitReader.prototype, 'getCurrentBranch').resolves('current')
const { experiments, messageSpy } = buildExperiments({
availableNbCommits: { main: 1 },
const { experiments, experimentsModel, messageSpy } = buildExperiments({
expShow: expShowFixture.slice(0, 2),
disposer: disposable
})

stub(experimentsModel, 'getNbOfCommitsToShow').returns(1)

await experiments.showWebview()

expect(messageSpy).to.be.calledWithMatch({
Expand Down

0 comments on commit a400131

Please sign in to comment.