"""Admin Dashboard - Real-time monitoring and control interface.""" import streamlit as st import time from datetime import datetime from typing import Dict, List, Any import asyncio # Page configuration st.set_page_config( page_title="LogiFlow AI - Agent Control Center", page_icon="πŸ€–", layout="wide", initial_sidebar_state="expanded" ) # Custom CSS st.markdown(""" """, unsafe_allow_html=True) class DashboardState: """Shared state for dashboard.""" def __init__(self): self.agents = { "JARVIS": {"status": "idle", "tasks_completed": 0, "current_task": None}, "ORDER_AGENT": {"status": "idle", "tasks_completed": 0, "current_task": None}, "DISPATCH_AGENT": {"status": "idle", "tasks_completed": 0, "current_task": None}, "FLEET_AGENT": {"status": "idle", "tasks_completed": 0, "current_task": None}, "HUB_AGENT": {"status": "idle", "tasks_completed": 0, "current_task": None}, "CUSTOMER_AGENT": {"status": "idle", "tasks_completed": 0, "current_task": None}, "EXCEPTION_AGENT": {"status": "idle", "tasks_completed": 0, "current_task": None}, "ROUTE_OPTIMIZER": {"status": "idle", "tasks_completed": 0, "current_task": None}, } self.orders = [] self.vehicles = [] self.hubs = [] self.messages = [] def update_agent_status(self, agent_id: str, status: str, task: str = None): if agent_id in self.agents: self.agents[agent_id]["status"] = status self.agents[agent_id]["current_task"] = task if status == "idle" and task is None: self.agents[agent_id]["tasks_completed"] += 1 def add_message(self, sender: str, recipient: str, message_type: str, content: str): self.messages.append({ "timestamp": datetime.now(), "sender": sender, "recipient": recipient, "type": message_type, "content": content }) if len(self.messages) > 100: self.messages = self.messages[-100:] # Initialize state if 'state' not in st.session_state: st.session_state.state = DashboardState() def render_header(): """Render the dashboard header.""" col1, col2, col3 = st.columns([2, 3, 1]) with col1: st.markdown("""

πŸ€– LogiFlow AI

Agent Control Center

""", unsafe_allow_html=True) with col2: st.markdown("""

🟒 System Operational

All agents active and monitoring

""", unsafe_allow_html=True) with col3: st.markdown(f"""

{datetime.now().strftime('%Y-%m-%d')}

{datetime.now().strftime('%H:%M:%S')}

""", unsafe_allow_html=True) def render_agent_grid(state: DashboardState): """Render agent status cards.""" st.markdown("## πŸ€– Agent Status") cols = st.columns(4) agent_icons = { "JARVIS": "🧠", "ORDER_AGENT": "πŸ“¦", "DISPATCH_AGENT": "🚚", "FLEET_AGENT": "πŸš›", "HUB_AGENT": "🏭", "CUSTOMER_AGENT": "πŸ’¬", "EXCEPTION_AGENT": "⚠️", "ROUTE_OPTIMIZER": "πŸ—ΊοΈ" } for i, (agent_id, info) in enumerate(state.agents.items()): with cols[i % 4]: status_class = f"status-{info['status']}" icon = agent_icons.get(agent_id, "πŸ€–") st.markdown(f"""

{icon} {agent_id}

{info['status'].upper()}

Tasks: {info['tasks_completed']}

Current: {info['current_task'] or 'None'}

""", unsafe_allow_html=True) def render_metrics(state: DashboardState): """Render key metrics.""" col1, col2, col3, col4, col5 = st.columns(5) metrics = [ ("Active Orders", len([o for o in state.orders if o.get("status") != "delivered"]), "πŸ“¦"), ("Vehicles Active", len([v for v in state.vehicles if v.get("status") == "in_transit"]), "πŸš›"), ("Hubs Online", len(state.hubs), "🏭"), ("Messages/min", len([m for m in state.messages if (datetime.now() - m["timestamp"]).seconds < 60]), "πŸ“¨"), ("Success Rate", "98.5%", "βœ…") ] for col, (label, value, icon) in zip([col1, col2, col3, col4, col5], metrics): with col: st.markdown(f"""
{icon}
{value}
{label}
""", unsafe_allow_html=True) def render_order_list(state: DashboardState): """Render recent orders.""" st.markdown("## πŸ“¦ Recent Orders") if not state.orders: st.info("No orders yet. Create an order to see it here.") return for order in state.orders[-10:][::-1]: status_colors = { "received": "🟑", "in_transit": "πŸ”΅", "delivered": "🟒", "cancelled": "πŸ”΄" } status_icon = status_colors.get(order.get("status", "received"), "βšͺ") with st.expander(f"{status_icon} {order.get('order_id', 'N/A')} - {order.get('customer_name', 'Unknown')}"): col1, col2 = st.columns(2) with col1: st.write(f"**Status:** {order.get('status', 'Unknown')}") st.write(f"**Priority:** {order.get('category', {}).get('priority', 'Medium')}") st.write(f"**Created:** {order.get('created_at', 'N/A')}") with col2: st.write(f"**Pickup:** {order.get('pickup_address', {}).get('city', 'N/A')}") st.write(f"**Delivery:** {order.get('delivery_address', {}).get('city', 'N/A')}") st.write(f"**Items:** {len(order.get('items', []))}") def render_vehicle_fleet(state: DashboardState): """Render vehicle fleet status.""" st.markdown("## πŸš› Vehicle Fleet") if not state.vehicles: st.info("No vehicles registered.") return for vehicle in state.vehicles[:20]: status_icons = { "available": "🟒", "in_transit": "πŸ”΅", "maintenance": "🟑", "offline": "⚫" } icon = status_icons.get(vehicle.get("status", "available"), "βšͺ") col1, col2, col3, col4 = st.columns(4) with col1: st.write(f"{icon} **{vehicle.get('vehicle_id', 'N/A')}**") with col2: st.write(f"Type: {vehicle.get('type', 'N/A')}") with col3: st.write(f"Hub: {vehicle.get('hub', 'N/A')}") with col4: st.write(f"Status: {vehicle.get('status', 'N/A')}") st.divider() def render_hub_network(state: DashboardState): """Render hub network status.""" st.markdown("## 🏭 Hub Network") cols = st.columns(4) hub_data = [ {"id": "DL-HUB-01", "name": "Delhi North", "load": 150, "capacity": 500}, {"id": "DL-HUB-02", "name": "Delhi South", "load": 200, "capacity": 400}, {"id": "MU-HUB-01", "name": "Mumbai West", "load": 300, "capacity": 600}, {"id": "MU-HUB-02", "name": "Mumbai East", "load": 250, "capacity": 550}, {"id": "BL-HUB-01", "name": "Bangalore", "load": 180, "capacity": 500}, {"id": "HY-HUB-01", "name": "Hyderabad", "load": 220, "capacity": 450}, {"id": "PU-HUB-01", "name": "Pune", "load": 160, "capacity": 400}, {"id": "KL-HUB-01", "name": "Kolkata", "load": 140, "capacity": 350}, ] for i, hub in enumerate(hub_data): with cols[i % 4]: utilization = (hub["load"] / hub["capacity"]) * 100 color = "🟒" if utilization < 70 else "🟑" if utilization < 90 else "πŸ”΄" st.markdown(f"""

{color} {hub['name']}

ID: {hub['id']}

Load: {hub['load']}/{hub['capacity']}

Utilization: {utilization:.1f}%

""", unsafe_allow_html=True) def render_message_feed(state: DashboardState): """Render real-time message feed.""" st.markdown("## πŸ“¨ Agent Communication Feed") if not state.messages: st.info("No messages yet.") return for msg in state.messages[-20:][::-1]: st.markdown(f"""
{msg['timestamp'].strftime('%H:%M:%S')} [{msg['sender']}] β†’ [{msg['recipient']}] {msg['type']}

{msg['content']}

""", unsafe_allow_html=True) def render_order_creator(): """Render order creation form.""" st.markdown("## βž• Create New Order") with st.form("order_form"): col1, col2 = st.columns(2) with col1: st.write("**Customer Details**") customer_name = st.text_input("Customer Name", "Rajesh Kumar") customer_phone = st.text_input("Phone", "+919876543210") customer_email = st.text_input("Email", "rajesh@example.com") with col2: st.write("**Order Priority**") priority = st.selectbox("Priority", ["low", "medium", "high", "urgent"]) items_count = st.number_input("Number of Items", 1, 50, 3) weight = st.number_input("Total Weight (kg)", 0.1, 500.0, 5.0) st.write("**Pickup Address**") col_p1, col_p2, col_p3 = st.columns(3) with col_p1: pickup_city = st.text_input("City", "Delhi") with col_p2: pickup_state = st.text_input("State", "Delhi") with col_p3: pickup_pincode = st.text_input("Pincode", "110001") st.write("**Delivery Address**") col_d1, col_d2, col_d3 = st.columns(3) with col_d1: delivery_city = st.text_input("City", "Mumbai", key="del_city") with col_d2: delivery_state = st.text_input("State", "Maharashtra", key="del_state") with col_d3: delivery_pincode = st.text_input("Pincode", "400001", key="del_pin") submitted = st.form_submit_button("πŸš€ Create Order via AI Agents", use_container_width=True) if submitted: st.success("Order created! Watch the agents process it in real-time.") # Simulate order being created if 'state' in st.session_state: state = st.session_state.state order_id = f"ORD-{datetime.now().strftime('%Y%m%d')}-{len(state.orders) + 1:04d}" state.orders.append({ "order_id": order_id, "customer_name": customer_name, "customer_phone": customer_phone, "status": "received", "priority": priority, "pickup_address": {"city": pickup_city, "pincode": pickup_pincode}, "delivery_address": {"city": delivery_city, "pincode": delivery_pincode}, "items": [{"name": f"Item {i}", "weight": weight/items_count} for i in range(int(items_count))], "created_at": datetime.now().isoformat(), "category": {"priority": priority} }) state.add_message("ADMIN", "JARVIS", "ORDER_SUBMIT", f"New order {order_id} submitted") state.update_agent_status("ORDER_AGENT", "working", f"Validating {order_id}") st.rerun() def render_exception_panel(state: DashboardState): """Render exception handling panel.""" st.markdown("## ⚠️ Exception Management") col1, col2 = st.columns(2) with col1: st.write("**Quick Actions**") if st.button("🚨 Simulate Delay Exception"): state.add_message("SYSTEM", "EXCEPTION_AGENT", "EXCEPTION", "Simulated delay detected") st.warning("Delay exception triggered - Exception Agent handling...") with col2: st.write("**Exception Queue**") st.info("No active exceptions - System running smoothly") def main(): """Main dashboard function.""" # Sidebar navigation st.sidebar.markdown("## 🧭 Navigation") page = st.sidebar.radio("Go to", [ "πŸ“Š Overview", "πŸ€– Agents", "πŸ“¦ Orders", "πŸš› Fleet", "🏭 Hubs", "⚠️ Exceptions", "πŸ’¬ Messages", "βž• Create Order" ]) # Initialize state state = st.session_state.state # Simulate some data if empty if not state.vehicles: state.vehicles = [ {"vehicle_id": "DL-V-001", "type": "van", "hub": "DL-HUB-01", "status": "in_transit", "location": {"lat": 28.61, "lng": 77.20}}, {"vehicle_id": "DL-V-002", "type": "van", "hub": "DL-HUB-01", "status": "available", "location": {"lat": 28.62, "lng": 77.21}}, {"vehicle_id": "MU-V-001", "type": "van", "hub": "MU-HUB-01", "status": "in_transit", "location": {"lat": 19.07, "lng": 72.87}}, {"vehicle_id": "BL-V-001", "type": "van", "hub": "BL-HUB-01", "status": "available", "location": {"lat": 12.97, "lng": 77.59}}, {"vehicle_id": "DL-B-001", "type": "bike", "hub": "DL-HUB-01", "status": "in_transit", "location": {"lat": 28.63, "lng": 77.19}}, ] if not state.hubs: state.hubs = [ {"hub_id": "DL-HUB-01", "name": "Delhi North Hub"}, {"hub_id": "DL-HUB-02", "name": "Delhi South Hub"}, {"hub_id": "MU-HUB-01", "name": "Mumbai West Hub"}, {"hub_id": "BL-HUB-01", "name": "Bangalore Hub"}, ] # Render based on selection if page == "πŸ“Š Overview": render_header() render_metrics(state) render_agent_grid(state) col_overview1, col_overview2 = st.columns(2) with col_overview1: render_order_list(state) with col_overview2: render_hub_network(state) elif page == "πŸ€– Agents": render_header() st.markdown("# πŸ€– Agent Control Center") render_agent_grid(state) st.markdown("### Agent Communication Pattern") st.markdown(""" ``` JARVIS (Master) β”œβ”€β”€ ORDER_AGENT β†’ Validates & categorizes orders β”œβ”€β”€ DISPATCH_AGENT β†’ Assigns routes & zones β”œβ”€β”€ FLEET_AGENT β†’ Manages vehicle allocation β”œβ”€β”€ HUB_AGENT β†’ Coordinates hub operations β”œβ”€β”€ CUSTOMER_AGENT β†’ Sends notifications β”œβ”€β”€ EXCEPTION_AGENT β†’ Handles problems └── ROUTE_OPTIMIZER β†’ Plans optimal paths ``` """) elif page == "πŸ“¦ Orders": render_header() render_order_list(state) render_order_creator() elif page == "πŸš› Fleet": render_header() render_vehicle_fleet(state) elif page == "🏭 Hubs": render_header() render_hub_network(state) elif page == "⚠️ Exceptions": render_header() render_exception_panel(state) elif page == "πŸ’¬ Messages": render_header() render_message_feed(state) elif page == "βž• Create Order": render_header() render_order_creator() # Auto-refresh time.sleep(1) st.rerun() if __name__ == "__main__": main()