-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
137 lines (109 loc) · 4.44 KB
/
app.py
File metadata and controls
137 lines (109 loc) · 4.44 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
136
137
"""
Docker Container Resource Usage Dashboard
A Streamlit app for monitoring Docker container resource usage.
"""
import streamlit as st
import time
import pandas as pd
from docker_stats import DockerStats
from dashboard import container_list, metrics, charts
# Page config
st.set_page_config(
page_title="Docker Container Dashboard",
page_icon="🐳",
layout="wide",
initial_sidebar_state="expanded"
)
# Initialize session state for storing container history
if 'container_histories' not in st.session_state:
st.session_state.container_histories = {} # Dict to store history by container ID
if 'last_refresh' not in st.session_state:
st.session_state.last_refresh = time.time()
# Initialize Docker stats collector
@st.cache_resource
def get_docker_stats():
return DockerStats()
docker_stats = get_docker_stats()
# Dashboard title
st.title("🐳 Docker Container Resource Dashboard")
# Check Docker connection
if not docker_stats.test_docker_connection():
st.error("❌ Cannot connect to Docker. Make sure Docker is running.")
st.stop()
# Sidebar controls
st.sidebar.header("Dashboard Controls")
# Refresh interval selector
refresh_interval = st.sidebar.slider(
"Refresh Interval (seconds)",
min_value=2,
max_value=60,
value=5
)
# Refresh button
refresh = st.sidebar.button("Refresh Now")
# Max history data points to keep
history_limit = st.sidebar.slider(
"History Data Points",
min_value=10,
max_value=500,
value=100
)
# Auto-refresh logic
if (time.time() - st.session_state.last_refresh) > refresh_interval or refresh:
with st.spinner("Fetching container stats..."):
# Get all container stats
all_stats_df = docker_stats.get_all_container_stats()
# Update container histories
for _, row in all_stats_df.iterrows():
container_id = row['id']
container_stats = row.to_dict()
# Initialize history for new containers
if container_id not in st.session_state.container_histories:
st.session_state.container_histories[container_id] = []
# Add current stats to history
st.session_state.container_histories[container_id].append(container_stats)
# Limit history size
if len(st.session_state.container_histories[container_id]) > history_limit:
st.session_state.container_histories[container_id] = st.session_state.container_histories[container_id][-history_limit:]
st.session_state.last_refresh = time.time()
# Get container list for selection
containers_df = pd.DataFrame(docker_stats.get_containers())
else:
# Use cached container list if not refreshing
containers_df = pd.DataFrame(docker_stats.get_containers())
all_stats_df = docker_stats.get_all_container_stats()
# Display system overview metrics
if 'all_stats_df' in locals() and not all_stats_df.empty:
metrics.display_summary_metrics(all_stats_df)
# Display system-wide charts
st.subheader("System Resource Usage")
charts.create_system_overview_chart(all_stats_df)
# Container selection and detailed view
if not containers_df.empty:
st.markdown("---")
selected_container_id = container_list.display_container_list(containers_df)
if selected_container_id:
st.markdown("---")
# Display detailed metrics for selected container
if selected_container_id in st.session_state.container_histories:
container_history = st.session_state.container_histories[selected_container_id]
latest_stats = container_history[-1] if container_history else {}
metrics.display_container_metrics(latest_stats, container_history)
charts.create_resource_usage_charts(container_history)
else:
st.info(f"Waiting for stats data for container {selected_container_id}...")
else:
st.warning("No running containers found")
# Display last refresh time
st.sidebar.caption(f"Last refreshed: {time.strftime('%H:%M:%S')}")
st.sidebar.caption(f"Next auto-refresh in: {refresh_interval - int(time.time() - st.session_state.last_refresh)} seconds")
# About section in sidebar
st.sidebar.markdown("---")
st.sidebar.subheader("About")
st.sidebar.info(
"""
**Docker Container Resource Dashboard**
Monitor resource usage of your Docker containers in real-time.
GitHub: https://github.com/im3an/WhaleSight.git
"""
)