riemann-dash 0.2.1 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/.gitignore +9 -0
  2. data/Gemfile +7 -0
  3. data/Gemfile.lock +52 -0
  4. data/README.markdown +29 -5
  5. data/Rakefile.rb +11 -0
  6. data/bin/riemann-dash +2 -2
  7. data/example/config.rb +17 -0
  8. data/lib/riemann/dash/app.rb +32 -0
  9. data/lib/riemann/dash/config.rb +154 -0
  10. data/lib/riemann/dash/controller/css.rb +1 -1
  11. data/lib/riemann/dash/controller/index.rb +6 -36
  12. data/lib/riemann/dash/public/dash.js +44 -18
  13. data/lib/riemann/dash/public/format.js +3 -3
  14. data/lib/riemann/dash/public/persistence.js +2 -2
  15. data/lib/riemann/dash/public/subs.js +63 -48
  16. data/lib/riemann/dash/public/util.js +37 -44
  17. data/lib/riemann/dash/public/vendor/backbone.js +1571 -0
  18. data/lib/riemann/dash/public/vendor/jquery/jquery-1.9.1.min.js +5 -0
  19. data/lib/riemann/dash/public/{jquery-ui-1.9.0.custom.min.js → vendor/jquery/jquery-ui-1.9.0.custom.min.js} +0 -0
  20. data/lib/riemann/dash/public/{jquery.quickfit.js → vendor/jquery/jquery.quickfit.js} +0 -0
  21. data/lib/riemann/dash/public/vendor/jquery/jquery.simplemodal.1.4.4.min.js +26 -0
  22. data/lib/riemann/dash/public/vendor/lodash.min.js +40 -0
  23. data/lib/riemann/dash/public/vendor/smoothie.js +376 -0
  24. data/lib/riemann/dash/public/{toastr.css → vendor/toastr/toastr.css} +1 -1
  25. data/lib/riemann/dash/public/{toastr.js → vendor/toastr/toastr.js} +0 -0
  26. data/lib/riemann/dash/public/views/gauge.js +8 -5
  27. data/lib/riemann/dash/public/views/grid.js +138 -67
  28. data/lib/riemann/dash/public/views/timeseries.js +230 -0
  29. data/lib/riemann/dash/public/views/title.js +6 -3
  30. data/lib/riemann/dash/version.rb +2 -2
  31. data/lib/riemann/dash/views/css.scss +52 -2
  32. data/lib/riemann/dash/views/index.erubis +38 -192
  33. data/lib/riemann/dash.rb +3 -97
  34. data/riemann-dash.gemspec +28 -0
  35. data/sh/c +1 -0
  36. data/sh/env.rb +2 -0
  37. data/sh/test +1 -0
  38. data/test/config_test.rb +114 -0
  39. data/test/fixtures/config/basic_config.rb +2 -0
  40. data/test/fixtures/config/ws_config.rb +1 -0
  41. data/test/fixtures/ws_config/dummy_config.json +1 -0
  42. data/test/fixtures/ws_config/pretty_printed_config.json +6 -0
  43. data/test/test_helper.rb +10 -0
  44. metadata +43 -18
  45. data/lib/riemann/dash/public/jquery-1.7.2.min.js +0 -4
  46. data/lib/riemann/dash/public/jquery.json-2.2.min.js +0 -31
  47. data/lib/riemann/dash/public/jquery.simplemodal.1.4.3.min.js +0 -26
  48. data/lib/riemann/dash/public/mustache.js +0 -597
  49. data/lib/riemann/dash/public/underscore-min.js +0 -5
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ pkg/
2
+ ._*
3
+ .sass-cache/
4
+ *~
5
+ .DS_Store
6
+ .*.swp
7
+ *.log
8
+ /test/tmp/
9
+ /config
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem 'pry'
7
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,52 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ reslike-dash (0.2.2)
5
+ erubis (>= 2.7.0)
6
+ multi_json (= 1.3.6)
7
+ riemann-client (>= 0.0.7)
8
+ sass (>= 3.1.14)
9
+ sinatra (>= 1.3.2)
10
+ thin (>= 1.3.1)
11
+
12
+ GEM
13
+ remote: https://rubygems.org/
14
+ specs:
15
+ beefcake (0.3.7)
16
+ coderay (1.0.9)
17
+ daemons (1.1.9)
18
+ erubis (2.7.0)
19
+ eventmachine (1.0.3)
20
+ method_source (0.8.1)
21
+ mtrc (0.0.4)
22
+ multi_json (1.3.6)
23
+ pry (0.9.12)
24
+ coderay (~> 1.0.5)
25
+ method_source (~> 0.8)
26
+ slop (~> 3.4)
27
+ rack (1.5.2)
28
+ rack-protection (1.4.0)
29
+ rack
30
+ riemann-client (0.0.8)
31
+ beefcake (>= 0.3.5)
32
+ mtrc (>= 0.0.4)
33
+ trollop (>= 1.16.2)
34
+ sass (3.2.7)
35
+ sinatra (1.3.5)
36
+ rack (~> 1.4)
37
+ rack-protection (~> 1.3)
38
+ tilt (~> 1.3, >= 1.3.3)
39
+ slop (3.4.3)
40
+ thin (1.5.0)
41
+ daemons (>= 1.0.9)
42
+ eventmachine (>= 0.12.6)
43
+ rack (>= 1.0.0)
44
+ tilt (1.3.5)
45
+ trollop (2.0)
46
+
47
+ PLATFORMS
48
+ ruby
49
+
50
+ DEPENDENCIES
51
+ pry
52
+ reslike-dash!
data/README.markdown CHANGED
@@ -7,8 +7,8 @@ Get started
7
7
  ==========
8
8
 
9
9
  ``` bash
10
- gem install riemann-dash
11
- riemann-dash
10
+ $ gem install riemann-dash
11
+ $ riemann-dash
12
12
  ```
13
13
 
14
14
  Then open http://localhost:4567 in a browser. Riemann-dash will connect to the local host (relative to your browser) by default, and show you a small manual.
@@ -19,10 +19,34 @@ Configuring
19
19
  Riemann-dash takes an optional config file, which you can specify as the first
20
20
  command-line argument. If none is given, it looks for a file in the local
21
21
  directory: config.rb. That file can override any configuration options on the
22
- Dash class (hence all Sinatra configuration) as well as the Riemann client
23
- options, etc.
22
+ Dash class, and hence, all Sinatra configuration.
24
23
 
25
24
  ``` ruby
26
25
  set :port, 6000 # HTTP server on port 6000
27
- config[:client][:host] = 'my.ustate.server'
26
+ config[:ws_config] = 'custom/config.json' # Specify custom workspace config
28
27
  ```
28
+
29
+
30
+
31
+ Development
32
+ ===========
33
+
34
+ $ git clone git://github.com/aphyr/riemann-dash.git
35
+ $ cd riemann-dash
36
+ $ bundle
37
+
38
+ Testing
39
+ =======
40
+ # run tests
41
+ $ sh/test
42
+
43
+ Releasing
44
+ ==========
45
+ $ rake build
46
+ $ rake release
47
+
48
+ REPL
49
+ ====
50
+ $ sh/c
51
+ > irb :001 > Riemann::Dash::VERSION
52
+ > => "0.2.2"
data/Rakefile.rb ADDED
@@ -0,0 +1,11 @@
1
+ #$:.unshift(File.join(File.dirname(__FILE__), 'lib'))
2
+ #require 'rubygems'
3
+ require "bundler/gem_tasks"
4
+
5
+ task :default => :test
6
+ require 'rake/testtask'
7
+ Rake::TestTask.new do |t|
8
+ t.libs.push "lib"
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ t.verbose = false
11
+ end
data/bin/riemann-dash CHANGED
@@ -3,5 +3,5 @@
3
3
  $LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib')))
4
4
  require 'riemann/dash'
5
5
 
6
- Riemann::Dash.load ARGV.first
7
- Riemann::Dash.run!
6
+ Riemann::Dash::App.load ARGV.first
7
+ Riemann::Dash::App.run!
data/example/config.rb ADDED
@@ -0,0 +1,17 @@
1
+ # Serve HTTP traffic on this port
2
+ set :port, 4567
3
+
4
+ riemann_base = '.'
5
+ riemann_src = "#{riemann_base}/lib/riemann/dash"
6
+
7
+ # Add custom controllers in controller/
8
+ config.store[:controllers] = ["#{riemann_src}/controller"]
9
+
10
+ # Use the local view directory instead of the default
11
+ config.store[:views] = "#{riemann_src}/views"
12
+
13
+ # Specify a custom path to your workspace config.json
14
+ config.store[:ws_config] = "#{riemann_base}/config/config.json"
15
+
16
+ # Serve static files from this directory
17
+ config.store[:public] = "#{riemann_src}/public"
@@ -0,0 +1,32 @@
1
+ module Riemann
2
+ module Dash
3
+ class App < Sinatra::Base
4
+ # A little dashboard sinatra application.
5
+
6
+ require 'yaml'
7
+ require 'find'
8
+ require 'erubis'
9
+ require 'sass'
10
+
11
+ def self.config
12
+ Riemann::Dash::Config.instance
13
+ end
14
+
15
+ def config
16
+ self.class.config
17
+ end
18
+
19
+ def self.load(filename)
20
+ filename ||= 'config.rb'
21
+ unless config.load_config(filename)
22
+ # Configuration failed; load a default view.
23
+ puts "No configuration loaded; using defaults."
24
+ end
25
+
26
+ config.load_controllers
27
+ config.setup_views
28
+ config.setup_public_dir
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,154 @@
1
+ class Riemann::Dash::Config
2
+ attr_accessor :config_path
3
+ attr_accessor :store
4
+
5
+ def initialize
6
+ self.store = {}
7
+ setup_default_values
8
+ end
9
+
10
+ def self.instance
11
+ @instance ||= Riemann::Dash::Config.new
12
+ end
13
+
14
+ def self.reset!
15
+ @instance = nil
16
+ end
17
+
18
+ def setup_default_values
19
+ store.merge!({
20
+ :controllers => [File.join(File.dirname(__FILE__), 'controller')],
21
+ :views => File.join(File.dirname(__FILE__), 'views'),
22
+ :ws_config => File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '..', 'config', 'config.json')),
23
+ :public => File.join(File.dirname(__FILE__), 'public')
24
+ })
25
+ end
26
+
27
+ def ws_config_file
28
+ store[:ws_config]
29
+ end
30
+
31
+ # backwards compatible forwarder to store-ivar
32
+ def [](k)
33
+ store[k]
34
+ end
35
+
36
+ def []=(k,v)
37
+ store[k] = v
38
+ end
39
+
40
+
41
+ # Executes the configuration file.
42
+ def load_config(path)
43
+ self.config_path = path
44
+ begin
45
+ Riemann::Dash::App.instance_eval File.read(config_path)
46
+ true
47
+ rescue Errno::ENOENT
48
+ false
49
+ end
50
+ end
51
+
52
+ def load_controllers
53
+ store[:controllers].each { |d| load_controllers_from(d) }
54
+ end
55
+
56
+ def setup_views
57
+ Riemann::Dash::App.set :views, File.expand_path(store[:views])
58
+ end
59
+
60
+ def setup_public_dir
61
+ require 'riemann/dash/rack/static'
62
+ Riemann::Dash::App.use Riemann::Dash::Static, :root => store[:public]
63
+ end
64
+
65
+ # Load controllers.
66
+ def load_controllers_from(dir)
67
+ sorted_controller_list(dir).each do |r|
68
+ require r
69
+ end
70
+ end
71
+
72
+ # Controllers can be regular old one-file-per-class, but
73
+ # if you prefer a little more modularity, this method will allow you to
74
+ # define all controller methods in their own files. For example, get
75
+ # "/posts/*/edit" can live in controller/posts/_/edit.rb. The sorting
76
+ # system provided here requires files in the correct order to handle
77
+ # wildcards appropriately.
78
+ def sorted_controller_list(dir)
79
+ rbs = []
80
+ Find.find(
81
+ File.expand_path(dir)
82
+ ) do |path|
83
+ rbs << path if path =~ /\.rb$/
84
+ end
85
+
86
+ # Sort paths with _ last, becase those are wildcards.
87
+ rbs.sort! do |a, b|
88
+ as = a.split File::SEPARATOR
89
+ bs = b.split File::SEPARATOR
90
+
91
+ # Compare common subpaths
92
+ l = [as.size, bs.size].min
93
+ catch :x do
94
+ (0...l).each do |i|
95
+ a, b = as[i], bs[i]
96
+ if a[/^_/] and not b[/^_/]
97
+ throw :x, 1
98
+ elsif b[/^_/] and not a[/^_/]
99
+ throw :x, -1
100
+ elsif ord = (a <=> b) and ord != 0
101
+ throw :x, ord
102
+ end
103
+ end
104
+
105
+ # All subpaths are identical; sort longest first
106
+ if as.size > bs.size
107
+ throw :x, -1
108
+ elsif as.size < bs.size
109
+ throw :x, -1
110
+ else
111
+ throw :x, 0
112
+ end
113
+ end
114
+ end
115
+ end
116
+
117
+
118
+ require 'multi_json'
119
+ require 'fileutils'
120
+
121
+
122
+ def read_ws_config
123
+ if File.exists? ws_config_file
124
+ File.read(ws_config_file)
125
+ else
126
+ MultiJson.encode({})
127
+ end
128
+ end
129
+
130
+ def update_ws_config(update)
131
+ update = MultiJson.decode(update)
132
+
133
+ # Read old config
134
+ if File.exists? ws_config_file
135
+ old = MultiJson.decode File.read(ws_config_file)
136
+ else
137
+ old = {}
138
+ end
139
+
140
+ new_config = {}
141
+
142
+ # Server
143
+ new_config['server'] = update['server'] or old['server']
144
+
145
+ #p update['workspaces']
146
+ new_config['workspaces'] = update['workspaces'] or old['workspaces']
147
+
148
+ # Save new config
149
+ FileUtils.mkdir_p File.dirname(ws_config_file)
150
+ File.open(ws_config_file, 'w') do |f|
151
+ f.write(MultiJson.encode(new_config, :pretty => true))
152
+ end
153
+ end
154
+ end
@@ -1,4 +1,4 @@
1
- class Riemann::Dash
1
+ class Riemann::Dash::App
2
2
  get '/css' do
3
3
  scss :css, :layout => false
4
4
  end
@@ -1,50 +1,20 @@
1
- class Riemann::Dash
2
- require 'multi_json'
3
- require 'fileutils'
4
- require 'set'
5
-
6
- WS_CONFIG_FILE = "config/config.json"
7
-
1
+ class Riemann::Dash::App
8
2
  get '/' do
9
3
  erb :index, :layout => false
10
4
  end
11
5
 
12
6
  get '/config', :provides => 'json' do
13
- if File.exists? WS_CONFIG_FILE
14
- send_file WS_CONFIG_FILE, :type => :json
15
- else
16
- MultiJson.encode({})
17
- end
7
+ content_type "application/json"
8
+ config.read_ws_config
18
9
  end
19
10
 
20
11
  post '/config' do
21
12
  # Read update
22
13
  request.body.rewind
23
- update = MultiJson.decode(request.body.read)
24
-
25
- # Read old config
26
- if File.exists? WS_CONFIG_FILE
27
- old = MultiJson.decode File.read WS_CONFIG_FILE
28
- else
29
- old = {}
30
- end
31
-
32
- new = {}
33
-
34
- # Server
35
- new['server'] = update['server'] or old['server']
36
-
37
- p update['workspaces']
38
- new['workspaces'] = update['workspaces'] or old['workspaces']
39
-
40
- # Save new config
41
- FileUtils.mkdir_p 'config'
42
- File.open(WS_CONFIG_FILE, 'w') do |f|
43
- f.write(MultiJson.encode(new))
44
- end
14
+ config.update_ws_config(request.body.read)
45
15
 
46
16
  # Return current config
47
17
  content_type "application/json"
48
- MultiJson.encode(new)
18
+ config.read_ws_config
49
19
  end
50
- end
20
+ end
@@ -37,14 +37,18 @@ dash = (function() {
37
37
  }
38
38
 
39
39
  // Find a workspace by ID.
40
- var workspace = function(id) {
41
- for (var i = 0; i < workspaces.length; i++) {
42
- if (workspaces[i].id === id) {
43
- return workspaces[i];
44
- }
45
- }
46
- return null;
47
- }
40
+ var workspaceByProperty = function(property) {
41
+ return function(match) {
42
+ return _.find(workspaces, function(w) {
43
+ return _.isEqual(match, w[property]);
44
+ }) || null;
45
+ // for some reason we can't return undefined and we must
46
+ // explicitly return null upon failure
47
+ };
48
+ };
49
+
50
+ var workspace = workspaceByProperty("id");
51
+ var workspaceByName = workspaceByProperty("name");
48
52
 
49
53
  // Get current workspace.
50
54
  var currentWorkspace = function() {
@@ -60,7 +64,7 @@ dash = (function() {
60
64
  var currentIndex = currentWorkspaceIndex();
61
65
  if (currentIndex != null) {
62
66
  //console.log(util.merge(currentWorkspace(), {view: currentView.json()}));
63
- workspaces[currentIndex] =
67
+ workspaces[currentIndex] =
64
68
  util.merge(currentWorkspace(), {view: currentView.json()});
65
69
  }
66
70
  toolbar.workspaces(workspaces);
@@ -73,6 +77,19 @@ dash = (function() {
73
77
  return w;
74
78
  }
75
79
 
80
+ var getLocation = function() {
81
+ return window.location.hash.slice(1);
82
+ };
83
+
84
+ var toLocation = function(workspace) {
85
+ window.history.pushState(workspace, workspace.name, "#" + workspace.name);
86
+ };
87
+
88
+ var switchWorkspaceByName = function(name) {
89
+ var workspace = workspaceByName(name) || workspaces[0];
90
+ return switchWorkspace(workspace);
91
+ };
92
+
76
93
  // Switch between workspaces.
77
94
  var switchWorkspace = function(workspace) {
78
95
  // Shut down current setup
@@ -87,6 +104,13 @@ dash = (function() {
87
104
  currentWorkspaceId = workspace.id;
88
105
  toolbar.workspace(workspace);
89
106
 
107
+ document.title = workspace.name;
108
+
109
+ // update URL
110
+ if (getLocation() !== workspace.name) {
111
+ toLocation(workspace);
112
+ }
113
+
90
114
  // Create new view
91
115
  currentView = view.reify(
92
116
  $.extend({container: $('#view')}, workspace.view));
@@ -121,22 +145,24 @@ dash = (function() {
121
145
  });
122
146
  toolbar.workspaces(workspaces);
123
147
  }
124
-
148
+
125
149
  // Ensure there's a default workspace.
126
150
  if (workspaces.length === 0) {
127
151
  workspaces = [defaultWorkspace];
128
152
  }
129
153
 
130
- // Don't preserve current state.
131
154
  var replacement = workspace(currentWorkspace.id);
132
155
  currentWorkspaceId = null;
133
- if (replacement) {
134
- // Load current workspace, if it's still there
135
- switchWorkspace(replacement);
136
- } else {
137
- // Or revert to the first workspace
156
+
157
+ var currentLocation = getLocation()
158
+ if (currentLocation) { // check URL first
159
+ switchWorkspaceByName(currentLocation);
160
+ } else if (replacement) { // otherwise use replacement
161
+ switchWorkspace(replacement)
162
+ } else { // failing that use the first
138
163
  switchWorkspace(workspaces[0]);
139
164
  }
165
+
140
166
  });
141
167
  }
142
168
 
@@ -150,7 +176,7 @@ dash = (function() {
150
176
  workspaces: workspaces
151
177
  },
152
178
  function() { toastr.info("Configuration saved.") },
153
- function(xhr, msg) {
179
+ function(xhr, msg) {
154
180
  console.log("Error saving config", msg);
155
181
  toastr.error("Error saving config: " + msg);
156
182
  }
@@ -249,7 +275,7 @@ dash = (function() {
249
275
  } catch (e) { }
250
276
  }
251
277
  });
252
-
278
+
253
279
  return {
254
280
  workspaces: function() { return workspaces },
255
281
  currentWorkspace: function() { return currentWorkspace },
@@ -6,15 +6,15 @@ var format = (function() {
6
6
  return Math.round(number * base) / base;
7
7
  }
8
8
 
9
- var metricTemplate = '<div class="bar {{class}}" style="width: {{percent}}%">{{metric}}</div>';
9
+ var metricTemplate = _.template('<div class="bar {{state}}" style="width: {{percent}}%">{{metric}}</div>');
10
10
  var metric = function(e, max) {
11
11
  var max = (max || 1);
12
12
  var data = {
13
- 'class': e.state,
13
+ 'state': e.state,
14
14
  'percent': (e.metric / max * 100),
15
15
  'metric': formatFloat(e.metric)
16
16
  }
17
- return Mustache.render(metricTemplate, data);
17
+ return metricTemplate(data);
18
18
  };
19
19
 
20
20
  return {
@@ -3,7 +3,7 @@ var persistence = (function() {
3
3
  // Saves configuration to persistent store. Calls success() or error() when
4
4
  // complete.
5
5
  var save = function(config, success, error) {
6
- jQuery.ajax('/config', {
6
+ jQuery.ajax('config', {
7
7
  type: 'POST',
8
8
  success: success,
9
9
  error: error,
@@ -15,7 +15,7 @@ var persistence = (function() {
15
15
 
16
16
  // Returns configuration from persistent store.
17
17
  var load = function(success, error) {
18
- jQuery.ajax('/config', {
18
+ jQuery.ajax('config', {
19
19
  type: 'GET',
20
20
  success: success,
21
21
  error: error,