flamingo 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,6 +11,7 @@ require 'sinatra/base'
11
11
 
12
12
  require 'flamingo/version'
13
13
  require 'flamingo/config'
14
+ require 'flamingo/stats/time_series'
14
15
  require 'flamingo/dispatch_event'
15
16
  require 'flamingo/dispatch_error'
16
17
  require 'flamingo/stream_params'
@@ -27,6 +28,7 @@ require 'flamingo/daemon/flamingod'
27
28
  require 'flamingo/logging/formatter'
28
29
  require 'flamingo/web/server'
29
30
 
31
+
30
32
  module Flamingo
31
33
 
32
34
  class << self
@@ -0,0 +1,134 @@
1
+ module Flamingo
2
+ module Stats
3
+ class TimeSeries
4
+
5
+ attr_accessor :name, :seconds_per_tick, :total_ticks
6
+
7
+ class << self
8
+ def create(name,seconds_per_tick,total_ticks)
9
+ redis.set("stats:#{name}:schema","#{seconds_per_tick},#{total_ticks}")
10
+ new(name,seconds_per_tick,total_ticks)
11
+ end
12
+
13
+ def get(name)
14
+ schema_value = redis.get("stats:#{name}:schema")
15
+ raise "No schema specified" unless schema_value
16
+ seconds_per_tick, total_ticks = schema_value.split(",").map{|v| v.to_i}
17
+ new(name,seconds_per_tick,total_ticks)
18
+ end
19
+
20
+ def redis
21
+ Flamingo.redis
22
+ end
23
+
24
+ end
25
+
26
+ def initialize(name,seconds_per_tick,total_ticks)
27
+ self.name = name
28
+ self.seconds_per_tick = seconds_per_tick
29
+ self.total_ticks = total_ticks
30
+ end
31
+
32
+ def data(ticks=total_ticks,time=Time.now.utc)
33
+ range = tick_range(ticks,time)
34
+ keys = range.map {|t| value_key(t) }
35
+ values = redis.mget(keys).map {|v| (v || 0).to_f }
36
+ range.map {|t| t * seconds_per_tick }.zip(values)
37
+ end
38
+
39
+ def each_datum(ticks=total_ticks,time=Time.now.utc)
40
+ tick_range(ticks,time).each do |tick|
41
+ yield(tick*seconds_per_tick,value(tick))
42
+ end
43
+ end
44
+
45
+ def tick_range(ticks=total_ticks,time=Time.now.utc)
46
+ end_tick = to_tick(time)
47
+ start_tick = (end_tick - ticks) + 1
48
+ (start_tick..end_tick)
49
+ end
50
+
51
+ def average(ticks=total_ticks,time=Time.now.utc)
52
+ count = 0
53
+ sum = 0
54
+ each_datum do |ts, value|
55
+ count += 1
56
+ sum += value
57
+ end
58
+ if count == 0
59
+ nil
60
+ else
61
+ sum / count
62
+ end
63
+ end
64
+
65
+ def increment(val=1,time=Time.now.utc)
66
+ modify_value_at(time) do |val_key|
67
+ redis.incrby(val_key,val)
68
+ end
69
+ end
70
+
71
+ def set(val,time=Time.now.utc)
72
+ modify_value_at(time) do |val_key|
73
+ redis.set(val_key,val)
74
+ end
75
+ end
76
+
77
+ private
78
+ def modify_value_at(time=Time.now.utc)
79
+ t = to_tick(time)
80
+ return false unless valid?(t)
81
+ val_key = value_key(t)
82
+ yield(val_key)
83
+ ver_key = version_key(t)
84
+ prev_tick = redis.getset(ver_key,t)
85
+ if prev_tick && prev_tick.to_i < t
86
+ redis.del(value_key(prev_tick.to_i))
87
+ end
88
+ true
89
+ end
90
+
91
+ def value(tick)
92
+ val = redis.get(value_key(tick))
93
+ val ? val.to_f : 0
94
+ end
95
+
96
+ def to_tick(time=Time.now.utc)
97
+ (time.to_i / seconds_per_tick)
98
+ end
99
+
100
+ def now_tick
101
+ to_tick(Time.now.utc)
102
+ end
103
+
104
+ def valid?(tick)
105
+ nt = now_tick
106
+ (nt - total_ticks) <= tick && tick <= nt
107
+ end
108
+
109
+ def tick_series(tick)
110
+ tick / total_ticks
111
+ end
112
+
113
+ def tick_pos(tick)
114
+ tick % total_ticks
115
+ end
116
+
117
+ def value_key(tick)
118
+ s = tick_series(tick)
119
+ pos = tick_pos(tick)
120
+ "stats:#{name}:#{s}:#{pos}"
121
+ end
122
+
123
+ def version_key(tick)
124
+ pos = tick_pos(tick)
125
+ "stats:#{name}:v:#{pos}"
126
+ end
127
+
128
+ def redis
129
+ self.class.redis
130
+ end
131
+
132
+ end
133
+ end
134
+ end
@@ -25,7 +25,7 @@ module Flamingo
25
25
  end
26
26
 
27
27
  def connect(options)
28
- conn_opts = {:ssl => true, :user_agent => "Flamingo/0.1" }.
28
+ conn_opts = {:ssl => false, :user_agent => "Flamingo/0.1" }.
29
29
  merge(options).merge(:path=>path)
30
30
  Twitter::JSONStream.connect(conn_opts)
31
31
  end
@@ -59,4 +59,4 @@ module Flamingo
59
59
 
60
60
  end
61
61
 
62
- end
62
+ end
@@ -1,3 +1,3 @@
1
1
  module Flamingo
2
- Version = VERSION = '0.2.0'
2
+ Version = VERSION = '0.2.1'
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flamingo
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 0
10
- version: 0.2.0
9
+ - 1
10
+ version: 0.2.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Hayes Davis
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-08-01 00:00:00 -05:00
19
+ date: 2010-10-25 00:00:00 -07:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -171,6 +171,7 @@ files:
171
171
  - lib/flamingo/dispatch_error.rb
172
172
  - lib/flamingo/dispatch_event.rb
173
173
  - lib/flamingo/logging/formatter.rb
174
+ - lib/flamingo/stats/time_series.rb
174
175
  - lib/flamingo/stream.rb
175
176
  - lib/flamingo/stream_params.rb
176
177
  - lib/flamingo/subscription.rb