def feedback_handler()

in backend_complete/src/main.py [0:0]


def feedback_handler(event, _):
    # Try to decode a JSON document from the body
    try:
        body = json.loads(event["body"])
    except json.JSONDecodeError:
        return helpers.message({"message": "Invalid JSON document"}, 400)

    # Validate the JSON document
    if "photo" not in body:
        return helpers.message({"message": "Missing 'photo' key in body"}, 400)

    if "dog" not in body:
        return helpers.message({"message": "Missing 'dog' key in body"}, 400)

    user_dog = body["dog"]

    # Try to extract the photo from the JSON document
    try:
        photo = base64.b64decode(body["photo"])
    except binascii.Error:
        return helpers.message({"message": "Invalid base64 string for 'photo'"}, 400)

    # Check if the system finds a dog
    dog = has_dog(photo)

    # Store if there was a dog or not as a custom metric
    helpers.metric("DogNoDog", "Dog", int(dog))

    # Store the type of error as a custom metric.
    # FalsePositive: there are no dogs in the picture but the system detected one.
    # FalseNegative: there is a dog in the picture but the system did not detect it.
    # Match: the system correctly detected that there is a dog.
    if dog and not user_dog:
        helpers.metric("DogNoDog", "FalsePositive", 1)
    elif not dog and user_dog:
        helpers.metric("DogNoDog", "FalseNegative", 1)
    else:
        helpers.metric("DogNoDog", "Match", 1)

    # Store the feedback on S3
    save_feedback(photo, user_dog, dog)

    # Send message back to the user
    return helpers.response({"message": "Feedback received"})