Skip to content

Commit

Permalink
Merge pull request #112 from ral-facilities/111_create_count_queries_…
Browse files Browse the repository at this point in the history
…for_table

111 create count queries for table
  • Loading branch information
louise-davies authored Nov 5, 2019
2 parents 7140077 + b9293c8 commit 274c0c1
Showing 1 changed file with 50 additions and 9 deletions.
59 changes: 50 additions & 9 deletions common/database_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ def _set_filter_fields(self):
self.included_field = self.field.split(".")[1]
self.field = self.field.split(".")[0]


def apply_filter(self, query):
try:
field = getattr(query.table, self.field)
Expand Down Expand Up @@ -558,15 +557,15 @@ def get_investigations_for_user_count(user_id, filters):
class InstrumentFacilityCyclesQuery(ReadQuery):
def __init__(self, instrument_id):
super().__init__(FACILITYCYCLE)
investigationInstrument = aliased(INSTRUMENT)
investigation_instrument = aliased(INSTRUMENT)
self.base_query = self.base_query \
.join(FACILITYCYCLE.FACILITY) \
.join(FACILITY.INSTRUMENT) \
.join(FACILITY.INVESTIGATION) \
.join(INVESTIGATION.INVESTIGATIONINSTRUMENT) \
.join(investigationInstrument, INVESTIGATIONINSTRUMENT.INSTRUMENT) \
.join(investigation_instrument, INVESTIGATIONINSTRUMENT.INSTRUMENT) \
.filter(INSTRUMENT.ID == instrument_id) \
.filter(investigationInstrument.ID == INSTRUMENT.ID) \
.filter(investigation_instrument.ID == INSTRUMENT.ID) \
.filter(INVESTIGATION.STARTDATE >= FACILITYCYCLE.STARTDATE) \
.filter(INVESTIGATION.STARTDATE <= FACILITYCYCLE.ENDDATE)

Expand All @@ -583,6 +582,23 @@ def get_facility_cycles_for_instrument(instrument_id, filters):
return get_filtered_read_query_results(filter_handler, filters, query)


class InstrumentFacilityCyclesCountQuery(CountQuery):

def __init__(self, instrument_id):
super().__init__(FACILITYCYCLE)
investigation_instrument = aliased(INSTRUMENT)
self.base_query = self.base_query\
.join(FACILITYCYCLE.FACILITY) \
.join(FACILITY.INSTRUMENT) \
.join(FACILITY.INVESTIGATION) \
.join(INVESTIGATION.INVESTIGATIONINSTRUMENT) \
.join(investigation_instrument, INVESTIGATIONINSTRUMENT.INSTRUMENT) \
.filter(INSTRUMENT.ID == instrument_id) \
.filter(investigation_instrument.ID == INSTRUMENT.ID) \
.filter(INVESTIGATION.STARTDATE >= FACILITYCYCLE.STARTDATE) \
.filter(INVESTIGATION.STARTDATE <= FACILITYCYCLE.ENDDATE)


def get_facility_cycles_for_instrument_count(instrument_id, filters):
"""
Given an instrument_id get the facility cycles count where the instrument has investigations that occur within
Expand All @@ -591,22 +607,26 @@ def get_facility_cycles_for_instrument_count(instrument_id, filters):
:param instrument_id: The id of the instrument
:return: The count of the facility cycles
"""
return len(get_facility_cycles_for_instrument(instrument_id, filters))
with InstrumentFacilityCyclesCountQuery(instrument_id) as query:
filter_handler = FilterOrderHandler()
filter_handler.add_filters(filters)
filter_handler.apply_filters(query)
return query.get_count()


class InstrumentFacilityCycleInvestigationsQuery(ReadQuery):
def __init__(self, instrument_id, facility_cycle_id):
super().__init__(INVESTIGATION)
investigationInstrument = aliased(INSTRUMENT)
investigation_instrument = aliased(INSTRUMENT)
self.base_query = self.base_query \
.join(INVESTIGATION.FACILITY) \
.join(FACILITY.FACILITYCYCLE) \
.join(FACILITY.INSTRUMENT) \
.join(INVESTIGATION.INVESTIGATIONINSTRUMENT) \
.join(investigationInstrument, INVESTIGATIONINSTRUMENT.INSTRUMENT) \
.join(investigation_instrument, INVESTIGATIONINSTRUMENT.INSTRUMENT) \
.filter(INSTRUMENT.ID == instrument_id) \
.filter(FACILITYCYCLE.ID == facility_cycle_id) \
.filter(investigationInstrument.ID == INSTRUMENT.ID) \
.filter(investigation_instrument.ID == INSTRUMENT.ID) \
.filter(INVESTIGATION.STARTDATE >= FACILITYCYCLE.STARTDATE) \
.filter(INVESTIGATION.STARTDATE <= FACILITYCYCLE.ENDDATE)

Expand All @@ -624,6 +644,23 @@ def get_investigations_for_instrument_in_facility_cycle(instrument_id, facility_
return get_filtered_read_query_results(filter_handler, filters, query)


class InstrumentFacilityCycleInvestigationsCountQuery(CountQuery):
def __init__(self, instrument_id, facility_cycle_id):
super().__init__(INVESTIGATION)
investigation_instrument = aliased(INSTRUMENT)
self.base_query = self.base_query \
.join(INVESTIGATION.FACILITY) \
.join(FACILITY.FACILITYCYCLE) \
.join(FACILITY.INSTRUMENT) \
.join(INVESTIGATION.INVESTIGATIONINSTRUMENT) \
.join(investigation_instrument, INVESTIGATIONINSTRUMENT.INSTRUMENT) \
.filter(INSTRUMENT.ID == instrument_id) \
.filter(FACILITYCYCLE.ID == facility_cycle_id) \
.filter(investigation_instrument.ID == INSTRUMENT.ID) \
.filter(INVESTIGATION.STARTDATE >= FACILITYCYCLE.STARTDATE) \
.filter(INVESTIGATION.STARTDATE <= FACILITYCYCLE.ENDDATE)


def get_investigations_for_instrument_in_facility_cycle_count(instrument_id, facility_cycle_id, filters):
"""
Given an instrument id and facility cycle id, get the count of the investigations that use the given instrument in
Expand All @@ -633,4 +670,8 @@ def get_investigations_for_instrument_in_facility_cycle_count(instrument_id, fac
:param facility_cycle_id: the ID of the facility cycle
:return: The investigations count
"""
return len(get_investigations_for_instrument_in_facility_cycle(instrument_id, facility_cycle_id, filters))
with InstrumentFacilityCycleInvestigationsCountQuery(instrument_id, facility_cycle_id) as query:
filter_handler = FilterOrderHandler()
filter_handler.add_filters(filters)
filter_handler.apply_filters(query)
return query.get_count()

0 comments on commit 274c0c1

Please sign in to comment.