dashing_api 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +108 -0
- data/lib/dashing_api/api.rb +304 -0
- data/lib/dashing_api/helperFunctions.rb +272 -0
- data/lib/dashing_api/version.rb +3 -0
- data/lib/dashing_api.rb +6 -0
- metadata +99 -0
data/README.md
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
# Dashing API
|
2
|
+
|
3
|
+
## Test it locally
|
4
|
+
|
5
|
+
Build a gem:
|
6
|
+
```sh
|
7
|
+
rake build
|
8
|
+
```
|
9
|
+
|
10
|
+
Install:
|
11
|
+
```sh
|
12
|
+
rake install
|
13
|
+
```
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
```sh
|
20
|
+
gem 'dashing_api'
|
21
|
+
```
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
```sh
|
26
|
+
bundle
|
27
|
+
```
|
28
|
+
|
29
|
+
Or install it yourself as:
|
30
|
+
|
31
|
+
```sh
|
32
|
+
gem install dashing_api
|
33
|
+
```
|
34
|
+
|
35
|
+
## API endpoints
|
36
|
+
|
37
|
+
List the existing widgets:
|
38
|
+
```sh
|
39
|
+
curl http://$DASHING_HOST/widgets/
|
40
|
+
```
|
41
|
+
|
42
|
+
List the existing dashboards:
|
43
|
+
```sh
|
44
|
+
curl http://$DASHING_HOST/dashboards/
|
45
|
+
```
|
46
|
+
|
47
|
+
Check if a dashboard exists:
|
48
|
+
```sh
|
49
|
+
curl http://$DASHING_HOST/dashboards/:id
|
50
|
+
```
|
51
|
+
|
52
|
+
Get the current status of a tile:
|
53
|
+
```sh
|
54
|
+
curl -i -H "Accept: application/json" http://$DASHING_HOST/tiles/:id.json
|
55
|
+
```
|
56
|
+
|
57
|
+
Check if a job script exist for data-id:
|
58
|
+
```sh
|
59
|
+
curl -i http://$DASHING_HOST/jobs/:id
|
60
|
+
```
|
61
|
+
|
62
|
+
Check if a tile exists on a dashboard:
|
63
|
+
```sh
|
64
|
+
curl -i http://$DASHING_HOST/tiles/:dashboard/:hosts
|
65
|
+
```
|
66
|
+
|
67
|
+
Delete a dashboard:
|
68
|
+
```sh
|
69
|
+
curl -X DELETE -H "Content-type: application/json" -d '{"auth_token": "$DASHING_AUTH_TOKEN"}' http://$DASHING_HOST/dashboards/:dashboard
|
70
|
+
```
|
71
|
+
|
72
|
+
Rename a dashboard:
|
73
|
+
```sh
|
74
|
+
curl -i -H 'Accept: application/json' -X PUT -d '{"auth_token": "$DASHING_AUTH_TOKEN", "from": "", "to": ""}' http://$DASHING_HOST/dashboards/
|
75
|
+
```
|
76
|
+
|
77
|
+
Replace a tile on a dashboard:
|
78
|
+
```sh
|
79
|
+
curl -i -H 'Accept: application/json' -X PUT -d '{"auth_token": "$DASHING_AUTH_TOKEN", "dashboard": "", "from": "", "to": ""}' http://$DASHING_HOST/tiles/
|
80
|
+
```
|
81
|
+
|
82
|
+
Create a dashboard
|
83
|
+
```sh
|
84
|
+
curl -i -H 'Accept: application/json' -X POST -d '{"auth_token": "$DASHING_AUTH_TOKEN", "tiles": {"hosts": [" "," "], "titles": [" ", " "], "widgets": [" ", " "], "urls": [" ", " "]}}' http://$DASHING_HOST/dashboards/:dashboard
|
85
|
+
```
|
86
|
+
|
87
|
+
Delete a tile/ tiles from a dashboard:
|
88
|
+
```sh
|
89
|
+
curl -i -H 'Accept: application/json' -X DELETE -d '{"auth_token": "$DASHING_AUTH_TOKEN", "tiles": [" ", " "]}' http://$DASHING_HOST/tiles/:dashboard
|
90
|
+
```
|
91
|
+
|
92
|
+
Add a tile/tiles to a dashboard
|
93
|
+
```sh
|
94
|
+
curl -i -H 'Accept: application/json' -X PUT -d '{"auth_token": "$DASHING_AUTH_TOKEN", "tiles": {"hosts": [" "," "], "titles": [" ", " "], "widgets": [" ", " "], "urls": [" ", " "]}}' http://$DASHING_HOST/tiles/:dashboard
|
95
|
+
```
|
96
|
+
|
97
|
+
Ping hosts and add to/ remove tiles from a dashboard
|
98
|
+
```sh
|
99
|
+
curl -i -H 'Accept: application/json' -X PUT -d '{"auth_token": "$DASHING_AUTH_TOKEN", "tiles": {"hosts": [" "," "], "titles": [" ", " "], "widgets": [" ", " "], "urls": [" ", " "]}}' http://$DASHING_HOST/ping/:dashboard
|
100
|
+
```
|
101
|
+
|
102
|
+
## Contributing
|
103
|
+
|
104
|
+
1. Fork it
|
105
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
106
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
107
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
108
|
+
5. Create new Pull Request
|
@@ -0,0 +1,304 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'sinatra'
|
3
|
+
require_relative 'helperFunctions'
|
4
|
+
|
5
|
+
functions = HelperFunctions.new
|
6
|
+
|
7
|
+
# A noobie way of overriding not_found block for 404
|
8
|
+
error 404 do
|
9
|
+
content_type :json
|
10
|
+
{ :error => 404, :message => @message }.to_json
|
11
|
+
end
|
12
|
+
|
13
|
+
error 403 do
|
14
|
+
content_type :json
|
15
|
+
{ :error => 403, :message => @message }.to_json
|
16
|
+
end
|
17
|
+
|
18
|
+
# Get the current status of the tile
|
19
|
+
get '/tiles/:id.json' do
|
20
|
+
content_type :json
|
21
|
+
if data = settings.history[params[:id]]
|
22
|
+
data.split[1]
|
23
|
+
else
|
24
|
+
@message = "Host " + params[:id] + "does not exist"
|
25
|
+
404
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# List all widgets
|
30
|
+
get '/widgets/' do
|
31
|
+
content_type :json
|
32
|
+
widgets = Array.new()
|
33
|
+
|
34
|
+
Dir.entries(settings.root+'/widgets/').each do |widget|
|
35
|
+
if !File.directory? widget
|
36
|
+
widgets.push widget
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
{ :widgets => widgets }.to_json
|
41
|
+
end
|
42
|
+
|
43
|
+
# List all dashboards
|
44
|
+
get '/dashboards/' do
|
45
|
+
content_type :json
|
46
|
+
dashboards = Array.new()
|
47
|
+
|
48
|
+
# Get the name of the dashboard only. Strip the path
|
49
|
+
Dir.entries(settings.root+'/dashboards/').each do |dashboard|
|
50
|
+
dashArray = dashboard.split("/")
|
51
|
+
dashboard = dashArray[dashArray.length-1]
|
52
|
+
if dashboard.include? ".erb"
|
53
|
+
dashboards.push dashboard
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
{ :dashboards => dashboards }.to_json
|
58
|
+
end
|
59
|
+
|
60
|
+
# Check is a dashboard exists
|
61
|
+
get '/dashboards/:dashboard' do
|
62
|
+
dashboard = params[:dashboard]
|
63
|
+
if functions.dashboardExists(dashboard, settings.root)
|
64
|
+
{ :dashboard => dashboard, :message => 'Dashboard ' +dashboard+ ' exists' }.to_json
|
65
|
+
else
|
66
|
+
@message = "Dashboard " + dashboard + " does not exist"
|
67
|
+
404
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# Rename a dashboard
|
72
|
+
put '/dashboards/' do
|
73
|
+
request.body.rewind
|
74
|
+
body = JSON.parse(request.body.read)
|
75
|
+
from = body["from"]
|
76
|
+
to = body["to"]
|
77
|
+
|
78
|
+
if functions.checkAuthToken(body, settings.auth_token)
|
79
|
+
if functions.dashboardExists(from, settings.root)
|
80
|
+
File.rename(settings.root+'/dashboards/'+from+'.erb', settings.root+'/dashboards/'+to+'.erb')
|
81
|
+
{ :message => 'Dashboard Renamed from ' + from +' to ' + to }.to_json
|
82
|
+
else
|
83
|
+
@message = "Dashboard " + from + " does not exist"
|
84
|
+
404
|
85
|
+
end
|
86
|
+
else
|
87
|
+
@message = "Invalid API Key!"
|
88
|
+
403
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
# Delete a dashboard
|
93
|
+
delete '/dashboards/:dashboard' do
|
94
|
+
request.body.rewind
|
95
|
+
body = JSON.parse(request.body.read)
|
96
|
+
dashboard = params[:dashboard]
|
97
|
+
|
98
|
+
if functions.checkAuthToken(body, settings.auth_token)
|
99
|
+
if dashboard != settings.default_dashboard
|
100
|
+
if functions.dashboardExists(dashboard, settings.root)
|
101
|
+
File.delete(settings.root+'/dashboards/'+dashboard+'.erb')
|
102
|
+
{ :dashboard => dashboard, :message => 'Dashboard ' +dashboard+ ' deleted' }.to_json
|
103
|
+
status 202
|
104
|
+
else
|
105
|
+
@message = "Dashboard " + dashboard + " does not exist"
|
106
|
+
404
|
107
|
+
end
|
108
|
+
else
|
109
|
+
@message = "Cannot delete the default dashboard"
|
110
|
+
403
|
111
|
+
end
|
112
|
+
else
|
113
|
+
@message = "Invalid API Key"
|
114
|
+
403
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
|
119
|
+
# Check if a job script is working
|
120
|
+
get '/jobs/:id' do
|
121
|
+
content_type :json
|
122
|
+
hostName = params[:id]
|
123
|
+
if settings.history[hostName]
|
124
|
+
{ :tile => hostName, :message => 'Host' +hostName+ ' has a job script' }.to_json
|
125
|
+
else
|
126
|
+
@message = "Host " + hostName + " does not have a job script"
|
127
|
+
404
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
# Check if a tile exists on a dashboard
|
132
|
+
get '/tiles/:dashboard/:hosts' do
|
133
|
+
hosts = params[:hosts]
|
134
|
+
dashboard = params[:dashboard]
|
135
|
+
if functions.dashboardExists(dashboard, settings.root)
|
136
|
+
output = functions.tileExists(dashboard, hosts, settings.root)
|
137
|
+
if output.empty?
|
138
|
+
{ :dashboard => dashboard, :tiles => hosts, :message => 'Tiles exists on the dashboard' }.to_json
|
139
|
+
else
|
140
|
+
@message = "Tiles " +output.join(',')+ "does not exist on the dashboard " +dashboard
|
141
|
+
404
|
142
|
+
end
|
143
|
+
else
|
144
|
+
@message = "Dashboard " + dashboard + " does not exist"
|
145
|
+
404
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
|
150
|
+
# Replace a tile id on a dashboard
|
151
|
+
put '/tiles/' do
|
152
|
+
request.body.rewind
|
153
|
+
body = JSON.parse(request.body.read)
|
154
|
+
dashboard = body["dashboard"]
|
155
|
+
from = body["from"]
|
156
|
+
to = body["to"]
|
157
|
+
|
158
|
+
if functions.checkAuthToken(body, settings.auth_token)
|
159
|
+
if dashboard != settings.default_dashboard
|
160
|
+
if functions.dashboardExists(dashboard, settings.root)
|
161
|
+
output = functions.tileExists(dashboard,from, settings.root)
|
162
|
+
if output.empty?
|
163
|
+
File.write(settings.root+"/dashboards/"+dashboard+".erb",File.open(settings.root+"/dashboards/"+dashboard+".erb",&:read).gsub(from,to))
|
164
|
+
{ :dashboard => dashboard, :tile => to, :message => 'Tile Renamed' }.to_json
|
165
|
+
else
|
166
|
+
@message = "Tile " + from + " does not exist on the dashboard " + dashboard + ". Make sure you have given the correct tile name"
|
167
|
+
404
|
168
|
+
end
|
169
|
+
else
|
170
|
+
@message = "Dashboard " + dashboard + " does not exist"
|
171
|
+
404
|
172
|
+
end
|
173
|
+
else
|
174
|
+
@message = "Cannot modify the default dashboard"
|
175
|
+
403
|
176
|
+
end
|
177
|
+
else
|
178
|
+
@message = "Invalid API Key!"
|
179
|
+
403
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
# Create a new dashboard
|
184
|
+
post '/dashboards/:dashboard' do
|
185
|
+
request.body.rewind
|
186
|
+
body = JSON.parse(request.body.read)
|
187
|
+
dashboard = params[:dashboard]
|
188
|
+
|
189
|
+
if functions.checkAuthToken(body, settings.auth_token)
|
190
|
+
if !functions.dashboardExists(dashboard, settings.root)
|
191
|
+
if functions.createDashboard(body, dashboard, settings.root)
|
192
|
+
dashboardLink = "http://"+functions.getHost()+"/"+dashboard
|
193
|
+
{ :message => 'Dashboard Created', :dashboardLink => dashboardLink }.to_json
|
194
|
+
else
|
195
|
+
{ :message => 'Error while creating the dashboard. Try again!' }.to_json
|
196
|
+
end
|
197
|
+
else
|
198
|
+
status 400
|
199
|
+
{ :dashboard => dashboard, :message => 'Dashboard already exists' }.to_json
|
200
|
+
end
|
201
|
+
else
|
202
|
+
@message = "Invalid API Key!"
|
203
|
+
403
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
|
208
|
+
# Delete a tile
|
209
|
+
delete '/tiles/:dashboard' do
|
210
|
+
request.body.rewind
|
211
|
+
body = JSON.parse(request.body.read)
|
212
|
+
dashboard = params[:dashboard]
|
213
|
+
tiles = body["tiles"]
|
214
|
+
|
215
|
+
if functions.checkAuthToken(body, settings.auth_token)
|
216
|
+
if dashboard != settings.default_dashboard
|
217
|
+
if functions.dashboardExists(dashboard, settings.root)
|
218
|
+
output = functions.tileExists(dashboard, tiles, settings.root)
|
219
|
+
if output.empty?
|
220
|
+
functions.deleteTile(dashboard, tiles, settings.root)
|
221
|
+
{ :message => 'Tiles '+tiles.join(',')+ ' removed from the dashboard ' +dashboard }.to_json
|
222
|
+
status 202
|
223
|
+
else
|
224
|
+
@message = "Hosts "+output.join(',')+" are not on the dashboard " + dashboard
|
225
|
+
404
|
226
|
+
end
|
227
|
+
else
|
228
|
+
@message = "Dashboard "+dashboard+" does not exist!"
|
229
|
+
404
|
230
|
+
end
|
231
|
+
else
|
232
|
+
@message = "Cant modify the default dashboard"
|
233
|
+
403
|
234
|
+
end
|
235
|
+
else
|
236
|
+
@message = "Invalid API Key!"
|
237
|
+
403
|
238
|
+
end
|
239
|
+
end
|
240
|
+
|
241
|
+
#Add tile to dashboard
|
242
|
+
put '/tiles/:dashboard' do
|
243
|
+
request.body.rewind
|
244
|
+
body = JSON.parse(request.body.read)
|
245
|
+
dashboard = params[:dashboard]
|
246
|
+
tiles = body["tiles"]["hosts"]
|
247
|
+
|
248
|
+
if functions.checkAuthToken(body, settings.auth_token)
|
249
|
+
if dashboard != settings.default_dashboard
|
250
|
+
if functions.dashboardExists(dashboard, settings.root)
|
251
|
+
output = functions.tileExists(dashboard, tiles, settings.root)
|
252
|
+
if output.empty?
|
253
|
+
{ :message => 'Tiles '+tiles.join(',')+ ' already on the dashboard ' +dashboard }.to_json
|
254
|
+
else
|
255
|
+
if functions.addTile(dashboard, body, settings.root)
|
256
|
+
{ :message => 'Tiles '+tiles.join(',')+ ' added to the dashboard '+ dashboard }.to_json
|
257
|
+
else
|
258
|
+
@message = "Please provide all details for the tiles"
|
259
|
+
404
|
260
|
+
end
|
261
|
+
end
|
262
|
+
else
|
263
|
+
@message = "Dashboard "+dashboard+" does not exist!"
|
264
|
+
404
|
265
|
+
end
|
266
|
+
else
|
267
|
+
@message = "Cant modify the default dashboard"
|
268
|
+
403
|
269
|
+
end
|
270
|
+
else
|
271
|
+
@message = "Invalid API Key!"
|
272
|
+
403
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
276
|
+
|
277
|
+
#Ping and add/ remove tile
|
278
|
+
|
279
|
+
put '/ping/:dashboard' do
|
280
|
+
request.body.rewind
|
281
|
+
body = JSON.parse(request.body.read)
|
282
|
+
dashboard = params[:dashboard]
|
283
|
+
|
284
|
+
if functions.checkAuthToken(body, settings.auth_token)
|
285
|
+
if dashboard != settings.default_dashboard
|
286
|
+
if functions.dashboardExists(dashboard, settings.root)
|
287
|
+
if functions.pingHosts(dashboard, body, settings.root)
|
288
|
+
{ :message => "Tiles added/ removed successfully" }.to_json
|
289
|
+
else
|
290
|
+
{ :message => "Body contains duplicate values" }.to_json
|
291
|
+
end
|
292
|
+
else
|
293
|
+
@message = "Dashboard "+dashboard+" does not exist!"
|
294
|
+
404
|
295
|
+
end
|
296
|
+
else
|
297
|
+
@message = "Cant modify the default dashboard"
|
298
|
+
403
|
299
|
+
end
|
300
|
+
else
|
301
|
+
@message = "Invalid API Key!"
|
302
|
+
403
|
303
|
+
end
|
304
|
+
end
|
@@ -0,0 +1,272 @@
|
|
1
|
+
require 'erb'
|
2
|
+
require 'socket'
|
3
|
+
require 'nokogiri'
|
4
|
+
require 'open-uri'
|
5
|
+
require 'net/ping'
|
6
|
+
|
7
|
+
def newDashboardTemplate()
|
8
|
+
%{
|
9
|
+
<script type='text/javascript'>
|
10
|
+
$(function myFunction() {
|
11
|
+
$('li').live('click', function(e) {
|
12
|
+
var widget = $(this).find('widget');
|
13
|
+
var url = widget.data('url');
|
14
|
+
window.open(url, '_blank', "width=900, height=900, scrollvars=yes, toolbar=yes, resizable=yes");
|
15
|
+
});
|
16
|
+
});
|
17
|
+
</script>
|
18
|
+
|
19
|
+
<div class="gridster">
|
20
|
+
<ul>
|
21
|
+
<% for i in 0..body["tiles"]["hosts"].length-1 %>
|
22
|
+
<li data-row="1" data-col="1" data-sizex="2" data-sizey="2" onClick="myFunction()">
|
23
|
+
<div data-id="<%= body["tiles"]["hosts"][i] %>" data-view="<%= body["tiles"]["widgets"][i] %>" data-unordered="true" data-title="<%= body["tiles"]["titles"][i] %>" data-url="<%= body["tiles"]["urls"][i] %>" data-bind-style="status" style="style"></div>
|
24
|
+
</li>
|
25
|
+
<% end %>
|
26
|
+
</ul>
|
27
|
+
</div>
|
28
|
+
}
|
29
|
+
end
|
30
|
+
|
31
|
+
def addDeleteTileTemplate()
|
32
|
+
%{
|
33
|
+
<script type='text/javascript'>
|
34
|
+
$(function myFunction() {
|
35
|
+
$('li').live('click', function(e) {
|
36
|
+
var widget = $(this).find('widget');
|
37
|
+
var url = widget.data('url');
|
38
|
+
window.open(url, '_blank', "width=900, height=900, scrollvars=yes, toolbar=yes, resizable=yes");
|
39
|
+
});
|
40
|
+
});
|
41
|
+
</script>
|
42
|
+
<div class="gridster">
|
43
|
+
<ul>
|
44
|
+
<% for i in 0..body.length-1%>
|
45
|
+
<%= body[i] %>
|
46
|
+
<% end %>
|
47
|
+
</ul>
|
48
|
+
</div>
|
49
|
+
}
|
50
|
+
end
|
51
|
+
|
52
|
+
class HelperFunctions
|
53
|
+
|
54
|
+
include ERB::Util
|
55
|
+
|
56
|
+
def initialize
|
57
|
+
@newDashboardTemplate = newDashboardTemplate
|
58
|
+
@addDeleteTileTemplate = addDeleteTileTemplate
|
59
|
+
end
|
60
|
+
|
61
|
+
# Check if the auth_token is a valid
|
62
|
+
def checkAuthToken(body, token)
|
63
|
+
auth_token = body["auth_token"]
|
64
|
+
if token == auth_token
|
65
|
+
return true
|
66
|
+
else
|
67
|
+
return false
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# Check if a nagios host/hosts exists on a dashboard
|
72
|
+
def tileExists(dashboard, hosts, directory)
|
73
|
+
if hosts.kind_of?(Array)
|
74
|
+
arrHosts = hosts
|
75
|
+
else
|
76
|
+
arrHosts = hosts.split(",")
|
77
|
+
end
|
78
|
+
|
79
|
+
doNotExist = Array.new
|
80
|
+
for hosts in arrHosts
|
81
|
+
host = "data-id=\""+hosts+"\""
|
82
|
+
if File.foreach(directory+'/dashboards/'+dashboard+'.erb').any?{ |l| l[host] }
|
83
|
+
next
|
84
|
+
else
|
85
|
+
doNotExist.push(hosts)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
return doNotExist
|
89
|
+
end
|
90
|
+
|
91
|
+
# Check if the dashboard exists within the dashboard folder
|
92
|
+
def dashboardExists(dashboardName, directory)
|
93
|
+
if File.exist?(directory+'/dashboards/'+dashboardName+'.erb')
|
94
|
+
return true
|
95
|
+
else
|
96
|
+
return false
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def render(body, template)
|
101
|
+
ERB.new(template).result(binding)
|
102
|
+
end
|
103
|
+
|
104
|
+
def save(file, body, template)
|
105
|
+
File.new(file, "w")
|
106
|
+
File.open(file, "w+") do |f|
|
107
|
+
f.write(render(body, template))
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def checkArray(body)
|
112
|
+
hostLen = body["tiles"]["hosts"].length
|
113
|
+
widgetLen = body["tiles"]["widgets"].length
|
114
|
+
titleLen = body["tiles"]["titles"].length
|
115
|
+
|
116
|
+
if hostLen.equal?(widgetLen) and widgetLen.equal?(titleLen)
|
117
|
+
return true
|
118
|
+
else
|
119
|
+
return false
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def createDashboard(body, dashboard, directory)
|
124
|
+
dashboard = directory+'/dashboards/'+dashboard+'.erb'
|
125
|
+
if checkArray(body)
|
126
|
+
save(dashboard, body, @newDashboardTemplate)
|
127
|
+
return true
|
128
|
+
else
|
129
|
+
return false
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def getHtmlElements(dashboard, hosts)
|
134
|
+
liElements = Array.new
|
135
|
+
finalElement = Array.new
|
136
|
+
doc = Nokogiri::HTML(open(dashboard))
|
137
|
+
liElements = doc.search('div > ul > li')
|
138
|
+
|
139
|
+
liElements.each do |item|
|
140
|
+
element = item.to_s
|
141
|
+
if hosts.any? { |w| element[w] }
|
142
|
+
next
|
143
|
+
else
|
144
|
+
finalElement.push(element)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
return finalElement
|
148
|
+
end
|
149
|
+
|
150
|
+
def deleteTile(dashboard, hosts, directory)
|
151
|
+
liElements = Array.new
|
152
|
+
finalElement = Array.new
|
153
|
+
dashboard = directory+'/dashboards/'+dashboard+'.erb'
|
154
|
+
|
155
|
+
doc = Nokogiri::HTML(open(dashboard))
|
156
|
+
liElements = doc.search('div > ul > li')
|
157
|
+
|
158
|
+
liElements.each do |item|
|
159
|
+
element = item.to_s
|
160
|
+
if hosts.any? { |w| element[w] }
|
161
|
+
next
|
162
|
+
else
|
163
|
+
finalElement.push(element)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
save(dashboard, finalElement, @addDeleteTileTemplate)
|
167
|
+
end
|
168
|
+
|
169
|
+
def addTile(dashboard, body, directory)
|
170
|
+
dashboard = directory+'/dashboards/'+dashboard+'.erb'
|
171
|
+
finalElement = Array.new
|
172
|
+
if checkArray(body)
|
173
|
+
finalElement = getHtmlElements(dashboard, body['tiles']['hosts'])
|
174
|
+
|
175
|
+
for i in 0..body["tiles"]["hosts"].length-1
|
176
|
+
host = body["tiles"]["hosts"][i]
|
177
|
+
widget = body["tiles"]["widgets"][i]
|
178
|
+
title = body["tiles"]["titles"][i]
|
179
|
+
url = body["tiles"]["urls"][i]
|
180
|
+
|
181
|
+
tileElement = ["<li data-row=\"1\" data-col=\"1\" data-sizex=\"2\" data-sizey=\"2\" onClick=\"myFunction()\">
|
182
|
+
<div data-id=\"", host,"\" data-view=\"", widget,"\" data-unordered=\"true\" data-title=\"", title,"\" data-url=\"", url, "\" data-bind-style=\"status\" style=\"style\"></div>
|
183
|
+
</li>" ].join
|
184
|
+
finalElement.push(tileElement)
|
185
|
+
end
|
186
|
+
save(dashboard, finalElement, @addDeleteTileTemplate)
|
187
|
+
return true
|
188
|
+
else
|
189
|
+
return false
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
def up?(host)
|
194
|
+
check = Net::Ping::External.new(host)
|
195
|
+
return check.ping?
|
196
|
+
end
|
197
|
+
|
198
|
+
|
199
|
+
def uniq?(array)
|
200
|
+
if array.length == array.uniq.length
|
201
|
+
return true
|
202
|
+
else
|
203
|
+
return false
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
def pingHosts(dashboard, body, directory)
|
208
|
+
notFound = Array.new
|
209
|
+
|
210
|
+
upArr = Array.new
|
211
|
+
downArr = Array.new
|
212
|
+
|
213
|
+
if uniq?(body["tiles"]["hosts"])
|
214
|
+
body["tiles"]["hosts"].each do |host|
|
215
|
+
if up?(host)
|
216
|
+
upArr.push(host)
|
217
|
+
else
|
218
|
+
downArr.push(host)
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
upOutput = tileExists(dashboard, upArr, directory)
|
223
|
+
|
224
|
+
if !upOutput.empty?
|
225
|
+
hosts = Array.new
|
226
|
+
titles = Array.new
|
227
|
+
widgets = Array.new
|
228
|
+
urls = Array.new
|
229
|
+
|
230
|
+
for i in 0..upOutput.length-1
|
231
|
+
host = upOutput[i]
|
232
|
+
for j in 0..body["tiles"]["hosts"].length-1
|
233
|
+
checkhost = body["tiles"]["hosts"][j]
|
234
|
+
if checkhost == host
|
235
|
+
hosts.push(host)
|
236
|
+
widgets.push(body["tiles"]["widgets"][j])
|
237
|
+
titles.push(body["tiles"]["titles"][j])
|
238
|
+
urls.push(body["tiles"]["urls"][j])
|
239
|
+
else
|
240
|
+
next
|
241
|
+
end
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
jsonArray = { :tiles => { :hosts => hosts, :widgets => widgets, :titles => titles, :urls => urls} }.to_json
|
246
|
+
objArray = JSON.parse(jsonArray)
|
247
|
+
|
248
|
+
addTile(dashboard, objArray, directory)
|
249
|
+
end
|
250
|
+
|
251
|
+
downOutput = tileExists(dashboard, downArr, directory)
|
252
|
+
|
253
|
+
if !downOutput.empty?
|
254
|
+
tileToRemove = downArr - downOutput
|
255
|
+
else
|
256
|
+
tileToRemove = downArr
|
257
|
+
end
|
258
|
+
|
259
|
+
deleteTile(dashboard, tileToRemove, directory)
|
260
|
+
return true
|
261
|
+
else
|
262
|
+
return false
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
|
267
|
+
# Get the hostname
|
268
|
+
def getHost()
|
269
|
+
return Socket.gethostname
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
data/lib/dashing_api.rb
ADDED
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dashing_api
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- FT
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2016-03-11 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: net-ping
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.7'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.7'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.3'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '1.3'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: API for interaction with dashing
|
63
|
+
email:
|
64
|
+
- jermila.dhas@ft.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- README.md
|
70
|
+
- lib/dashing_api.rb
|
71
|
+
- lib/dashing_api/version.rb
|
72
|
+
- lib/dashing_api/api.rb
|
73
|
+
- lib/dashing_api/helperFunctions.rb
|
74
|
+
homepage: https://github.com/Financial-Times/dashing_api.git
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ! '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 1.8.23
|
96
|
+
signing_key:
|
97
|
+
specification_version: 3
|
98
|
+
summary: API for interaction with dashing
|
99
|
+
test_files: []
|