Spaces:
Running
Running
Commit ·
9286802
1
Parent(s): da2774c
Almost Done
Browse files
server.py
CHANGED
|
@@ -137,8 +137,8 @@ def run_automation_process(session_id):
|
|
| 137 |
if workflow == 'void':
|
| 138 |
results = bot_instance.process_void_list(patient_data)
|
| 139 |
elif workflow == 'refund':
|
| 140 |
-
|
| 141 |
-
results = bot_instance.process_refund_list(patient_data,
|
| 142 |
is_terminated = bot_instance.termination_event.is_set()
|
| 143 |
except Exception as e:
|
| 144 |
print(f"[Server Log] Fatal error in automation thread: {e}"); is_crash = True
|
|
@@ -194,57 +194,62 @@ def status_page():
|
|
| 194 |
APP_STATUS_HTML = """<!DOCTYPE html><html lang="en"><head><title>API Status</title><style>body{font-family:sans-serif;display:flex;justify-content:center;align-items:center;height:100vh;margin:0;background:#f0f2f5;}.status-box{text-align:center;padding:40px 60px;background:white;border-radius:12px;box-shadow:0 8px 30px rgba(0,0,0,0.1);}h1{font-size:24px;color:#333;margin-bottom:10px;} .indicator{font-size:18px;font-weight:600;padding:8px 16px;border-radius:20px;}.active{color:#28a745;background-color:#e9f7ea;}</style></head><body><div class="status-box"><h1>Hillside Automation API</h1><div class="indicator active">● Active</div></div></body></html>"""
|
| 195 |
return Response(APP_STATUS_HTML)
|
| 196 |
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
|
| 202 |
-
@
|
| 203 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
session_id = 'user_session'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
try:
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
prn_lookup_dict = {extract_patient_name(row['Patient Name']): row['PRN'] for _, row in df_app_filtered.iterrows()}
|
| 217 |
|
| 218 |
master_df = df_quantum.copy()
|
| 219 |
master_df['PRN'] = master_df['Name'].apply(lambda name: prn_lookup_dict.get(name, ""))
|
| 220 |
master_df['Status'] = ''
|
| 221 |
-
|
| 222 |
session_data[session_id]['patient_data_for_report'] = master_df
|
| 223 |
session_data[session_id]['patient_data'] = master_df.to_dict('records')
|
| 224 |
|
| 225 |
socketio.emit('data_processed')
|
| 226 |
print(f"[Server Log] Data prepared. Total records: {len(master_df)}")
|
| 227 |
-
return jsonify({"message": "Data processed successfully."})
|
| 228 |
except Exception as e:
|
| 229 |
-
|
| 230 |
-
return jsonify({"error": str(e)}), 500
|
| 231 |
-
|
| 232 |
-
@socketio.on('connect')
|
| 233 |
-
def handle_connect():
|
| 234 |
-
print(f'Frontend connected.')
|
| 235 |
-
emit('email_list', {'emails': get_email_list()})
|
| 236 |
|
| 237 |
-
@socketio.on('initialize_session')
|
| 238 |
-
def handle_init(data):
|
| 239 |
-
session_id = 'user_session'
|
| 240 |
-
session_data[session_id] = {
|
| 241 |
-
'emails': data['emails'],
|
| 242 |
-
'filename': data['filename'],
|
| 243 |
-
'workflow': data['workflow'],
|
| 244 |
-
'start_date': data.get('start_date'),
|
| 245 |
-
'end_date': data.get('end_date')
|
| 246 |
-
}
|
| 247 |
-
|
| 248 |
@socketio.on('start_login')
|
| 249 |
def handle_login(credentials):
|
| 250 |
global bot_instance
|
|
@@ -278,4 +283,4 @@ if __name__ == '__main__':
|
|
| 278 |
print(f" Frontend URL: {FRONTEND_ORIGIN}")
|
| 279 |
print(f" Port: {os.getenv('PORT', 7860)}")
|
| 280 |
print("====================================================================")
|
| 281 |
-
socketio.run(app, host='0.0.0.0', port=int(os.getenv('PORT', 7860)))
|
|
|
|
| 137 |
if workflow == 'void':
|
| 138 |
results = bot_instance.process_void_list(patient_data)
|
| 139 |
elif workflow == 'refund':
|
| 140 |
+
start_date = data.get('start_date'); end_date = data.get('end_date')
|
| 141 |
+
results = bot_instance.process_refund_list(patient_data, start_date, end_date)
|
| 142 |
is_terminated = bot_instance.termination_event.is_set()
|
| 143 |
except Exception as e:
|
| 144 |
print(f"[Server Log] Fatal error in automation thread: {e}"); is_crash = True
|
|
|
|
| 194 |
APP_STATUS_HTML = """<!DOCTYPE html><html lang="en"><head><title>API Status</title><style>body{font-family:sans-serif;display:flex;justify-content:center;align-items:center;height:100vh;margin:0;background:#f0f2f5;}.status-box{text-align:center;padding:40px 60px;background:white;border-radius:12px;box-shadow:0 8px 30px rgba(0,0,0,0.1);}h1{font-size:24px;color:#333;margin-bottom:10px;} .indicator{font-size:18px;font-weight:600;padding:8px 16px;border-radius:20px;}.active{color:#28a745;background-color:#e9f7ea;}</style></head><body><div class="status-box"><h1>Hillside Automation API</h1><div class="indicator active">● Active</div></div></body></html>"""
|
| 195 |
return Response(APP_STATUS_HTML)
|
| 196 |
|
| 197 |
+
@socketio.on('connect')
|
| 198 |
+
def handle_connect():
|
| 199 |
+
print(f'Frontend connected.')
|
| 200 |
+
emit('email_list', {'emails': get_email_list()})
|
| 201 |
|
| 202 |
+
@socketio.on('get_email_list')
|
| 203 |
+
def handle_get_email_list():
|
| 204 |
+
emit('email_list', {'emails': get_email_list()})
|
| 205 |
+
|
| 206 |
+
@socketio.on('initialize_session')
|
| 207 |
+
def handle_init(data):
|
| 208 |
session_id = 'user_session'
|
| 209 |
+
session_data.setdefault(session_id, {})
|
| 210 |
+
session_data[session_id].update({
|
| 211 |
+
'emails': data.get('emails', []), 'filename': data.get('filename'),
|
| 212 |
+
'workflow': data.get('workflow'), 'start_date': data.get('start_date'),
|
| 213 |
+
'end_date': data.get('end_date'),
|
| 214 |
+
})
|
| 215 |
try:
|
| 216 |
+
app_b64 = data.get('app_data_b64'); quantum_b64 = data.get('quantum_data_b64')
|
| 217 |
+
if not app_b64 or not quantum_b64:
|
| 218 |
+
emit('error', {'message': 'Both data files are required.'}); return
|
| 219 |
+
|
| 220 |
+
def load_df_from_b64(b64str):
|
| 221 |
+
raw = base64.b64decode(b64str); bio = io.BytesIO(raw)
|
| 222 |
+
try: return pd.read_excel(bio)
|
| 223 |
+
except Exception: bio.seek(0); return pd.read_csv(bio)
|
| 224 |
+
|
| 225 |
+
df_app = load_df_from_b64(app_b64); df_quantum = load_df_from_b64(quantum_b64)
|
| 226 |
+
|
| 227 |
+
if 'Patient Name' not in df_app.columns or 'PRN' not in df_app.columns:
|
| 228 |
+
emit('error', {'message': "App Data must contain 'Patient Name' and 'PRN' columns."}); return
|
| 229 |
+
if 'Name' not in df_quantum.columns:
|
| 230 |
+
emit('error', {'message': "Quantum Data must contain a 'Name' column."}); return
|
| 231 |
+
|
| 232 |
+
def extract_patient_name(raw_name):
|
| 233 |
+
if not isinstance(raw_name, str): return ""
|
| 234 |
+
name_only = raw_name.split('DOB')[0].strip()
|
| 235 |
+
return re.sub(r'[:\d\-\s]+$', '', name_only).strip()
|
| 236 |
+
|
| 237 |
+
df_app_filtered = df_app.dropna(subset=['PRN'])
|
| 238 |
+
df_app_filtered = df_app_filtered[df_app_filtered['PRN'].astype(str).str.strip() != '']
|
| 239 |
prn_lookup_dict = {extract_patient_name(row['Patient Name']): row['PRN'] for _, row in df_app_filtered.iterrows()}
|
| 240 |
|
| 241 |
master_df = df_quantum.copy()
|
| 242 |
master_df['PRN'] = master_df['Name'].apply(lambda name: prn_lookup_dict.get(name, ""))
|
| 243 |
master_df['Status'] = ''
|
| 244 |
+
|
| 245 |
session_data[session_id]['patient_data_for_report'] = master_df
|
| 246 |
session_data[session_id]['patient_data'] = master_df.to_dict('records')
|
| 247 |
|
| 248 |
socketio.emit('data_processed')
|
| 249 |
print(f"[Server Log] Data prepared. Total records: {len(master_df)}")
|
|
|
|
| 250 |
except Exception as e:
|
| 251 |
+
emit('error', {'message': f'Error preparing data: {e}'})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 252 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
@socketio.on('start_login')
|
| 254 |
def handle_login(credentials):
|
| 255 |
global bot_instance
|
|
|
|
| 283 |
print(f" Frontend URL: {FRONTEND_ORIGIN}")
|
| 284 |
print(f" Port: {os.getenv('PORT', 7860)}")
|
| 285 |
print("====================================================================")
|
| 286 |
+
socketio.run(app, host='0.0.0.0', port=int(os.getenv('PORT', 7860)))```
|