Module: Rollup::Utils
- Defined in:
- lib/rollup/utils.rb
Constant Summary collapse
- DATE_INTERVALS =
%w(day week month quarter year)
Class Method Summary collapse
- .adapter_name ⇒ Object
- .check_dimensions ⇒ Object
- .date_interval?(interval) ⇒ Boolean
- .dimensions_supported? ⇒ Boolean
- .make_series(result, interval) ⇒ Object
- .mysql? ⇒ Boolean
- .postgresql? ⇒ Boolean
- .sqlite? ⇒ Boolean
- .time_sql(interval) ⇒ Object
Class Method Details
.adapter_name ⇒ Object
35 36 37 |
# File 'lib/rollup/utils.rb', line 35 def adapter_name Rollup.connection.adapter_name end |
.check_dimensions ⇒ Object
31 32 33 |
# File 'lib/rollup/utils.rb', line 31 def check_dimensions raise "Dimensions not supported" unless dimensions_supported? end |
.date_interval?(interval) ⇒ Boolean
20 21 22 |
# File 'lib/rollup/utils.rb', line 20 def date_interval?(interval) DATE_INTERVALS.include?(interval.to_s) end |
.dimensions_supported? ⇒ Boolean
24 25 26 27 28 29 |
# File 'lib/rollup/utils.rb', line 24 def dimensions_supported? unless defined?(@dimensions_supported) @dimensions_supported = postgresql? && Rollup.column_names.include?("dimensions") end @dimensions_supported end |
.make_series(result, interval) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/rollup/utils.rb', line 51 def make_series(result, interval) series = {} if Utils.date_interval?(interval) result.each do |row| series[row[0].to_date] = row[1] end else time_zone = Rollup.time_zone if result.any? && result[0][0].is_a?(Time) result.each do |row| series[row[0].in_time_zone(time_zone)] = row[1] end else utc = ActiveSupport::TimeZone["Etc/UTC"] result.each do |row| # row can be time or string series[utc.parse(row[0]).in_time_zone(time_zone)] = row[1] end end end series end |
.mysql? ⇒ Boolean
43 44 45 |
# File 'lib/rollup/utils.rb', line 43 def mysql? adapter_name =~ /mysql|trilogy/i end |
.postgresql? ⇒ Boolean
39 40 41 |
# File 'lib/rollup/utils.rb', line 39 def postgresql? adapter_name =~ /postg/i end |
.sqlite? ⇒ Boolean
47 48 49 |
# File 'lib/rollup/utils.rb', line 47 def sqlite? adapter_name =~ /sqlite/i end |
.time_sql(interval) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/rollup/utils.rb', line 6 def time_sql(interval) if date_interval?(interval) if postgresql? "rollups.time::date" elsif sqlite? "date(rollups.time)" else "CAST(rollups.time AS date)" end else :time end end |