def feedback(): """Feedback Service to provide user input on which false predictions this model provided.""" try: content = request.json logging.info("id: {} ".format(content["lad_id"])) logging.info("anomaly: {} ".format(content["is_anomaly"])) logging.info("notes: {} ".format(content["notes"])) if not content["lad_id"] or not content["is_anomaly"]: raise Exception("This service requires that you provide the id" + ", anomaly=True|False and notes are optional ") fs = FactStore() # Note id is the prediction id that is found in the email. if (fs.write_feedback(predict_id=content["lad_id"], notes=content["notes"], anomaly_status=bool(content["is_anomaly"])) is False): raise Exception("Predict ID must be unique. This anomaly" + " feedback has been reported before") except Exception as e: logging.info(e) result = {"feedback_service": "failure", "error_msg": "{0}".format(e)} return make_response(jsonify(result), 403) else: result = {"feedback_service": "success"} return make_response(jsonify(result), 200) return ""
def false_anomaly(): """Tag false anomalies in database.""" content = request.get_json() fs = FactStore() id = content["predict_id"] # Tracking event in fact-store res = fs.write_event(content["predict_id"], content["message"], content["score"], content["anomaly_status"]) # Returning status if this anomaly is real anomaly_status== false return jsonify({"false_anomaly": res})
def feedback(): """Feedback Service to provide user input on which false predictions this model provided.""" try: content = request.json # When deploying fact_store per customer you should set env var customer_id = os.getenv("CUSTOMER_ID") logging.info("id: {} ".format(content["lad_id"])) logging.info("anomaly: {} ".format(content["is_anomaly"])) logging.info("message: {} ".format(content["message"])) logging.info("customer_id: {} ".format(customer_id)) if not content["lad_id"] or not content["is_anomaly"]: raise Exception("This service requires that you provide the id" + ", anomaly=True|False and notes are optional ") fs = FactStore() # Record feedback HUMAN_FEEDBACK_COUNT.labels( customer_id=customer_id, anomaly_status=content["is_anomaly"]).inc() # Note id is the prediction id that is found in the email. if (fs.write_feedback(predict_id=content["lad_id"], message=content["message"], anomaly_status=bool(content["is_anomaly"]), customer_id=customer_id) is False): raise Exception("Predict ID must be unique. This anomaly" + " feedback has been reported before") except Exception as e: logging.info(e) HUMAN_FEEDBACK_ERROR_COUNT.labels( err_msg="failure to write to db in factstore").inc() result = {"feedback_service": "failure", "error_msg": "{0}".format(e)} return make_response(jsonify(result), 403) else: result = {"feedback_service": "success"} return make_response(jsonify(result), 200) return ""
def test_feedback_inserted(): """Test inserting events into the fact_store.""" fact_store = FactStore(True) fact_store.session.query(FeedbackModel).delete() fact_store.write_feedback( predict_id="a2b35c5b-016d-4e2c-8ec5-87d1b962b2f8", notes="222JSJSJJS", anomaly_status=True) fact_store.write_feedback( predict_id="18bd090d-ae27-4b19-a0db-ed5f589b4e2e", notes="SSJJSJS", anomaly_status=True) fact_store.write_feedback( predict_id="74a6b1bd-efea-4e7b-87a9-8f7330885160", notes="AJJSJS", anomaly_status=False) items = fact_store.readall_feedback() assert len(items) is 3
def test_events_inserted(): """Test inserting feedback into the fact_store.""" fact_store = FactStore(True) fact_store.session.query(EventModel).delete() fact_store.write_event(predict_id="a2b35c5b-016d-4e2c-8ec5-87d1b962b2f8", message="kssksjs", score=3.1, anomaly_status=True) fact_store.write_event(predict_id="18bd090d-ae27-4b19-a0db-ed5f589b4e2e", message="JSJSJS", score=2.2, anomaly_status=False) fact_store.write_event(predict_id="74a6b1bd-efea-4e7b-87a9-8f7330885160", message="sjsjsjsj", score=8.3, anomaly_status=True) items = fact_store.session.query(EventModel).all() assert len(items) is 3
def false_positive(): """Service to provide list of false anomalies to be relabeled during ml training run.""" fs = FactStore() df = fs.readall_false_positive() return jsonify({"feedback": df})