add negation to the trigger agent

Andrew Cantino 11 年之前
父節點
當前提交
8fc2c1a74a
共有 2 個文件被更改,包括 39 次插入1 次删除
  1. 5 1
      app/models/agents/trigger_agent.rb
  2. 34 0
      spec/models/agents/trigger_agent_spec.rb

+ 5 - 1
app/models/agents/trigger_agent.rb

@@ -2,7 +2,7 @@ module Agents
2 2
   class TriggerAgent < Agent
3 3
     cannot_be_scheduled!
4 4
 
5
-    VALID_COMPARISON_TYPES = %w[regex field<value field<=value field==value field>=value field>value]
5
+    VALID_COMPARISON_TYPES = %w[regex !regex field<value field<=value field==value field!=value field>=value field>value]
6 6
 
7 7
     description <<-MD
8 8
       Use a TriggerAgent to watch for a specific value in an Event payload.
@@ -52,6 +52,8 @@ module Agents
52 52
           case rule[:type]
53 53
             when "regex"
54 54
               value_at_path.to_s =~ Regexp.new(rule[:value], Regexp::IGNORECASE)
55
+            when "!regex"
56
+              value_at_path.to_s !~ Regexp.new(rule[:value], Regexp::IGNORECASE)
55 57
             when "field>value"
56 58
               value_at_path.to_f > rule[:value].to_f
57 59
             when "field>=value"
@@ -62,6 +64,8 @@ module Agents
62 64
               value_at_path.to_f <= rule[:value].to_f
63 65
             when "field==value"
64 66
               value_at_path.to_s == rule[:value].to_s
67
+            when "field!=value"
68
+              value_at_path.to_s != rule[:value].to_s
65 69
             else
66 70
               raise "Invalid :type of #{rule[:type]} in TriggerAgent##{id}"
67 71
           end

+ 34 - 0
spec/models/agents/trigger_agent_spec.rb

@@ -71,6 +71,24 @@ describe Agents::TriggerAgent do
71 71
       }.should change { Event.count }.by(1)
72 72
     end
73 73
 
74
+    it "handles negated regex" do
75
+      @event.payload[:foo]["bar"][:baz] = "a2b"
76
+      @checker.options[:rules][0] = {
77
+                                      :type => "!regex",
78
+                                      :value => "a\\db",
79
+                                      :path => "foo.bar.baz",
80
+                                    }
81
+
82
+      lambda {
83
+        @checker.receive([@event])
84
+      }.should_not change { Event.count }
85
+
86
+      @event.payload[:foo]["bar"][:baz] = "a22b"
87
+      lambda {
88
+        @checker.receive([@event])
89
+      }.should change { Event.count }.by(1)
90
+    end
91
+
74 92
     it "puts can extract values into the message based on paths" do
75 93
       @checker.receive([@event])
76 94
       Event.last.payload[:message].should == "I saw 'a2b' from Joe"
@@ -106,6 +124,22 @@ describe Agents::TriggerAgent do
106 124
       }.should change { Event.count }.by(1)
107 125
     end
108 126
 
127
+    it "handles negated comparisons" do
128
+      @event.payload[:foo]["bar"][:baz] = "hello world"
129
+      @checker.options[:rules].first[:type] = "field!=value"
130
+      @checker.options[:rules].first[:value] = "hello world"
131
+
132
+      lambda {
133
+        @checker.receive([@event])
134
+      }.should_not change { Event.count }
135
+
136
+      @checker.options[:rules].first[:value] = "hello there"
137
+
138
+      lambda {
139
+        @checker.receive([@event])
140
+      }.should change { Event.count }.by(1)
141
+    end
142
+
109 143
     it "does fine without dots in the path" do
110 144
       @event.payload = { :hello => "world" }
111 145
       @checker.options[:rules].first[:type] = "field==value"