File size: 17,067 Bytes
63e7b53
 
 
ee55d84
96e57ad
ee55d84
 
96e57ad
5768935
 
5515ec2
63e7b53
5768935
d972d64
2a2b008
ee55d84
2a2b008
 
 
 
5768935
63e7b53
2a2b008
63e7b53
5515ec2
 
 
63e7b53
 
3b3cff8
4845982
 
63e7b53
 
 
71cbe4b
a2bc3de
2d2ac3e
7f56c93
2d2ac3e
7f56c93
2d2ac3e
580986d
 
 
 
 
 
 
7f56c93
580986d
 
 
 
 
2d2ac3e
580986d
 
 
0f47fe3
 
 
 
5515ec2
0f47fe3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5515ec2
0f47fe3
2d2ac3e
580986d
 
 
2d2ac3e
2a2b008
7f56c93
2d2ac3e
 
 
7f56c93
 
2d2ac3e
 
 
5515ec2
 
580986d
 
 
 
 
 
20b325a
 
2d2ac3e
7f56c93
2d2ac3e
7f56c93
2d2ac3e
7f56c93
 
 
2d2ac3e
5515ec2
580986d
0f47fe3
2d2ac3e
7f56c93
2d2ac3e
 
 
 
 
5515ec2
 
7a82de2
a2bc3de
20b325a
63e7b53
5768935
 
7f56c93
 
6538ff6
63e7b53
 
7f56c93
63e7b53
5515ec2
0f47fe3
580986d
68f51fe
79d95f5
 
63e7b53
 
5515ec2
 
63e7b53
 
5515ec2
565afe0
7f56c93
 
63e7b53
 
580986d
9b6af7f
580986d
5078a4a
0806440
2d2ac3e
ee55d84
 
0f47fe3
 
 
 
f363e6d
595d411
2d2ac3e
0f47fe3
5768935
1675488
7f56c93
 
ee55d84
2d2ac3e
 
ee55d84
2d2ac3e
 
 
 
 
e48c284
2d2ac3e
1675488
5768935
1675488
2d2ac3e
ee55d84
5515ec2
580986d
 
2d2ac3e
 
27bcfa5
 
 
580986d
27bcfa5
595d411
 
5515ec2
595d411
580986d
595d411
 
 
9286802
595d411
 
47657a7
8637a2e
79d95f5
 
 
 
 
 
 
595d411
1da4d9a
 
595d411
 
 
 
1da4d9a
595d411
 
 
1da4d9a
 
595d411
 
 
 
 
 
79d95f5
 
 
 
 
595d411
 
5768935
ee55d84
68f51fe
1675488
68f51fe
f363e6d
da2774c
ee55d84
 
5768935
79d95f5
580986d
5515ec2
580986d
6538ff6
df5dc85
 
30ba375
7f56c93
5515ec2
5768935
 
 
7f56c93
 
5768935
 
63e7b53
 
 
7f56c93
5768935
7a82de2
63e7b53
 
bb99b47
7f56c93
a72aeff
 
 
5515ec2
a72aeff
 
5515ec2
 
 
 
 
812961a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import eventlet
eventlet.monkey_patch()

import pandas as pd
import io
import threading
import os
import base64
import json
import re
import logging
from datetime import datetime
from flask import Flask, Response, request, jsonify
from flask_socketio import SocketIO, emit
from flask_cors import CORS
from worker import QuantumBot
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
from dotenv import load_dotenv

load_dotenv()

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret-key-for-hillside-automation'
FRONTEND_ORIGIN = os.getenv('FRONTEND_URL', 'https://quantbot.netlify.app')
CORS(app, resources={r"/*": {"origins": [FRONTEND_ORIGIN, "http://localhost:3000", "http://127.0.0.1:5500", "null"]}})
socketio = SocketIO(app, cors_allowed_origins=[FRONTEND_ORIGIN, "http://localhost:3000", "http://127.0.0.1:5500", "null"], async_mode='eventlet')

bot_instance = None
session_data = {}

class GmailApiService:
    def __init__(self):
        self.sender_email = os.getenv('EMAIL_SENDER'); self.service = None
        try:
            from google.oauth2 import service_account; from googleapiclient.discovery import build
            base64_creds = os.getenv('GDRIVE_SA_KEY_BASE64')
            if not self.sender_email:
                logger.warning("EMAIL_SENDER not found in secrets. Emailing will be disabled.")
                return
            if not base64_creds:
                logger.warning("GDRIVE_SA_KEY_BASE64 not found in secrets. Emailing will be disabled.")
                return
            
            creds_json = base64.b64decode(base64_creds).decode('utf-8'); creds_dict = json.loads(creds_json)
            credentials = service_account.Credentials.from_service_account_info(
                creds_dict, 
                scopes=['https://www.googleapis.com/auth/gmail.send']
            ).with_subject(self.sender_email)
            
            self.service = build('gmail', 'v1', credentials=credentials)
            logger.info("Gmail API Service initialized successfully.")
        except Exception:
            logger.exception("CRITICAL ERROR: Gmail API Service initialization failed.")
    
    def create_professional_email_template(self, subject, status_text, stats, custom_name):
        status_color = "#28a745" if "completed" in status_text else "#ffc107" if "terminated" in status_text else "#dc3545"
        current_date = datetime.now().strftime("%B %d, %Y at %I:%M %p")
        html_template = f"""
        <!DOCTYPE html>
        <html><head><title>{subject}</title><style>
        body{{font-family: 'Segoe UI', sans-serif; background-color: #f8f8f8; color: #2c2c2c;}}
        .email-container{{max-width: 700px; margin: 20px auto; background-color: #ffffff; border-radius: 12px; overflow: hidden; box-shadow: 0 8px 32px rgba(139, 0, 0, 0.15);}}
        .header{{background: linear-gradient(135deg, #8A0303 0%, #4c00ff 100%); color: white; padding: 40px 30px; text-align: center;}}
        .header h1{{font-size: 32px;}} .header p{{font-size: 18px; opacity: 0.95;}}
        .status-banner{{background: {status_color}; color: white; text-align: center; padding: 18px; font-size: 16px; font-weight: 600; text-transform: uppercase;}}
        .content{{padding: 40px 30px;}} h3{{color: #8A0303; margin-bottom: 20px; font-size: 20px;}}
        .stats-grid{{display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 20px; margin: 35px 0;}}
        .stat-card{{background: #f8f9fa; border: 1px solid #e0e0e0; border-radius: 12px; padding: 25px 15px; text-align: center;}}
        .stat-number{{font-size: 36px; font-weight: 700; color: #4c00ff; margin-bottom: 10px;}}
        .attachments-section{{background: #f8f5ff; border: 1px solid #e0e0e0; border-radius: 12px; padding: 25px; margin: 35px 0;}}
        .footer{{background: #2c2c2c; color: white; padding: 35px 30px; text-align: center;}}
        </style></head>
        <body><div class="email-container"><div class="header"><h1>Hillside's Quantum Automation</h1><p>Patient Processing Report</p></div>
        <div class="status-banner">Process {status_text}</div>
        <div class="content"><h3>Processing Results</h3>
        <div class="stats-grid">
            <div class="stat-card"><div class="stat-number">{stats['total']}</div><div>Total in File</div></div>
            <div class="stat-card"><div class="stat-number">{stats['processed']}</div><div>Attempted</div></div>
            <div class="stat-card"><div class="stat-number" style="color: var(--success);">{stats['successful']}</div><div>Done</div></div>
            <div class="stat-card"><div class="stat-number" style="color: var(--warning);">{stats['bad']}</div><div>Bad State</div></div>
            <div class="stat-card"><div class="stat-number" style="color: var(--skipped);">{stats['skipped']}</div><div>Skipped</div></div>
        </div>
        <div class="attachments-section"><h3>Attached Reports</h3>
        <ul><li><b>{custom_name}_Full.csv:</b> The complete report with the final status for every patient.</li>
        <li><b>{custom_name}_Bad.csv:</b> A filtered list of patients that resulted in a "Bad" state.</li>
        <li><b>{custom_name}_Skipped.csv:</b> A filtered list of patients that were skipped due to having no PRN.</li></ul></div></div>
        <div class="footer"><p>&copy; {datetime.now().year} Hillside Automation. This is an automated report.</p></div></div></body></html>
        """
        return html_template
    
    def send_report(self, recipients, subject, body, attachments=None):
        if not self.service or not recipients:
            logger.error("Email not sent. Service not initialized or no recipients.")
            return False
        try:
            from googleapiclient.errors import HttpError
            message = MIMEMultipart(); message['From'] = self.sender_email; message['To'] = ', '.join(recipients); message['Subject'] = subject
            message.attach(MIMEText(body, 'html'))
            if attachments:
                for filename, content in attachments.items():
                    part = MIMEBase('application', 'octet-stream'); part.set_payload(content.encode('utf-8'))
                    encoders.encode_base64(part); part.add_header('Content-Disposition', f'attachment; filename="{filename}"')
                    message.attach(part)
            raw_message = base64.urlsafe_b64encode(message.as_bytes()).decode('utf-8')
            sent_message = self.service.users().messages().send(userId='me', body={'raw': raw_message}).execute()
            logger.info(f"Email sent successfully! Message ID: {sent_message['id']}")
            return True
        except HttpError as error:
            logger.exception(f"An HTTP error occurred while sending email: {error}")
            return False
        except Exception:
            logger.exception("A general error occurred while sending email")
            return False

class GoogleDriveService:
    def __init__(self):
        self.creds = None; self.service = None; self.folder_id = os.getenv('GOOGLE_DRIVE_FOLDER_ID')
        try:
            from google.oauth2 import service_account; from googleapiclient.discovery import build
            base64_creds = os.getenv('GDRIVE_SA_KEY_BASE64')
            if not base64_creds or not self.folder_id: raise ValueError("Google Drive secrets not found.")
            creds_json = base64.b64decode(base64_creds).decode('utf-8'); creds_dict = json.loads(creds_json)
            self.creds = service_account.Credentials.from_service_account_info(creds_dict, scopes=['https://www.googleapis.com/auth/drive'])
            self.service = build('drive', 'v3', credentials=self.creds)
            logger.info("Google Drive Service initialized successfully.")
        except Exception: logger.exception("G-Drive CRITICAL ERROR: Could not initialize service")
    
    def upload_file(self, filename, file_content):
        if not self.service: return False
        try:
            from googleapiclient.http import MediaIoBaseUpload
            file_metadata = {'name': filename, 'parents': [self.folder_id]}
            media = MediaIoBaseUpload(io.BytesIO(file_content.encode('utf-8')), mimetype='text/csv', resumable=True)
            self.service.files().create(body=file_metadata, media_body=media, fields='id').execute()
            logger.info(f"File '{filename}' uploaded to Google Drive."); return True
        except Exception: logger.exception("G-Drive ERROR: File upload failed"); return False

email_service = GmailApiService()
drive_service = GoogleDriveService()

def get_email_list():
    try:
        with open('config/emails.conf', 'r') as f: return [line.strip() for line in f if line.strip()]
    except FileNotFoundError: return []

def run_automation_process(session_id):
    global bot_instance
    results = []; is_terminated = False; is_crash = False
    try:
        logger.info(f"Starting automation thread for session: {session_id}")
        data = session_data.get(session_id, {}); patient_data = data.get('patient_data'); workflow = data.get('workflow')
        if not patient_data: raise ValueError("No patient data prepared for automation.")
        socketio.emit('initial_stats', {'total': len(patient_data)})
        date_range = {'start_date': data.get('start_date'), 'end_date': data.get('end_date')}
        results = bot_instance.process_patient_list(patient_data, workflow, date_range)
        is_terminated = bot_instance.termination_event.is_set()
    except Exception as e:
        logger.exception("Fatal error in automation thread")
        is_crash = True
        socketio.emit('error', {'message': f'A fatal error occurred: {e}'})
    finally:
        logger.info("Automation thread finished. Generating final reports...")
        generate_and_send_reports(session_id, results, is_crash_report=is_crash, is_terminated=is_terminated)
        if bot_instance: bot_instance.shutdown(); bot_instance = None
        if session_id in session_data: del session_data[session_id]

def generate_and_send_reports(session_id, results, is_crash_report=False, is_terminated=False):
    logger.info(f"Preparing final reports for session {session_id}.")
    data = session_data.get(session_id, {})
    if not data: logger.error("Session data not found. Cannot generate report."); return

    full_df = pd.DataFrame(data.get('patient_data_for_report'))
    if results:
        result_df = pd.DataFrame(results)
        if not result_df.empty:
            result_df.set_index('Name', inplace=True)
            full_df.set_index('Name', inplace=True)
            full_df.update(result_df)
            full_df.reset_index(inplace=True)
    full_df['Status'] = full_df['Status'].fillna('Not Processed')
    
    final_report_df = full_df[['Name', 'PRN', 'Status']]
    bad_df = final_report_df[final_report_df['Status'] == 'Bad']
    skipped_df = final_report_df[final_report_df['Status'] == 'Skipped - No PRN']
    
    timestamp = datetime.now().strftime("%d_%b_%Y"); custom_name = data.get('filename') or timestamp
    full_report_name = f"{custom_name}_Full.csv"; bad_report_name = f"{custom_name}_Bad.csv"; skipped_report_name = f"{custom_name}_Skipped.csv"
    
    full_report_content = final_report_df.to_csv(index=False)
    drive_service.upload_file(full_report_name, full_report_content)
    
    attachments = {
        full_report_name: full_report_content,
        bad_report_name: bad_df.to_csv(index=False),
        skipped_report_name: skipped_df.to_csv(index=False)
    }
    status_text = "terminated by user" if is_terminated else "crashed" if is_crash_report else "completed successfully"
    stats = {
        'total': len(full_df), 'processed': len(results),
        'successful': len(full_df[full_df['Status'] == 'Done']),
        'bad': len(bad_df), 'skipped': len(skipped_df)
    }
    subject = f"Automation Report [{status_text.upper()}]: {custom_name}"
    professional_body = email_service.create_professional_email_template(subject, status_text, stats, custom_name)
    
    logger.info("Attempting to send final email report...")
    email_service.send_report(data.get('emails'), subject, professional_body, attachments)
    socketio.emit('process_complete', {'message': f'Process {status_text}. Report sent.'})

@app.route('/')
def status_page():
    return Response("""<!DOCTYPE html>...""") # Omitted for brevity

@socketio.on('connect')
def handle_connect():
    logger.info('Frontend connected.')
    emit('email_list', {'emails': get_email_list()})

@socketio.on('get_email_list')
def handle_get_email_list():
    emit('email_list', {'emails': get_email_list()})

@socketio.on('initialize_session')
def handle_init(data):
    session_id = 'user_session'
    try:
        logger.info("Initializing session and processing files...")
        session_data.setdefault(session_id, {})
        session_data[session_id].update({
            'emails': data.get('emails', []), 'filename': data.get('filename'),
            'workflow': data.get('workflow'), 'start_date': data.get('start_date'),
            'end_date': data.get('end_date'),
        })
        app_b64 = data.get('app_data_b64'); quantum_b64 = data.get('quantum_data_b64')
        app_filename = data.get('app_data_filename', '').lower()
        quantum_filename = data.get('quantum_data_filename', '').lower()
        if not app_b64 or not quantum_b64:
            emit('error', {'message': 'Both data files are required.'}); return

        def load_df_from_b64(b64str, filename):
            raw = base64.b64decode(b64str); bio = io.BytesIO(raw)
            if filename.endswith('.xlsx'): return pd.read_excel(bio)
            return pd.read_csv(bio)

        df_app = load_df_from_b64(app_b64, app_filename)
        df_quantum = load_df_from_b64(quantum_b64, quantum_filename)

        if 'Patient Name' not in df_app.columns or 'PRN' not in df_app.columns:
            emit('error', {'message': "App Data must contain 'Patient Name' and 'PRN' columns."}); return
        if 'Name' not in df_quantum.columns:
            emit('error', {'message': "Quantum Data must contain a 'Name' column."}); return

        def extract_patient_name(raw_name):
            if not isinstance(raw_name, str): return ""
            name_only = raw_name.split('DOB')[0].strip()
            return re.sub(r'[:\d\-\s]+$', '', name_only).strip()

        df_app_filtered = df_app.dropna(subset=['PRN'])
        df_app_filtered = df_app_filtered[df_app_filtered['PRN'].astype(str).str.strip() != '']
        prn_lookup_dict = {extract_patient_name(row['Patient Name']): row['PRN'] for _, row in df_app_filtered.iterrows()}
        
        master_df = df_quantum.copy()
        master_df['PRN'] = master_df['Name'].apply(lambda name: prn_lookup_dict.get(name, ""))
        master_df['Status'] = ''
        
        session_data[session_id]['patient_data_for_report'] = master_df
        session_data[session_id]['patient_data'] = master_df.to_dict('records')
        
        socketio.emit('data_processed')
        logger.info(f"Data prepared. Total records: {len(master_df)}")
    except Exception:
        logger.exception("Error preparing data")
        emit('error', {'message': f'Error preparing data. See backend logs.'})

@socketio.on('start_login')
def handle_login(credentials):
    global bot_instance
    if bot_instance: bot_instance.shutdown()
    bot_instance = QuantumBot(socketio, app, logger)
    is_success, error_message = bot_instance.initialize_driver()
    if is_success:
        is_login_success, login_error = bot_instance.login(credentials['username'], credentials['password'])
        if is_login_success: emit('otp_required')
        else: emit('error', {'message': f'Login failed: {login_error}'})
    else:
        emit('error', {'message': f'Failed to initialize bot: {error_message}'})

@socketio.on('submit_otp')
def handle_otp(data):
    if not bot_instance: return emit('error', {'message': 'Bot not initialized.'})
    is_success, error_message = bot_instance.submit_otp(data['otp'])
    if is_success:
        emit('login_successful')
        session_id = 'user_session'
        socketio.start_background_task(run_automation_process, session_id)
    else: emit('error', {'message': f'OTP failed: {error_message}'})

@socketio.on('terminate_process')
def handle_terminate():
    if bot_instance: logger.info("Termination signal received from frontend."); bot_instance.stop()

if __name__ == '__main__':
    logger.info("====================================================================")
    logger.info("  🤗 Hillside Automation - Definitive Dual-Workflow Version")
    logger.info(f"  Frontend URL: {FRONTEND_ORIGIN}")
    logger.info(f"  Port: {os.getenv('PORT', 7860)}")
    logger.info("====================================================================")
    socketio.run(app, host='0.0.0.0', port=int(os.getenv('PORT', 7860)))