riemann-dash 0.2.1 → 0.2.3

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.
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
@@ -1,4 +1,5 @@
1
1
  var subs = (function() {
2
+
2
3
  // What server shall we connect to by default?
3
4
  var server;
4
5
 
@@ -25,18 +26,13 @@ var subs = (function() {
25
26
 
26
27
  // Close a subscription's websocket channel.
27
28
  var close = function(sub) {
28
- if (sub.ws == null) {
29
- return sub;
30
- }
31
- sub.ws.close();
32
- sub.ws == null;
33
- return sub;
29
+ return sub.close();
34
30
  }
35
31
 
36
32
  // Closes a subscription and deletes it from the subscription manager.
37
33
  var unsubscribe = function(sub) {
38
34
  delete subs[sub.id];
39
- close(sub);
35
+ return sub.close();
40
36
  }
41
37
 
42
38
  // Unsubscribe from all subscriptions.
@@ -46,61 +42,80 @@ var subs = (function() {
46
42
 
47
43
  // Open a subscription's websocket channel.
48
44
  var open = function(sub) {
49
- if (sub.ws != null && sub.ws.readyState != WebSocket.CLOSED) {
50
- return sub;
51
- }
45
+ return sub.open();
46
+ }
52
47
 
53
- var f = sub.f;
54
- var queryString = "query=" + encodeURI(sub.query);
55
- var uri = "ws://" + server + "/index?subscribe=true&" + queryString;
56
- sub.ws = new WebSocket(uri);
57
- var $ws = $(sub.ws);
58
-
59
- $ws.bind('open', function() {
60
- console.log("Socket opened", sub.query);
61
- });
48
+ var Subscription = Backbone.Model.extend({
62
49
 
63
- $ws.bind('close', function(e) {
64
- console.log("Socket closed", sub.query);
65
- sub.ws = null;
66
- });
50
+ initialize: function(id, query, f) {
51
+ this.id = id;
52
+ this.query = query;
53
+ this.f = f;
54
+ },
67
55
 
68
- $ws.bind('error', function(e) {
69
- console.log("Socket error", sub.query);
70
- errorQueue.push(e);
71
- ws.close();
72
- });
56
+ isOpen: function() {
57
+ return this.ws && (this.ws.readyState != WebSocket.CLOSED)
58
+ },
59
+ isClosed: function() { return !this.isOpen() },
73
60
 
74
- $ws.bind('message', function(e) {
75
- t1 = Date.now();
76
- if (active) {
77
- f(JSON.parse(e.originalEvent.data));
78
- }
79
- load1(t1, Date.now());
80
- load5(t1, Date.now());
81
- });
61
+ url: function() {
62
+ var queryString = "query=" + encodeURI(this.query);
63
+ return "ws://" + server + "/index?subscribe=true&" + queryString;
64
+ },
82
65
 
83
- return sub;
84
- }
66
+ open: function() {
67
+ if (this.isOpen()) return this;
68
+ var ws = this.ws = new WebSocket(this.url());
69
+
70
+ ws.onopen = _.bind(function() {
71
+ console.log("Socket opened", this.query);
72
+ }, this);
73
+
74
+ ws.onclose = _.bind(function(e) {
75
+ console.log("Socket closed", this.query);
76
+ this.ws = null;
77
+ }, this);
78
+
79
+ ws.onerror = _.bind(function(e) {
80
+ console.log("Socket error", this.query);
81
+ errorQueue.push(e);
82
+ this.close();
83
+ }, this);
84
+
85
+ ws.onmessage = _.bind(function(e) {
86
+ t1 = Date.now();
87
+ if (active) {
88
+ this.f(JSON.parse(e.data));
89
+ }
90
+ load1(t1, Date.now());
91
+ load5(t1, Date.now());
92
+ }, this);
93
+
94
+ return this;
95
+
96
+ },
97
+
98
+ close: function() {
99
+ if (this.ws) {
100
+ this.ws.close();
101
+ this.ws = void 0;
102
+ }
103
+ return this;
104
+ }
105
+ });
85
106
 
86
107
  // Add a subscription. Returns a subscription object. Subscriptions are
87
108
  // opened immediately.
88
109
  var subscribe = function(query, f) {
89
- var sub = {
90
- id: newId(),
91
- query: query,
92
- f: f,
93
- ws: null
94
- }
110
+ var sub = new Subscription(newId(), query, f).open();
95
111
  subs[sub.id] = sub;
96
- open(sub);
97
112
  return sub;
98
113
  }
99
114
 
100
115
  // Reconnect all inactive subs.
101
116
  var converge = function() {
102
117
  var closed = _.filter(subs, function(sub) {
103
- return (sub.ws == null || sub.ws.readyState == WebSocket.CLOSED);
118
+ return sub.isClosed();
104
119
  });
105
120
  if (_.isEmpty(closed)) {
106
121
  // Done here.
@@ -108,7 +123,7 @@ var subs = (function() {
108
123
  }
109
124
 
110
125
  // Display reconnection notice
111
- toastr.warning(_.size(closed) + " lost connections");
126
+ toastr.warning(_.size(closed) + " lost connections—check the server field above.");
112
127
 
113
128
  // Reopen
114
129
  _.each(closed, function(sub) {
@@ -120,7 +135,7 @@ var subs = (function() {
120
135
  if (errorQueue.length == 0) {
121
136
  return;
122
137
  }
123
- _.warning(errorQueue.length + " socket errors");
138
+ toastr.warning(errorQueue.length + " socket errors");
124
139
  errorQueue.length = 0;
125
140
  converge();
126
141
  }
@@ -1,53 +1,46 @@
1
1
  var util = (function() {
2
- // Unique IDs
3
- var uniqueId = function(bytes) {
4
- bytes = bytes || 20;
5
- var s = '';
6
- for (var i = 0; i < bytes * 2; i++) {
7
- s += Math.floor(Math.random() * 16).toString(16);
8
- }
9
- return s;
10
- }
11
-
12
- // Merge two maps nondestructively.
13
- var merge = function(m1, m2) {
14
- return _.extend(_.clone(m1), m2);
15
- }
2
+ return {
3
+ merge: function(m1, m2) {
4
+ // Merge two maps nondestructively.
5
+ return _.extend({}, m1, m2)
6
+ },
7
+ slur: function(period, f) {
8
+ // Wraps a function in another, which calls f at most once every period
9
+ // milliseconds. Tries to minimize latency.
16
10
 
17
- // Wraps a function in another, which calls f at most once every period
18
- // milliseconds. Tries to minimize latency.
19
- var slur = function(period, f) {
20
- var lastRun = new Date();
21
- lastRun.setYear(0);
22
- var queued = false;
23
- var execute = function(context, args) {
24
- f.apply(context, args);
25
- lastRun = new Date();
26
- queued = false;
27
- };
11
+ var lastRun = new Date();
12
+ lastRun.setYear(0);
13
+ var queued = false;
14
+ var execute = function(context, args) {
15
+ f.apply(context, args);
16
+ lastRun = new Date();
17
+ queued = false;
18
+ };
28
19
 
29
- return function() {
30
- // If queued, do nothing
31
- if (queued) {
32
- return;
33
- }
20
+ return function() {
21
+ // If queued, do nothing
22
+ if (queued) {
23
+ return;
24
+ }
34
25
 
35
- var dt = (new Date()) - lastRun;
36
- if (period <= dt) {
37
- // We're free to go
38
- execute(this, arguments);
39
- } else {
40
- // Too soon, enqueue a new job.
41
- window.setTimeout(execute, period - dt, this, arguments);
26
+ var dt = (new Date()) - lastRun;
27
+ if (period <= dt) {
28
+ // We're free to go
29
+ execute(this, arguments);
30
+ }
31
+ else {
32
+ // Too soon, enqueue a new job.
33
+ window.setTimeout(execute, period - dt, this, arguments);
34
+ }
42
35
  }
36
+ },
37
+ uniqueId: function(length) {
38
+ // Unique-ish IDs as a length sized string of hex
39
+ var id = '', hex = '0123456789abcdef';
40
+ _(length || 40).times(function() { id += hex[_.random(15)]; });
41
+ return id;
43
42
  }
44
- }
45
-
46
- return {
47
- merge: merge,
48
- slur: slur,
49
- uniqueId: uniqueId
50
- }
43
+ };
51
44
  })();
52
45
 
53
46
  $(function() {