peak_detector_agent_spec.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. require 'spec_helper'
  2. describe Agents::PeakDetectorAgent do
  3. before do
  4. @valid_params = {
  5. 'name' => "my peak detector agent",
  6. 'options' => {
  7. 'expected_receive_period_in_days' => "2",
  8. 'group_by_path' => "filter",
  9. 'value_path' => "count",
  10. 'message' => "A peak was found"
  11. }
  12. }
  13. @agent = Agents::PeakDetectorAgent.new(@valid_params)
  14. @agent.user = users(:bob)
  15. @agent.save!
  16. end
  17. describe "#receive" do
  18. it "tracks and groups by the group_by_path" do
  19. events = build_events(:keys => ['count', 'filter'],
  20. :values => [[1, "something"], [2, "something"], [3, "else"]])
  21. @agent.receive events
  22. expect(@agent.memory['data']['something'].map(&:first)).to eq([1, 2])
  23. expect(@agent.memory['data']['something'].last.last).to be_within(10).of((100 - 1).hours.ago.to_i)
  24. expect(@agent.memory['data']['else'].first.first).to eq(3)
  25. expect(@agent.memory['data']['else'].first.last).to be_within(10).of((100 - 2).hours.ago.to_i)
  26. end
  27. it "works without a group_by_path as well" do
  28. @agent.options['group_by_path'] = ""
  29. events = build_events(:keys => ['count'], :values => [[1], [2]])
  30. @agent.receive events
  31. expect(@agent.memory['data']['no_group'].map(&:first)).to eq([1, 2])
  32. end
  33. it "keeps a rolling window of data" do
  34. @agent.options['window_duration_in_days'] = 5/24.0
  35. @agent.receive build_events(:keys => ['count'],
  36. :values => [1, 2, 3, 4, 5, 6, 7, 8].map {|i| [i]},
  37. :pattern => { 'filter' => "something" })
  38. expect(@agent.memory['data']['something'].map(&:first)).to eq([4, 5, 6, 7, 8])
  39. end
  40. it "finds peaks" do
  41. build_events(:keys => ['count'],
  42. :values => [5, 6,
  43. 4, 5,
  44. 4, 5,
  45. 15, 11, # peak
  46. 8, 50, # ignored because it's too close to the first peak
  47. 4, 5].map {|i| [i]},
  48. :pattern => { 'filter' => "something" }).each.with_index do |event, index|
  49. expect {
  50. @agent.receive([event])
  51. }.to change { @agent.events.count }.by( index == 6 ? 1 : 0 )
  52. end
  53. expect(@agent.events.last.payload['peak']).to eq(15.0)
  54. expect(@agent.memory['peaks']['something'].length).to eq(1)
  55. end
  56. it "keeps a rolling window of peaks" do
  57. @agent.options['min_peak_spacing_in_days'] = 1/24.0
  58. @agent.receive build_events(:keys => ['count'],
  59. :values => [1, 1, 1, 1, 1, 1, 10, 1, 1, 1, 1, 1, 1, 1, 10, 1].map {|i| [i]},
  60. :pattern => { 'filter' => "something" })
  61. expect(@agent.memory['peaks']['something'].length).to eq(2)
  62. end
  63. end
  64. describe "validation" do
  65. before do
  66. expect(@agent).to be_valid
  67. end
  68. it "should validate presence of message" do
  69. @agent.options['message'] = nil
  70. expect(@agent).not_to be_valid
  71. end
  72. it "should validate presence of expected_receive_period_in_days" do
  73. @agent.options['expected_receive_period_in_days'] = ""
  74. expect(@agent).not_to be_valid
  75. end
  76. it "should validate presence of value_path" do
  77. @agent.options['value_path'] = ""
  78. expect(@agent).not_to be_valid
  79. end
  80. end
  81. end