class ConceptQL::Operators::OneInTwoOut

Attributes

db[R]

Public Instance Methods

query(db) click to toggle source
# File lib/conceptql/operators/one_in_two_out.rb, line 39
def query(db)
  @db = db
  first_valid_event.from_self
end

Private Instance Methods

all_inpatient_events() click to toggle source
# File lib/conceptql/operators/one_in_two_out.rb, line 53
def all_inpatient_events
  condition_events
    .where(provenance_type: to_concept_id(:inpatient))
    .from_self
end
all_valid_events() click to toggle source
# File lib/conceptql/operators/one_in_two_out.rb, line 112
def all_valid_events
  valid_inpatient_events.union(valid_outpatient_events, all: true)
end
condition_events() click to toggle source
# File lib/conceptql/operators/one_in_two_out.rb, line 46
def condition_events
  db[stream.evaluate(db)]
    .where(criterion_domain: 'condition_occurrence')
    .exclude(provenance_type: nil)
    .from_self
end
first_valid_event() click to toggle source
# File lib/conceptql/operators/one_in_two_out.rb, line 116
def first_valid_event
  all_valid_events
    .select_append { |o| o.row_number(:over, partition: :person_id, order: [ :start_date, :criterion_id ]){}.as(:rn) }
    .from_self
    .where(rn: 1)
end
outpatient_events() click to toggle source
# File lib/conceptql/operators/one_in_two_out.rb, line 72
def outpatient_events
  condition_events
    .where(provenance_type: to_concept_id(:claim) + to_concept_id(:outpatient))
    .from_self
end
valid_inpatient_events() click to toggle source
# File lib/conceptql/operators/one_in_two_out.rb, line 59
def valid_inpatient_events
  q = all_inpatient_events
  unless options[:inpatient_length_of_stay].nil? || options[:inpatient_length_of_stay].to_i.zero?
    q = q.where{ |o| Sequel.date_sub(o.end_date, o.start_date) > options[:inpatient_length_of_stay].to_i }
  end

  if options[:inpatient_return_date] != 'Admit Date'
    q = q.select(*(query_cols - [:start_date])).select_append(:end_date___start_date)
  end

  q.from_self.select(*dynamic_columns).from_self
end
valid_outpatient_events() click to toggle source
# File lib/conceptql/operators/one_in_two_out.rb, line 78
def valid_outpatient_events

  min_gap = options[:outpatient_minimum_gap] || "30d"

  max_gap = options[:outpatient_maximum_gap]

  q = outpatient_events.from_self(alias: :initial)
        .join(outpatient_events.as(:confirm), initial__person_id: :confirm__person_id)
        .exclude(initial__criterion_id: :confirm__criterion_id)

  # In order to avoid many more comparisons of initial to confirm events, we now
  # filter the join by having only confirm events that come on or after initial events
  #
  # This ensures that initial events represent initial events and confirm events
  # represent confirming events
  q = q.exclude{confirm__start_date < initial__start_date}

  if min_gap.present?
    q = q.where { confirm__start_date >= DateAdjuster.new(min_gap).adjust(:initial__start_date) }
  end

  if max_gap.present?
    q = q.where { confirm__start_date <= DateAdjuster.new(max_gap).adjust(:initial__start_date) }
  end

  if options[:outpatient_event_to_return] != 'Initial Event'
    q = q.select_all(:confirm)
  else
    q = q.select_all(:initial)
  end

  q.from_self
end