Module: Mongoid::Gator::Readers

Included in:
Gatorer
Defined in:
lib/gator/readers.rb

Constant Summary collapse

HOUR =
"HOUR"
DAY =
"DAY"
MONTH =
"MONTH"
DEFAULT_GRAIN =
DAY

Instance Method Summary collapse

Instance Method Details

#group_by(date, grain, opts = {}) ⇒ Object

Group_by - retuns a collection for a specific key



82
83
84
85
86
87
88
89
90
91
# File 'lib/gator/readers.rb', line 82

def group_by(date,grain,opts={})
    # Get Offset
    if date.is_a?(Range)
        off_set = date.last.utc_offset
    else
        off_set = date.utc_offset
    end
    data = collection_for_group(date,HOUR,0,opts)
    return data
end

#last(total_days = 7, opts = {}) ⇒ Object



26
27
28
# File 'lib/gator/readers.rb', line 26

def last(total_days = 7,opts={})
  total_for((Time.zone.now - total_days.days)..Time.zone.now, DEFAULT_GRAIN, opts).to_i
end

#on(date, opts = {}) ⇒ Object

On - Gets total for a specified day on DAY level



22
23
24
# File 'lib/gator/readers.rb', line 22

def on(date,opts={})
  total_for(date, DEFAULT_GRAIN,opts).to_i
end

#range(date, grain = DEFAULT_GRAIN, opts = {}) ⇒ Object

Range - retuns a collection for a specified range on specified level



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/gator/readers.rb', line 31

def range(date, grain=DEFAULT_GRAIN, opts={})
    data = collection_for(date,grain,opts)

    # Add Zero values for dates missing
    # May want to look into a way to get mongo to do this
    if date.is_a?(Range)
      start_date = date.first
      end_date = date.last

      case grain
        when HOUR
          start_date = start_date.change(:sec=>0).change(:min => 0)
          end_date = end_date.change(:sec=>0).change(:min => 0) - 1.hour
          data = data.group_by {|d| (Time.zone.at(d["date"].to_i).change(:sec=>0).change(:min => 0)).to_i }
        when DAY
          start_date = start_date.change(:hour=>0).change(:sec=>0).change(:min => 0)
          end_date = end_date.change(:hour=>0).change(:sec=>0).change(:min => 0)
          data = data.group_by {|d| (Time.zone.at(d["date"].to_i).change(:hour=>0).change(:sec=>0).change(:min => 0)).to_i }
        when MONTH
          start_date = start_date.change(:day=>1).change(:hour=>0).change(:sec=>0).change(:min => 0)
          end_date = end_date.change(:day=>1).change(:hour=>0).change(:sec=>0).change(:min => 0)
          data = data.group_by {|d| (Time.zone.at(d["date"].to_i).change(:day=>1).change(:hour=>0).change(:sec=>0).change(:min => 0)).to_i }
      end

      # Initialize result set array
      result_set = []
      
      # Build Result Set by Time Zone
      while start_date <= end_date
        if data[start_date.to_i].nil?
           result_set << {"date" => "#{start_date.to_i}", @for => 0}
        else
          result_set << {"date" => "#{start_date.to_i}", @for => data[start_date.to_i].map{|di| di[@for.to_s].to_i}.inject(0, :+)}
        end
        
        case grain
          when HOUR
            start_date = start_date + 1.hour
          when DAY
            start_date = start_date + 1.day
          when MONTH
            start_date = start_date + 1.month
        end
      end
    end

    return result_set
end

#today(opts = {}) ⇒ Object

Today - Gets total for today on DAY level



12
13
14
# File 'lib/gator/readers.rb', line 12

def today(opts={})
  total_for(Time.zone.now, DEFAULT_GRAIN, opts).to_i
end

#yesterday(opts = {}) ⇒ Object

Yesterday - Gets total for tomorrow on DAY level



17
18
19
# File 'lib/gator/readers.rb', line 17

def yesterday(opts={})
  total_for(Time.zone.now - 1.day, DEFAULT_GRAIN,opts).to_i
end