Module: DatastaxRails::StatsMethods

Included in:
Relation
Defined in:
lib/datastax_rails/relation/stats_methods.rb

Overview

Methods for computing statistics (sum, maximum, minimum, average, and stddev)

Constant Summary collapse

STATS_FIELDS =
{ 'sum' => 'sum', 'maximum' => 'max', 'minimum' => 'min', 'average' => 'mean', 'stddev' => 'stddev' }

Instance Method Summary collapse

Instance Method Details

#average(field) ⇒ Fixnum, Float

Calculates the average of the field listed. Field must be indexed as a number.

Parameters:

  • field (Symbol)

    the field to calculate

Returns:

  • (Fixnum, Float)

    the average of the columns that match the query


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/datastax_rails/relation/stats_methods.rb', line 46

%w(sum maximum minimum average stddev).each do |op|
  define_method(op) do |field|
    calculate_stats(field)
    @stats[field] ? @stats[field][STATS_FIELDS[op]] : 0
  end

  define_method("grouped_#{op}") do |field|
    self.op unless @group_value
    calculate_stats(field)
    values = {}
    @stats[field]['facets'][@group_value].each do |k, v|
      values[k] = v[STATS_FIELDS[op]]
    end
    values
  end
end

#grouped_average(field) ⇒ Hash

Calculates the average of the field listed for a grouped query.

Parameters:

  • field (Symbol)

    the field to calculate

Returns:

  • (Hash)

    the average of the columns that match the query by group. Group name is the key.


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/datastax_rails/relation/stats_methods.rb', line 46

%w(sum maximum minimum average stddev).each do |op|
  define_method(op) do |field|
    calculate_stats(field)
    @stats[field] ? @stats[field][STATS_FIELDS[op]] : 0
  end

  define_method("grouped_#{op}") do |field|
    self.op unless @group_value
    calculate_stats(field)
    values = {}
    @stats[field]['facets'][@group_value].each do |k, v|
      values[k] = v[STATS_FIELDS[op]]
    end
    values
  end
end

#grouped_maximum(field) ⇒ Hash

Calculates the sum of the field listed for a grouped query.

Parameters:

  • field (Symbol)

    the field to calculate

Returns:

  • (Hash)

    the sum of the columns that match the query by group. Group name is the key.


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/datastax_rails/relation/stats_methods.rb', line 46

%w(sum maximum minimum average stddev).each do |op|
  define_method(op) do |field|
    calculate_stats(field)
    @stats[field] ? @stats[field][STATS_FIELDS[op]] : 0
  end

  define_method("grouped_#{op}") do |field|
    self.op unless @group_value
    calculate_stats(field)
    values = {}
    @stats[field]['facets'][@group_value].each do |k, v|
      values[k] = v[STATS_FIELDS[op]]
    end
    values
  end
end

#grouped_minimum(field) ⇒ Hash

Calculates the minimum of the field listed for a grouped query.

Parameters:

  • field (Symbol)

    the field to calculate

Returns:

  • (Hash)

    the minimum of the columns that match the query by group. Group name is the key.


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/datastax_rails/relation/stats_methods.rb', line 46

%w(sum maximum minimum average stddev).each do |op|
  define_method(op) do |field|
    calculate_stats(field)
    @stats[field] ? @stats[field][STATS_FIELDS[op]] : 0
  end

  define_method("grouped_#{op}") do |field|
    self.op unless @group_value
    calculate_stats(field)
    values = {}
    @stats[field]['facets'][@group_value].each do |k, v|
      values[k] = v[STATS_FIELDS[op]]
    end
    values
  end
end

#grouped_stddev(field) ⇒ Hash

Calculates the standard deviation of the field listed for a grouped query.

Parameters:

  • field (Symbol)

    the field to calculate

Returns:

  • (Hash)

    the standard deviation of the columns that match the query by group. Group name is the key.


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/datastax_rails/relation/stats_methods.rb', line 46

%w(sum maximum minimum average stddev).each do |op|
  define_method(op) do |field|
    calculate_stats(field)
    @stats[field] ? @stats[field][STATS_FIELDS[op]] : 0
  end

  define_method("grouped_#{op}") do |field|
    self.op unless @group_value
    calculate_stats(field)
    values = {}
    @stats[field]['facets'][@group_value].each do |k, v|
      values[k] = v[STATS_FIELDS[op]]
    end
    values
  end
end

#grouped_sum(field) ⇒ Hash

Calculates the sum of the field listed for a grouped query.

Parameters:

  • field (Symbol)

    the field to calculate

Returns:

  • (Hash)

    the sum of the columns that match the query by group. Group name is the key.


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/datastax_rails/relation/stats_methods.rb', line 46

%w(sum maximum minimum average stddev).each do |op|
  define_method(op) do |field|
    calculate_stats(field)
    @stats[field] ? @stats[field][STATS_FIELDS[op]] : 0
  end

  define_method("grouped_#{op}") do |field|
    self.op unless @group_value
    calculate_stats(field)
    values = {}
    @stats[field]['facets'][@group_value].each do |k, v|
      values[k] = v[STATS_FIELDS[op]]
    end
    values
  end
end

#maximum(field) ⇒ Fixnum, Float

Calculates the maximum value of the field listed. Field must be indexed as a number.

Parameters:

  • field (Symbol)

    the field to calculate

Returns:

  • (Fixnum, Float)

    the maximum value of the rows that match the query


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/datastax_rails/relation/stats_methods.rb', line 46

%w(sum maximum minimum average stddev).each do |op|
  define_method(op) do |field|
    calculate_stats(field)
    @stats[field] ? @stats[field][STATS_FIELDS[op]] : 0
  end

  define_method("grouped_#{op}") do |field|
    self.op unless @group_value
    calculate_stats(field)
    values = {}
    @stats[field]['facets'][@group_value].each do |k, v|
      values[k] = v[STATS_FIELDS[op]]
    end
    values
  end
end

#minimum(field) ⇒ Fixnum, Float

Calculates the minimum of the field listed. Field must be indexed as a number.

Parameters:

  • field (Symbol)

    the field to calculate

Returns:

  • (Fixnum, Float)

    the minimum of the columns that match the query


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/datastax_rails/relation/stats_methods.rb', line 46

%w(sum maximum minimum average stddev).each do |op|
  define_method(op) do |field|
    calculate_stats(field)
    @stats[field] ? @stats[field][STATS_FIELDS[op]] : 0
  end

  define_method("grouped_#{op}") do |field|
    self.op unless @group_value
    calculate_stats(field)
    values = {}
    @stats[field]['facets'][@group_value].each do |k, v|
      values[k] = v[STATS_FIELDS[op]]
    end
    values
  end
end

#stddev(field) ⇒ Fixnum, Float

Calculates the standard deviation of the field listed. Field must be indexed as a number.

Parameters:

  • field (Symbol)

    the field to calculate

Returns:

  • (Fixnum, Float)

    the standard deviation of the columns that match the query


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/datastax_rails/relation/stats_methods.rb', line 46

%w(sum maximum minimum average stddev).each do |op|
  define_method(op) do |field|
    calculate_stats(field)
    @stats[field] ? @stats[field][STATS_FIELDS[op]] : 0
  end

  define_method("grouped_#{op}") do |field|
    self.op unless @group_value
    calculate_stats(field)
    values = {}
    @stats[field]['facets'][@group_value].each do |k, v|
      values[k] = v[STATS_FIELDS[op]]
    end
    values
  end
end

#sum(field) ⇒ Fixnum, Float

Calculates the sum of the field listed. Field must be indexed as a number.

Parameters:

  • field (Symbol)

    the field to calculate

Returns:

  • (Fixnum, Float)

    the sum of the column value rows that match the query


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/datastax_rails/relation/stats_methods.rb', line 46

%w(sum maximum minimum average stddev).each do |op|
  define_method(op) do |field|
    calculate_stats(field)
    @stats[field] ? @stats[field][STATS_FIELDS[op]] : 0
  end

  define_method("grouped_#{op}") do |field|
    self.op unless @group_value
    calculate_stats(field)
    values = {}
    @stats[field]['facets'][@group_value].each do |k, v|
      values[k] = v[STATS_FIELDS[op]]
    end
    values
  end
end