Module: Grafana::Modules::Dashboard

Included in:
Dashboards
Defined in:
lib/grafana/modules/dashboard.rb

Instance Method Summary collapse

Instance Method Details

#create_dashboard(dashboard:, **options) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/grafana/modules/dashboard.rb', line 10

def create_dashboard(dashboard:, **options)
  # TODO: Error if dashboard is not hash specification
  # TODO: error if dashboard IDs are set (should be an update if so)

  req_body = {
    dashboard: dashboard,
    message: options[:message],
    folderId: options[:folder_id],
    overwrite: false # Creating new dashboard should not overwrite existing dashboards
  }

  post('/api/dashboards/db', req_body)
end

#dashboard(uid:) ⇒ Object



68
69
70
# File 'lib/grafana/modules/dashboard.rb', line 68

def dashboard(uid:)
  get("/api/dashboards/uid/#{uid}")
end

#dashboard_permissions(dashboard_id:) ⇒ Object



80
81
82
# File 'lib/grafana/modules/dashboard.rb', line 80

def dashboard_permissions(dashboard_id:)
  get("/api/dashboards/id/#{dashboard_id}/permissions")
end

#dashboard_search(**options) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/grafana/modules/dashboard.rb', line 51

def dashboard_search(**options)
  options = options.slice(:query, :tag, :dashboard_ids, :folder_ids, :starred, :limit, :page)

  options[:folderIds] = Array.wrap(options.delete(:folder_ids)) if options[:folder_ids].present?
  options[:dashboardIds] = Array.wrap(options.delete(:dashboard_ids)) if options[:dashboard_ids].present?

  options.delete(:starred) unless options[:starred].is_a?(Boolean)
  options.delete(:limit) unless options[:limit].is_a?(Integer) && options[:limit] <= 5000
  options.delete(:page) unless options[:page].is_a?(Integer)

  options.merge({ type: 'dash-db' })

  search_url = '/api/search'
  search_url = "?#{URI.encode_www_form(options)}"
  get(search_url)
end

#dashboard_tagsObject



76
77
78
# File 'lib/grafana/modules/dashboard.rb', line 76

def dashboard_tags
  get('/api/dashboards/tags')
end

#dashboard_version(dashboard_id:, version_id:) ⇒ Object



93
94
95
# File 'lib/grafana/modules/dashboard.rb', line 93

def dashboard_version(dashboard_id:, version_id:)
  get("/api/dashboards/id/#{dashboard_id}/versions/#{version_id}")
end

#dashboard_versions(dashboard_id:) ⇒ Object



89
90
91
# File 'lib/grafana/modules/dashboard.rb', line 89

def dashboard_versions(dashboard_id:)
  get("/api/dashboards/id/#{dashboard_id}/versions")
end

#delete_dashboard(uid:) ⇒ Object



72
73
74
# File 'lib/grafana/modules/dashboard.rb', line 72

def delete_dashboard(uid:)
  delete("/api/dashboards/uid/#{uid}")
end

#diff_dashboards(base_id:, base_version:, new_version:, new_id: nil, diff_type: 'json') ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/grafana/modules/dashboard.rb', line 101

def diff_dashboards(base_id:, base_version:, new_version:, new_id: nil, diff_type: 'json')
  req_body = {
    base: {
      dashboardId: base_id,
      version: base_version
    },
    new: {
      dashboardId: new_id || base_id,
      version: new_version
    },
    diffType: diff_type
  }

  post('/api/dashboards/calculate-diff', req_body)
end

#home_dashboardObject



6
7
8
# File 'lib/grafana/modules/dashboard.rb', line 6

def home_dashboard
  get('/api/dashboards/home')
end

#overwrite_dashboard(dashboard:, **options) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/grafana/modules/dashboard.rb', line 40

def overwrite_dashboard(dashboard:, **options)
  req_body = {
    dashboard: dashboard,
    message: options[:message],
    folderId: options[:folder_id],
    overwrite: true # Overwriting dashboard (if exists)
  }

  post('/api/dashboards/db', req_body)
end

#restore_dashboard_version(dashboard_id:, version_id:) ⇒ Object



97
98
99
# File 'lib/grafana/modules/dashboard.rb', line 97

def restore_dashboard_version(dashboard_id:, version_id:)
  post("/api/dashboards/id/#{dashboard_id}/restore", { version: version_id })
end

#update_dashboard(uid:, **options) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/grafana/modules/dashboard.rb', line 24

def update_dashboard(uid:, **options)
  message = options.delete(:message)
  folder_id = options.delete(:folder_id)

  old_dashboard = dashboard(uid: uid)

  req_body = {
    dashboard: old_dashboard.merge(options),
    message: message,
    folderId: folder_id,
    overwrite: true # Updating dashboard should overwrite existing dashboards
  }

  post('/api/dashboards/db', req_body)
end

#update_dashboard_permissions(dashboard_id:, permissions:) ⇒ Object



84
85
86
87
# File 'lib/grafana/modules/dashboard.rb', line 84

def update_dashboard_permissions(dashboard_id:, permissions:)
  # TODO: error if permissions is not array of hashes
  post("/api/dashboards/id/#{dashboard_id}/permissions", { items: permissions })
end