-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathdownload_dashboard.rb
72 lines (56 loc) · 2.25 KB
/
download_dashboard.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
require 'looker-sdk'
require 'xlsxtream' # https://github.com/felixbuenemann/xlsxtream
# get API creds from environment variables
sdk = LookerSDK::Client.new(
:client_id => ENV['LOOKERSDK_CLIENT_ID'],
:client_secret => ENV['LOOKERSDK_CLIENT_SECRET'],
:api_endpoint => ENV['LOOKERSDK_BASE_URL']
)
def one_csv_per_tile(dashboard_to_export, looker)
#function to download the results for each dashboard query as its own csv file
begin
dash_info = looker.dashboard(dashboard_to_export)
rescue StandardError => msg
puts "#{msg}"
puts "⚠️\tDashboard ID Not Found"
end
dash_info[:dashboard_elements].each { | dashboardelem |
unless dashboardelem[:resultmaker].nil? && dashboardelem[:type] != "vis"
query_id = dashboardelem[:result_maker][:query_id]
$cleaned_tile_title = dashboardelem[:title].gsub(/[^0-9a-z ]/i, '').gsub(/[ ]/i, '_')
results = looker.run_query(query_id, "csv")
puts "Processing results for tile: #{$cleaned_tile_title}..."
File.open("#{$cleaned_tile_title}.csv", 'w') { |file| file.write(results) }
end
}
end
def one_excel_for_dash(dashboard_to_export, looker)
#function to download the results for each dashboard query as a tab in one Excel file
begin
dash_info = looker.dashboard(dashboard_to_export)
rescue StandardError => msg
puts "#{msg}"
puts "⚠️\tDashboard ID Not Found"
end
$cleaned_dash_title = dash_info[:title].gsub(/[^0-9a-z ]/i, '').gsub(/[ ]/i, '_')
Xlsxtream::Workbook.open("#{$cleaned_dash_title}.xlsx") do | xlsx |
dash_info[:dashboard_elements].each { | dashboardelem |
unless dashboardelem[:resultmaker].nil? && dashboardelem[:type] != "vis"
query_id = dashboardelem[:result_maker][:query_id]
$cleaned_tile_title = dashboardelem[:title].gsub(/[^0-9a-z ]/i, '').gsub(/[ ]/i, '_')
results = looker.run_query(query_id, "csv")
puts "Processing results for tile: #{$cleaned_tile_title}..."
xlsx.write_worksheet "#{$cleaned_tile_title}" do | sheet |
results.each_line do | line |
sheet << line.split(',')
end
end
end
}
end
end
# change the number below to match the id of the dashboard to download
dashboard_to_export = 23
# uncomment the function you need
# one_csv_per_tile(dashboard_to_export, sdk)
# one_excel_for_dash(dashboard_to_export, sdk)