peak_detector_agent.rb 4.6 KB

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