Module: Grafana::Playlist

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

Overview

Instance Method Summary collapse

Instance Method Details

#create_playlist(params) ⇒ Object

‘POST /api/playlists/`

**Example Request**:

“‘bash PUT /api/playlists/1 HTTP/1.1 Accept: application/json Content-Type: application/json Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk

{
  "name": "my playlist",
  "interval": "5m",
  "items": [
    {
      "type": "dashboard_by_id",
      "value": "3",
      "order": 1,
      "title":"my third dasboard"
    },
    {
      "type": "dashboard_by_tag",
      "value": "myTag",
      "order": 2,
      "title":"my other dasboard"
    }
  ]
}

“‘

**Example Response**:

“‘json HTTP/1.1 200 Content-Type: application/json

{
  "id": 1,
  "name": "my playlist",
  "interval": "5m"
}

“‘

Raises:

  • (ArgumentError)


312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/grafana/playlist.rb', line 312

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

#      v, mv = version.values
#      return { 'status' => 404, 'message' => format( 'folder has been supported in Grafana since version 5. you use version %s', v) } if(mv < 5)

  name     = validate( params, required: true , var: 'name'      , type: String )
  interval = validate( params, required: true , var: 'interval'  , type: String )
  items    = validate( params, required: true , var: 'items'     , type: Array )

  return { 'status' => 404, 'message' => 'There are no elements for a playlist' } if(items.count == 0)

  payload_items = create_playlist_items(items)

  payload = {
    name:     name,
    interval: interval,
    items:    payload_items
  }
  payload.reject!{ |_k, v| v.nil? }

  endpoint = '/api/playlists'

  post(endpoint, payload.to_json)
end

#delete_playlist(playlist_id, multi_result = false) ⇒ Object

Delete a playlist

‘DELETE /api/playlists/:id`

**Example Request**:

“‘bash DELETE /api/playlists/1 HTTP/1.1 Accept: application/json Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk “`

**Example Response**:

“‘json HTTP/1.1 200 Content-Type: application/json {} “`

Raises:

  • (ArgumentError)


475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
# File 'lib/grafana/playlist.rb', line 475

def delete_playlist(playlist_id, multi_result = false )

  if( playlist_id.is_a?(String) && playlist_id.is_a?(Integer) )
    raise ArgumentError.new(format('wrong type. \'playlist_id\' must be an String (for an Playlist name) or an Integer (for an Playlist Id), given \'%s\'', playlist_id.class.to_s))
  end
  raise ArgumentError.new('missing \'playlist_id\'') if( playlist_id.size.zero? )

  _playlists = playlists

  data = []

  begin
    status  = _playlists.dig('status')
    message = _playlists.dig('message')

    if( status == 200 )

      data = message.select { |k| k['id'] == playlist_id } if( playlist_id.is_a?(Integer) )
      data = message.select { |k| k['name'] == playlist_id } if( playlist_id.is_a?(String) )

      return { 'status' => 404, 'message' => 'no playlist found' } if( !data.is_a?(Array) || data.count == 0 || status.to_i != 200 )
      return { 'status' => 404, 'message' => format('found %d playlists with name %s', data.count, playlist_id ) } if( data.count > 1 && multi_result == false )
    else
      return _playlists
    end
  rescue
    return { 'status' => 404, 'message' => 'no playlists found' } if( playlists.nil? || playlists == false || playlists.dig('status').to_i != 200 )
  end

  if( multi_result == true )

    result = { 'status' => 0, 'message' => 'under development' }
    data.each do |x|

      endpoint = format( '/api/playlists/%d', x.dig('id') )

      begin
        result = delete( endpoint )
      rescue => error
        logger.error( "error: #{error}" )
      end
    end

    return result
  else

    playlist_id = data.first.dig('id')

    endpoint = format( '/api/playlists/%d', playlist_id )

    result = delete( endpoint )

    if(result.dig('status').to_i == 500)
      # check if the playlist exists
      r = playlist( playlist_id )
      return { 'status' => 200, 'message' => 'playlist deleted' } if(r.dig('status').to_i == 404)
    end

    return result
  end

end

#playlist(playlist_id) ⇒ Object

Get one playlist

‘GET /api/playlists/:id`

**Example Request**:

“‘bash GET /api/playlists/1 HTTP/1.1 Accept: application/json Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk “`

**Example Response**:

“‘json HTTP/1.1 200 Content-Type: application/json {

"id" : 1,
"name": "my playlist",
"interval": "5m",
"orgId": "my org",
"items": [
  {
    "id": 1,
    "playlistId": 1,
    "type": "dashboard_by_id",
    "value": "3",
    "order": 1,
    "title":"my third dasboard"
  },
  {
    "id": 2,
    "playlistId": 1,
    "type": "dashboard_by_tag",
    "value": "myTag",
    "order": 2,
    "title":"my other dasboard"
  }
]

} “‘

Raises:

  • (ArgumentError)


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/grafana/playlist.rb', line 111

def playlist( playlist_id )

  if( playlist_id.is_a?(String) && playlist_id.is_a?(Integer) )
    raise ArgumentError.new(format('wrong type. \'playlist_id\' must be an String (for an Playlist name) or an Integer (for an Playlist Id), given \'%s\'', playlist_id.class.to_s))
  end
  raise ArgumentError.new('missing \'playlist_id\'') if( playlist_id.size.zero? )

  if(playlist_id.is_a?(String))

    data = playlists
    status = data.dig('status')
    d = data.dig('message')
    data = d.select { |k| k['name'] == playlist_id }

    return { 'status' => 404, 'message' => format( 'No Playlist \'%s\' found', playlist_id) } if( data.size == 0 )

    if( data.size != 0 )

      _d = []
      data.each do |k,v|
        _d << playlist( k['id'] )
      end
      return { 'status' => status, 'playlists' => _d }
    end
#        return { 'status' => 200, 'message' => data } if( data.size != 0 )
  end

  raise format('playlist id can not be 0') if( playlist_id.zero? )

  endpoint = format('/api/playlists/%d', playlist_id )

  @logger.debug("Attempting to get existing playlist id #{playlist_id} (GET #{endpoint})") if  @debug

  result = get(endpoint)

  return { 'status' => 404, 'message' => 'playlist is empty', 'items' => [] } if( result.dig('status') == 404 )

  return result
end

#playlist_dashboards(playlist_id) ⇒ Object

Get Playlist dashboards

‘GET /api/playlists/:id/dashboards`

**Example Request**:

“‘bash GET /api/playlists/1/dashboards HTTP/1.1 Accept: application/json Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk “`

**Example Response**:

“‘json HTTP/1.1 200 Content-Type: application/json [

{
  "id": 3,
  "title": "my third dasboard",
  "order": 1,
},
{
  "id": 5,
  "title":"my other dasboard"
  "order": 2,

}

] “‘

Raises:

  • (ArgumentError)


258
259
260
261
262
263
264
265
266
267
# File 'lib/grafana/playlist.rb', line 258

def playlist_dashboards( playlist_id )

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

  endpoint = format('/api/playlists/%s/dashboards', playlist_id)

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

#playlist_items(playlist_id, multi_result = false) ⇒ Object

‘GET /api/playlists/:id/items`

**Example Request**:

“‘bash GET /api/playlists/1/items HTTP/1.1 Accept: application/json Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk “`

**Example Response**:

“‘json HTTP/1.1 200 Content-Type: application/json [

{
  "id": 1,
  "playlistId": 1,
  "type": "dashboard_by_id",
  "value": "3",
  "order": 1,
  "title":"my third dasboard"
},
{
  "id": 2,
  "playlistId": 1,
  "type": "dashboard_by_tag",
  "value": "myTag",
  "order": 2,
  "title":"my other dasboard"
}

] “‘

Raises:

  • (ArgumentError)


188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/grafana/playlist.rb', line 188

def playlist_items( playlist_id, multi_result = false )

  if( playlist_id.is_a?(String) && playlist_id.is_a?(Integer) )
    raise ArgumentError.new(format('wrong type. \'playlist_id\' must be an String (for an playlist name) or an Integer (for an playlist Id), given \'%s\'', playlist_id.class.to_s))
  end
  raise ArgumentError.new('missing \'playlist_id\'') if( playlist_id.size.zero? )

  _playlists = playlists

  begin
    status  = _playlists.dig('status')
    message = _playlists.dig('message')

    if( status == 200 )

      data = message.select { |k| k['id'] == playlist_id } if( playlist_id.is_a?(Integer) )
      data = message.select { |k| k['name'] == playlist_id } if( playlist_id.is_a?(String) )

      return { 'status' => 404, 'message' => 'No Playlist found' } if( !data.is_a?(Array) || data.count == 0 || status.to_i != 200 )
      return { 'status' => 404, 'message' => format('found %d playlists with name %s', data.count, playlist_id ) } if( data.count > 1 && multi_result == false )

      id = data.first.dig('id')
    else
      return _playlists
    end
  rescue
    return { 'status' => 404, 'message' => 'No Playlists found' } if( playlists.nil? || playlists == false || playlists.dig('status').to_i != 200 )
  end

  endpoint = "/api/playlists/#{id}/items"

  result = get( endpoint )

  return { 'status' => 404, 'message' => 'playlist is empty' } if( result.dig('status') == 404 )

  return result
end

#playlistsObject

Playlist API

Search Playlist

‘GET /api/playlists`

Get all existing playlist for the current organization using pagination

**Example Request**:

“‘bash GET /api/playlists HTTP/1.1 Accept: application/json Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk “`

Querystring Parameters:

These parameters are used as querystring parameters.

- **query** - Limit response to playlist having a name like this value.
- **limit** - Limit response to *X* number of playlist.

**Example Response**:

“‘json HTTP/1.1 200 Content-Type: application/json [

{
  "id": 1,
  "name": "my playlist",
  "interval": "5m"
}

] “‘



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/grafana/playlist.rb', line 55

def playlists

  endpoint = '/api/playlists'

  @logger.debug("Attempting to get all existing playlists (GET #{endpoint})") if @debug

  playlists = get( endpoint )

  return { 'status' => 404, 'message' => 'No Playlists found' } if( playlists.nil? || playlists == false || playlists.dig('status').to_i != 200 )

  playlists
end

#update_playlist(params) ⇒ Object

Update a playlist

‘PUT /api/playlists/:id`

**Example Request**:

“‘bash PUT /api/playlists/1 HTTP/1.1 Accept: application/json Content-Type: application/json Authorization: Bearer eyJrIjoiT0tTcG1pUlY2RnVKZTFVaDFsNFZXdE9ZWmNrMkZYbk

{
  "name": "my playlist",
  "interval": "5m",
  "items": [
    {
      "playlistId": 1,
      "type": "dashboard_by_id",
      "value": "3",
      "order": 1,
      "title":"my third dasboard"
    },
    {
      "playlistId": 1,
      "type": "dashboard_by_tag",
      "value": "myTag",
      "order": 2,
      "title":"my other dasboard"
    }
  ]
}

“‘

**Example Response**:

“‘json HTTP/1.1 200 Content-Type: application/json {

"id" : 1,
"name": "my playlist",
"interval": "5m",
"orgId": "my org",
"items": [
  {
    "id": 1,
    "playlistId": 1,
    "type": "dashboard_by_id",
    "value": "3",
    "order": 1,
    "title":"my third dasboard"
  },
  {
    "id": 2,
    "playlistId": 1,
    "type": "dashboard_by_tag",
    "value": "myTag",
    "order": 2,
    "title":"my other dasboard"
  }
]

} “‘

Raises:

  • (ArgumentError)


404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/grafana/playlist.rb', line 404

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

  playlist_id   = validate( params, required: true , var: 'playlist' )
  name          = validate( params, required: false, var: 'name' )
  interval      = validate( params, required: false, var: 'interval', type: String )
  # organisation = validate( params, required: false, var: 'organisation' )
  items         = validate( params, required: false, var: 'items', type: Array )

  _playlists    = playlists

  data = []

  begin
    status  = _playlists.dig('status')
    message = _playlists.dig('message')

    if( status == 200 )
      data = message.select { |k| k['id'] == playlist_id } if( playlist_id.is_a?(Integer) )
      data = message.select { |k| k['name'] == playlist_id } if( playlist_id.is_a?(String) )

      return { 'status' => 404, 'message' => 'no playlist found' } if( !data.is_a?(Array) || data.count == 0 || status.to_i != 200 )
      return { 'status' => 404, 'message' => format('found %d playlists with name %s', data.count, playlist_id ) } if( data.count > 1 && multi_result == false )
    else
      return _playlists
    end
  rescue
    return { 'status' => 404, 'message' => 'no playlists found' } if( playlists.nil? || playlists == false || playlists.dig('status').to_i != 200 )
  end

  playlist_id   = data.first.dig('id')
  playlist_name = data.first.dig('name')
  payload_items = create_playlist_items(items, playlist_id)

  payload = {
    id:       playlist_id,
    name:     name,
    interval: interval,
    items:    payload_items
  }
  payload.reject!{ |_k, v| v.nil? }

  endpoint = format( '/api/playlists/%d', playlist_id )

  put( endpoint, payload.to_json )

end