Class: Grafana::Panel

Inherits:
Object
  • Object
show all
Defined in:
lib/grafana/panel.rb

Overview

Representation of one specific panel in a Dashboard instance.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, dashboard) ⇒ Panel

Returns a new instance of Panel.

Parameters:

  • model (Hash)

    converted JSON Hash of the panel

  • dashboard (Dashboard)

    parent Dashboard object



12
13
14
15
16
17
18
19
20
# File 'lib/grafana/panel.rb', line 12

def initialize(model, dashboard)
  @model = model
  @dashboard = dashboard

  @datasource_uid_or_name = @model['datasource']
  if @model['datasource'].is_a?(Hash)
    @datasource_uid_or_name = @model['datasource']['uid']
  end
end

Instance Attribute Details

#dashboardDashboard (readonly)

Returns parent Dashboard object.

Returns:



7
8
9
# File 'lib/grafana/panel.rb', line 7

def dashboard
  @dashboard
end

#modelObject (readonly)

Returns the value of attribute model.



8
9
10
# File 'lib/grafana/panel.rb', line 8

def model
  @model
end

Instance Method Details

#datasourceDatasource

Returns datasource object specified for the current panel.

Returns:

  • (Datasource)

    datasource object specified for the current panel



44
45
46
47
48
49
50
# File 'lib/grafana/panel.rb', line 44

def datasource
  if datasource_kind_is_uid?
    dashboard.grafana.datasource_by_uid(@datasource_uid_or_name)
  else
    dashboard.grafana.datasource_by_name(@datasource_uid_or_name)
  end
end

#field(field) ⇒ String

Returns content of the requested field or if not found.

Returns:

  • (String)

    content of the requested field or if not found



23
24
25
26
27
# File 'lib/grafana/panel.rb', line 23

def field(field)
  return @model[field] if @model.key?(field)

  nil
end

#idString

Returns panel ID.

Returns:

  • (String)

    panel ID



30
31
32
# File 'lib/grafana/panel.rb', line 30

def id
  @model['id']
end

#query(query_letter) ⇒ String

Returns query string for the requested query letter.

Returns:

  • (String)

    query string for the requested query letter

Raises:



53
54
55
56
57
58
# File 'lib/grafana/panel.rb', line 53

def query(query_letter)
  query_item = @model['targets'].select { |item| item['refId'].to_s == query_letter.to_s }.first
  raise QueryLetterDoesNotExistError.new(query_letter, self) unless query_item

  datasource.raw_query_from_panel_model(query_item)
end

#render_urlString

Returns relative rendering URL for the panel, to create an image out of it.

Returns:

  • (String)

    relative rendering URL for the panel, to create an image out of it



61
62
63
# File 'lib/grafana/panel.rb', line 61

def render_url
  "/render/d-solo/#{@dashboard.id}?panelId=#{@model['id']}"
end

#resolve_variable_datasource(variables) ⇒ Object

This method should always be called before the datasource method of a panel is invoked, to ensure that the variable names in the datasource field are resolved.

Parameters:

  • variables (Hash)

    variables hash, which should be use to resolve variable datasource



39
40
41
# File 'lib/grafana/panel.rb', line 39

def resolve_variable_datasource(variables)
  @datasource_uid_or_name = AbstractDatasource.new(nil).replace_variables(@datasource_uid_or_name, variables, 'raw') if @datasource_uid_or_name.is_a?(String)
end