Class: LcpRuby::EmbedProviders::Grafana

Inherits:
Base
  • Object
show all
Defined in:
lib/lcp_ruby/embed_providers/grafana.rb

Instance Attribute Summary

Attributes inherited from Base

#site_url

Instance Method Summary collapse

Constructor Details

#initialize(site_url:, api_token: nil, org_id: nil) ⇒ Grafana

Returns a new instance of Grafana.



4
5
6
7
8
# File 'lib/lcp_ruby/embed_providers/grafana.rb', line 4

def initialize(site_url:, api_token: nil, org_id: nil)
  @site_url = site_url.chomp("/")
  @api_token = api_token
  @org_id = org_id
end

Instance Method Details

#embed_url(resource_type:, resource_id:, params: {}, user: nil) ⇒ Object



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
# File 'lib/lcp_ruby/embed_providers/grafana.rb', line 10

def embed_url(resource_type:, resource_id:, params: {}, user: nil)
  path = case resource_type.to_s
  when "dashboard"
    "/d/#{resource_id}"
  when "panel"
    dashboard_uid = params["dashboard_uid"] || params[:dashboard_uid]
    "/d-solo/#{dashboard_uid}?panelId=#{resource_id}"
  else
    "/d/#{resource_id}"
  end

  url = "#{@site_url}#{path}"

  query_parts = []
  query_parts << "orgId=#{@org_id}" if @org_id
  query_parts << "auth_token=#{@api_token}" if @api_token
  query_parts << "kiosk" # Enable kiosk mode for clean embeds

  params.each do |key, value|
    next if key.to_s == "dashboard_uid"
    query_parts << "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"
  end

  url += "?#{query_parts.join('&')}" if query_parts.any?
  url
end