Skip to content

Commit

Permalink
Refactor event type SQL filter construction
Browse files Browse the repository at this point in the history
We construct filter in two steps:

 1. we collect the conditions and
 2. join them using and operator.

This reduces the amount of string manipulation and conditional
statements considerably.
  • Loading branch information
Tadej Borovšak committed Aug 17, 2018
1 parent 5fc5098 commit 5e54ac2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 35 deletions.
54 changes: 23 additions & 31 deletions app/controllers/application_controller/timelines.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,43 +101,35 @@ def tl_build_timeline_report_options
to_dt = create_time_in_utc("#{yy}-#{mm}-#{dd} 23:59:59", session[:user_tz]) # Get tz 11pm in user's time zone
end

temp_clause = @tl_record.event_where_clause(@tl_options.evt_type)

cond = "( "
cond = cond << temp_clause[0]
params = temp_clause.slice(1, temp_clause.length)

event_set = @tl_options.event_set
if !event_set.empty?
if @tl_options.policy_events? && @tl_options.policy.result != "both"
where_clause = [") and (timestamp >= ? and timestamp <= ?) and (event_type in (?)) and (result = ?)",
from_dt,
to_dt,
event_set.flatten,
@tl_options.policy.result]
else
where_clause = [") and (timestamp >= ? and timestamp <= ?) and (event_type in (?))",
from_dt,
to_dt,
event_set.flatten]
end
else
where_clause = [") and (timestamp >= ? and timestamp <= ?)",
from_dt,
to_dt]
end
cond << where_clause[0]
rec_cond, *rec_params = @tl_record.event_where_clause(@tl_options.evt_type)
conditions = [rec_cond, "timestamp >= ?", "timestamp <= ?"]
parameters = rec_params + [from_dt, to_dt]

tl_add_event_type_conditions(conditions, parameters)
tl_add_policy_conditions(conditions, parameters) if @tl_options.policy_events?

params2 = where_clause.slice(1, where_clause.length - 1)
params = params.concat(params2)
@report.where_clause = [cond, *params]
condition = conditions.join(") and (")
@report.where_clause = ["(#{condition})"] + parameters
@report.rpt_options ||= {}
@report.rpt_options[:categories] =
@tl_options.management_events? ? @tl_options.management.categories : @tl_options.policy.categories
@report.rpt_options[:categories] = @tl_options.categories
@title = @report.title
end
end

def tl_add_event_type_conditions(conditions, parameters)
unless @tl_options.event_set.empty?
conditions << "event_type in (?)"
parameters << @tl_options.event_set.flatten
end
end

def tl_add_policy_conditions(conditions, parameters)
if @tl_options.policy.result != "both"
conditions << "result = ?"
parameters << @tl_options.policy.result
end
end

def tl_build_timeline(refresh = nil)
tl_build_init_options(refresh) # Intialize options(refresh) if !@report
@ajax_action = "tl_chooser"
Expand Down
12 changes: 8 additions & 4 deletions app/controllers/application_controller/timelines/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ def drop_cache
@events = @event_groups = @lvl_text_value = nil
end

def event_groups
@event_groups ||= EmsEvent.event_groups
end

def levels_text_and_value
@lvl_text_value ||= group_levels.map { |level| [level.to_s.titleize, level] }
end

private

def event_groups
@event_groups ||= EmsEvent.event_groups
end

def group_levels
EmsEvent::GROUP_LEVELS
end
Expand Down Expand Up @@ -143,6 +143,10 @@ def event_set
(policy_events? ? policy : management).event_set
end

def categories
(policy_events? ? policy : management).categories
end

def drop_cache
[policy, management].each(&:drop_cache)
end
Expand Down

0 comments on commit 5e54ac2

Please sign in to comment.