def FindHospitalsNearestToPointN(
        self, request: FindHospitalsNearestToPointNRequest, context
    ):
        coordinate = request.coordinate
        lat = coordinate.lat
        lon = coordinate.lon

        previous_page_token = request.previous_page_token
        offset = previous_page_token + request.page_size

        wkt_element = create_point_wkt_element(lat, lon)

        with db.db_session(db.DB_ENGINE) as sesh:
            query = (
                sesh.query(Db_Hospital)
                .order_by(
                    Comparator.distance_centroid(Db_Hospital.location, wkt_element)
                )
                .slice(previous_page_token, offset)
            )

            results: List[Db_Hospital] = query.all()
        print(f'Got {len(results)} hospitals')
        hospitals = [h.as_proto_hospital() for h in results]

        next_page_token = None
        if len(results) < request.page_size:
            next_page_token = -1
        else:
            next_page_token = offset
        print(next_page_token)
        return FindHospitalsNearestToPointNResponse(
            hospitals=hospitals,
            next_page_token=next_page_token,
        )
    def FindHospitalsInBoundary(self, request: FindHospitalsInBoundaryRequest, context):
        boundary = request.geom
        previous_page_token = request.previous_page_token
        offset = previous_page_token + request.page_size
        # deserialize and query
        # get points nearest to and paginate
        shapely_geometry = shapely_grpc_deserializer.deserialize(boundary)
        print(boundary)
        print(shapely_geometry)

        intersects_boundary = from_shape(shapely_geometry, srid=4326)
        print(intersects_boundary)
        with db.db_session(db.DB_ENGINE) as sesh:
            query = sesh.query(Db_Hospital).filter(
                Db_Hospital.location.ST_Intersects(intersects_boundary)
            )
            query = query.slice(previous_page_token, offset)
            results: List[Db_Hospital] = query.all()

        next_page_token = None
        if len(results) < request.page_size:
            next_page_token = -1
        else:
            next_page_token = offset

        hospitals = [h.as_proto_hospital() for h in results]
        return FindHospitalsInBoundaryResponse(
            hospitals=hospitals,
            next_page_token=next_page_token,
        )
    def FindHospital(self, request: SearchHospitalRequest, context):
        search_key = request.name

        previous_page_token = request.previous_page_token
        offset = previous_page_token + request.page_size
        # search hospital
        with db.db_session(db.DB_ENGINE) as sesh:
            query = (
                sesh.query(Db_Hospital)
                .filter(Db_Hospital.name.like(f"%{search_key}%"))
                .slice(previous_page_token, offset)
            )
            results: List[Db_Hospital] = query.all()

        next_page_token = None
        if len(results) < request.page_size:
            next_page_token = -1
        else:
            next_page_token = offset

        pb_hospitals = [h.as_proto_hospital() for h in results]
        return SearchHospitalResponse(
            hospitals=pb_hospitals,
            previous_page_token=next_page_token,
        )
 def GetHospital(self, request: GetHospitalRequest, context):
     request_uuid = request.uuid
     # get hospital
     with db.db_session(db.DB_ENGINE) as sesh:
         query = sesh.query(Db_Hospital).filter(Db_Hospital.id == request_uuid)
         try:
             hospital = query.one()
             hospital_proto = hospital.as_proto_hospital()
             return hospital_proto
         except NoResultFound:
             raise NotFound(
                 details=f"Hospital with uuid -> {request_uuid} not found"
             )
         except DBError:
             raise InvalidArgument(f"{request_uuid} is invalid.")
    def FindHospitalsInCountry(self, request: FindHospitalsInCountryRequest, context):
        country = request.country

        # search hospital
        with db.db_session(db.DB_ENGINE) as sesh:
            query = (
                sesh.query(Db_Hospital)
                .filter(Db_Hospital.country == country)
            )
            print(str(query))
            results: List[Db_Hospital] = query.all()
        # next_page_token = None
        # if len(results) < request.page_size:
        #     next_page_token = -1
        # else:
        #     next_page_token = offset

        pb_hospitals = [h.as_proto_hospital() for h in results]
        return FindHospitalsInCountryResponse(
            hospitals=pb_hospitals,
        )
    def CreateHospital(self, request: CreateHospitalRequest, context):
        print(request)
        hospital = request.hospital

        name = hospital.name
        coordinate = hospital.location
        contacts = hospital.contacts
        facility_type = hospital.facility_type
        ownership = hospital.ownership
        country = hospital.country
        textual_description = hospital.textual_description
        email = hospital.email
        wkt_element = create_point_wkt_element(coordinate.lat, coordinate.lon)
        # create hospital
        if len(name) < 3:
            raise InvalidArgument("A valid hospital name is required")
        if coordinate.lat == 0.0 or coordinate.lon == 0.0:
            raise InvalidArgument("A valid hospital coordinate is required")
        if len(country) == 0:
            raise InvalidArgument("A valid country is required")

        with db.db_session(db.DB_ENGINE) as sesh:
            new_hospital = Db_Hospital(
                name=name,
                phone_contacts=contacts,
                ownership=ownership,
                country=country,
                facility_type=facility_type,
                email=email,
                textual_description=textual_description,
                location=wkt_element,
            )
            sesh.add(new_hospital)
            sesh.commit()

            return new_hospital.as_proto_hospital()
Beispiel #7
0
    port = server.add_insecure_port(_LISTEN_ADDRESS_TEMPLATE % SERVER_PORT)
    server.start()

    # try:
    #     yield server,port
    # finally:
    #     server.stop(0)
    return server, port


if __name__ == "__main__":
    print('Starting main')

    logging.basicConfig(level=logging.INFO)

    with db.db_session(db.DB_ENGINE) as sesh:
        import_hospitals_data(sesh)

    server, port = run_server()

    # with run_server(args.port) as (server,port):
    logging.info(f'Server running @ localhost:{port}')
    if gc.IS_DEBUG:
        print_all_timings()

    # try:
    #     while True:
    #         time.sleep(_ONE_DAY_IN_SECONDS)
    # except KeyboardInterrupt:
    #     print('KeyboardInterrupt')
    # server.stop(0)