Module: Cubicle::Query::Dsl::TimeIntelligence

Included in:
Cubicle::Query::Dsl
Defined in:
lib/cubicle/query/dsl/time_intelligence.rb

Instance Method Summary collapse

Instance Method Details

#from(time = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/cubicle/query/dsl/time_intelligence.rb', line 55

def from(time = nil)
  return @from_date unless time
  @from_date = if time.is_a?(Symbol)
    Time.send(time) if Time.respond_to?(time)
    Date.send(time).to_time if Date.respond_to?(time)
  else
    time.to_time
  end
  self
end

#last(duration, as_of = Time.now) ⇒ Object Also known as: for_the_last



24
25
26
27
28
29
# File 'lib/cubicle/query/dsl/time_intelligence.rb', line 24

def last(duration,as_of = Time.now)
  duration = 1.send(duration) if [:year,:month,:week,:day].include?(duration)
  period = duration.parts[0][0]
  @from_date = duration.ago(as_of).advance(period=>1)
  @to_date = as_of
end

#last_complete(duration, as_of = Time.now) ⇒ Object Also known as: for_the_last_complete



32
33
34
35
36
37
38
# File 'lib/cubicle/query/dsl/time_intelligence.rb', line 32

def last_complete(duration,as_of = Time.now)
  duration = 1.send(duration) if [:year,:month,:week,:day].include?(duration)
  period = duration.parts[0][0]
  @to_date = as_of.beginning_of(period)
  @from_date = duration.ago(@to_date)
  @to_date_filter = "$lt"
end

#mtd(as_of = Time.now) ⇒ Object Also known as: month_to_date



82
83
84
# File 'lib/cubicle/query/dsl/time_intelligence.rb', line 82

def mtd(as_of = Time.now)
  this :month, as_of
end

#next(duration, as_of = Time.now) ⇒ Object Also known as: for_the_next



41
42
43
44
45
46
# File 'lib/cubicle/query/dsl/time_intelligence.rb', line 41

def next(duration,as_of = Time.now)
  duration = 1.send(duration) if [:year,:month,:week,:day].include?(duration)
  period = duration.parts[0][0]
  @to_date = duration.from_now(as_of).advance(period=>-1)
  @from_date = as_of
end

#this(period, as_of = Time.now) ⇒ Object



49
50
51
52
53
# File 'lib/cubicle/query/dsl/time_intelligence.rb', line 49

def this(period,as_of = Time.now)
  @from_date = as_of.beginning_of(period)
  @to_date = as_of
  self
end

#time_dimension(dimension = nil) ⇒ Object Also known as: date_dimension



16
17
18
19
20
21
# File 'lib/cubicle/query/dsl/time_intelligence.rb', line 16

def time_dimension(dimension = nil)
  return (@time_dimension ||= @aggregation.time_dimension) unless dimension
  @time_dimension = dimension.is_a?(Cubicle::Dimension) ? dimension : @aggregation.dimensions[unalias(dimension)]
  raise "No dimension matching the name #{unalias(dimension)} could be found in the underlying data source" unless @time_dimension
  #select @time_dimension unless selected?(dimension)
end

#time_range(date_range = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/cubicle/query/dsl/time_intelligence.rb', line 5

def time_range(date_range = nil)
  return nil unless date_range || @from_date || @to_date
  unless date_range
    start,stop = @from_date || Time.now, @to_date || Time.now
    return @to_date_filter=="$lte" ? start..stop : start...stop
  end

  @to_date_filter = date_range.exclude_end? ? "$lt" : "$lte"
  @from_date, @to_date = date_range.first, date_range.last if date_range
end

#until(time = nil) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/cubicle/query/dsl/time_intelligence.rb', line 66

def until(time = nil)
  return @to_date unless time
  @to_date = if time.is_a?(Symbol)
    Time.send(time) if Time.respond_to?(time)
    Date.send(time).to_time if Date.respond_to?(time)
  else
    time.to_time
  end
  self
end

#ytd(as_of = Time.now) ⇒ Object Also known as: year_to_date



77
78
79
# File 'lib/cubicle/query/dsl/time_intelligence.rb', line 77

def ytd(as_of = Time.now)
  this :year, as_of
end