def classify_flight_delays_realtime(): """POST API for classifying flight delays""" # Define the form fields to process api_field_type_map = \ { "DepDelay": float, "Carrier": str, "FlightDate": str, "Dest": str, "FlightNum": str, "Origin": str } # Fetch the values for each field from the form object api_form_values = {} for api_field_name, api_field_type in api_field_type_map.items(): api_form_values[api_field_name] = request.form.get(api_field_name, type=api_field_type) # Set the direct values, which excludes Date prediction_features = {} for key, value in api_form_values.items(): prediction_features[key] = value # Set the derived values prediction_features['Distance'] = predict_utils.get_flight_distance( client, api_form_values['Origin'], api_form_values['Dest'] ) # Turn the date into DayOfYear, DayOfMonth, DayOfWeek date_features_dict = predict_utils.get_regression_date_args( api_form_values['FlightDate'] ) for api_field_name, api_field_value in date_features_dict.items(): prediction_features[api_field_name] = api_field_value # Add a timestamp prediction_features['Timestamp'] = predict_utils.get_current_timestamp() # Create a unique ID for this message unique_id = str(uuid.uuid4()) prediction_features['UUID'] = unique_id message_bytes = json.dumps(prediction_features).encode() producer.send(PREDICTION_TOPIC, message_bytes) response = {"status": "OK", "id": unique_id} return json_util.dumps(response)
def classify_flight_delays(): """POST API for classifying flight delays""" api_field_type_map = \ { "DepDelay": float, "Carrier": str, "FlightDate": str, "Dest": str, "FlightNum": str, "Origin": str } api_form_values = {} for api_field_name, api_field_type in api_field_type_map.items(): api_form_values[api_field_name] = request.form.get(api_field_name, type=api_field_type) # Set the direct values, which excludes Date prediction_features = {} for key, value in api_form_values.items(): prediction_features[key] = value # Set the derived values prediction_features['Distance'] = predict_utils.get_flight_distance( client, api_form_values['Origin'], api_form_values['Dest'] ) # Turn the date into DayOfYear, DayOfMonth, DayOfWeek date_features_dict = predict_utils.get_regression_date_args( api_form_values['FlightDate'] ) for api_field_name, api_field_value in date_features_dict.items(): prediction_features[api_field_name] = api_field_value # Add a timestamp prediction_features['Timestamp'] = predict_utils.get_current_timestamp() client.agile_data_science.prediction_tasks.insert_one( prediction_features ) return json_util.dumps(prediction_features)