Class: Nrde::DashboardExporter

Inherits:
Object
  • Object
show all
Defined in:
lib/nrde/dashboard_exporter.rb

Class Method Summary collapse

Class Method Details

.exporter(guid, personal_api_key = nil, width = '2000', height = '2000', new_relic_region = 'US') ⇒ Object



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
# File 'lib/nrde/dashboard_exporter.rb', line 3

def self.exporter(guid, personal_api_key = nil, width = '2000', height = '2000', new_relic_region = 'US')
  begin
    raise "A New Relic API key is required." if personal_api_key.nil?
    headers = {}
    headers["Content-Type"] = "application/json"
    headers["API-Key"] = personal_api_key
    endpointUrl = new_relic_region == 'EU' ? 'https://api.eu.newrelic.com/graphql' : 'https://api.newrelic.com/graphql'
    raise "Dashboard GUID required." if guid.nil?
    nr_query_json = {"query" => "mutation { dashboardCreateSnapshotUrl(guid: \"#{guid}\")}"}
    
    #Get Dashborad Snapshot URL
    uri = URI.parse(endpointUrl)
    https = Net::HTTP.new(uri.host,uri.port)
    https.use_ssl = true
    req = Net::HTTP::Post.new(uri.path, headers)
    req.body = nr_query_json.to_json
    res = https.request(req)
    nr_snapshopt_url = JSON.parse(res.body)['data']['dashboardCreateSnapshotUrl']
    #Download File as PDF
    nr_download_uri = URI("#{nr_snapshopt_url}&width=#{width}&height=#{height}")
    response = Net::HTTP.get_response(nr_download_uri)
    return response
  rescue => e
    p "PDF Download failed #{nr_download_uri} #{response.code}"
  end
end