@@ -40,6 +40,7 @@ import {
4040 MultiSourceEncoding ,
4141 unmergeConcatenatedFields
4242} from '../multiSource/collect'
43+ import { StrokeDashEncoding } from '../multiSource/constants'
4344
4445type CheckpointPlotAccumulator = {
4546 iterations : Record < string , number >
@@ -502,49 +503,95 @@ export const collectTemplates = (data: PlotsOutput): TemplateAccumulator => {
502503}
503504
504505const updateDatapoints = (
505- datapoints : unknown [ ] ,
506+ path : string ,
507+ revisionData : RevisionData ,
508+ selectedRevisions : string [ ] ,
506509 key : string ,
507510 fields : string [ ]
508511) : unknown [ ] =>
509- datapoints . map ( data => {
510- const obj = data as Record < string , unknown >
511- return {
512- ...obj ,
513- [ key ] : mergeFields ( fields . map ( field => obj [ field ] as string ) )
512+ selectedRevisions
513+ . flatMap ( revision =>
514+ revisionData ?. [ revision ] ?. [ path ] . map ( data => {
515+ const obj = data as Record < string , unknown >
516+ return {
517+ ...obj ,
518+ [ key ] : mergeFields ( fields . map ( field => obj [ field ] as string ) )
519+ }
520+ } )
521+ )
522+ . filter ( Boolean )
523+
524+ const updateRevisions = (
525+ selectedRevisions : string [ ] ,
526+ domain : string [ ]
527+ ) : string [ ] => {
528+ const revisions : string [ ] = [ ]
529+ for ( const revision of selectedRevisions ) {
530+ for ( const entry of domain ) {
531+ revisions . push ( mergeFields ( [ revision , entry ] ) )
514532 }
515- } )
533+ }
534+ return revisions
535+ }
516536
517- const stringifyDatapoints = (
518- datapoints : unknown [ ] ,
519- field : string | undefined ,
520- isMultiView : boolean
521- ) : string => {
522- if ( ! field || ( ! isMultiView && ! isConcatenatedField ( field ) ) ) {
523- return JSON . stringify ( datapoints )
537+ const transformRevisionData = (
538+ path : string ,
539+ selectedRevisions : string [ ] ,
540+ revisionData : RevisionData ,
541+ isMultiView : boolean ,
542+ multiSourceEncodingUpdate : { strokeDash : StrokeDashEncoding }
543+ ) : { revisions : string [ ] ; datapoints : unknown [ ] } => {
544+ const field = multiSourceEncodingUpdate . strokeDash ?. field
545+ const isMultiSource = ! ! field
546+
547+ const transformNeeded =
548+ isMultiSource && ( isMultiView || isConcatenatedField ( field ) )
549+
550+ if ( ! transformNeeded ) {
551+ return {
552+ datapoints : selectedRevisions
553+ . flatMap ( revision => revisionData ?. [ revision ] ?. [ path ] )
554+ . filter ( Boolean ) ,
555+ revisions : selectedRevisions
556+ }
524557 }
525558
526559 const fields = unmergeConcatenatedFields ( field )
527-
528560 if ( isMultiView ) {
529561 fields . unshift ( 'rev' )
530- return JSON . stringify ( updateDatapoints ( datapoints , 'rev' , fields ) )
562+ return {
563+ datapoints : updateDatapoints (
564+ path ,
565+ revisionData ,
566+ selectedRevisions ,
567+ 'rev' ,
568+ fields
569+ ) ,
570+ revisions : updateRevisions (
571+ selectedRevisions ,
572+ multiSourceEncodingUpdate . strokeDash . scale . domain
573+ )
574+ }
531575 }
532576
533- return JSON . stringify ( updateDatapoints ( datapoints , field , fields ) )
577+ return {
578+ datapoints : updateDatapoints (
579+ path ,
580+ revisionData ,
581+ selectedRevisions ,
582+ field ,
583+ fields
584+ ) ,
585+ revisions : selectedRevisions
586+ }
534587}
535588
536589const fillTemplate = (
537590 template : string ,
538- datapoints : unknown [ ] ,
539- field ?: string
591+ datapoints : unknown [ ]
540592) : TopLevelSpec => {
541- const isMultiView = isMultiViewPlot ( JSON . parse ( template ) )
542-
543593 return JSON . parse (
544- template . replace (
545- '"<DVC_METRIC_DATA>"' ,
546- stringifyDatapoints ( datapoints , field , isMultiView )
547- )
594+ template . replace ( '"<DVC_METRIC_DATA>"' , JSON . stringify ( datapoints ) )
548595 ) as TopLevelSpec
549596}
550597
@@ -562,30 +609,26 @@ const collectTemplateGroup = (
562609 const template = templates [ path ]
563610
564611 if ( template ) {
565- const datapoints = selectedRevisions
566- . flatMap ( revision => revisionData ?. [ revision ] ?. [ path ] )
567- . filter ( Boolean )
568-
612+ const isMultiView = isMultiViewPlot ( JSON . parse ( template ) )
569613 const multiSourceEncodingUpdate = multiSourceEncoding [ path ] || { }
570-
571- const content = extendVegaSpec (
572- fillTemplate (
573- template ,
574- datapoints ,
575- multiSourceEncodingUpdate . strokeDash ?. field
576- ) ,
577- size ,
578- {
579- ...multiSourceEncodingUpdate ,
580- color : revisionColors
581- }
614+ const { datapoints, revisions } = transformRevisionData (
615+ path ,
616+ selectedRevisions ,
617+ revisionData ,
618+ isMultiView ,
619+ multiSourceEncodingUpdate
582620 )
583621
622+ const content = extendVegaSpec ( fillTemplate ( template , datapoints ) , size , {
623+ ...multiSourceEncodingUpdate ,
624+ color : revisionColors
625+ } )
626+
584627 acc . push ( {
585628 content,
586629 id : path ,
587630 multiView : isMultiViewPlot ( content ) ,
588- revisions : selectedRevisions ,
631+ revisions,
589632 type : PlotsType . VEGA
590633 } )
591634 }
0 commit comments