@@ -12,7 +12,9 @@ import {
1212 TemplatePlotSection ,
1313 PlotsType ,
1414 CustomPlotData ,
15- CustomPlotValues
15+ CustomPlotValues ,
16+ ComparisonRevisionData ,
17+ ComparisonPlotImg
1618} from '../webview/contract'
1719import { PlotsOutput } from '../../cli/dvc/contract'
1820import { splitColumnPath } from '../../experiments/columns/paths'
@@ -34,6 +36,12 @@ import {
3436import { StrokeDashEncoding } from '../multiSource/constants'
3537import { exists } from '../../fileSystem'
3638import { hasKey } from '../../util/object'
39+ import { MULTI_IMAGE_PATH_REG } from '../../cli/dvc/constants'
40+ import {
41+ getFileNameWithoutExt ,
42+ getParent ,
43+ getPathArray
44+ } from '../../fileSystem/util'
3745
3846export const getCustomPlotId = ( metric : string , param : string ) =>
3947 `custom-${ metric } -${ param } `
@@ -126,18 +134,31 @@ export type RevisionData = {
126134 [ label : string ] : RevisionPathData
127135}
128136
137+ type ComparisonDataImgPlot = ImagePlot & { ind ?: number }
138+
129139export type ComparisonData = {
130140 [ label : string ] : {
131- [ path : string ] : ImagePlot
141+ [ path : string ] : ComparisonDataImgPlot [ ]
132142 }
133143}
134144
145+ const getMultiImagePath = ( path : string ) =>
146+ getParent ( getPathArray ( path ) , 0 ) as string
147+
148+ const getMultiImageInd = ( path : string ) => {
149+ const fileName = getFileNameWithoutExt ( path )
150+ return Number ( fileName )
151+ }
152+
135153const collectImageData = (
136154 acc : ComparisonData ,
137155 path : string ,
138156 plot : ImagePlot
139157) => {
158+ const isMultiImgPlot = MULTI_IMAGE_PATH_REG . test ( path )
159+ const pathLabel = isMultiImgPlot ? getMultiImagePath ( path ) : path
140160 const id = plot . revisions ?. [ 0 ]
161+
141162 if ( ! id ) {
142163 return
143164 }
@@ -146,7 +167,17 @@ const collectImageData = (
146167 acc [ id ] = { }
147168 }
148169
149- acc [ id ] [ path ] = plot
170+ if ( ! acc [ id ] [ pathLabel ] ) {
171+ acc [ id ] [ pathLabel ] = [ ]
172+ }
173+
174+ const imgPlot : ComparisonDataImgPlot = { ...plot }
175+
176+ if ( isMultiImgPlot ) {
177+ imgPlot . ind = getMultiImageInd ( path )
178+ }
179+
180+ acc [ id ] [ pathLabel ] . push ( imgPlot )
150181}
151182
152183const collectDatapoints = (
@@ -202,6 +233,16 @@ const collectPathData = (acc: DataAccumulator, path: string, plots: Plot[]) => {
202233 }
203234}
204235
236+ const sortComparisonImgPaths = ( acc : DataAccumulator ) => {
237+ for ( const [ label , paths ] of Object . entries ( acc . comparisonData ) ) {
238+ for ( const path of Object . keys ( paths ) ) {
239+ acc . comparisonData [ label ] [ path ] . sort (
240+ ( img1 , img2 ) => ( img1 . ind || 0 ) - ( img2 . ind || 0 )
241+ )
242+ }
243+ }
244+ }
245+
205246export const collectData = ( output : PlotsOutput ) : DataAccumulator => {
206247 const { data } = output
207248 const acc = {
@@ -213,6 +254,72 @@ export const collectData = (output: PlotsOutput): DataAccumulator => {
213254 collectPathData ( acc , path , plots )
214255 }
215256
257+ sortComparisonImgPaths ( acc )
258+
259+ return acc
260+ }
261+
262+ type ComparisonPlotsAcc = { path : string ; revisions : ComparisonRevisionData } [ ]
263+
264+ type GetComparisonPlotImg = (
265+ img : ImagePlot ,
266+ id : string ,
267+ path : string
268+ ) => ComparisonPlotImg
269+
270+ const collectSelectedPathComparisonPlots = ( {
271+ acc,
272+ comparisonData,
273+ path,
274+ selectedRevisionIds,
275+ getComparisonPlotImg
276+ } : {
277+ acc : ComparisonPlotsAcc
278+ comparisonData : ComparisonData
279+ path : string
280+ selectedRevisionIds : string [ ]
281+ getComparisonPlotImg : GetComparisonPlotImg
282+ } ) => {
283+ const pathRevisions = {
284+ path,
285+ revisions : { } as ComparisonRevisionData
286+ }
287+
288+ for ( const id of selectedRevisionIds ) {
289+ const imgs = comparisonData [ id ] ?. [ path ]
290+ pathRevisions . revisions [ id ] = {
291+ id,
292+ imgs : imgs
293+ ? imgs . map ( img => getComparisonPlotImg ( img , id , path ) )
294+ : [ { errors : undefined , loading : false , url : undefined } ]
295+ }
296+ }
297+ acc . push ( pathRevisions )
298+ }
299+
300+ export const collectSelectedComparisonPlots = ( {
301+ comparisonData,
302+ paths,
303+ selectedRevisionIds,
304+ getComparisonPlotImg
305+ } : {
306+ comparisonData : ComparisonData
307+ paths : string [ ]
308+ selectedRevisionIds : string [ ]
309+ getComparisonPlotImg : GetComparisonPlotImg
310+ } ) => {
311+ const acc : ComparisonPlotsAcc = [ ]
312+
313+ for ( const path of paths ) {
314+ collectSelectedPathComparisonPlots ( {
315+ acc,
316+ comparisonData,
317+ getComparisonPlotImg,
318+ path,
319+ selectedRevisionIds
320+ } )
321+ }
322+
216323 return acc
217324}
218325
0 commit comments