peak_detector_agent_spec.rb 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. require 'spec_helper'
  2. require 'models/concerns/liquid_interpolatable'
  3. describe Agents::PeakDetectorAgent do
  4. before do
  5. @valid_params = {
  6. 'name' => "my peak detector agent",
  7. 'options' => {
  8. 'expected_receive_period_in_days' => "2",
  9. 'group_by_path' => "filter",
  10. 'value_path' => "count",
  11. 'message' => "A peak was found"
  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. @agent.memory['data']['something'].map(&:first).should == [1, 2]
  24. @agent.memory['data']['something'].last.last.should be_within(10).of((100 - 1).hours.ago.to_i)
  25. @agent.memory['data']['else'].first.first.should == 3
  26. @agent.memory['data']['else'].first.last.should 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. @agent.memory['data']['no_group'].map(&:first).should == [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. @agent.memory['data']['something'].map(&:first).should == [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. lambda {
  51. @agent.receive([event])
  52. }.should change { @agent.events.count }.by( index == 6 ? 1 : 0 )
  53. end
  54. @agent.events.last.payload['peak'].should == 15.0
  55. @agent.memory['peaks']['something'].length.should == 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. @agent.memory['peaks']['something'].length.should == 2
  63. end
  64. end
  65. describe "validation" do
  66. before do
  67. @agent.should be_valid
  68. end
  69. it "should validate presence of message" do
  70. @agent.options['message'] = nil
  71. @agent.should_not be_valid
  72. end
  73. it "should validate presence of expected_receive_period_in_days" do
  74. @agent.options['expected_receive_period_in_days'] = ""
  75. @agent.should_not be_valid
  76. end
  77. it "should validate presence of value_path" do
  78. @agent.options['value_path'] = ""
  79. @agent.should_not be_valid
  80. end
  81. end
  82. end