Module: Grafana::Tools

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

Instance Method Summary collapse

Instance Method Details

#regenerate_template_ids(params) ⇒ Hash

regenerate Row Ids in the Dashboard usefull for an automatic generated Dashboard

Examples:

params = {
  dashboard: {
    rows: [

    ]
  }
}
regenerate_template_ids( params )

Parameters:

  • params (Hash)

    params

Returns:

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/grafana/tools.rb', line 52

def regenerate_template_ids( 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? )

  rows = params.dig('dashboard','rows')
  # name   = validate( params, required: true, var: 'name', type: String )

  unless( rows.nil? )

    # counter = 1
    id_counter = 10
    rows.each_with_index do |r, _counter|
      panel = r.dig('panels')
      next if( panel.nil? )
      panel.each do |p|
        p['id']   = id_counter
        id_counter = id_counter +=1 # id_counter+1 # id_counter +=1 ??
      end
    end
  end

  JSON.generate( params )
end

#slug(text) ⇒ Object

return a slugged string for Grafana Dashboards

Examples:

slug( 'test dashboard' )

Parameters:

  • text (String)

    text

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/grafana/tools.rb', line 15

def slug( text )

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

  begin
  if( text =~ /\s/ && text =~ /-/ )
#        if( text =~ /-/ )
      text = text.gsub( /\s+/, '' )
    else
      text = text.gsub( /\s+/, '-' )
#        end
  end

  rescue => e
    puts e
  end

  text.downcase
end

#valid_json?(json) ⇒ Boolean

check, it an String a valid Json

Examples:

valid_json?( json )

Parameters:

  • json (String)

    json

Returns:



86
87
88
89
90
91
92
93
94
# File 'lib/grafana/tools.rb', line 86

def valid_json?( json )
  begin
    JSON.parse( json )
    return true
  rescue JSON::ParserError => e
    @logger.error("json parse error: #{e}") if @debug
    return false
  end
end