@@ -57,7 +57,7 @@ import {
5757} from '../multiSource/collect'
5858import { isDvcError } from '../../cli/dvc/reader'
5959import { ErrorsModel } from '../errors/model'
60- import { openFileInEditor , writeJson } from '../../fileSystem'
60+ import { openFileInEditor , writeCsv , writeJson } from '../../fileSystem'
6161import { Toast } from '../../vscode/toast'
6262
6363export class PlotsModel extends ModelWithPersistence {
@@ -227,21 +227,15 @@ export class PlotsModel extends ModelWithPersistence {
227227 return selectedRevisions
228228 }
229229
230- public savePlotData ( plotId : string , filePath : string ) {
231- const foundCustomPlot = this . customPlotsOrder . find (
232- ( { metric, param } ) => getCustomPlotId ( metric , param ) === plotId
233- )
234-
235- const rawData = foundCustomPlot
236- ? this . getCustomPlotData ( foundCustomPlot )
237- : this . getSelectedTemplatePlotData ( plotId )
230+ public savePlotDataAsJson ( filePath : string , plotId : string ) {
231+ void this . savePlotData ( filePath , plotId , data => {
232+ writeJson ( filePath , data , true )
233+ return Promise . resolve ( )
234+ } )
235+ }
238236
239- try {
240- writeJson ( filePath , rawData as unknown as Record < string , unknown > , true )
241- void openFileInEditor ( filePath )
242- } catch {
243- void Toast . showError ( 'Cannot write to file' )
244- }
237+ public savePlotDataAsCsv ( filePath : string , plotId : string ) {
238+ void this . savePlotData ( filePath , plotId , data => writeCsv ( filePath , data ) )
245239 }
246240
247241 public getTemplatePlots (
@@ -479,4 +473,25 @@ export class PlotsModel extends ModelWithPersistence {
479473
480474 return collectCustomPlotRawData ( orderValue , experiments )
481475 }
476+
477+ private async savePlotData (
478+ filePath : string ,
479+ plotId : string ,
480+ writeToFile : ( rawData : Array < Record < string , unknown > > ) => Promise < void >
481+ ) {
482+ const foundCustomPlot = this . customPlotsOrder . find (
483+ ( { metric, param } ) => getCustomPlotId ( metric , param ) === plotId
484+ )
485+
486+ const rawData = foundCustomPlot
487+ ? this . getCustomPlotData ( foundCustomPlot )
488+ : this . getSelectedTemplatePlotData ( plotId )
489+
490+ try {
491+ await writeToFile ( rawData )
492+ void openFileInEditor ( filePath )
493+ } catch {
494+ void Toast . showError ( 'Cannot write to file' )
495+ }
496+ }
482497}
0 commit comments