1
0

event_formatting_agent.rb 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. module Agents
  2. class EventFormattingAgent < Agent
  3. cannot_be_scheduled!
  4. can_dry_run!
  5. description <<-MD
  6. The Event Formatting Agent allows you to format incoming Events, adding new fields as needed.
  7. For example, here is a possible Event:
  8. {
  9. "high": {
  10. "celsius": "18",
  11. "fahreinheit": "64"
  12. },
  13. "date": {
  14. "epoch": "1357959600",
  15. "pretty": "10:00 PM EST on January 11, 2013"
  16. },
  17. "conditions": "Rain showers",
  18. "data": "This is some data"
  19. }
  20. You may want to send this event to another Agent, for example a Twilio Agent, which expects a `message` key.
  21. You can use an Event Formatting Agent's `instructions` setting to do this in the following way:
  22. "instructions": {
  23. "message": "Today's conditions look like {{conditions}} with a high temperature of {{high.celsius}} degrees Celsius.",
  24. "subject": "{{data}}",
  25. "created_at": "{{created_at}}"
  26. }
  27. Names here like `conditions`, `high` and `data` refer to the corresponding values in the Event hash.
  28. The special key `created_at` refers to the timestamp of the Event, which can be reformatted by the `date` filter, like `{{created_at | date:"at %I:%M %p" }}`.
  29. The upstream agent of each received event is accessible via the key `agent`, which has the following attributes: #{''.tap { |s| s << AgentDrop.instance_methods(false).map { |m| "`#{m}`" }.join(', ') }}.
  30. Have a look at the [Wiki](https://github.com/huginn/huginn/wiki/Formatting-Events-using-Liquid) to learn more about liquid templating.
  31. Events generated by this possible Event Formatting Agent will look like:
  32. {
  33. "message": "Today's conditions look like Rain showers with a high temperature of 18 degrees Celsius.",
  34. "subject": "This is some data"
  35. }
  36. In `matchers` setting you can perform regular expression matching against contents of events and expand the match data for use in `instructions` setting. Here is an example:
  37. {
  38. "matchers": [
  39. {
  40. "path": "{{date.pretty}}",
  41. "regexp": "\\A(?<time>\\d\\d:\\d\\d [AP]M [A-Z]+)",
  42. "to": "pretty_date"
  43. }
  44. ]
  45. }
  46. This virtually merges the following hash into the original event hash:
  47. "pretty_date": {
  48. "time": "10:00 PM EST",
  49. "0": "10:00 PM EST on January 11, 2013"
  50. "1": "10:00 PM EST"
  51. }
  52. So you can use it in `instructions` like this:
  53. "instructions": {
  54. "message": "Today's conditions look like {{conditions}} with a high temperature of {{high.celsius}} degrees Celsius according to the forecast at {{pretty_date.time}}.",
  55. "subject": "{{data}}"
  56. }
  57. If you want to retain original contents of events and only add new keys, then set `mode` to `merge`, otherwise set it to `clean`.
  58. To CGI escape output (for example when creating a link), use the Liquid `uri_escape` filter, like so:
  59. {
  60. "message": "A peak was on Twitter in {{group_by}}. Search: https://twitter.com/search?q={{group_by | uri_escape}}"
  61. }
  62. MD
  63. event_description do
  64. "Events will have the following fields%s:\n\n %s" % [
  65. case options['mode'].to_s
  66. when 'merge'
  67. ', merged with the original contents'
  68. when /\{/
  69. ', conditionally merged with the original contents'
  70. end,
  71. Utils.pretty_print(Hash[options['instructions'].keys.map { |key|
  72. [key, "..."]
  73. }])
  74. ]
  75. end
  76. def validate_options
  77. errors.add(:base, "instructions and mode need to be present.") unless options['instructions'].present? && options['mode'].present?
  78. if options['mode'].present? && !options['mode'].to_s.include?('{{') && !%[clean merge].include?(options['mode'].to_s)
  79. errors.add(:base, "mode must be 'clean' or 'merge'")
  80. end
  81. validate_matchers
  82. end
  83. def default_options
  84. {
  85. 'instructions' => {
  86. 'message' => "You received a text {{text}} from {{fields.from}}",
  87. 'agent' => "{{agent.type}}",
  88. 'some_other_field' => "Looks like the weather is going to be {{fields.weather}}"
  89. },
  90. 'matchers' => [],
  91. 'mode' => "clean",
  92. }
  93. end
  94. def working?
  95. !recent_error_logs?
  96. end
  97. def receive(incoming_events)
  98. matchers = compiled_matchers
  99. incoming_events.each do |event|
  100. interpolate_with(event) do
  101. apply_compiled_matchers(matchers, event) do
  102. formatted_event = interpolated['mode'].to_s == "merge" ? event.payload.dup : {}
  103. formatted_event.merge! interpolated['instructions']
  104. create_event payload: formatted_event
  105. end
  106. end
  107. end
  108. end
  109. private
  110. def validate_matchers
  111. matchers = options['matchers'] or return
  112. unless matchers.is_a?(Array)
  113. errors.add(:base, "matchers must be an array if present")
  114. return
  115. end
  116. matchers.each do |matcher|
  117. unless matcher.is_a?(Hash)
  118. errors.add(:base, "each matcher must be a hash")
  119. next
  120. end
  121. regexp, path, to = matcher.values_at(*%w[regexp path to])
  122. if regexp.present?
  123. begin
  124. Regexp.new(regexp)
  125. rescue
  126. errors.add(:base, "bad regexp found in matchers: #{regexp}")
  127. end
  128. else
  129. errors.add(:base, "regexp is mandatory for a matcher and must be a string")
  130. end
  131. errors.add(:base, "path is mandatory for a matcher and must be a string") if !path.present?
  132. errors.add(:base, "to must be a string if present in a matcher") if to.present? && !to.is_a?(String)
  133. end
  134. end
  135. def compiled_matchers
  136. if matchers = options['matchers']
  137. matchers.map { |matcher|
  138. regexp, path, to = matcher.values_at(*%w[regexp path to])
  139. [Regexp.new(regexp), path, to]
  140. }
  141. end
  142. end
  143. def apply_compiled_matchers(matchers, event, &block)
  144. return yield if matchers.nil?
  145. # event.payload.dup does not work; HashWithIndifferentAccess is
  146. # a source of trouble here.
  147. hash = {}.update(event.payload)
  148. matchers.each do |re, path, to|
  149. m = re.match(interpolate_string(path, hash)) or next
  150. mhash =
  151. if to
  152. case value = hash[to]
  153. when Hash
  154. value
  155. else
  156. hash[to] = {}
  157. end
  158. else
  159. hash
  160. end
  161. m.size.times do |i|
  162. mhash[i.to_s] = m[i]
  163. end
  164. m.names.each do |name|
  165. mhash[name] = m[name]
  166. end
  167. end
  168. interpolate_with(hash, &block)
  169. end
  170. end
  171. end