def get_status(patient_id): """Determine whether or not a specified patient is tachycardic based on previously available heart rate Args: patient_id: Specified patient id Returns: d (Response instance): Whether or not the patient is tachycardic and the time stamp associated with the most recent heart rate measurement Excepts: Input Error: If specified user does not exist EmptyHrListError: If no heart rate measurements exist for specified user """ connect("mongodb://*****:*****@ds037768.mlab.com:37768/bme_590") r = int(patient_id) try: check_id_exists(r) except InputError as inst4: logging.warning("Specified a user that does not exist") return jsonify({"message": inst4.message}) try: check_list_empty(r) except EmptyHrListError as inst5: logging.warning("No heart rate measurements exist for specified user") return jsonify({"message": inst5.message}) (age, recent_hr, recent_time_str) = data_for_is_tach(r) is_tach = is_tachycardic(age, recent_hr) if is_tach == 1: d = { "message": "patient is tachycardic", "timestamp of recent hr measurement": recent_time_str } logging.info("successfully returned that patient is tachycardic") return jsonify(d) if is_tach == 0: d = { "message": "patient is tachycardic", "timestamp of recent hr measurement": recent_time_str } logging.info("successfully returned that patient is not tachycardic") return jsonify(d)
def heart_rate(): """Saves heart rate measurement and time stamp (associated with a patient id) to database, checks if patient is tachycardic, and sends email if patient is tachycardic Returns: Result (Response instance): Description noting successful addition of heart rate or explanation of error exception Excepts: InputError: If specified user does not exist ValidationError: If a required key is not present in request """ connect("mongodb://*****:*****@ds037768.mlab.com:37768/bme_590") r = request.get_json() # parses input request data as json patient_id = r["patient_id"] hr = r["heart_rate"] try: check_id_exists(patient_id) except InputError as inst3: logging.warning("Specified a user that does not exist") return jsonify({"message": inst3.message}) try: validate_post_heart_rate(r) except ValidationError as inst4: logging.warning(inst4.message) return jsonify({"message": inst4.message}) dt = str(datetime.datetime.now()) id_user = User.objects.raw({"_id": patient_id}) id_user.update({"$push": {"heart_rate": r["heart_rate"]}}) id_user.update({"$push": {"time_stamp": dt}}) logging.info("stored heart rate measurement and associated time stamp") (age, recent_hr, recent_time_str) = data_for_is_tach(patient_id) is_tach = is_tachycardic(age, hr) if is_tach == 1: send_email(patient_id, recent_time_str) logging.info("Patient {0} is tachycardic " "and email sent".format(patient_id)) result = {"message": "heartrate added successfully to the database"} logging.info("heartrate added successfully to the database") return jsonify(result)
def test_check_id_exists(): connect("mongodb://*****:*****@ds037097.mlab.com:3" "7097/_test_database_") patient = test_User(patient_id=1, attending_email="*****@*****.**", user_age=20) patient.save() patient = test_User(patient_id=2, attending_email="*****@*****.**", user_age=20) patient.save() patient = test_User(patient_id=3, attending_email="*****@*****.**", user_age=20) patient.save() with pytest.raises(InputError): check_id_exists(1000)
def get_average_heart_rate(patient_id): """Calculates patient's average heart rate over all measurements stored for specified user Args: patient_id: Specified patient id Returns: avg_heart_rate (Response instance): average heart rate over all measurements stored for specified user Excepts: Input Error: If specified user does not exist EmptyHrListError: If no heart rate measurements exist for specified user """ connect("mongodb://*****:*****@ds037768.mlab.com:37768/bme_590") r = int(patient_id) try: check_id_exists(r) except InputError as inst4: logging.warning("Specified a user that does not exist") return jsonify({"message": inst4.message}) try: check_list_empty(r) except EmptyHrListError as inst5: logging.warning("No heart rate measurements exist for specified user") return jsonify({"message": inst5.message}) p = User.objects.raw({"_id": r}).first() hr_list = p.heart_rate avg_heart_rate = mean(hr_list) logging.info("Successfully returned average " "heart rate for specified user") return jsonify(avg_heart_rate)
def get_heart_rates(patient_id): """Returns all previous heart rate measurements for specified patient Args: patient_id: Specified patient id Returns: hr_list (Response instance): all previous heart rate measurements for specified patient Excepts: Input Error: If specified user does not exist EmptyHrListError: if no heart rate measurements exist for specified user """ connect("mongodb://*****:*****@ds037768.mlab.com:37768/bme_590") r = int(patient_id) try: check_id_exists(r) except InputError as inst4: logging.warning("Specified a user that does not exist") return jsonify({"message": inst4.message}) try: check_list_empty(r) except EmptyHrListError as inst5: logging.warning("No heart rate measurements exist for specified user") return jsonify({"message": inst5.message}) p = User.objects.raw({"_id": r}).first() hr_list = p.heart_rate logging.info("Successfully returned all previous " "heart rate measurements for specified patient") return jsonify(hr_list)
def internal_average(): """Calculates patient's internal average heart rate over measurements since a specified time Returns: internal_avg (Response instance): heart rate average since specified time\ Excepts: ValidationError: If a required key is not present in request ValueError: If inputted time_stamp data for 'heart_rate_average_since' with incorrect format Input Error: If specified user does not exist EmptyHrListError: If no heart rate measurements exist for specified user NoHrSinceError: If no heart rate measurements exist since specified time """ connect("mongodb://*****:*****@ds037768.mlab.com:37768/bme_590") r = request.get_json() try: validate_post_int_avg(r) except ValidationError as inst: logging.warning(inst.message) return jsonify({"message": inst.message}) patient_id_str = r["patient_id"] patient_id = int(patient_id_str) try: d = datetime.datetime.strptime(r["heart_rate_average_since"], "%Y-%m-%d %H:%M:%S.%f") except ValueError: logging.warning("inputted time_stamp data for " "'heart_rate_average_since' " "with incorrect format. " "Use format '%Y-%m-%d %H:%M:%S.%f'") return jsonify("inputted time_stamp data for " "'heart_rate_average_since' " "with incorrect format. " "Use format '%Y-%m-%d %H:%M:%S.%f'") try: check_id_exists(patient_id) except InputError: logging.warning("Specified a user that does not exist") return jsonify("Specified a user that does not exist") try: check_list_empty(patient_id) except EmptyHrListError as inst5: logging.warning("No heart rate measurements exist for specified user") return jsonify({"message": inst5.message}) (time_stamps, hr_list) = data_for_internal_average(patient_id) heart_rates_since = hr_since_specified_time(d, time_stamps, hr_list) try: check_hr_since_list_empty(heart_rates_since) except NoHrSinceError: logging.warning("No heart rate measurements " "taken since specified date") return jsonify("No heart rate measurements " "taken since specified date") internal_avg = mean(heart_rates_since) return jsonify(internal_avg)