ustate-client 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -22,6 +22,17 @@ module UState
22
22
  State.average states, *a
23
23
  end
24
24
  end
25
+
26
+ # Average states with the same service across varying hosts.
27
+ # Useful when you don't know the exact services you'd like to fold.
28
+ # The resulting host for each folded state will be nil.
29
+ def average_over_hosts(query)
30
+ fold query do |states|
31
+ State.partition(states, :service).values.map do |slice|
32
+ State.sum slice, {host: nil}
33
+ end
34
+ end
35
+ end
25
36
 
26
37
  # Combines states matching query with the given block. The block
27
38
  # receives an array of states which presently match.
@@ -51,8 +62,13 @@ module UState
51
62
  @folds.each do |f, query|
52
63
  matching = @index.query(Query.new(string: query))
53
64
  unless matching.empty?
54
- if combined = f[matching]
65
+ case combined = f[matching]
66
+ when State
55
67
  @index << combined
68
+ when Array
69
+ combined.each do |s|
70
+ @index << s
71
+ end
56
72
  end
57
73
  end
58
74
  sleep interval
@@ -71,5 +87,15 @@ module UState
71
87
  State.sum states, *a
72
88
  end
73
89
  end
90
+
91
+ # Sum states with the same service across varying hosts.
92
+ # Useful when you don't know the exact services you'd like to fold.
93
+ def sum_over_hosts(query)
94
+ fold query do |states|
95
+ State.partition(states, :service).values.map do |slice|
96
+ State.sum slice, {host: nil}
97
+ end
98
+ end
99
+ end
74
100
  end
75
101
  end
@@ -42,6 +42,11 @@ class UState::Client
42
42
  end
43
43
  end
44
44
 
45
+ # Returns an array of states matching query.
46
+ def [](query)
47
+ query(query).states || []
48
+ end
49
+
45
50
  def connect
46
51
  @socket = TCPSocket.new(@host, @port)
47
52
  end
@@ -80,6 +80,7 @@ module UState
80
80
  rescue ZeroDivisionError
81
81
  0
82
82
  end
83
+ size = "%.2f" % size
83
84
 
84
85
  tag opts[:tag], h(text),
85
86
  :class => "state #{s.state}",
@@ -1,16 +1,11 @@
1
1
  class UState::Query
2
2
  class And < Node
3
- def initialize(a,b)
4
- @a = a
5
- @b = b
6
- end
3
+ include Narity
7
4
 
8
5
  def ===(state)
9
- @a === state and @b === state
10
- end
11
-
12
- def inspect
13
- inspect_helper @a, @b
6
+ as.all? do |a|
7
+ a === state
8
+ end
14
9
  end
15
10
  end
16
11
  end
@@ -1,8 +1,10 @@
1
1
  class UState::Query
2
2
  class Approximately < Node
3
+ include Binarity
4
+
3
5
  def initialize(field, value)
4
- @field = field
5
- @value = case value
6
+ @a = field
7
+ @b = case value
6
8
  when String
7
9
  r = value.chars.inject('') do |r, c|
8
10
  if c == '%'
@@ -18,11 +20,7 @@ class UState::Query
18
20
  end
19
21
 
20
22
  def ===(state)
21
- @value === state.send(@field)
22
- end
23
-
24
- def inspect
25
- inspect_helper @field, @value
23
+ @b === state.send(@a)
26
24
  end
27
25
  end
28
26
  end
@@ -1,4 +1,7 @@
1
1
  require 'ustate/query/node'
2
+ require 'ustate/query/binarity'
3
+ require 'ustate/query/narity'
4
+
2
5
  require 'ustate/query/and'
3
6
  require 'ustate/query/approximately'
4
7
  require 'ustate/query/equals'
@@ -0,0 +1,41 @@
1
+ module UState::Query::Binarity
2
+ def initialize(a, b)
3
+ @a = a
4
+ @b = b
5
+ end
6
+
7
+ def ==(o)
8
+ self.class == o.class and
9
+ a == o.a and
10
+ b == o.b
11
+ end
12
+
13
+ def a
14
+ @a
15
+ end
16
+
17
+ def b
18
+ @b
19
+ end
20
+
21
+ def children
22
+ [@a, @b]
23
+ end
24
+
25
+ def children=(c)
26
+ raise ArgumentError unless c.size == 2
27
+ @a, @b = c
28
+ end
29
+
30
+ def inspect
31
+ inspect_helper @a, @b
32
+ end
33
+
34
+ def mass
35
+ 1 + (@a.mass rescue 1) + (@b.mass rescue 1)
36
+ end
37
+
38
+ def to_s
39
+ "(#{@a} #{self.class.to_s.split('::').last.downcase} #{@b})"
40
+ end
41
+ end
@@ -1,16 +1,8 @@
1
1
  class UState::Query
2
2
  class Equals < Node
3
- def initialize(field, value)
4
- @field = field
5
- @value = value
6
- end
7
-
3
+ include Binarity
8
4
  def ===(state)
9
- state.send(@field) == @value
10
- end
11
-
12
- def inspect
13
- inspect_helper @field, @value
5
+ state.send(@a) == @b
14
6
  end
15
7
  end
16
8
  end
@@ -1,10 +1,15 @@
1
1
  class UState::Query
2
2
  class False < Node
3
- def initialize
3
+ def ==(other)
4
+ other.kind_of? False
4
5
  end
5
6
 
6
7
  def ===(state)
7
8
  false
8
9
  end
10
+
11
+ def to_s
12
+ false
13
+ end
9
14
  end
10
15
  end
@@ -1,16 +1,9 @@
1
1
  class UState::Query
2
2
  class Greater < Node
3
- def initialize(field, value)
4
- @field = field
5
- @value = value
6
- end
3
+ include Binarity
7
4
 
8
5
  def ===(state)
9
- x = state.send(@field) and x > @value
10
- end
11
-
12
- def inspect
13
- inspect_helper @field, @value
6
+ x = state.send(@a) and x > @b
14
7
  end
15
8
  end
16
9
  end
@@ -1,16 +1,9 @@
1
1
  class UState::Query
2
2
  class GreaterEqual < Node
3
- def initialize(field, value)
4
- @field = field
5
- @value = value
6
- end
3
+ include Binarity
7
4
 
8
5
  def ===(state)
9
- x = state.send(@field) and x >= @value
10
- end
11
-
12
- def inspect
13
- inspect_helper @field, @value
6
+ x = state.send(@a) and x >= @b
14
7
  end
15
8
  end
16
9
  end
@@ -1,16 +1,9 @@
1
1
  class UState::Query
2
2
  class Less < Node
3
- def initialize(field, value)
4
- @field = field
5
- @value = value
6
- end
3
+ include Binarity
7
4
 
8
5
  def ===(state)
9
- x = state.send(@field) and x < @value
10
- end
11
-
12
- def inspect
13
- inspect_helper @field, @value
6
+ x = state.send(@a) and x < @b
14
7
  end
15
8
  end
16
9
  end
@@ -1,16 +1,9 @@
1
1
  class UState::Query
2
2
  class LessEqual < Node
3
- def initialize(field, value)
4
- @field = field
5
- @value = value
6
- end
7
-
8
- def ===(state)
9
- x = state.send(@field) and x <= @value
10
- end
3
+ include Binarity
11
4
 
12
- def inspect
13
- inspect_helper @field, @value
5
+ def ===(state)
6
+ x = state.send(@a) and x <= @b
14
7
  end
15
8
  end
16
9
  end
@@ -0,0 +1,42 @@
1
+ module UState::Query::Narity
2
+ def initialize(*as)
3
+ @as = as.to_set
4
+ end
5
+
6
+ def ==(o)
7
+ self.class == o.class and
8
+ a == o.a
9
+ end
10
+
11
+ def as
12
+ @as
13
+ end
14
+
15
+ def as=(as)
16
+ @as = as.to_set
17
+ end
18
+
19
+ def children
20
+ @as
21
+ end
22
+
23
+ def children=(c)
24
+ @as = c.to_set
25
+ end
26
+
27
+ def inspect
28
+ inspect_helper *@as
29
+ end
30
+
31
+ def mass
32
+ @as.inject(1) do |sum, a|
33
+ sum + (a.mass rescue 1)
34
+ end
35
+ end
36
+
37
+ def to_s
38
+ "(" + @as.map do |a|
39
+ a.to_s
40
+ end.join(" #{self.class.to_s.split("::").last.downcase} ") + ")"
41
+ end
42
+ end
@@ -1,4 +1,6 @@
1
1
  class UState::Query
2
+ require 'set'
3
+
2
4
  class Node
3
5
  def indent(s, d = 1)
4
6
  (" " * d) + s.gsub("\n", "\n" + (" " * d))
@@ -11,5 +13,9 @@ class UState::Query
11
13
  def inspect
12
14
  inspect_helper
13
15
  end
16
+
17
+ def mass
18
+ 1
19
+ end
14
20
  end
15
21
  end
@@ -1,15 +1,32 @@
1
1
  class UState::Query
2
2
  class Not < Node
3
+ attr_reader :a
3
4
  def initialize(a)
4
5
  @a = a
5
6
  end
6
7
 
8
+ def ==(o)
9
+ o.kind_of? Not and @a == o.a
10
+ end
11
+
7
12
  def ===(state)
8
13
  not @a === state
9
14
  end
10
15
 
16
+ def children
17
+ [@a]
18
+ end
19
+
20
+ def children=(c)
21
+ raise ArgumentError unless c.size == 1
22
+ end
23
+
11
24
  def inspect
12
25
  inspect_helper @a
13
26
  end
27
+
28
+ def mass
29
+ 1 + @a.mass
30
+ end
14
31
  end
15
32
  end
@@ -1,16 +1,9 @@
1
1
  class UState::Query
2
2
  class NotEquals < Node
3
- def initialize(field, value)
4
- @field = field
5
- @value = value
6
- end
3
+ include Binarity
7
4
 
8
5
  def ===(state)
9
- state.send(@field) != @value
10
- end
11
-
12
- def inspect
13
- inspect_helper @field, @value
6
+ state.send(@a) != @b
14
7
  end
15
8
  end
16
9
  end
@@ -0,0 +1,113 @@
1
+ class UState::Query
2
+ class Optimizer
3
+ def self.[](node)
4
+ new.optimize node
5
+ end
6
+
7
+ def initialize
8
+ end
9
+
10
+ # Remove true and false from and/or
11
+ def collapse_boolean_literals(node)
12
+ map node do |n|
13
+ case n
14
+ when And
15
+ if n.children.any? do |c|
16
+ False === c
17
+ end
18
+ puts "Collapsing and(false)"
19
+ False.new
20
+ else
21
+ n.children.delete_if do |c|
22
+ True === c
23
+ end
24
+ n
25
+ end
26
+ when Or
27
+ if n.children.any? do |c|
28
+ True === c
29
+ end
30
+ puts "Collapsing or(true)"
31
+ True.new
32
+ else
33
+ n.children.delete_if do |c|
34
+ False === c
35
+ end
36
+ n
37
+ end
38
+ else
39
+ n
40
+ end
41
+ end
42
+ end
43
+
44
+ # Eliminate useless ands/ors
45
+ def deref_ands_ors(node)
46
+ map node do |n|
47
+ case n
48
+ when And
49
+ case n.children.size
50
+ when 0
51
+ True.new
52
+ when 1
53
+ n.children.first
54
+ else
55
+ n
56
+ end
57
+ when Or
58
+ case n.children.size
59
+ when 0
60
+ False.new
61
+ when 1
62
+ n.children.first
63
+ else
64
+ n
65
+ end
66
+ else
67
+ n
68
+ end
69
+ end
70
+ end
71
+
72
+ def optimize(node)
73
+ mass = node.mass + 1
74
+
75
+ while node.mass < mass
76
+ mass = node.mass
77
+ node = transform node
78
+ end
79
+
80
+ node
81
+ end
82
+
83
+ def map(node, &block)
84
+ # Transform node
85
+ puts "Map #{node}"
86
+ node = block[node] or raise RuntimeError "Mapped to nil"
87
+ puts "Node is now #{node}"
88
+
89
+ # Apply map to children
90
+ if node.respond_to? :children
91
+ puts "Node has children"
92
+ node.children.map! do |n|
93
+ map n, &block
94
+ end
95
+ end
96
+
97
+ # Return node
98
+ node
99
+ end
100
+
101
+ def traverse(node, &block)
102
+ block[node]
103
+ node.children.each do |child|
104
+ traverse child, &block
105
+ end
106
+ end
107
+
108
+ def transform(node)
109
+ puts "Transform ------------\n#{node.inspect}"
110
+ deref_ands_ors collapse_boolean_literals node
111
+ end
112
+ end
113
+ end
@@ -1,16 +1,11 @@
1
1
  class UState::Query
2
2
  class Or < Node
3
- def initialize(a,b)
4
- @a = a
5
- @b = b
6
- end
7
-
3
+ include Narity
4
+
8
5
  def ===(state)
9
- @a === state or @b === state
10
- end
11
-
12
- def inspect
13
- inspect_helper @a, @b
6
+ @as.any? do |a|
7
+ a === state
8
+ end
14
9
  end
15
10
  end
16
11
  end
@@ -1,10 +1,15 @@
1
1
  class UState::Query
2
2
  class True < Node
3
- def initialize
3
+ def ==(o)
4
+ o.kind_of? True
4
5
  end
5
6
 
6
7
  def ===(state)
7
8
  true
8
9
  end
10
+
11
+ def to_s
12
+ true
13
+ end
9
14
  end
10
15
  end
@@ -122,8 +122,14 @@ module UState
122
122
  # Partition a list of states by a field
123
123
  # Returns a hash of field_value => state
124
124
  def self.partition(states, field)
125
- states.inject(Hash.new { [] }) do |p, state|
126
- p[state.send(field)] << state
125
+ states.inject({}) do |p, state|
126
+ k = state.send field
127
+ if p.include? k
128
+ p[k] << state
129
+ else
130
+ p[k] = [state]
131
+ end
132
+ p
127
133
  end
128
134
  end
129
135
 
@@ -1,3 +1,3 @@
1
1
  module UState
2
- VERSION = '0.0.7'
2
+ VERSION = '0.0.8'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ustate-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-05 00:00:00.000000000 -07:00
13
- default_executable:
12
+ date: 2011-10-07 00:00:00.000000000Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: beefcake
17
- requirement: &17908120 !ruby/object:Gem::Requirement
16
+ requirement: &73289770 !ruby/object:Gem::Requirement
18
17
  none: false
19
18
  requirements:
20
19
  - - ! '>='
@@ -22,10 +21,10 @@ dependencies:
22
21
  version: 0.3.5
23
22
  type: :runtime
24
23
  prerelease: false
25
- version_requirements: *17908120
24
+ version_requirements: *73289770
26
25
  - !ruby/object:Gem::Dependency
27
26
  name: trollop
28
- requirement: &17907540 !ruby/object:Gem::Requirement
27
+ requirement: &73289260 !ruby/object:Gem::Requirement
29
28
  none: false
30
29
  requirements:
31
30
  - - ! '>='
@@ -33,10 +32,10 @@ dependencies:
33
32
  version: 1.16.2
34
33
  type: :runtime
35
34
  prerelease: false
36
- version_requirements: *17907540
35
+ version_requirements: *73289260
37
36
  - !ruby/object:Gem::Dependency
38
37
  name: mtrc
39
- requirement: &17907040 !ruby/object:Gem::Requirement
38
+ requirement: &73276480 !ruby/object:Gem::Requirement
40
39
  none: false
41
40
  requirements:
42
41
  - - ! '>='
@@ -44,7 +43,7 @@ dependencies:
44
43
  version: 0.0.4
45
44
  type: :runtime
46
45
  prerelease: false
47
- version_requirements: *17907040
46
+ version_requirements: *73276480
48
47
  description:
49
48
  email: aphyr@aphyr.com
50
49
  executables: []
@@ -52,53 +51,55 @@ extensions: []
52
51
  extra_rdoc_files: []
53
52
  files:
54
53
  - lib/ustate.rb
55
- - lib/ustate/query/not_equals.rb
56
- - lib/ustate/query/equals.rb
57
- - lib/ustate/query/false.rb
58
- - lib/ustate/query/node.rb
59
- - lib/ustate/query/true.rb
60
- - lib/ustate/query/or.rb
61
- - lib/ustate/query/approximately.rb
62
- - lib/ustate/query/greater.rb
63
- - lib/ustate/query/and.rb
64
- - lib/ustate/query/less.rb
65
- - lib/ustate/query/ast.rb
66
- - lib/ustate/query/less_equal.rb
67
- - lib/ustate/query/greater_equal.rb
68
- - lib/ustate/query/not.rb
54
+ - lib/ustate/graphite.rb
69
55
  - lib/ustate/query_string.treetop
70
- - lib/ustate/server.rb
56
+ - lib/ustate/message.rb
71
57
  - lib/ustate/auto_state.rb
72
- - lib/ustate/emailer.rb
73
- - lib/ustate/query.rb
74
- - lib/ustate/version.rb
75
- - lib/ustate/aggregator.rb
76
- - lib/ustate/state.rb
77
- - lib/ustate/reaper.rb
78
- - lib/ustate/graphite.rb
79
- - lib/ustate/client/query.rb
80
58
  - lib/ustate/metric_thread.rb
81
- - lib/ustate/query_string.rb
82
- - lib/ustate/server/index.rb
59
+ - lib/ustate/aggregator.rb
60
+ - lib/ustate/version.rb
61
+ - lib/ustate/server/graphite.rb
83
62
  - lib/ustate/server/backends.rb
84
- - lib/ustate/server/backends/tcp.rb
85
63
  - lib/ustate/server/backends/base.rb
86
- - lib/ustate/server/graphite.rb
64
+ - lib/ustate/server/backends/tcp.rb
65
+ - lib/ustate/server/index.rb
87
66
  - lib/ustate/server/connection.rb
67
+ - lib/ustate/reaper.rb
68
+ - lib/ustate/server.rb
69
+ - lib/ustate/dash.rb
70
+ - lib/ustate/state.rb
71
+ - lib/ustate/query.rb
72
+ - lib/ustate/client/query.rb
88
73
  - lib/ustate/client.rb
89
- - lib/ustate/message.rb
90
- - lib/ustate/dash/helper/renderer.rb
91
- - lib/ustate/dash/views/css.scss
74
+ - lib/ustate/dash/rack/static.rb
92
75
  - lib/ustate/dash/views/index.erubis
93
76
  - lib/ustate/dash/views/layout.erubis
94
- - lib/ustate/dash/rack/static.rb
77
+ - lib/ustate/dash/views/css.scss
95
78
  - lib/ustate/dash/controller/index.rb
96
79
  - lib/ustate/dash/controller/css.rb
97
80
  - lib/ustate/dash/config.rb
98
- - lib/ustate/dash.rb
81
+ - lib/ustate/dash/helper/renderer.rb
82
+ - lib/ustate/query/or.rb
83
+ - lib/ustate/query/not_equals.rb
84
+ - lib/ustate/query/ast.rb
85
+ - lib/ustate/query/optimizer.rb
86
+ - lib/ustate/query/less_equal.rb
87
+ - lib/ustate/query/equals.rb
88
+ - lib/ustate/query/binarity.rb
89
+ - lib/ustate/query/approximately.rb
90
+ - lib/ustate/query/true.rb
91
+ - lib/ustate/query/greater_equal.rb
92
+ - lib/ustate/query/node.rb
93
+ - lib/ustate/query/not.rb
94
+ - lib/ustate/query/less.rb
95
+ - lib/ustate/query/and.rb
96
+ - lib/ustate/query/false.rb
97
+ - lib/ustate/query/greater.rb
98
+ - lib/ustate/query/narity.rb
99
+ - lib/ustate/query_string.rb
100
+ - lib/ustate/emailer.rb
99
101
  - LICENSE
100
102
  - README.markdown
101
- has_rdoc: true
102
103
  homepage: https://github.com/aphyr/ustate
103
104
  licenses: []
104
105
  post_install_message:
@@ -119,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
120
  version: '0'
120
121
  requirements: []
121
122
  rubyforge_project: ustate-client
122
- rubygems_version: 1.6.2
123
+ rubygems_version: 1.8.10
123
124
  signing_key:
124
125
  specification_version: 3
125
126
  summary: Client for the distributed state server ustate.