Skip to content

Commit 0caf778

Browse files
committed
[NEP-17473] Add Export endpoint
1 parent b204995 commit 0caf778

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

lib/superset/dashboard/export.rb

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

lib/superset/file_utilities.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'zip'
2+
3+
module Superset
4+
module FileUtilities
5+
def unzip_file(zip_file, destination)
6+
entries = []
7+
Zip::File.open(zip_file) do |zip|
8+
zip.each do |entry|
9+
entry_path = File.join(destination, entry.name)
10+
entries << entry_path
11+
FileUtils.mkdir_p(File.dirname(entry_path))
12+
zip.extract(entry, entry_path)
13+
end
14+
end
15+
puts entries
16+
entries # return array of extracted files
17+
end
18+
end
19+
end

0 commit comments

Comments
 (0)