peak_detector_agent_spec.rb 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. require 'rails_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. 'min_events' => "4"
  12. }
  13. }
  14. @agent = Agents::PeakDetectorAgent.new(@valid_params)
  15. @agent.user = users(:bob)
  16. @agent.save!
  17. end
  18. describe "#receive" do
  19. it "tracks and groups by the group_by_path" do
  20. events = build_events(:keys => ['count', 'filter'],
  21. :values => [[1, "something"], [2, "something"], [3, "else"]])
  22. @agent.receive events
  23. expect(@agent.memory['data']['something'].map(&:first)).to eq([1, 2])
  24. expect(@agent.memory['data']['something'].last.last).to be_within(10).of((100 - 1).hours.ago.to_i)
  25. expect(@agent.memory['data']['else'].first.first).to eq(3)
  26. expect(@agent.memory['data']['else'].first.last).to be_within(10).of((100 - 2).hours.ago.to_i)
  27. end
  28. it "works without a group_by_path as well" do
  29. @agent.options['group_by_path'] = ""
  30. events = build_events(:keys => ['count'], :values => [[1], [2]])
  31. @agent.receive events
  32. expect(@agent.memory['data']['no_group'].map(&:first)).to eq([1, 2])
  33. end
  34. it "keeps a rolling window of data" do
  35. @agent.options['window_duration_in_days'] = 5/24.0
  36. @agent.receive build_events(:keys => ['count'],
  37. :values => [1, 2, 3, 4, 5, 6, 7, 8].map {|i| [i]},
  38. :pattern => { 'filter' => "something" })
  39. expect(@agent.memory['data']['something'].map(&:first)).to eq([4, 5, 6, 7, 8])
  40. end
  41. it "finds peaks" do
  42. build_events(:keys => ['count'],
  43. :values => [5, 6,
  44. 4, 5,
  45. 4, 5,
  46. 15, 11, # peak
  47. 8, 50, # ignored because it's too close to the first peak
  48. 4, 5].map {|i| [i]},
  49. :pattern => { 'filter' => "something" }).each.with_index do |event, index|
  50. expect {
  51. @agent.receive([event])
  52. }.to change { @agent.events.count }.by( index == 6 ? 1 : 0 )
  53. end
  54. expect(@agent.events.last.payload['peak']).to eq(15.0)
  55. expect(@agent.memory['peaks']['something'].length).to eq(1)
  56. end
  57. it "keeps a rolling window of peaks" do
  58. @agent.options['min_peak_spacing_in_days'] = 1/24.0
  59. @agent.receive build_events(:keys => ['count'],
  60. :values => [1, 1, 1, 1, 1, 1, 10, 1, 1, 1, 1, 1, 1, 1, 10, 1].map {|i| [i]},
  61. :pattern => { 'filter' => "something" })
  62. expect(@agent.memory['peaks']['something'].length).to eq(2)
  63. end
  64. it 'waits and accumulates min events before triggering for peaks' do
  65. @agent.options['min_peak_spacing_in_days'] = 1/24.0
  66. @agent.options['min_events'] = '10'
  67. @agent.receive build_events(:keys => ['count'],
  68. :values => [1, 1, 1, 1, 1, 1, 10, 1, 1, 1, 1, 1, 1, 1, 10, 1].map {|i| [i]},
  69. :pattern => { 'filter' => "something" })
  70. expect(@agent.memory['peaks']['something'].length).to eq(1)
  71. end
  72. it 'raised an exception if the extracted data can not be casted to a float' do
  73. event = Event.new(payload: {count: ["not working"]})
  74. expect {
  75. @agent.receive([event])
  76. }.to raise_error(NoMethodError, /undefined method `to_f'/)
  77. end
  78. end
  79. describe "validation" do
  80. before do
  81. expect(@agent).to be_valid
  82. end
  83. it "should validate presence of message" do
  84. @agent.options['message'] = nil
  85. expect(@agent).not_to be_valid
  86. end
  87. it "should validate presence of expected_receive_period_in_days" do
  88. @agent.options['expected_receive_period_in_days'] = ""
  89. expect(@agent).not_to be_valid
  90. end
  91. it "should validate presence of value_path" do
  92. @agent.options['value_path'] = ""
  93. expect(@agent).not_to be_valid
  94. end
  95. it "should validate search_url" do
  96. @agent.options['search_url'] = 'https://twitter.com/'
  97. expect(@agent).not_to be_valid
  98. @agent.options['search_url'] = 'https://twitter.com/{q}'
  99. expect(@agent).to be_valid
  100. end
  101. end
  102. end