<commit_before># encoding: UTF-8
require_relative 'spec_helper'

describe Fluent::CalcOutput do
  before { Fluent::Test.setup }
  CONFIG = %[
    input_key message
  ]
  let(:tag) { 'foo.bar' }
  let(:driver) { Fluent::Test::OutputTestDriver.new(Fluent::CalcOutput, tag).configure(config) }

  describe 'test configure' do
    describe 'bad configuration' do
      context 'invalid aggregate' do
        let(:config) do
          CONFIG + %[
          aggregate foo
          ]
        end
        it { expect { driver }.to raise_error(Fluent::ConfigError) }
      end

      context 'no tag for aggregate all' do
        let(:config) do
          CONFIG + %[
          aggregate all
          ]
        end
        it { expect { driver }.to raise_error(Fluent::ConfigError) }
      end
    end

    describe 'good configuration' do
      context "nothing" do
        let(:config) { '' }
        it { expect { driver }.to_not raise_error(Fluent::ConfigError) }
      end

      context 'sum/max/min/avg' do
        let(:config) do
          CONFIG + %[
            sum _count$
            max _max$
            min _min$
            avg _avg$
          ]
        end
        it { expect { driver }.to_not raise_error(Fluent::ConfigError) }
      end

      context "check default" do
        subject { driver.instance }
        let(:config) { CONFIG }
        its(:interval) { should == 5 }
        its(:tag) { should be_nil }
        its(:add_tag_prefix) { should == 'calc' }
        its(:aggregate) { should == 'tag' }
      end
    end
  end

  describe 'test emit' do
    let(:time) { Time.now.to_i }
    let(:messages) do
      [
        {"4xx_count"=>1,"5xx_count"=>2,"reqtime_max"=>6,"reqtime_min"=>1,"reqtime_avg"=>3},
        {"4xx_count"=>2,"5xx_count"=>2,"reqtime_max"=>5,"reqtime_min"=>2,"reqtime_avg"=>2},
        {"4xx_count"=>3,"5xx_count"=>2,"reqtime_max"=>1,"reqtime_min"=>3,"reqtime_avg"=>4},
      ]
    end
    let(:emit) do
      driver.run { messages.each {|message| driver.emit(message, time) } }
      driver.instance.flush_emit(0)
    end

    context 'interval' do
      pending
    end

    context 'sum/max/min/avg' do
      let(:config) do
        CONFIG + %[
          sum _count$
          max _max$
          min _min$
          avg _avg$
        ]
      end
      before do
        Fluent::Engine.stub(:now).and_return(time)
        Fluent::Engine.should_receive(:emit).with("calc.#{tag}", time, {
          "4xx_count"=>6,"5xx_count"=>6,"reqtime_max"=>6,"reqtime_min"=>1,"reqtime_avg"=>3.0
        })
      end
      it { emit }
    end

    context 'tag' do
      let(:config) do
        CONFIG + %[
          tag foo
        ]
      end
      before do
        Fluent::Engine.stub(:now).and_return(time)
        Fluent::Engine.should_receive(:emit).with("foo", time, {})
      end
      it { emit }
    end

    context 'add_tag_prefix' do
      let(:config) do
        CONFIG + %[
          add_tag_prefix foo
        ]
      end
      before do
        Fluent::Engine.stub(:now).and_return(time)
        Fluent::Engine.should_receive(:emit).with("foo.#{tag}", time, {})
      end
      it { emit }
    end

    context 'aggregate all' do
      let(:config) do
        CONFIG + %[
          aggregate all
          tag foo
          sum _count$
          max _max$
          min _min$
          avg _avg$
        ]
      end
      before do
        Fluent::Engine.stub(:now).and_return(time)
        Fluent::Engine.should_receive(:emit).with("foo", time, { 
          "4xx_count"=>6,"5xx_count"=>6,"reqtime_max"=>6,"reqtime_min"=>1,"reqtime_avg"=>3.0
        })
      end
      it { emit }
    end
  end
end
<commit_msg>aggregate all test<commit_after># encoding: UTF-8
require_relative 'spec_helper'

class Fluent::Test::OutputTestDriver
  def emit_with_tag(record, time=Time.now, tag = nil)
    @tag = tag if tag
    emit(record, time)
  end
end

describe Fluent::CalcOutput do
  before { Fluent::Test.setup }
  CONFIG = %[]
  let(:tag) { 'foo.bar' }
  let(:driver) { Fluent::Test::OutputTestDriver.new(Fluent::CalcOutput, tag).configure(config) }

  describe 'test configure' do
    describe 'bad configuration' do
      context 'invalid aggregate' do
        let(:config) do
          CONFIG + %[
            aggregate foo
          ]
        end
        it { expect { driver }.to raise_error(Fluent::ConfigError) }
      end

      context 'no tag for aggregate all' do
        let(:config) do
          CONFIG + %[
            aggregate all
          ]
        end
        it { expect { driver }.to raise_error(Fluent::ConfigError) }
      end
    end

    describe 'good configuration' do
      context "nothing" do
        let(:config) { '' }
        it { expect { driver }.to_not raise_error(Fluent::ConfigError) }
      end

      context 'sum/max/min/avg' do
        let(:config) do
          CONFIG + %[
            sum _count$
            max _max$
            min _min$
            avg _avg$
          ]
        end
        it { expect { driver }.to_not raise_error(Fluent::ConfigError) }
      end

      context "check default" do
        subject { driver.instance }
        let(:config) { CONFIG }
        its(:interval) { should == 5 }
        its(:tag) { should be_nil }
        its(:add_tag_prefix) { should == 'calc' }
        its(:aggregate) { should == 'tag' }
      end
    end
  end

  describe 'test emit' do
    let(:time) { Time.now.to_i }
    let(:messages) do
      [
        {"4xx_count"=>1,"5xx_count"=>2,"reqtime_max"=>6,"reqtime_min"=>1,"reqtime_avg"=>3},
        {"4xx_count"=>2,"5xx_count"=>2,"reqtime_max"=>5,"reqtime_min"=>2,"reqtime_avg"=>2},
        {"4xx_count"=>3,"5xx_count"=>2,"reqtime_max"=>1,"reqtime_min"=>3,"reqtime_avg"=>4},
      ]
    end
    let(:emit) do
      driver.run { messages.each {|message| driver.emit(message, time) } }
      driver.instance.flush_emit(0)
    end

    context 'interval' do
      pending
    end

    context 'sum/max/min/avg' do
      let(:config) do
        CONFIG + %[
          sum _count$
          max _max$
          min _min$
          avg _avg$
        ]
      end
      before do
        Fluent::Engine.stub(:now).and_return(time)
        Fluent::Engine.should_receive(:emit).with("calc.#{tag}", time, {
          "4xx_count"=>6,"5xx_count"=>6,"reqtime_max"=>6,"reqtime_min"=>1,"reqtime_avg"=>3.0
        })
      end
      it { emit }
    end

    context 'tag' do
      let(:config) do
        CONFIG + %[
          tag foo
        ]
      end
      before do
        Fluent::Engine.stub(:now).and_return(time)
        Fluent::Engine.should_receive(:emit).with("foo", time, {})
      end
      it { emit }
    end

    context 'add_tag_prefix' do
      let(:config) do
        CONFIG + %[
          add_tag_prefix foo
        ]
      end
      before do
        Fluent::Engine.stub(:now).and_return(time)
        Fluent::Engine.should_receive(:emit).with("foo.#{tag}", time, {})
      end
      it { emit }
    end

    context 'aggregate' do
      let(:emit) do
        driver.run { messages.each {|message| driver.emit_with_tag(message, time, 'foo.bar') } }
        driver.run { messages.each {|message| driver.emit_with_tag(message, time, 'foo.bar2') } }
        driver.instance.flush_emit(0)
      end

      context 'aggregate all' do
        let(:config) do
          CONFIG + %[
          aggregate all
          tag foo
          sum _count$
          max _max$
          min _min$
          avg _avg$
        ]
        end
        before do
          Fluent::Engine.stub(:now).and_return(time)
          Fluent::Engine.should_receive(:emit).with("foo", time, {
            "4xx_count"=>12,"5xx_count"=>12,"reqtime_max"=>6,"reqtime_min"=>1,"reqtime_avg"=>3.0
          })
        end
        it { emit }
      end

      context 'aggregate tag' do
        let(:config) do
          CONFIG + %[
          aggregate tag
          add_tag_prefix calc
          sum _count$
          max _max$
          min _min$
          avg _avg$
          ]
        end
        before do
          Fluent::Engine.stub(:now).and_return(time)
          Fluent::Engine.should_receive(:emit).with("calc.foo.bar", time, {
            "4xx_count"=>6,"5xx_count"=>6,"reqtime_max"=>6,"reqtime_min"=>1,"reqtime_avg"=>3.0
          })
          Fluent::Engine.should_receive(:emit).with("calc.foo.bar2", time, {
            "4xx_count"=>6,"5xx_count"=>6,"reqtime_max"=>6,"reqtime_min"=>1,"reqtime_avg"=>3.0
          })
        end
        it { emit }
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.13602638087386645, 'fraction_numerical': 0.011894947591567543, 'mean_word_length': 1.9222298692360633, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2059526', 'n_tokens_mistral': 2813, 'n_tokens_neox': 2777, 'n_words': 546}