peak_detector_agent.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. require 'pp'
  2. module Agents
  3. class PeakDetectorAgent < Agent
  4. cannot_be_scheduled!
  5. description <<-MD
  6. Use a PeakDetectorAgent to 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.
  7. The `value_path` value is a [JSONPaths](http://goessner.net/articles/JsonPath/) to the value of interest. `group_by_path` is a hash path that will be used to group values, if present.
  8. Set `expected_receive_period_in_days` to the maximum amount of time that you'd expect to pass between Events being received by this Agent.
  9. You may set `window_duration_in_days` to change the default memory window length of `14` days,
  10. `min_peak_spacing_in_days` to change the default minimum peak spacing of `2` days (peaks closer together will be ignored), and
  11. `std_multiple` to change the default standard deviation threshold multiple of `3`.
  12. MD
  13. event_description <<-MD
  14. Events look like:
  15. {
  16. "message": "Your message",
  17. "peak": 6,
  18. "peak_time": 3456789242,
  19. "grouped_by": "something"
  20. }
  21. MD
  22. def validate_options
  23. unless options['expected_receive_period_in_days'].present? && options['message'].present? && options['value_path'].present?
  24. errors.add(:base, "expected_receive_period_in_days, value_path, and message are required")
  25. end
  26. end
  27. def default_options
  28. {
  29. 'expected_receive_period_in_days' => "2",
  30. 'group_by_path' => "filter",
  31. 'value_path' => "count",
  32. 'message' => "A peak of {{count}} was found in {{filter}}"
  33. }
  34. end
  35. def working?
  36. last_receive_at && last_receive_at > interpolated['expected_receive_period_in_days'].to_i.days.ago && !recent_error_logs?
  37. end
  38. def receive(incoming_events)
  39. incoming_events.sort_by(&:created_at).each do |event|
  40. group = group_for(event)
  41. remember group, event
  42. check_for_peak group, event
  43. end
  44. end
  45. private
  46. def check_for_peak(group, event)
  47. memory['peaks'] ||= {}
  48. memory['peaks'][group] ||= []
  49. if memory['data'][group].length > 4 && (memory['peaks'][group].empty? || memory['peaks'][group].last < event.created_at.to_i - peak_spacing)
  50. average_value, standard_deviation = stats_for(group, :skip_last => 1)
  51. newest_value, newest_time = memory['data'][group][-1].map(&:to_f)
  52. if newest_value > average_value + std_multiple * standard_deviation
  53. memory['peaks'][group] << newest_time
  54. memory['peaks'][group].reject! { |p| p <= newest_time - window_duration }
  55. create_event :payload => { 'message' => interpolated(event)['message'], 'peak' => newest_value, 'peak_time' => newest_time, 'grouped_by' => group.to_s }
  56. end
  57. end
  58. end
  59. def stats_for(group, options = {})
  60. data = memory['data'][group].map { |d| d.first.to_f }
  61. data = data[0...(data.length - (options[:skip_last] || 0))]
  62. length = data.length.to_f
  63. mean = 0
  64. mean_variance = 0
  65. data.each do |value|
  66. mean += value
  67. end
  68. mean /= length
  69. data.each do |value|
  70. variance = (value - mean)**2
  71. mean_variance += variance
  72. end
  73. mean_variance /= length
  74. standard_deviation = Math.sqrt(mean_variance)
  75. [mean, standard_deviation]
  76. end
  77. def window_duration
  78. if interpolated['window_duration'].present? # The older option
  79. interpolated['window_duration'].to_i
  80. else
  81. (interpolated['window_duration_in_days'] || 14).to_f.days
  82. end
  83. end
  84. def std_multiple
  85. (interpolated['std_multiple'] || 3).to_f
  86. end
  87. def peak_spacing
  88. if interpolated['peak_spacing'].present? # The older option
  89. interpolated['peak_spacing'].to_i
  90. else
  91. (interpolated['min_peak_spacing_in_days'] || 2).to_f.days
  92. end
  93. end
  94. def group_for(event)
  95. ((interpolated['group_by_path'].present? && Utils.value_at(event.payload, interpolated['group_by_path'])) || 'no_group')
  96. end
  97. def remember(group, event)
  98. memory['data'] ||= {}
  99. memory['data'][group] ||= []
  100. memory['data'][group] << [ Utils.value_at(event.payload, interpolated['value_path']), event.created_at.to_i ]
  101. cleanup group
  102. end
  103. def cleanup(group)
  104. newest_time = memory['data'][group].last.last
  105. memory['data'][group].reject! { |value, time| time <= newest_time - window_duration }
  106. end
  107. end
  108. end