def get_count_with_filters(client, entity_type, filters):
    """
    Get the number of results of a given entity, based on the filters provided in the
    request. This acts very much like `get_entity_with_filters()` but returns the number
    of results, as opposed to a JSON object of data.

    :param client: ICAT client containing an authenticated user
    :type client: :class:`icat.client.Client`
    :param entity_type: The type of entity requested to manipulate data with
    :type entity_type: :class:`str`
    :param filters: The list of filters to be applied to the request
    :type filters: List of specific implementations :class:`QueryFilter`
    :return: The number of records of the given entity (of type integer), using the
        filters to restrict the result of the query
    """
    log.info(
        "Getting the number of results of %s, also using the request's filters",
        entity_type,
    )

    query = ICATQuery(client, entity_type, aggregate="COUNT")

    filter_handler = FilterOrderHandler()
    filter_handler.manage_icat_filters(filters, query.query)

    data = query.execute_query(client, True)

    # Only ever 1 element in a count query result
    return data[0]
def get_investigations_for_instrument_in_facility_cycle(
    client, instrument_id, facilitycycle_id, filters, count_query=False,
):
    """
    Given Instrument and Facility Cycle IDs, get investigations that use the given
    instrument in the given cycle

    :param client: ICAT client containing an authenticated user
    :type client: :class:`icat.client.Client`
    :param instrument_id: ID of the instrument from the request
    :type instrument_id: :class:`int`
    :param facilitycycle_id: ID of the facilityCycle from the request
    :type facilitycycle_id: :class:`int`
    :param filters: The list of filters to be applied to the request
    :type filters: List of specific implementations :class:`QueryFilter`
    :param count_query: Flag to determine if the query in this function should be used
        as a count query. Used for
        `get_investigations_for_instrument_in_facility_cycle_count()`
    :type count_query: :class:`bool`
    :return: A list of Investigations that match the query
    """
    log.info(
        "Getting a list of investigations from the specified instrument and facility"
        " cycle, for ISIS",
    )

    query_aggregate = "COUNT:DISTINCT" if count_query else "DISTINCT"
    query = ICATQuery(client, "Investigation", aggregate=query_aggregate)
    query.isis_endpoint = True

    instrument_id_check = PythonICATWhereFilter(
        "facility.instruments.id", instrument_id, "eq",
    )
    investigation_instrument_id_check = PythonICATWhereFilter(
        "investigationInstruments.instrument.id", instrument_id, "eq",
    )
    facility_cycle_id_check = PythonICATWhereFilter(
        "facility.facilityCycles.id", facilitycycle_id, "eq",
    )
    facility_cycle_start_date_check = PythonICATWhereFilter(
        "facility.facilityCycles.startDate", "o.startDate", "lte",
    )
    facility_cycle_end_date_check = PythonICATWhereFilter(
        "facility.facilityCycles.endDate", "o.startDate", "gte",
    )

    required_filters = [
        instrument_id_check,
        investigation_instrument_id_check,
        facility_cycle_id_check,
        facility_cycle_start_date_check,
        facility_cycle_end_date_check,
    ]
    filters.extend(required_filters)
    filter_handler = FilterOrderHandler()
    filter_handler.manage_icat_filters(filters, query.query)

    data = query.execute_query(client, True)

    return data
def get_entity_with_filters(client, entity_type, filters):
    """
    Gets all the records of a given entity, based on the filters provided in the request

    :param client: ICAT client containing an authenticated user
    :type client: :class:`icat.client.Client`
    :param entity_type: The type of entity requested to manipulate data with
    :type entity_type: :class:`str`
    :param filters: The list of filters to be applied to the request
    :type filters: List of specific implementations :class:`QueryFilter`
    :return: The list of records of the given entity, using the filters to restrict the
        result of the query
    """
    log.info("Getting entity using request's filters")

    query = ICATQuery(client, entity_type)

    filter_handler = FilterOrderHandler()
    filter_handler.manage_icat_filters(filters, query.query)

    data = query.execute_query(client, True)

    return data