Class: PgHistogram::Histogram

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_histogram/histogram.rb

Constant Summary collapse

BUCKET_COL =
'bucket'
FREQUENCY_COL =
'frequency'
ROUND_METHODS_BY_DIRECTION =
{
  nil => :round,
  down:  :floor,
  up:    :ceil
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(query, column_name, bucket_size = 0.5) ⇒ Histogram

column_name name must be safe for SQL injection



14
15
16
17
18
# File 'lib/pg_histogram/histogram.rb', line 14

def initialize(query, column_name, bucket_size = 0.5)
  @query = query
  @column = column_name.to_s
  @bucket_size = bucket_size
end

Instance Attribute Details

#bucket_sizeObject (readonly)

Returns the value of attribute bucket_size.



3
4
5
# File 'lib/pg_histogram/histogram.rb', line 3

def bucket_size
  @bucket_size
end

#columnObject (readonly)

Returns the value of attribute column.



3
4
5
# File 'lib/pg_histogram/histogram.rb', line 3

def column
  @column
end

#queryObject (readonly)

Returns the value of attribute query.



3
4
5
# File 'lib/pg_histogram/histogram.rb', line 3

def query
  @query
end

Instance Method Details

#maxObject



36
37
38
# File 'lib/pg_histogram/histogram.rb', line 36

def max
  @max ||= round_to_increment(query.maximum(column), :up)
end

#minObject



32
33
34
# File 'lib/pg_histogram/histogram.rb', line 32

def min
  @min ||= round_to_increment(query.minimum(column), :down)
end

#resultsObject

returns histogram as hash bucket minimum as a key frequency as value



23
24
25
26
27
28
29
30
# File 'lib/pg_histogram/histogram.rb', line 23

def results
  # error handling case
  if max == min
    { min => query.where("#{column} = ?", min).count }
  else
    labeled_histogram
  end
end