1
1
/* eslint-disable no-mixed-spaces-and-tabs */
2
2
import { ExperimentData } from './db_types' ;
3
3
import { ResultsCsv , ProjectZip } from '../lib/mongodb_types' ;
4
+ import { getDocumentFromId } from "./mongodb_funcs" ;
4
5
5
6
export const DB_COLLECTION_EXPERIMENTS = 'Experiments' ;
6
7
@@ -31,8 +32,31 @@ const downloadArbitraryFile = (url: string, name: string) => {
31
32
document . body . removeChild ( anchor ) ;
32
33
} ;
33
34
35
+ const formatFilename = ( name : string , timestamp : string , extension : string ) => {
36
+ const formattedName = name . replace ( / [ ^ a - z A - Z 0 - 9 - _ ] / g, '_' ) ;
37
+ const formattedTimestamp = formatTimestamp ( timestamp ) ;
38
+ return `${ formattedName } =>${ formattedTimestamp } .${ extension } ` ;
39
+ } ;
40
+
41
+ const formatTimestamp = ( timestamp : string ) => {
42
+ const partiallyFormattedTimestamp = new Date ( timestamp ) . toISOString ( ) . replace ( / [: .] / g, '-' ) ;
43
+ let formatted = partiallyFormattedTimestamp . replace ( / Z $ / , '' ) ;
44
+ formatted = formatted . replace ( 'T' , '_' ) ;
45
+ const [ date , time ] = formatted . split ( '_' ) ;
46
+ const timeParts = time . split ( '-' ) ;
47
+ let hours = parseInt ( timeParts [ 0 ] , 10 ) - 5 ;
48
+ timeParts [ 0 ] = hours . toString ( ) ;
49
+ const formattedTime = timeParts . join ( '-' ) ;
50
+ return `${ date } _${ formattedTime } ` ;
51
+ } ;
52
+
34
53
export const downloadExperimentResults = async ( expId : string ) => {
35
54
console . log ( `Downloading results for ${ expId } ...` ) ;
55
+
56
+ const expDoc = await getDocumentFromId ( expId ) ;
57
+ const expName = expDoc [ 'name' ] ;
58
+ const expCreated = expDoc [ 'created' ] ;
59
+
36
60
await fetch ( `/api/download/csv/${ expId } ` ) . then ( ( response ) => {
37
61
if ( response ?. ok ) {
38
62
return response . json ( ) ;
@@ -42,7 +66,8 @@ export const downloadExperimentResults = async (expId: string) => {
42
66
console . log ( record ) ;
43
67
const csvContents = record . resultContent ;
44
68
const url = `data:text/plain;charset=utf-8,${ encodeURIComponent ( csvContents ) } ` ;
45
- downloadArbitraryFile ( url , `result${ expId } .csv` ) ;
69
+ const filename = formatFilename ( expName , expCreated , 'csv' ) ;
70
+ downloadArbitraryFile ( url , filename ) ;
46
71
} ) . catch ( ( response : Response ) => {
47
72
console . warn ( 'Error downloading results' , response . status ) ;
48
73
response . json ( ) . then ( ( json : any ) => {
@@ -57,6 +82,11 @@ export const downloadExperimentResults = async (expId: string) => {
57
82
58
83
export const downloadExperimentProjectZip = async ( expId : string ) => {
59
84
console . log ( `Downloading project zip for ${ expId } ...` ) ;
85
+
86
+ const expDoc = await getDocumentFromId ( expId ) ;
87
+ const expName = expDoc [ 'name' ] ;
88
+ const expCreated = expDoc [ 'created' ] ;
89
+
60
90
await fetch ( `/api/download/zip/${ expId } ` ) . then ( ( response ) => {
61
91
if ( response ?. ok ) {
62
92
return response . json ( ) ;
@@ -66,7 +96,8 @@ export const downloadExperimentProjectZip = async (expId: string) => {
66
96
console . log ( record ) ;
67
97
const zipContents = record . fileContent ;
68
98
const url = `data:text/plain;base64,${ encodeURIComponent ( zipContents ) } ` ;
69
- downloadArbitraryFile ( url , `project_${ expId } .zip` ) ;
99
+ const filename = formatFilename ( expName , expCreated , 'zip' ) ;
100
+ downloadArbitraryFile ( url , filename ) ;
70
101
} ) . catch ( ( response : Response ) => {
71
102
console . warn ( 'Error downloading results' , response . status ) ;
72
103
response . json ( ) . then ( ( json : any ) => {
0 commit comments