statsd 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,299 @@
1
+ /*
2
+ Flot plugin for selecting regions.
3
+
4
+ The plugin defines the following options:
5
+
6
+ selection: {
7
+ mode: null or "x" or "y" or "xy",
8
+ color: color
9
+ }
10
+
11
+ You enable selection support by setting the mode to one of "x", "y" or
12
+ "xy". In "x" mode, the user will only be able to specify the x range,
13
+ similarly for "y" mode. For "xy", the selection becomes a rectangle
14
+ where both ranges can be specified. "color" is color of the selection.
15
+
16
+ When selection support is enabled, a "plotselected" event will be emitted
17
+ on the DOM element you passed into the plot function. The event
18
+ handler gets one extra parameter with the ranges selected on the axes,
19
+ like this:
20
+
21
+ placeholder.bind("plotselected", function(event, ranges) {
22
+ alert("You selected " + ranges.xaxis.from + " to " + ranges.xaxis.to)
23
+ // similar for yaxis, secondary axes are in x2axis
24
+ // and y2axis if present
25
+ });
26
+
27
+ The "plotselected" event is only fired when the user has finished
28
+ making the selection. A "plotselecting" event is fired during the
29
+ process with the same parameters as the "plotselected" event, in case
30
+ you want to know what's happening while it's happening,
31
+
32
+ A "plotunselected" event with no arguments is emitted when the user
33
+ clicks the mouse to remove the selection.
34
+
35
+ The plugin allso adds the following methods to the plot object:
36
+
37
+ - setSelection(ranges, preventEvent)
38
+
39
+ Set the selection rectangle. The passed in ranges is on the same
40
+ form as returned in the "plotselected" event. If the selection
41
+ mode is "x", you should put in either an xaxis (or x2axis) object,
42
+ if the mode is "y" you need to put in an yaxis (or y2axis) object
43
+ and both xaxis/x2axis and yaxis/y2axis if the selection mode is
44
+ "xy", like this:
45
+
46
+ setSelection({ xaxis: { from: 0, to: 10 }, yaxis: { from: 40, to: 60 } });
47
+
48
+ setSelection will trigger the "plotselected" event when called. If
49
+ you don't want that to happen, e.g. if you're inside a
50
+ "plotselected" handler, pass true as the second parameter.
51
+
52
+ - clearSelection(preventEvent)
53
+
54
+ Clear the selection rectangle. Pass in true to avoid getting a
55
+ "plotunselected" event.
56
+
57
+ - getSelection()
58
+
59
+ Returns the current selection in the same format as the
60
+ "plotselected" event. If there's currently no selection, the
61
+ function returns null.
62
+
63
+ */
64
+
65
+ (function ($) {
66
+ function init(plot) {
67
+ var selection = {
68
+ first: { x: -1, y: -1}, second: { x: -1, y: -1},
69
+ show: false,
70
+ active: false
71
+ };
72
+
73
+ // FIXME: The drag handling implemented here should be
74
+ // abstracted out, there's some similar code from a library in
75
+ // the navigation plugin, this should be massaged a bit to fit
76
+ // the Flot cases here better and reused. Doing this would
77
+ // make this plugin much slimmer.
78
+ var savedhandlers = {};
79
+
80
+ function onMouseMove(e) {
81
+ if (selection.active) {
82
+ plot.getPlaceholder().trigger("plotselecting", [ getSelection() ]);
83
+
84
+ updateSelection(e);
85
+ }
86
+ }
87
+
88
+ function onMouseDown(e) {
89
+ if (e.which != 1) // only accept left-click
90
+ return;
91
+
92
+ // cancel out any text selections
93
+ document.body.focus();
94
+
95
+ // prevent text selection and drag in old-school browsers
96
+ if (document.onselectstart !== undefined && savedhandlers.onselectstart == null) {
97
+ savedhandlers.onselectstart = document.onselectstart;
98
+ document.onselectstart = function () { return false; };
99
+ }
100
+ if (document.ondrag !== undefined && savedhandlers.ondrag == null) {
101
+ savedhandlers.ondrag = document.ondrag;
102
+ document.ondrag = function () { return false; };
103
+ }
104
+
105
+ setSelectionPos(selection.first, e);
106
+
107
+ selection.active = true;
108
+
109
+ $(document).one("mouseup", onMouseUp);
110
+ }
111
+
112
+ function onMouseUp(e) {
113
+ // revert drag stuff for old-school browsers
114
+ if (document.onselectstart !== undefined)
115
+ document.onselectstart = savedhandlers.onselectstart;
116
+ if (document.ondrag !== undefined)
117
+ document.ondrag = savedhandlers.ondrag;
118
+
119
+ // no more draggy-dee-drag
120
+ selection.active = false;
121
+ updateSelection(e);
122
+
123
+ if (selectionIsSane())
124
+ triggerSelectedEvent();
125
+ else {
126
+ // this counts as a clear
127
+ plot.getPlaceholder().trigger("plotunselected", [ ]);
128
+ plot.getPlaceholder().trigger("plotselecting", [ null ]);
129
+ }
130
+
131
+ return false;
132
+ }
133
+
134
+ function getSelection() {
135
+ if (!selectionIsSane())
136
+ return null;
137
+
138
+ var x1 = Math.min(selection.first.x, selection.second.x),
139
+ x2 = Math.max(selection.first.x, selection.second.x),
140
+ y1 = Math.max(selection.first.y, selection.second.y),
141
+ y2 = Math.min(selection.first.y, selection.second.y);
142
+
143
+ var r = {};
144
+ var axes = plot.getAxes();
145
+ if (axes.xaxis.used)
146
+ r.xaxis = { from: axes.xaxis.c2p(x1), to: axes.xaxis.c2p(x2) };
147
+ if (axes.x2axis.used)
148
+ r.x2axis = { from: axes.x2axis.c2p(x1), to: axes.x2axis.c2p(x2) };
149
+ if (axes.yaxis.used)
150
+ r.yaxis = { from: axes.yaxis.c2p(y1), to: axes.yaxis.c2p(y2) };
151
+ if (axes.y2axis.used)
152
+ r.y2axis = { from: axes.y2axis.c2p(y1), to: axes.y2axis.c2p(y2) };
153
+ return r;
154
+ }
155
+
156
+ function triggerSelectedEvent() {
157
+ var r = getSelection();
158
+
159
+ plot.getPlaceholder().trigger("plotselected", [ r ]);
160
+
161
+ // backwards-compat stuff, to be removed in future
162
+ var axes = plot.getAxes();
163
+ if (axes.xaxis.used && axes.yaxis.used)
164
+ plot.getPlaceholder().trigger("selected", [ { x1: r.xaxis.from, y1: r.yaxis.from, x2: r.xaxis.to, y2: r.yaxis.to } ]);
165
+ }
166
+
167
+ function clamp(min, value, max) {
168
+ return value < min? min: (value > max? max: value);
169
+ }
170
+
171
+ function setSelectionPos(pos, e) {
172
+ var o = plot.getOptions();
173
+ var offset = plot.getPlaceholder().offset();
174
+ var plotOffset = plot.getPlotOffset();
175
+ pos.x = clamp(0, e.pageX - offset.left - plotOffset.left, plot.width());
176
+ pos.y = clamp(0, e.pageY - offset.top - plotOffset.top, plot.height());
177
+
178
+ if (o.selection.mode == "y")
179
+ pos.x = pos == selection.first? 0: plot.width();
180
+
181
+ if (o.selection.mode == "x")
182
+ pos.y = pos == selection.first? 0: plot.height();
183
+ }
184
+
185
+ function updateSelection(pos) {
186
+ if (pos.pageX == null)
187
+ return;
188
+
189
+ setSelectionPos(selection.second, pos);
190
+ if (selectionIsSane()) {
191
+ selection.show = true;
192
+ plot.triggerRedrawOverlay();
193
+ }
194
+ else
195
+ clearSelection(true);
196
+ }
197
+
198
+ function clearSelection(preventEvent) {
199
+ if (selection.show) {
200
+ selection.show = false;
201
+ plot.triggerRedrawOverlay();
202
+ if (!preventEvent)
203
+ plot.getPlaceholder().trigger("plotunselected", [ ]);
204
+ }
205
+ }
206
+
207
+ function setSelection(ranges, preventEvent) {
208
+ var axis, range, axes = plot.getAxes();
209
+ var o = plot.getOptions();
210
+
211
+ if (o.selection.mode == "y") {
212
+ selection.first.x = 0;
213
+ selection.second.x = plot.width();
214
+ }
215
+ else {
216
+ axis = ranges["xaxis"]? axes["xaxis"]: (ranges["x2axis"]? axes["x2axis"]: axes["xaxis"]);
217
+ range = ranges["xaxis"] || ranges["x2axis"] || { from:ranges["x1"], to:ranges["x2"] }
218
+ selection.first.x = axis.p2c(Math.min(range.from, range.to));
219
+ selection.second.x = axis.p2c(Math.max(range.from, range.to));
220
+ }
221
+
222
+ if (o.selection.mode == "x") {
223
+ selection.first.y = 0;
224
+ selection.second.y = plot.height();
225
+ }
226
+ else {
227
+ axis = ranges["yaxis"]? axes["yaxis"]: (ranges["y2axis"]? axes["y2axis"]: axes["yaxis"]);
228
+ range = ranges["yaxis"] || ranges["y2axis"] || { from:ranges["y1"], to:ranges["y2"] }
229
+ selection.first.y = axis.p2c(Math.min(range.from, range.to));
230
+ selection.second.y = axis.p2c(Math.max(range.from, range.to));
231
+ }
232
+
233
+ selection.show = true;
234
+ plot.triggerRedrawOverlay();
235
+ if (!preventEvent)
236
+ triggerSelectedEvent();
237
+ }
238
+
239
+ function selectionIsSane() {
240
+ var minSize = 5;
241
+ return Math.abs(selection.second.x - selection.first.x) >= minSize &&
242
+ Math.abs(selection.second.y - selection.first.y) >= minSize;
243
+ }
244
+
245
+ plot.clearSelection = clearSelection;
246
+ plot.setSelection = setSelection;
247
+ plot.getSelection = getSelection;
248
+
249
+ plot.hooks.bindEvents.push(function(plot, eventHolder) {
250
+ var o = plot.getOptions();
251
+ if (o.selection.mode != null)
252
+ eventHolder.mousemove(onMouseMove);
253
+
254
+ if (o.selection.mode != null)
255
+ eventHolder.mousedown(onMouseDown);
256
+ });
257
+
258
+
259
+ plot.hooks.drawOverlay.push(function (plot, ctx) {
260
+ // draw selection
261
+ if (selection.show && selectionIsSane()) {
262
+ var plotOffset = plot.getPlotOffset();
263
+ var o = plot.getOptions();
264
+
265
+ ctx.save();
266
+ ctx.translate(plotOffset.left, plotOffset.top);
267
+
268
+ var c = $.color.parse(o.selection.color);
269
+
270
+ ctx.strokeStyle = c.scale('a', 0.8).toString();
271
+ ctx.lineWidth = 1;
272
+ ctx.lineJoin = "round";
273
+ ctx.fillStyle = c.scale('a', 0.4).toString();
274
+
275
+ var x = Math.min(selection.first.x, selection.second.x),
276
+ y = Math.min(selection.first.y, selection.second.y),
277
+ w = Math.abs(selection.second.x - selection.first.x),
278
+ h = Math.abs(selection.second.y - selection.first.y);
279
+
280
+ ctx.fillRect(x, y, w, h);
281
+ ctx.strokeRect(x, y, w, h);
282
+
283
+ ctx.restore();
284
+ }
285
+ });
286
+ }
287
+
288
+ $.plot.plugins.push({
289
+ init: init,
290
+ options: {
291
+ selection: {
292
+ mode: null, // one of null, "x", "y" or "xy"
293
+ color: "#e8cfac"
294
+ }
295
+ },
296
+ name: 'selection',
297
+ version: '1.0'
298
+ });
299
+ })(jQuery);
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,94 @@
1
+ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2
+ <html>
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
5
+ <title>Flot Example</title>
6
+ <link href="layout.css" rel="stylesheet" type="text/css"></link>
7
+ <script language="javascript" type="text/javascript" src="/jquery-1.4.4.js"></script>
8
+ <script language="javascript" type="text/javascript" src="/jquery.flot.js"></script>
9
+ <script language="javascript" type="text/javascript" src="/jquery.flot.selection.js"></script>
10
+ </head>
11
+ <body>
12
+ <h1>Flot Example</h1>
13
+
14
+ <div id="placeholder" style="width:600px;height:300px;"></div>
15
+
16
+ <p>Example metric per day. Weekends are colored. Try zooming.
17
+ The plot below shows an overview.</p>
18
+
19
+ <div id="overview" style="margin-left:50px;margin-top:20px;width:400px;height:50px"></div>
20
+ <script id="source">
21
+ $(function () {
22
+
23
+ var d = [<% @stats.each do |stat| %>
24
+ [<%= stat['ts'] %>, <%= stat['values']['mean'] %>],
25
+ <% end %>];
26
+
27
+ if (d.length == 0) {
28
+ d = [[1196463600000, 0], [1196550000000, 0], [1196636400000, 0], [1196722800000, 77], [1196809200000, 3636], [1196895600000, 3575], [1196982000000, 2736], [1197068400000, 1086], [1197154800000, 676], [1197241200000, 1205], [1197327600000, 906], [1197414000000, 710], [1197500400000, 639], [1197586800000, 540], [1197673200000, 435], [1197759600000, 301], [1197846000000, 575], [1197932400000, 481], [1198018800000, 591], [1198105200000, 608], [1198191600000, 459], [1198278000000, 234], [1198364400000, 1352], [1198450800000, 686], [1198537200000, 279], [1198623600000, 449], [1198710000000, 468], [1198796400000, 392], [1198882800000, 282], [1198969200000, 208], [1199055600000, 229], [1199142000000, 177], [1199228400000, 374], [1199314800000, 436], [1199401200000, 404], [1199487600000, 253], [1199574000000, 218], [1199660400000, 476], [1199746800000, 462], [1199833200000, 448], [1199919600000, 442], [1200006000000, 403], [1200092400000, 204], [1200178800000, 194], [1200265200000, 327], [1200351600000, 374], [1200438000000, 507], [1200524400000, 546], [1200610800000, 482], [1200697200000, 283], [1200783600000, 221], [1200870000000, 483], [1200956400000, 523], [1201042800000, 528], [1201129200000, 483], [1201215600000, 452], [1201302000000, 270], [1201388400000, 222], [1201474800000, 439], [1201561200000, 559], [1201647600000, 521], [1201734000000, 477], [1201820400000, 442], [1201906800000, 252], [1201993200000, 236], [1202079600000, 525], [1202166000000, 477], [1202252400000, 386], [1202338800000, 409], [1202425200000, 408], [1202511600000, 237], [1202598000000, 193], [1202684400000, 357], [1202770800000, 414], [1202857200000, 393], [1202943600000, 353], [1203030000000, 364], [1203116400000, 215], [1203202800000, 214], [1203289200000, 356], [1203375600000, 399], [1203462000000, 334], [1203548400000, 348], [1203634800000, 243], [1203721200000, 126], [1203807600000, 157], [1203894000000, 288]];
29
+ }
30
+ // first correct the timestamps - they are recorded as the daily
31
+ // midnights in UTC+0100, but Flot always displays dates in UTC
32
+ // so we have to add one hour to hit the midnights in the plot
33
+ for (var i = 0; i < d.length; ++i)
34
+ d[i][0] += 60 * 60 * 1000;
35
+
36
+ // helper for returning the weekends in a period
37
+ function weekendAreas(axes) {
38
+ var markings = [];
39
+ var d = new Date(axes.xaxis.min);
40
+ // go to the first Saturday
41
+ d.setUTCDate(d.getUTCDate() - ((d.getUTCDay() + 1) % 7))
42
+ d.setUTCSeconds(0);
43
+ d.setUTCMinutes(0);
44
+ d.setUTCHours(0);
45
+ var i = d.getTime();
46
+ do {
47
+ // when we don't set yaxis, the rectangle automatically
48
+ // extends to infinity upwards and downwards
49
+ markings.push({ xaxis: { from: i, to: i + 2 * 24 * 60 * 60 * 1000 } });
50
+ i += 7 * 24 * 60 * 60 * 1000;
51
+ } while (i < axes.xaxis.max);
52
+
53
+ return markings;
54
+ }
55
+
56
+ var options = {
57
+ xaxis: { mode: "time" },
58
+ selection: { mode: "x" },
59
+ grid: { markings: weekendAreas }
60
+ };
61
+
62
+ var plot = $.plot($("#placeholder"), [d], options);
63
+
64
+ var overview = $.plot($("#overview"), [d], {
65
+ series: {
66
+ lines: { show: true, lineWidth: 1 },
67
+ shadowSize: 0
68
+ },
69
+ xaxis: { ticks: [], mode: "time" },
70
+ yaxis: { ticks: [], min: 0, autoscaleMargin: 0.1 },
71
+ selection: { mode: "x" }
72
+ });
73
+
74
+ // now connect the two
75
+
76
+ $("#placeholder").bind("plotselected", function (event, ranges) {
77
+ // do the zooming
78
+ plot = $.plot($("#placeholder"), [d],
79
+ $.extend(true, {}, options, {
80
+ xaxis: { min: ranges.xaxis.from, max: ranges.xaxis.to }
81
+ }));
82
+
83
+ // don't fire event on the overview to prevent eternal loop
84
+ overview.setSelection(ranges, true);
85
+ });
86
+
87
+ $("#overview").bind("plotselected", function (event, ranges) {
88
+ plot.setSelection(ranges);
89
+ });
90
+ });
91
+ </script>
92
+
93
+ </body>
94
+ </html>
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: statsd
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.4
6
+ platform: ruby
7
+ authors:
8
+ - Andrew Coldham
9
+ - Ben VandenBos
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2011-02-25 00:00:00 -08:00
15
+ default_executable:
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: eventmachine
19
+ prerelease: false
20
+ requirement: &id001 !ruby/object:Gem::Requirement
21
+ none: false
22
+ requirements:
23
+ - - ~>
24
+ - !ruby/object:Gem::Version
25
+ version: 0.12.10
26
+ type: :runtime
27
+ version_requirements: *id001
28
+ - !ruby/object:Gem::Dependency
29
+ name: mongo
30
+ prerelease: false
31
+ requirement: &id002 !ruby/object:Gem::Requirement
32
+ none: false
33
+ requirements:
34
+ - - ~>
35
+ - !ruby/object:Gem::Version
36
+ version: 1.2.0
37
+ type: :runtime
38
+ version_requirements: *id002
39
+ description: Ruby version of statsd.
40
+ email:
41
+ - quasor@me.com
42
+ executables: []
43
+
44
+ extensions: []
45
+
46
+ extra_rdoc_files: []
47
+
48
+ files:
49
+ - LICENSE
50
+ - README.md
51
+ - config.js
52
+ - config.yml
53
+ - em-server.rb
54
+ - exampleConfig.js
55
+ - lib/statsd.rb
56
+ - lib/statsd/echos.rb
57
+ - lib/statsd/graphite.rb
58
+ - lib/statsd/mongo.rb
59
+ - lib/statsd/server.rb
60
+ - netcat-example.sh
61
+ - php-example.php
62
+ - python_example.py
63
+ - stats.js
64
+ - statsd.gemspec
65
+ - webapp/Gemfile
66
+ - webapp/Gemfile.lock
67
+ - webapp/README.md
68
+ - webapp/app.rb
69
+ - webapp/bin/rackup
70
+ - webapp/bin/statsd-web
71
+ - webapp/config.yml
72
+ - webapp/public/jquery-1.4.4.js
73
+ - webapp/public/jquery.flot.js
74
+ - webapp/public/jquery.flot.selection.js
75
+ - webapp/vendor/cache/SystemTimer-1.2.2.gem
76
+ - webapp/vendor/cache/rack-1.2.1.gem
77
+ - webapp/vendor/cache/redis-2.1.1.gem
78
+ - webapp/vendor/cache/sinatra-1.1.3.gem
79
+ - webapp/vendor/cache/tilt-1.2.2.gem
80
+ - webapp/vendor/cache/vegas-0.1.8.gem
81
+ - webapp/views/chart.erb
82
+ has_rdoc: true
83
+ homepage: http://github.com/quasor/statsd
84
+ licenses: []
85
+
86
+ post_install_message:
87
+ rdoc_options: []
88
+
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ none: false
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: "0"
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: 1.3.6
103
+ requirements: []
104
+
105
+ rubyforge_project:
106
+ rubygems_version: 1.5.2
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: Ruby version of statsd.
110
+ test_files: []
111
+