Merge branch 'configurable_search_url'

This commit is contained in:
Akinori MUSHA 2017-05-25 07:05:13 +09:00
commit 53a046e843
3 changed files with 28 additions and 3 deletions

View file

@ -4,6 +4,8 @@ module Agents
class PeakDetectorAgent < Agent
cannot_be_scheduled!
DEFAULT_SEARCH_URL = 'https://twitter.com/search?q={q}'
description <<-MD
The Peak Detector Agent will watch for peaks in an event stream. When a peak is detected, the resulting Event will have a payload message of `message`. You can include extractions in the message, for example: `I saw a bar of: {{foo.bar}}`, have a look at the [Wiki](https://github.com/cantino/huginn/wiki/Formatting-Events-using-Liquid) for details.
@ -14,6 +16,8 @@ module Agents
You may set `window_duration_in_days` to change the default memory window length of `14` days, `min_peak_spacing_in_days` to change the default minimum peak spacing of `2` days (peaks closer together will be ignored), and `std_multiple` to change the default standard deviation threshold multiple of `3`.
You may set `min_events` for the minimal number of accumulated events before the agent starts detecting.
You may set `search_url` to point to something else than Twitter search, using the URI Template syntax defined in [RFC 6570](https://tools.ietf.org/html/rfc6570). Default value is `#{DEFAULT_SEARCH_URL}` where `{q}` will be replaced with group name.
MD
event_description <<-MD
@ -31,6 +35,15 @@ module Agents
unless options['expected_receive_period_in_days'].present? && options['message'].present? && options['value_path'].present? && options['min_events'].present?
errors.add(:base, "expected_receive_period_in_days, value_path, min_events and message are required")
end
begin
tmpl = search_url
rescue => e
errors.add(:base, "search_url must be a valid URI template: #{e.message}")
else
unless tmpl.keys.include?('q')
errors.add(:base, "search_url must include a variable named 'q'")
end
end
end
def default_options
@ -55,6 +68,10 @@ module Agents
end
end
def search_url
Addressable::Template.new(options[:search_url].presence || DEFAULT_SEARCH_URL)
end
private
def check_for_peak(group, event)

View file

@ -1,12 +1,12 @@
<% content_for :head do %>
<%= javascript_include_tag "graphing" %>
<% end %>
<% if @agent.memory[:data] && @agent.memory[:data].length > 0 %>
<h3>Recent Trends</h3>
<% search_url = @agent.search_url %>
<% @agent.memory[:data].each.with_index do |(group_name, data), index| %>
<div class="filter-group counts">
<div class='filter'><%= link_to group_name.to_s, "https://twitter.com/search?q=#{CGI::escape group_name.to_s}", :target => "blank" %></div>
<div class='filter'><%= link_to group_name.to_s, search_url.expand(q: group_name.to_s).to_s, target: '_blank' %></div>
<div class="chart-container group-<%= index.to_s %>">
<div class="y-axis"></div>

View file

@ -9,7 +9,7 @@ describe Agents::PeakDetectorAgent do
'group_by_path' => "filter",
'value_path' => "count",
'message' => "A peak was found",
'min_events' => "4",
'min_events' => "4"
}
}
@ -99,5 +99,13 @@ describe Agents::PeakDetectorAgent do
@agent.options['value_path'] = ""
expect(@agent).not_to be_valid
end
it "should validate search_url" do
@agent.options['search_url'] = 'https://twitter.com/'
expect(@agent).not_to be_valid
@agent.options['search_url'] = 'https://twitter.com/{q}'
expect(@agent).to be_valid
end
end
end