Class: PgHistogram::Histogram
- Inherits:
-
Object
- Object
- PgHistogram::Histogram
- 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
-
#bucket_size ⇒ Object
readonly
Returns the value of attribute bucket_size.
-
#column ⇒ Object
readonly
Returns the value of attribute column.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
Instance Method Summary collapse
-
#initialize(query, column_name, options = {}) ⇒ Histogram
constructor
column_name name must be safe for SQL injection.
- #max ⇒ Object
- #min ⇒ Object
-
#results ⇒ Object
returns histogram as hash bucket minimum as a key frequency as value.
Constructor Details
#initialize(query, column_name, options = {}) ⇒ Histogram
column_name name must be safe for SQL injection
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/pg_histogram/histogram.rb', line 14 def initialize(query, column_name, = {}) @query = query @column = column_name.to_s if .is_a? Hash if [:buckets] @min = [:min] || 0 @max = [:max] @buckets = [:buckets] @bucket_size = calculate_bucket_size else @min = [:min] @max = [:max] @bucket_size = ([:bucket_size] || 1).to_f end else @bucket_size = .to_f end end |
Instance Attribute Details
#bucket_size ⇒ Object (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 |
#column ⇒ Object (readonly)
Returns the value of attribute column.
3 4 5 |
# File 'lib/pg_histogram/histogram.rb', line 3 def column @column end |
#query ⇒ Object (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
#max ⇒ Object
49 50 51 |
# File 'lib/pg_histogram/histogram.rb', line 49 def max @max ||= round_to_increment(source_max, :up) end |
#min ⇒ Object
45 46 47 |
# File 'lib/pg_histogram/histogram.rb', line 45 def min @min ||= round_to_increment(source_min, :down) end |
#results ⇒ Object
returns histogram as hash bucket minimum as a key frequency as value
36 37 38 39 40 41 42 43 |
# File 'lib/pg_histogram/histogram.rb', line 36 def results # error handling case if max == min { min => subquery.where("#{pure_column} = ?", min).count } else labeled_histogram end end |