peak_detector_agent_spec.rb 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. @agent.memory['data']['something'].map(&:first).should == [1, 2]
  23. @agent.memory['data']['something'].last.last.should be_within(10).of((100 - 1).hours.ago.to_i)
  24. @agent.memory['data']['else'].first.first.should == 3
  25. @agent.memory['data']['else'].first.last.should 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. @agent.memory['data']['no_group'].map(&:first).should == [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. @agent.memory['data']['something'].map(&:first).should == [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. lambda {
  50. @agent.receive([event])
  51. }.should change { @agent.events.count }.by( index == 6 ? 1 : 0 )
  52. end
  53. @agent.events.last.payload['peak'].should == 15.0
  54. @agent.memory['peaks']['something'].length.should == 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. @agent.memory['peaks']['something'].length.should == 2
  62. end
  63. end
  64. describe "validation" do
  65. before do
  66. @agent.should be_valid
  67. end
  68. it "should validate presence of message" do
  69. @agent.options['message'] = nil
  70. @agent.should_not 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. @agent.should_not be_valid
  75. end
  76. it "should validate presence of value_path" do
  77. @agent.options['value_path'] = ""
  78. @agent.should_not be_valid
  79. end
  80. end
  81. end