Sha256: 6b87f54cb1bc5f62bdb70f522d78c30194dc1a7c100465b45600f31b40961747

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

module Prosperity
  class Extractors::Interval < Extractors::Base
    def self.key
      "interval"
    end

    def to_a
      if metric.sql?
        fragment = <<-SQL
          WITH prosperity_metric_count AS (
            #{metric.sql}
          )
          SELECT to_char(#{metric.group_by}, '#{period.db_strf_str}') AS bucket, #{aggregate.to_sql} AS result
          FROM prosperity_metric_count
          WHERE (#{metric.group_by} BETWEEN '#{@start_time.iso8601}' AND '#{@end_time.iso8601}')
          GROUP BY bucket
        SQL
        result = ActiveRecord::Base.connection.execute(fragment)
        s = result.to_a.inject({}) {|accum, el|
          accum.update(el["bucket"] => el["result"].to_f)
        }
      elsif metric.ruby?
        data = []
        period.each_period(start_time, end_time) do |start_time|
          new = metric.value_at.call(start_time, period)
          last = metric.value_at.call(start_time - period.duration, period)

          data << new - last
        end

        return data
      else
        s = scope.where("#{metric.group_by} BETWEEN ? AND ?", @start_time, @end_time)
        group_by_sql = "to_char(#{metric.group_by}, '#{period.db_strf_str}')"
        s = s.group(group_by_sql)
        s = aggregate.apply(s, group_by_sql: group_by_sql)
      end


      data = []

      period.each_period(start_time, end_time) do |start_time|
        str = start_time.strftime(period.ruby_strf_str)
        value = s.has_key?(str) ? s[str].to_f : 0.0
        data << value
      end

      data
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
prosperity-0.0.11 lib/prosperity/extractors/interval.rb