Class: ForestLiana::OperatorDateIntervalParser

Inherits:
Object
  • Object
show all
Defined in:
app/services/forest_liana/operator_date_interval_parser.rb

Constant Summary collapse

PERIODS =
{
  :$yesterday => { duration: 1, period: 'day' },
  :$previousWeek => { duration: 1, period: 'week' },
  :$previousMonth => { duration: 1, period: 'month' },
  :$previousQuarter => { duration: 3, period: 'month',
                        period_of_time: 'quarter' },
  :$previousYear => { duration: 1, period: 'year' },
  :$weekToDate => { duration: 1, period: 'week', to_date: true },
  :$monthToDate => { duration: 1, period: 'month', to_date: true },
  :$quarterToDate => { duration: 3, period: 'month',
                      period_of_time: 'quarter', to_date: true },
  :$yearToDate => { duration: 1, period: 'year', to_date: true }
}
PERIODS_PAST =
'$past'
PERIODS_FUTURE =
'$future'
PERIODS_TODAY =
'$today'
PERIODS_PREVIOUS_X_DAYS =
/^\$previous(\d+)Days$/
PERIODS_X_DAYS_TO_DATE =
/^\$(\d+)DaysToDate$/
PERIODS_X_HOURS_BEFORE =
/^\$(\d+)HoursBefore$/

Instance Method Summary collapse

Constructor Details

#initialize(value, timezone) ⇒ OperatorDateIntervalParser

Returns a new instance of OperatorDateIntervalParser.



25
26
27
28
# File 'app/services/forest_liana/operator_date_interval_parser.rb', line 25

def initialize(value, timezone)
  @value = value
  @timezone_offset = timezone.to_i
end

Instance Method Details

#get_interval_date_filterObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/services/forest_liana/operator_date_interval_parser.rb', line 68

def get_interval_date_filter
  return nil unless is_interval_date_value()

  return ">= '#{Time.now}'" if @value == PERIODS_FUTURE
  return "<= '#{Time.now}'" if @value == PERIODS_PAST

  if @value == PERIODS_TODAY
    return "BETWEEN '#{to_client_timezone(Time.now.beginning_of_day)}' " +
      "AND '#{to_client_timezone(Time.now.end_of_day)}'"
  end

  match = PERIODS_PREVIOUS_X_DAYS.match(@value)
  if match && match[1]
    return "BETWEEN '" +
      "#{to_client_timezone(Integer(match[1]).day.ago.beginning_of_day)}'" +
      " AND '#{to_client_timezone(1.day.ago.end_of_day)}'"
  end

  match = PERIODS_X_DAYS_TO_DATE.match(@value)
  if match && match[1]
    return "BETWEEN '" +
      "#{to_client_timezone((Integer(match[1]) - 1).day.ago.beginning_of_day)}'" +
      " AND '#{Time.now}'"
  end

  match = PERIODS_X_HOURS_BEFORE.match(@value)
  if match && match[1]
    return "< '#{to_client_timezone((Integer(match[1])).hour.ago)}'"
  end

  duration = PERIODS[@value.to_sym][:duration]
  period = PERIODS[@value.to_sym][:period]
  period_of_time = PERIODS[@value.to_sym][:period_of_time] ||
    PERIODS[@value.to_sym][:period]
  to_date = PERIODS[@value.to_sym][:to_date]

  if to_date
    from = to_client_timezone(Time.now.send("beginning_of_#{period_of_time}"))
    to = Time.now
  else
    from = to_client_timezone(duration.send(period).ago
      .send("beginning_of_#{period_of_time}"))
    to = to_client_timezone(1.send(period).ago
      .send("end_of_#{period_of_time}"))
  end
  "BETWEEN '#{from}' AND '#{to}'"
end

#get_interval_date_filter_for_previous_intervalObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/services/forest_liana/operator_date_interval_parser.rb', line 116

def get_interval_date_filter_for_previous_interval
  return nil unless has_previous_interval()

  if @value == PERIODS_TODAY
    return "BETWEEN '#{to_client_timezone(1.day.ago.beginning_of_day)}' AND " +
      "'#{to_client_timezone(1.day.ago.end_of_day)}'"
  end

  match = PERIODS_PREVIOUS_X_DAYS.match(@value)
  if match && match[1]
    return "BETWEEN '" +
      "#{to_client_timezone((Integer(match[1]) * 2).day.ago.beginning_of_day)}'" +
      " AND '#{to_client_timezone((Integer(match[1]) + 1).day.ago.end_of_day)}'"
  end

  match = PERIODS_X_DAYS_TO_DATE.match(@value)
  if match && match[1]
    return "BETWEEN '" +
      "#{to_client_timezone(((Integer(match[1]) * 2) - 1).day.ago.beginning_of_day)}'" +
      " AND '#{to_client_timezone(Integer(match[1]).day.ago)}'"
  end

  duration = PERIODS[@value.to_sym][:duration]
  period = PERIODS[@value.to_sym][:period]
  period_of_time = PERIODS[@value.to_sym][:period_of_time] ||
    PERIODS[@value.to_sym][:period]
  to_date = PERIODS[@value.to_sym][:to_date]

  if to_date
    from = to_client_timezone((duration).send(period).ago
             .send("beginning_of_#{period_of_time}"))
    to = to_client_timezone((duration).send(period).ago)
  else
    from = to_client_timezone((duration * 2).send(period).ago
             .send("beginning_of_#{period_of_time}"))
    to = to_client_timezone((1 + duration).send(period).ago
          .send("end_of_#{period_of_time}"))
  end
  "BETWEEN '#{from}' AND '#{to}'"
end

#has_previous_intervalObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/services/forest_liana/operator_date_interval_parser.rb', line 48

def has_previous_interval
  return false if @value.nil?
  return true if PERIODS[@value.to_sym]

  return true if PERIODS_TODAY == @value

  match = PERIODS_PREVIOUS_X_DAYS.match(@value)
  return true if match && match[1]

  match = PERIODS_X_DAYS_TO_DATE.match(@value)
  return true if match && match[1]

  false
end

#is_interval_date_valueObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/services/forest_liana/operator_date_interval_parser.rb', line 30

def is_interval_date_value
  return false if @value.nil?
  return true if PERIODS[@value.to_sym]

  return true if [PERIODS_PAST, PERIODS_FUTURE, PERIODS_TODAY].include? @value

  match = PERIODS_PREVIOUS_X_DAYS.match(@value)
  return true if match && match[1]

  match = PERIODS_X_DAYS_TO_DATE.match(@value)
  return true if match && match[1]

  match = PERIODS_X_HOURS_BEFORE.match(@value)
  return true if match && match[1]

  false
end

#to_client_timezone(date) ⇒ Object



63
64
65
66
# File 'app/services/forest_liana/operator_date_interval_parser.rb', line 63

def to_client_timezone(date)
  # NOTICE: By default, Rails store the dates without timestamp in the database.
  date - @timezone_offset.hours
end