Sha256: fb7d1962118d0c172026af9e62cbe10ff0a64ac6d7eadaa19795d27461a02cb6

Contents?: true

Size: 1.7 KB

Versions: 2

Compression:

Stored size: 1.7 KB

Contents

class C2::Reporter::Bucket
  attr_accessor :refreshed_at
  
  attr_accessor :series
  attr_accessor :series_cache
  
  def initialize(klass_name, scope_name, *series)
    @klass_name = klass_name
    @scope_name = scope_name

    series = [:count] if series.empty?
    @series = series.inject({}) do |m,v|
      name, *mutators = *[*v]
      m[name.to_sym] = {
        :id => name.to_sym,
        :name => name.to_s.titleize,
        :mutators => mutators,
        :data => []
      }
      m
    end
  end
  
  def klass
    @klass_cache ||= @klass_name.to_s.classify.constantize
  end
  
  def scope
    self.klass.send(@scope_name)
  end
  
  def id
    @scope_name.to_sym
  end
  
  def label
    @scope_name.to_s.titleize
  end
  
  def count
    self.scope.count
  end
  
  def refresh
    @series_cache = {}
    started_at = self.scope.where(:created_at.ne => nil).order(:created_at).only(:created_at).first().created_at || Time.now
    self.scope.each do |item|
      date = (item.created_at || started_at).to_date
      @series.each do |key,value|
        data = item.respond_to?(key) ? item.send(key) : 1
        @series_cache[key] ||= Hash.new(0)
        @series_cache[key][date] += value[:mutators].inject(data) { |memo,mutator| memo.send(mutator) }
      end
    end
    @series.map do |key,value|
      value[:start] = started_at.to_date
      value[:pointStart] = started_at.to_i * 1000
      value[:end] = Date.today
      value[:data] = (value[:start]..value[:end]).map { |d| @series_cache[key][d] }
    end
    @refreshed_at = Time.now.utc
  end
  
  def as_json(options={})
    {
      :id => self.id,
      :label => self.label,
      :count => self.count,
      :series => self.series.map {|k,v| v }
    }
  end
  
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
c2-0.1.13 app/models/c2/reporter/bucket.rb
c2-0.1.12 app/models/c2/reporter/bucket.rb