1
+ # Will export the zip file to /tmp/superset_dashboards with zip filename adjusted to include the dashboard_id
2
+ # Example zipfile: dashboard_#{dashboard_id}_export_#{datestamp}.zip
3
+ # Usage
4
+ # Superset::Dashboard::Export.new(dashboard_id: 15).perform
5
+ #
6
+
7
+ require 'superset/file_utilities'
8
+
9
+ module Superset
10
+ module Dashboard
11
+ class Export < Request
12
+ include FileUtilities
13
+
14
+ TMP_SUPERSET_DASHBOARD_PATH = '/tmp/superset_dashboards'
15
+
16
+ attr_reader :dashboard_id , :destination_path
17
+
18
+ def initialize ( dashboard_id : , destination_path : )
19
+ @dashboard_id = dashboard_id
20
+ @destination_path = destination_path
21
+ @download_path = TMP_SUPERSET_DASHBOARD_PATH
22
+ end
23
+
24
+ def perform
25
+ response
26
+ write_file_and_unzip
27
+ copy_export_files_to_destination_path if destination_path
28
+ end
29
+
30
+ # overriding the current happi get request as the returned packet is as string, ie not the usual hash
31
+ def response
32
+ @response ||= client . call (
33
+ :get ,
34
+ client . url ( route ) ,
35
+ client . param_check ( params )
36
+ )
37
+ end
38
+
39
+ def download_folder
40
+ File . dirname ( extracted_files [ 0 ] )
41
+ end
42
+
43
+ private
44
+
45
+ def route
46
+ "dashboard/export/"
47
+ end
48
+
49
+ def params
50
+ { "q" : "!(#{ dashboard_id } )" } # pulled off chrome dev tools doing a GUI export. Swagger interface not helpfull with this endpoint.
51
+ end
52
+
53
+ def write_file_and_unzip
54
+ create_tmp_dir
55
+ File . open ( zip_file_name , 'wb' ) { |fp | fp . write ( @response . body ) }
56
+
57
+ @extracted_files = unzip_file ( zip_file_name , tmp_uniq_dashboard_path )
58
+ end
59
+
60
+ def copy_export_files_to_destination_path
61
+ path_with_dash_id = File . join ( destination_path , dashboard_id . to_s )
62
+ FileUtils . mkdir_p ( path_with_dash_id ) unless File . directory? ( path_with_dash_id )
63
+
64
+ Dir . glob ( "#{ download_folder } /*" ) . each do |item |
65
+ FileUtils . cp_r ( item , path_with_dash_id )
66
+ end
67
+ end
68
+
69
+ def zip_file_name
70
+ @zip_file_name ||= "#{ tmp_uniq_dashboard_path } /dashboard_#{ dashboard_id } _export_#{ datestamp } .zip"
71
+ end
72
+
73
+ def create_tmp_dir
74
+ FileUtils . mkdir_p ( tmp_uniq_dashboard_path ) unless File . directory? ( tmp_uniq_dashboard_path )
75
+ end
76
+
77
+ # uniq random tmp folder name for each export
78
+ # this will allow us to do a wildcard glop on the folder to get the files
79
+ def tmp_uniq_dashboard_path
80
+ @tmp_uniq_dashboard_path ||= File . join ( TMP_SUPERSET_DASHBOARD_PATH , uuid )
81
+ end
82
+
83
+ def uuid
84
+ SecureRandom . uuid
85
+ end
86
+
87
+ def extracted_files
88
+ @extracted_files ||= [ ]
89
+ end
90
+
91
+ def datestamp
92
+ @datestamp ||= Time . now . strftime ( '%Y%m%d' )
93
+ end
94
+ end
95
+ end
96
+ end
0 commit comments