-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.py
More file actions
135 lines (113 loc) · 4.57 KB
/
ui.py
File metadata and controls
135 lines (113 loc) · 4.57 KB
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
import streamlit as st
import requests
import pandas as pd
from datetime import datetime
from streamlit_autorefresh import st_autorefresh
API_URL = "http://localhost:5000"
# State for tracking CPU over time
if "cpu_chart_data" not in st.session_state:
st.session_state.cpu_chart_data = pd.DataFrame(columns=["Time", "Node", "Available CPU"])
def status_icon(status):
return "🟢" if status == "UP" else "🔴"
def fetch_nodes():
try:
res = requests.get(f"{API_URL}/list_nodes")
res.raise_for_status()
nodes = res.json()
if not nodes:
return pd.DataFrame(), pd.DataFrame()
# Main table
df = pd.DataFrame(nodes)
df['status_icon'] = df['status'].apply(status_icon)
df['last_heartbeat'] = pd.to_datetime(df['last_heartbeat'], unit='s').dt.strftime('%Y-%m-%d %H:%M:%S')
table = df[["node_id", "cpu_total", "cpu_available", "status_icon", "last_heartbeat", "pods"]].rename(columns={
'node_id': 'ID',
'cpu_total': 'CPU Cores',
'cpu_available': 'Available CPU',
'status_icon': 'Status',
'last_heartbeat': 'Last Heartbeat',
'pods': 'Pods'
})
# Update chart history
now = datetime.now().strftime("%H:%M:%S")
chart_df = pd.DataFrame([{
"Time": now,
"Node": row["node_id"],
"Available CPU": row["cpu_available"]
} for _, row in df.iterrows()])
st.session_state.cpu_chart_data = pd.concat([st.session_state.cpu_chart_data, chart_df], ignore_index=True)
return table, st.session_state.cpu_chart_data
except Exception as e:
st.error(f"Failed to fetch nodes: {str(e)}")
return pd.DataFrame(), pd.DataFrame()
def fetch_pods():
try:
res = requests.get(f"{API_URL}/list_pods")
res.raise_for_status()
pods = res.json()
if not pods:
return pd.DataFrame()
df = pd.DataFrame(pods)
df['created_at'] = pd.to_datetime(df['created_at']).dt.strftime('%Y-%m-%d %H:%M:%S')
return df[["pod_id", "node_id", "cpu", "status", "created_at"]].rename(columns={
'pod_id': 'ID',
'node_id': 'Node',
'cpu': 'CPU',
'status': 'Status',
'created_at': 'Created At'
})
except Exception as e:
st.error(f"Failed to fetch pods: {str(e)}")
return pd.DataFrame()
def add_node():
with st.form("add_node_form"):
st.write("### Add New Node")
node_id = st.text_input("Node ID")
cpu_cores = st.number_input("CPU Cores", min_value=1, value=4)
if st.form_submit_button("Add Node") and node_id:
response = requests.post(
f"{API_URL}/add_node",
json={"node_id": node_id, "cpu_cores": cpu_cores}
)
if response.status_code == 201:
st.success(response.json().get("message", "Node added successfully"))
else:
st.error(response.json().get("error", "Failed to add node"))
def launch_pod():
with st.form("launch_pod_form"):
st.write("### Launch New Pod")
pod_id = st.text_input("Pod ID")
cpu_required = st.number_input("CPU Required", min_value=1, value=1)
if st.form_submit_button("Launch Pod") and pod_id:
response = requests.post(
f"{API_URL}/launch_pod",
json={"pod_id": pod_id, "cpu": cpu_required}
)
if response.status_code == 201:
st.success(response.json().get("message", "Pod launched successfully"))
else:
st.error(response.json().get("error", "Failed to launch pod"))
# UI Setup
st.set_page_config(page_title="Kawaii-Kube", layout="wide")
st.title("🌸 Kawaii-Kube: Cluster Simulator")
# Layout tabs
tab1, tab2 = st.tabs(["⚙️ Cluster Management", "📊 Monitoring"])
# Refresh every 10s
st_autorefresh(interval=10_000, key="refresh")
with tab1:
col1, col2 = st.columns(2)
with col1:
add_node()
with col2:
launch_pod()
with tab2:
st.write("### Cluster Nodes")
table_df, chart_df = fetch_nodes()
st.dataframe(table_df, use_container_width=True)
st.write("### CPU Availability Over Time")
if not chart_df.empty:
pivot_chart = chart_df.pivot(index="Time", columns="Node", values="Available CPU").fillna(method="ffill")
st.line_chart(pivot_chart)
st.write("### Running Pods")
st.dataframe(fetch_pods(), use_container_width=True)
st.caption(f"Last updated: {datetime.now().strftime('%H:%M:%S')}")