Module: Grafana::Dashboard

Included in:
Client
Defined in:
lib/grafana/dashboard.rb

Overview

Instance Method Summary collapse

Instance Method Details

#create_dashboard(params) ⇒ Hash

Create / Update dashboard

POST /api/dashboards/db

Examples:

params = {
  dashboard: {
    id: null,
    title: 'Production Overview',
    tags: [ 'templated' ],
    timezone": 'browser',
    rows: [
      {
      }
    ],
    'schemaVersion': 6,
    'version': 0
  },
  overwrite: false
}
create_dashboard( params )

Parameters:

Options Hash (params):

  • dashboard (Hash)
  • overwrite (Boolean) — default: true

Returns:

Raises:

  • (ArgumentError)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/grafana/dashboard.rb', line 55

def create_dashboard( params )

  raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )
  raise ArgumentError.new('missing \'params\'') if( params.size.zero? )

  dashboard = validate( params, required: true, var: 'dashboard', type: Hash )
  overwrite = validate( params, required: false, var: 'overwrite', type: Boolean ) || true

  dashboard = regenerate_template_ids( dashboard )

  db = JSON.parse( dashboard ) if( dashboard.is_a?(String) )
  title = db.dig('dashboard','title')

  endpoint = '/api/dashboards/db'

  payload = {
    dashboard: db.dig('dashboard'),
    overwrite: overwrite
  }
  payload.reject!{ |_k, v| v.nil? }

  @logger.debug("Creating dashboard: #{title} (POST /api/dashboards/db)") if @debug

  post( endpoint, payload.to_json )
end

#dashboard(name) ⇒ String

Get dashboard

Examples:

dashboard('dashboard for many foo')

Returns:

  • (String)

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
# File 'lib/grafana/dashboard.rb', line 16

def dashboard( name )

  raise ArgumentError.new(format('wrong type. \'name\' must be an String, given \'%s\'', name.class.to_s)) unless( name.is_a?(String) )
  raise ArgumentError.new('missing name') if( name.size.zero? )

  endpoint = format( '/api/dashboards/db/%s', slug(name) )

  @logger.debug( "Attempting to get dashboard (GET /api/dashboards/db/#{name})" ) if @debug

  get( endpoint )
end

#dashboard_tagsHash

Tags for Dashboard

Examples:

dashboard_tags

Returns:



123
124
125
126
127
128
129
130
# File 'lib/grafana/dashboard.rb', line 123

def dashboard_tags

  endpoint = '/api/dashboards/tags'

  @logger.debug("Attempting to get dashboard tags(GET #{endpoint})") if @debug

  get(endpoint)
end

#delete_dashboard(name) ⇒ Hash

Delete dashboard

Examples:

delete_dashboard('dashboard for many foo')

Returns:

Raises:

  • (ArgumentError)


88
89
90
91
92
93
94
95
96
97
98
# File 'lib/grafana/dashboard.rb', line 88

def delete_dashboard( name )

  raise ArgumentError.new(format('wrong type. \'name\' must be an String, given \'%s\'', name.class.to_s)) unless( name.is_a?(String) )
  raise ArgumentError.new('missing name') if( name.size.zero? )

  endpoint = format( '/api/dashboards/db/%s', slug(name) )

  @logger.debug("Deleting dashboard #{slug(name)} (DELETE #{endpoint})") if @debug

  delete(endpoint)
end

#home_dashboardHash

Gets the home dashboard

Examples:

home_dashboard

Returns:



107
108
109
110
111
112
113
114
# File 'lib/grafana/dashboard.rb', line 107

def home_dashboard

  endpoint = '/api/dashboards/home'

  @logger.debug("Attempting to get home dashboard (GET #{endpoint})") if @debug

  get(endpoint)
end

#import_dashboards_from_directory(directory) ⇒ Hash

import Dashboards from directory

Examples:

import_dashboards_from_directory( '/tmp/dashboards' )

Returns:

Raises:

  • (ArgumentError)


176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/grafana/dashboard.rb', line 176

def import_dashboards_from_directory( directory )

  raise ArgumentError.new('directory must be an String') unless( directory.is_a?(String) )

  result = {}

  dirs = Dir.glob( format( '%s/**.json', directory ) ).sort

  dirs.each do |f|

    @logger.debug( format( 'import \'%s\'', f ) ) if @debug

    dashboard = File.read( f )
    dashboard = JSON.parse( dashboard )

    result[f.to_s] ||= {}
    result[f.to_s] = create_dashboard( dashboard: dashboard )
  end

  result
end

#search_dashboards(params) ⇒ Hash

Search Dashboards

Examples:

searchDashboards( tags: host )
searchDashboards( tags: [ host, 'tag1' ] )
searchDashboards( tags: [ 'tag2' ] )
searchDashboards( query: title )
searchDashboards( starred: true )

Returns:

Raises:

  • (ArgumentError)


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/grafana/dashboard.rb', line 143

def search_dashboards( params )

  raise ArgumentError.new(format('wrong type. \'params\' must be an Hash, given \'%s\'', params.class.to_s)) unless( params.is_a?(Hash) )

  query   = validate( params, required: false, var: 'query', type: String )
  starred = validate( params, required: false, var: 'starred', type: Boolean )
  tags    = validate( params, required: false, var: 'tags' )

  api     = []
  api << format( 'query=%s', CGI.escape( query ) ) unless( query.nil? )
  api << format( 'starred=%s', starred ? 'true' : 'false' ) unless( starred.nil? )

  unless( tags.nil? )
    tags = tags.join( '&tag=' ) if( tags.is_a?( Array ) )
    api << format( 'tag=%s', tags )
  end

  api = api.join( '&' )

  endpoint = format( '/api/search/?%s' , api )

  @logger.debug("Attempting to search for dashboards (GET #{endpoint})") if @debug

  get( endpoint )
end