Class: C2::Reporter::Bucket
- Inherits:
-
Object
- Object
- C2::Reporter::Bucket
- Defined in:
- app/models/c2/reporter/bucket.rb
Instance Attribute Summary collapse
-
#refreshed_at ⇒ Object
Returns the value of attribute refreshed_at.
-
#series ⇒ Object
Returns the value of attribute series.
-
#series_cache ⇒ Object
Returns the value of attribute series_cache.
Instance Method Summary collapse
- #as_json(options = {}) ⇒ Object
- #count ⇒ Object
- #id ⇒ Object
-
#initialize(klass_name, scope_name, *series) ⇒ Bucket
constructor
A new instance of Bucket.
- #klass ⇒ Object
- #label ⇒ Object
- #refresh ⇒ Object
- #scope ⇒ Object
Constructor Details
#initialize(klass_name, scope_name, *series) ⇒ Bucket
Returns a new instance of Bucket.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'app/models/c2/reporter/bucket.rb', line 7 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 |
Instance Attribute Details
#refreshed_at ⇒ Object
Returns the value of attribute refreshed_at.
2 3 4 |
# File 'app/models/c2/reporter/bucket.rb', line 2 def refreshed_at @refreshed_at end |
#series ⇒ Object
Returns the value of attribute series.
4 5 6 |
# File 'app/models/c2/reporter/bucket.rb', line 4 def series @series end |
#series_cache ⇒ Object
Returns the value of attribute series_cache.
5 6 7 |
# File 'app/models/c2/reporter/bucket.rb', line 5 def series_cache @series_cache end |
Instance Method Details
#as_json(options = {}) ⇒ Object
64 65 66 67 68 69 70 71 |
# File 'app/models/c2/reporter/bucket.rb', line 64 def as_json(={}) { :id => self.id, :label => self.label, :count => self.count, :series => self.series.map {|k,v| v } } end |
#count ⇒ Object
40 41 42 |
# File 'app/models/c2/reporter/bucket.rb', line 40 def count self.scope.count end |
#id ⇒ Object
32 33 34 |
# File 'app/models/c2/reporter/bucket.rb', line 32 def id @scope_name.to_sym end |
#klass ⇒ Object
24 25 26 |
# File 'app/models/c2/reporter/bucket.rb', line 24 def klass @klass_cache ||= @klass_name.to_s.classify.constantize end |
#label ⇒ Object
36 37 38 |
# File 'app/models/c2/reporter/bucket.rb', line 36 def label @scope_name.to_s.titleize end |
#refresh ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'app/models/c2/reporter/bucket.rb', line 44 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 |
#scope ⇒ Object
28 29 30 |
# File 'app/models/c2/reporter/bucket.rb', line 28 def scope self.klass.send(@scope_name) end |