<commit_before>require "spec_helper"

describe VmdbIndex do
  context "#capture_metrics" do
    before(:each) do
      MiqDatabase.seed
      VmdbDatabase.seed
      MiqRegion.seed
      table = VmdbTable.where(:name => 'miq_regions').first
      @index = table.vmdb_indexes.first
    end

    it "populates vmdb_metrics columns" do
      @index.capture_metrics
      metrics = @index.vmdb_metrics
      metrics.length.should == 0

      @index.capture_metrics
      metrics = @index.vmdb_metrics
      metrics.length.should_not == 0

      metric = metrics.first
      columns = %w( rows pages otta percent_bloat wasted_bytes timestamp )

      columns.each do |column|
        metric.send(column).should_not be_nil
      end
    end

    it "verifies non-index related metrics columns are nil" do
      @index.capture_metrics
      metrics = @index.vmdb_metrics
      metrics.length.should == 0

      @index.capture_metrics
      metrics = @index.vmdb_metrics
      metrics.length.should_not == 0

      metric = metrics.first
      columns = %w( table_scans sequential_rows_read
                    rows_inserted rows_updated rows_deleted rows_hot_updated rows_live rows_dead
                )

      columns.each do |column|
        metric.send(column).should be_nil
      end
    end
  end

  context "#rollup_metrics" do
    before :each do
      db = VmdbDatabase.seed_self
      @evm_table  = FactoryGirl.create(:vmdb_table_evm, :vmdb_database => db,         :name => 'accounts')
      @evm_index  = FactoryGirl.create(:vmdb_index,     :vmdb_table    => @evm_table, :name => "accounts_pkey")

      ts = Time.gm(2012, 8, 15, 10, 00, 01)         # Need specific date in order to keep track of rollup data...

      FactoryGirl.create(:vmdb_metric_hourly, :resource => @evm_index, :timestamp => ts - 50.hours, :rows => 0,   :size => 0,    :wasted_bytes =>  0, :percent_bloat => 0.0)
      FactoryGirl.create(:vmdb_metric_hourly, :resource => @evm_index, :timestamp => ts - 49.hours, :rows => 10,  :size => 100,  :wasted_bytes =>  2, :percent_bloat => 0.2)
      FactoryGirl.create(:vmdb_metric_hourly, :resource => @evm_index, :timestamp => ts - 48.hours, :rows => 10,  :size => 100,  :wasted_bytes =>  2, :percent_bloat => 0.2)
      FactoryGirl.create(:vmdb_metric_hourly, :resource => @evm_index, :timestamp => ts - 47.hours, :rows => 20,  :size => 200,  :wasted_bytes =>  4, :percent_bloat => 0.4)
      FactoryGirl.create(:vmdb_metric_hourly, :resource => @evm_index, :timestamp => ts - 46.hours, :rows => 20,  :size => 200,  :wasted_bytes =>  4, :percent_bloat => 0.4)
      FactoryGirl.create(:vmdb_metric_hourly, :resource => @evm_index, :timestamp => ts - 45.hours, :rows => 20,  :size => 200,  :wasted_bytes =>  4, :percent_bloat => 0.4)
      FactoryGirl.create(:vmdb_metric_hourly, :resource => @evm_index, :timestamp => ts - 44.hours, :rows => 30,  :size => 300,  :wasted_bytes =>  6, :percent_bloat => 0.5)
      FactoryGirl.create(:vmdb_metric_hourly, :resource => @evm_index, :timestamp => ts - 43.hours, :rows => 40,  :size => 400,  :wasted_bytes =>  8, :percent_bloat => 0.6)
      FactoryGirl.create(:vmdb_metric_hourly, :resource => @evm_index, :timestamp => ts - 42.hours, :rows => 50,  :size => 500,  :wasted_bytes => 10, :percent_bloat => 0.7)<commit_msg>Improve VmdbIndex#capture_metrics spec by removing the seeding.<commit_after>require "spec_helper"

describe VmdbIndex do
  context "#capture_metrics" do
    let(:index) { FactoryGirl.create(:vmdb_index, :name => "accounts_pkey") }

    it "creates a vmdb_metrics record" do
      # The first capture just gets the raw data
      index.capture_metrics
      expect(index.vmdb_metrics).to be_empty
      expect(index.prior_raw_metrics).to_not be_nil

      # The next capture starts creating the metrics rows
      index.capture_metrics
      expect(index.vmdb_metrics.count).to eq(1)

      metric = index.vmdb_metrics.first

      # Verify the column contents
      columns = %w( rows pages otta percent_bloat wasted_bytes timestamp )
      columns.each do |column|
        expect(metric.send(column)).to_not be_nil
      end

      # Verify the non-index columns are nil
      columns = %w(
        table_scans sequential_rows_read rows_inserted rows_updated rows_deleted
        rows_hot_updated rows_live rows_dead
      )
      columns.each do |column|
        expect(metric.send(column)).to be_nil
      end
    end
  end

  context "#rollup_metrics" do
    before :each do
      db = VmdbDatabase.seed_self
      @evm_table  = FactoryGirl.create(:vmdb_table_evm, :vmdb_database => db,         :name => 'accounts')
      @evm_index  = FactoryGirl.create(:vmdb_index,     :vmdb_table    => @evm_table, :name => "accounts_pkey")

      ts = Time.gm(2012, 8, 15, 10, 00, 01)         # Need specific date in order to keep track of rollup data...

      FactoryGirl.create(:vmdb_metric_hourly, :resource => @evm_index, :timestamp => ts - 50.hours, :rows => 0,   :size => 0,    :wasted_bytes =>  0, :percent_bloat => 0.0)
      FactoryGirl.create(:vmdb_metric_hourly, :resource => @evm_index, :timestamp => ts - 49.hours, :rows => 10,  :size => 100,  :wasted_bytes =>  2, :percent_bloat => 0.2)
      FactoryGirl.create(:vmdb_metric_hourly, :resource => @evm_index, :timestamp => ts - 48.hours, :rows => 10,  :size => 100,  :wasted_bytes =>  2, :percent_bloat => 0.2)
      FactoryGirl.create(:vmdb_metric_hourly, :resource => @evm_index, :timestamp => ts - 47.hours, :rows => 20,  :size => 200,  :wasted_bytes =>  4, :percent_bloat => 0.4)
      FactoryGirl.create(:vmdb_metric_hourly, :resource => @evm_index, :timestamp => ts - 46.hours, :rows => 20,  :size => 200,  :wasted_bytes =>  4, :percent_bloat => 0.4)
      FactoryGirl.create(:vmdb_metric_hourly, :resource => @evm_index, :timestamp => ts - 45.hours, :rows => 20,  :size => 200,  :wasted_bytes =>  4, :percent_bloat => 0.4)
      FactoryGirl.create(:vmdb_metric_hourly, :resource => @evm_index, :timestamp => ts - 44.hours, :rows => 30,  :size => 300,  :wasted_bytes =>  6, :percent_bloat => 0.5)
      FactoryGirl.create(:vmdb_metric_hourly, :resource => @evm_index, :timestamp => ts - 43.hours, :rows => 40,  :size => 400,  :wasted_bytes =>  8, :percent_bloat => 0.6)
      FactoryGirl.create(:vmdb_metric_hourly, :resource => @evm_index, :timestamp => ts - 42.hours, :rows => 50,  :size => 500,  :wasted_bytes => 10, :percent_bloat => 0.7)
{'fraction_non_alphanumeric': 0.13348982785602503, 'fraction_numerical': 0.0323943661971831, 'mean_word_length': 3.2692050768203074, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 121, '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': '100500', 'n_tokens_mistral': 2423, 'n_tokens_neox': 2147, 'n_words': 509}