from flask import Flask, request, jsonify import psycopg2 app = Flask(__name__) # Dictionary to store the status counts for each source status_counts = { "source1": [], "source2": [], "source3": [], "source4": [], "source5": [] } # Dictionary to keep track of OK counts for each source ok_counts = { "source1": 0, "source2": 0, "source3": 0, "source4": 0, "source5": 0 } # Dictionary to track if a source is in force send mode force_send_mode = { "source1": False, "source2": False, "source3": False, "source4": False, "source5": False } def check_thres(): # call database # check threshold # return resulting value pass # Function to check if there are 5 consecutive failures def check_consecutive_failures(source, status_list): if len(status_list) >= 5 and status_list[-5:] == ['FAIL'] * 5: print(f"Source {source} has 5 consecutive FAILs!") force_send_mode[source] = True send_message(source, force_send=True) # Function to send a message def send_message(source, force_send=False): if force_send or ok_counts[source] >= 10: print(f"Sending message for {source} - Status: {'FORCE SEND' if force_send else 'OK SEND'}") ok_counts[source] = 0 # Reset counter after sending # Route to receive data from sources @app.route('/status', methods=['POST']) def receive_status(): data = request.json source = data.get('source') status = data.get('status') if source in status_counts: status_counts[source].append(status) # Ensure we keep only the latest 5 statuses to check for consecutive FAILs if len(status_counts[source]) > 5: status_counts[source].pop(0) if status == 'OK': ok_counts[source] += 1 if force_send_mode[source] and ok_counts[source] >= 10: # Revert to normal mode after 10 consecutive OKs force_send_mode[source] = False ok_counts[source] = 0 elif not force_send_mode[source]: send_message(source) else: ok_counts[source] = 0 # Reset counter on FAIL check_consecutive_failures(source, status_counts[source]) return jsonify({"message": "Status received"}), 200 if __name__ == '__main__': app.run(debug=True, port=5000)