Skip to content

Commit

Permalink
Merge pull request #448 from ral-facilities/remove-isis-endpoints-#432
Browse files Browse the repository at this point in the history
feat!: Remove code and references to ISIS specific table endpoints
  • Loading branch information
MRichards99 authored Sep 1, 2023
2 parents 60c539c + e667c78 commit 2825ea2
Show file tree
Hide file tree
Showing 18 changed files with 13 additions and 1,455 deletions.
10 changes: 4 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -509,19 +509,17 @@ api.add_resource(get_endpoint_resource, f"/{entity_name.lower()}")

## Endpoints

The logic for each endpoint is within `/src/resources`. They are split into entities,
non_entities and table_endpoints.
The logic for each endpoint is within `/src/resources` - they're split into entities and
non_entities.

The entities package contains `entity_map` which
maps entity names to their field name used in backend-specific code. The Database
Backend uses this for its mapped classes (explained below) and the Python ICAT Backend
uses this for interacting with ICAT objects within Python ICAT. In most instances, the
dictionary found in `entity_map.py` is simply mapping the plural entity name (used to
build the entity endpoints) to the singular version. The `entity_endpoint` module
contains the function that is used to generate endpoints at start up. `table_endpoints`
contains the endpoint classes that are table specific (currently these are the ISIS
specific endpoints required for their use cases). Finally, `non_entities` contains the
session endpoint for session handling.
contains the function that is used to generate endpoints at start up. Finally,
`non_entities` contains the session endpoint for session handling.

## Logging

Expand Down
59 changes: 5 additions & 54 deletions datagateway_api/src/api_start_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
from datagateway_api.src.resources.non_entities.sessions_endpoints import (
session_endpoints,
)
from datagateway_api.src.swagger.apispec_flask_restful import RestfulPlugin
from datagateway_api.src.swagger.initialise_spec import (
initialise_datagateway_api_spec,
initialise_search_api_spec,
)

if Config.config.search_api:
from datagateway_api.src.resources.search_api_endpoints import (
Expand All @@ -37,17 +42,6 @@
get_search_endpoint,
get_single_endpoint,
)
from datagateway_api.src.resources.table_endpoints.table_endpoints import ( # noqa: I202, B950
count_instrument_facility_cycles_endpoint,
count_instrument_investigation_endpoint,
instrument_facility_cycles_endpoint,
instrument_investigation_endpoint,
)
from datagateway_api.src.swagger.apispec_flask_restful import RestfulPlugin
from datagateway_api.src.swagger.initialise_spec import (
initialise_datagateway_api_spec,
initialise_search_api_spec,
)

log = logging.getLogger()

Expand Down Expand Up @@ -232,49 +226,6 @@ def create_api_endpoints(flask_app, api, specs):
)
datagateway_api_spec.path(resource=session_endpoint_resource, api=api)

# Table specific endpoints
instrument_facility_cycle_resource = instrument_facility_cycles_endpoint(
backend, client_pool=icat_client_pool,
)
api.add_resource(
instrument_facility_cycle_resource,
f"{datagateway_api_extension}/instruments/<int:id_>/facilitycycles",
endpoint="datagateway_isis_instrument_facility_cycle",
)
datagateway_api_spec.path(resource=instrument_facility_cycle_resource, api=api)

count_instrument_facility_cycle_res = count_instrument_facility_cycles_endpoint(
backend, client_pool=icat_client_pool,
)
api.add_resource(
count_instrument_facility_cycle_res,
f"{datagateway_api_extension}/instruments/<int:id_>/facilitycycles/count",
endpoint="datagateway_isis_count_instrument_facility_cycle",
)
datagateway_api_spec.path(resource=count_instrument_facility_cycle_res, api=api)

instrument_investigation_resource = instrument_investigation_endpoint(
backend, client_pool=icat_client_pool,
)
api.add_resource(
instrument_investigation_resource,
f"{datagateway_api_extension}/instruments/<int:instrument_id>"
f"/facilitycycles/<int:cycle_id>/investigations",
endpoint="datagateway_isis_instrument_investigation",
)
datagateway_api_spec.path(resource=instrument_investigation_resource, api=api)

count_instrument_investigation_res = count_instrument_investigation_endpoint(
backend, client_pool=icat_client_pool,
)
api.add_resource(
count_instrument_investigation_res,
f"{datagateway_api_extension}/instruments/<int:instrument_id>"
f"/facilitycycles/<int:cycle_id>/investigations/count",
endpoint="datagateway_isis_count_instrument_investigation",
)
datagateway_api_spec.path(resource=count_instrument_investigation_res, api=api)

# Ping endpoint
ping_resource = ping_endpoint(backend, client_pool=icat_client_pool)
api.add_resource(ping_resource, f"{datagateway_api_extension}/ping")
Expand Down
62 changes: 0 additions & 62 deletions datagateway_api/src/datagateway_api/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,65 +151,3 @@ def update_with_id(self, session_id, entity_type, id_, data):
:return: The updated entity.
"""
pass

@abstractmethod
def get_facility_cycles_for_instrument_with_filters(
self, session_id, instrument_id, filters,
):
"""
Given an instrument_id get facility cycles where the instrument has
investigations that occur within that cycle
:param session_id: The session id of the requesting user
:param filters: The filters to be applied to the query
:param instrument_id: The id of the instrument
:return: A list of facility cycle entities
"""
pass

@abstractmethod
def get_facility_cycles_for_instrument_count_with_filters(
self, session_id, instrument_id, filters,
):
"""
Given an instrument_id get the facility cycles count where the instrument has
investigations that occur within that cycle
:param session_id: The session id of the requesting user
:param filters: The filters to be applied to the query
:param instrument_id: The id of the instrument
:return: The count of the facility cycles
"""
pass

@abstractmethod
def get_investigations_for_instrument_facility_cycle_with_filters(
self, session_id, instrument_id, facilitycycle_id, filters,
):
"""
Given an instrument id and facility cycle id, get investigations that use the
given instrument in the given cycle
:param session_id: The session id of the requesting user
:param filters: The filters to be applied to the query
:param instrument_id: The id of the instrument
:param facility_cycle_id: the ID of the facility cycle
:return: The investigations
"""
pass

@abstractmethod
def get_investigation_count_instrument_facility_cycle_with_filters(
self, session_id, instrument_id, facilitycycle_id, filters,
):
"""
Given an instrument id and facility cycle id, get the count of the
investigations that use the given instrument in the given cycle
:param session_id: The session id of the requesting user
:param filters: The filters to be applied to the query
:param instrument_id: The id of the instrument
:param facility_cycle_id: the ID of the facility cycle
:return: The investigations count
"""
pass
36 changes: 0 additions & 36 deletions datagateway_api/src/datagateway_api/database/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,8 @@
create_rows_from_json,
db,
delete_row_by_id,
get_facility_cycles_for_instrument,
get_facility_cycles_for_instrument_count,
get_filtered_row_count,
get_first_filtered_row,
get_investigations_for_instrument_in_facility_cycle,
get_investigations_for_instrument_in_facility_cycle_count,
get_row_by_id,
get_rows_by_filter,
insert_row_into_table,
Expand Down Expand Up @@ -127,35 +123,3 @@ def delete_with_id(self, session_id, entity_type, id_, **kwargs):
def update_with_id(self, session_id, entity_type, id_, data, **kwargs):
table = get_entity_object_from_name(entity_type)
return update_row_from_id(table, id_, data)

@requires_session_id
@queries_records
def get_facility_cycles_for_instrument_with_filters(
self, session_id, instrument_id, filters, **kwargs,
):
return get_facility_cycles_for_instrument(instrument_id, filters)

@requires_session_id
@queries_records
def get_facility_cycles_for_instrument_count_with_filters(
self, session_id, instrument_id, filters, **kwargs,
):
return get_facility_cycles_for_instrument_count(instrument_id, filters)

@requires_session_id
@queries_records
def get_investigations_for_instrument_facility_cycle_with_filters(
self, session_id, instrument_id, facilitycycle_id, filters, **kwargs,
):
return get_investigations_for_instrument_in_facility_cycle(
instrument_id, facilitycycle_id, filters,
)

@requires_session_id
@queries_records
def get_investigation_count_instrument_facility_cycle_with_filters(
self, session_id, instrument_id, facilitycycle_id, filters, **kwargs,
):
return get_investigations_for_instrument_in_facility_cycle_count(
instrument_id, facilitycycle_id, filters,
)
150 changes: 1 addition & 149 deletions datagateway_api/src/datagateway_api/database/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import logging

from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import aliased

from datagateway_api.src.common.exceptions import (
AuthenticationError,
Expand All @@ -18,14 +17,7 @@
DatabaseIncludeFilter as IncludeFilter,
DatabaseWhereFilter as WhereFilter,
)
from datagateway_api.src.datagateway_api.database.models import (
FACILITY,
FACILITYCYCLE,
INSTRUMENT,
INVESTIGATION,
INVESTIGATIONINSTRUMENT,
SESSION,
)
from datagateway_api.src.datagateway_api.database.models import SESSION


log = logging.getLogger()
Expand Down Expand Up @@ -394,143 +386,3 @@ def patch_entities(table, json_list):
raise BadRequestError(f" Bad request made, request: {json_list}")

return results


class InstrumentFacilityCyclesQuery(ReadQuery):
def __init__(self, instrument_id):
super().__init__(FACILITYCYCLE)
investigation_instrument = aliased(INSTRUMENT)
self.base_query = (
self.base_query.join(FACILITYCYCLE.FACILITY)
.join(FACILITY.instruments)
.join(FACILITY.investigations)
.join(INVESTIGATION.investigationInstruments)
.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(instrument_id, filters):
"""
Given an instrument_id get facility cycles where the instrument has investigations
that occur within that cycle
:param filters: The filters to be applied to the query
:param instrument_id: The id of the instrument
:return: A list of facility cycle entities
"""
with InstrumentFacilityCyclesQuery(instrument_id) as query:
filter_handler = FilterOrderHandler()
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.instruments)
.join(FACILITY.investigations)
.join(INVESTIGATION.investigationInstruments)
.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 that cycle
:param filters: The filters to be applied to the query
:param instrument_id: The id of the instrument
:return: The count of the facility cycles
"""
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)
investigation_instrument = aliased(INSTRUMENT)
self.base_query = (
self.base_query.join(INVESTIGATION.FACILITY)
.join(FACILITY.facilityCycles)
.join(FACILITY.instruments)
.join(INVESTIGATION.investigationInstruments)
.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(
instrument_id, facility_cycle_id, filters,
):
"""
Given an instrument id and facility cycle id, get investigations that use the given
instrument in the given cycle
:param filters: The filters to be applied to the query
:param instrument_id: The id of the instrument
:param facility_cycle_id: the ID of the facility cycle
:return: The investigations
"""
filter_handler = FilterOrderHandler()
with InstrumentFacilityCycleInvestigationsQuery(
instrument_id, facility_cycle_id,
) as query:
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.facilityCycles)
.join(FACILITY.instruments)
.join(INVESTIGATION.investigationInstruments)
.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 the given cycle
:param filters: The filters to be applied to the query
:param instrument_id: The id of the instrument
:param facility_cycle_id: the ID of the facility cycle
:return: The investigations count
"""
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()
Loading

0 comments on commit 2825ea2

Please sign in to comment.