fastpluggy-postgres-tools 0.1.26__tar.gz → 0.1.34__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of fastpluggy-postgres-tools might be problematic. Click here for more details.
- {fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/PKG-INFO +1 -4
- {fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/README.md +0 -3
- {fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/fastpluggy_postgres_tools.egg-info/PKG-INFO +1 -4
- {fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/fastpluggy_postgres_tools.egg-info/SOURCES.txt +1 -0
- {fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/pyproject.toml +1 -1
- {fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/src/routers/alerts.py +0 -5
- {fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/src/routers/connections.py +0 -5
- fastpluggy_postgres_tools-0.1.34/src/routers/dashboard.py +498 -0
- {fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/src/routers/db_size.py +0 -5
- {fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/src/routers/extensions.py +0 -5
- {fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/src/routers/indexes.py +0 -5
- {fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/src/routers/queries.py +0 -5
- {fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/src/routers/replication.py +0 -5
- {fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/src/routers/sequences.py +0 -5
- {fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/src/routers/tables.py +2 -6
- {fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/src/routers/vacuum.py +0 -5
- fastpluggy_postgres_tools-0.1.34/src/templates/dashboard_cards.html.j2 +46 -0
- fastpluggy_postgres_tools-0.1.26/src/routers/dashboard.py +0 -336
- {fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/fastpluggy_postgres_tools.egg-info/dependency_links.txt +0 -0
- {fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/fastpluggy_postgres_tools.egg-info/entry_points.txt +0 -0
- {fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/fastpluggy_postgres_tools.egg-info/requires.txt +0 -0
- {fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/fastpluggy_postgres_tools.egg-info/top_level.txt +0 -0
- {fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/setup.cfg +0 -0
- {fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/src/__init__.py +0 -0
- {fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/src/plugin.py +0 -0
- {fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/src/routers/__init__.py +0 -0
- {fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/src/utils/__init__.py +0 -0
- {fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/src/utils/db_utils.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: fastpluggy-postgres-tools
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.34
|
|
4
4
|
Summary: PostgreSQL database monitoring and management plugin for FastPluggy
|
|
5
5
|
License-Expression: MIT
|
|
6
6
|
Requires-Python: >=3.10
|
|
@@ -18,8 +18,6 @@ This plugin provides a user-friendly interface to monitor and manage various asp
|
|
|
18
18
|
|
|
19
19
|
## Features
|
|
20
20
|
|
|
21
|
-
### Implemented Features
|
|
22
|
-
|
|
23
21
|
- **PostgreSQL Sequences Monitoring**:
|
|
24
22
|
- View comprehensive sequence information including name, last value, max value, and remaining capacity
|
|
25
23
|
- Monitor sequence usage with percentage metrics (percent used and percent remaining)
|
|
@@ -94,7 +92,6 @@ This plugin provides a user-friendly interface to monitor and manage various asp
|
|
|
94
92
|
- Database configuration recommendations
|
|
95
93
|
- Key metrics summary
|
|
96
94
|
|
|
97
|
-
### Planned Features
|
|
98
95
|
|
|
99
96
|
For detailed specifications of each feature, see the individual files in the `docs/feature/` directory.
|
|
100
97
|
|
|
@@ -8,8 +8,6 @@ This plugin provides a user-friendly interface to monitor and manage various asp
|
|
|
8
8
|
|
|
9
9
|
## Features
|
|
10
10
|
|
|
11
|
-
### Implemented Features
|
|
12
|
-
|
|
13
11
|
- **PostgreSQL Sequences Monitoring**:
|
|
14
12
|
- View comprehensive sequence information including name, last value, max value, and remaining capacity
|
|
15
13
|
- Monitor sequence usage with percentage metrics (percent used and percent remaining)
|
|
@@ -84,7 +82,6 @@ This plugin provides a user-friendly interface to monitor and manage various asp
|
|
|
84
82
|
- Database configuration recommendations
|
|
85
83
|
- Key metrics summary
|
|
86
84
|
|
|
87
|
-
### Planned Features
|
|
88
85
|
|
|
89
86
|
For detailed specifications of each feature, see the individual files in the `docs/feature/` directory.
|
|
90
87
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: fastpluggy-postgres-tools
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.34
|
|
4
4
|
Summary: PostgreSQL database monitoring and management plugin for FastPluggy
|
|
5
5
|
License-Expression: MIT
|
|
6
6
|
Requires-Python: >=3.10
|
|
@@ -18,8 +18,6 @@ This plugin provides a user-friendly interface to monitor and manage various asp
|
|
|
18
18
|
|
|
19
19
|
## Features
|
|
20
20
|
|
|
21
|
-
### Implemented Features
|
|
22
|
-
|
|
23
21
|
- **PostgreSQL Sequences Monitoring**:
|
|
24
22
|
- View comprehensive sequence information including name, last value, max value, and remaining capacity
|
|
25
23
|
- Monitor sequence usage with percentage metrics (percent used and percent remaining)
|
|
@@ -94,7 +92,6 @@ This plugin provides a user-friendly interface to monitor and manage various asp
|
|
|
94
92
|
- Database configuration recommendations
|
|
95
93
|
- Key metrics summary
|
|
96
94
|
|
|
97
|
-
### Planned Features
|
|
98
95
|
|
|
99
96
|
For detailed specifications of each feature, see the individual files in the `docs/feature/` directory.
|
|
100
97
|
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "fastpluggy-postgres-tools"
|
|
7
|
-
version = "0.1.
|
|
7
|
+
version = "0.1.34"
|
|
8
8
|
readme = "README.md"
|
|
9
9
|
description = "PostgreSQL database monitoring and management plugin for FastPluggy"
|
|
10
10
|
requires-python = ">=3.10"
|
|
@@ -220,11 +220,6 @@ async def test_webhook(webhook_url: str, webhook_service: str, message: str):
|
|
|
220
220
|
|
|
221
221
|
|
|
222
222
|
@alerts_router.get("")
|
|
223
|
-
@menu_entry(
|
|
224
|
-
label="Alerts & Webhooks",
|
|
225
|
-
icon="fa fa-bell",
|
|
226
|
-
parent="postgres_tools"
|
|
227
|
-
)
|
|
228
223
|
async def get_alerts_view(
|
|
229
224
|
request: Request,
|
|
230
225
|
db=Depends(get_db),
|
{fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/src/routers/connections.py
RENAMED
|
@@ -72,11 +72,6 @@ def get_lock_contention(db_manager: Session):
|
|
|
72
72
|
|
|
73
73
|
|
|
74
74
|
@connections_router.get("")
|
|
75
|
-
@menu_entry(
|
|
76
|
-
label="Connections & Locks",
|
|
77
|
-
icon="fa fa-link",
|
|
78
|
-
parent="postgres_tools"
|
|
79
|
-
)
|
|
80
75
|
async def get_connections_view(
|
|
81
76
|
request: Request,
|
|
82
77
|
db=Depends(get_db),
|
|
@@ -0,0 +1,498 @@
|
|
|
1
|
+
from fastapi import APIRouter, Depends, Request
|
|
2
|
+
from sqlalchemy import text
|
|
3
|
+
from sqlalchemy.orm import Session
|
|
4
|
+
from typing import List, Dict, Any
|
|
5
|
+
|
|
6
|
+
from fastpluggy.core.database import get_db
|
|
7
|
+
from fastpluggy.core.dependency import get_view_builder
|
|
8
|
+
from fastpluggy.core.widgets import TableWidget
|
|
9
|
+
from fastpluggy.core.widgets.categories.display.custom import CustomTemplateWidget
|
|
10
|
+
from fastpluggy_plugin.ui_tools.extra_widget.display.card import CardWidget
|
|
11
|
+
from fastpluggy.core.menu.decorator import menu_entry
|
|
12
|
+
|
|
13
|
+
dashboard_router = APIRouter()
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def get_cluster_health(db: Session):
|
|
17
|
+
"""
|
|
18
|
+
Get overall cluster health status
|
|
19
|
+
"""
|
|
20
|
+
# Check for high table bloat
|
|
21
|
+
bloat_query = text("""
|
|
22
|
+
SELECT
|
|
23
|
+
COUNT(*) AS count,
|
|
24
|
+
MAX(ROUND(100.0 * n_dead_tup / GREATEST(n_live_tup + n_dead_tup, 1), 2)) AS max_bloat
|
|
25
|
+
FROM pg_stat_user_tables
|
|
26
|
+
WHERE ROUND(100.0 * n_dead_tup / GREATEST(n_live_tup + n_dead_tup, 1), 2) > 40
|
|
27
|
+
""")
|
|
28
|
+
|
|
29
|
+
# Check for unused indexes
|
|
30
|
+
index_query = text("""
|
|
31
|
+
SELECT COUNT(*) AS count
|
|
32
|
+
FROM pg_stat_user_indexes
|
|
33
|
+
WHERE idx_scan = 0 AND pg_relation_size(indexrelid) > 10000000
|
|
34
|
+
""")
|
|
35
|
+
|
|
36
|
+
# Check for long-running queries
|
|
37
|
+
query_query = text("""
|
|
38
|
+
SELECT COUNT(*) AS count
|
|
39
|
+
FROM pg_stat_activity
|
|
40
|
+
WHERE state = 'active' AND query_start < NOW() - INTERVAL '5 minutes'
|
|
41
|
+
""")
|
|
42
|
+
|
|
43
|
+
# Check for replication lag
|
|
44
|
+
replication_query = text("""
|
|
45
|
+
SELECT
|
|
46
|
+
COUNT(*) AS count,
|
|
47
|
+
MAX(pg_wal_lsn_diff(pg_current_wal_lsn(), flush_lsn)) AS max_lag
|
|
48
|
+
FROM pg_stat_replication
|
|
49
|
+
WHERE pg_wal_lsn_diff(pg_current_wal_lsn(), flush_lsn) > 50000000
|
|
50
|
+
""")
|
|
51
|
+
|
|
52
|
+
alerts = []
|
|
53
|
+
status = "green"
|
|
54
|
+
|
|
55
|
+
try:
|
|
56
|
+
|
|
57
|
+
# Check table bloat
|
|
58
|
+
bloat_result = db.execute(bloat_query).mappings().first()
|
|
59
|
+
if bloat_result and bloat_result["count"] > 0:
|
|
60
|
+
alerts.append({
|
|
61
|
+
"module": "tables",
|
|
62
|
+
"message": f"{bloat_result['count']} tables with bloat > 40% (max: {bloat_result['max_bloat']}%)",
|
|
63
|
+
"severity": "error"
|
|
64
|
+
})
|
|
65
|
+
status = "red"
|
|
66
|
+
|
|
67
|
+
# Check unused indexes
|
|
68
|
+
index_result = db.execute(index_query).mappings().first()
|
|
69
|
+
if index_result and index_result["count"] > 0:
|
|
70
|
+
alerts.append({
|
|
71
|
+
"module": "indexes",
|
|
72
|
+
"message": f"{index_result['count']} unused indexes larger than 10MB",
|
|
73
|
+
"severity": "warning"
|
|
74
|
+
})
|
|
75
|
+
if status == "green":
|
|
76
|
+
status = "yellow"
|
|
77
|
+
|
|
78
|
+
# Check long-running queries
|
|
79
|
+
query_result = db.execute(query_query).mappings().first()
|
|
80
|
+
if query_result and query_result["count"] > 0:
|
|
81
|
+
alerts.append({
|
|
82
|
+
"module": "queries",
|
|
83
|
+
"message": f"{query_result['count']} queries running for more than 5 minutes",
|
|
84
|
+
"severity": "warning"
|
|
85
|
+
})
|
|
86
|
+
if status == "green":
|
|
87
|
+
status = "yellow"
|
|
88
|
+
|
|
89
|
+
# Check replication lag
|
|
90
|
+
try:
|
|
91
|
+
replication_result = db.execute(replication_query).mappings().first()
|
|
92
|
+
if replication_result and replication_result["count"] > 0:
|
|
93
|
+
alerts.append({
|
|
94
|
+
"module": "replication",
|
|
95
|
+
"message": f"{replication_result['count']} replicas with lag > 50MB",
|
|
96
|
+
"severity": "error"
|
|
97
|
+
})
|
|
98
|
+
status = "red"
|
|
99
|
+
except Exception:
|
|
100
|
+
# This might fail if not running on a primary server
|
|
101
|
+
pass
|
|
102
|
+
except Exception as e:
|
|
103
|
+
alerts.append({
|
|
104
|
+
"module": "system",
|
|
105
|
+
"message": f"Error checking cluster health: {str(e)}",
|
|
106
|
+
"severity": "error"
|
|
107
|
+
})
|
|
108
|
+
status = "red"
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
"status": status,
|
|
112
|
+
"alerts": alerts
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def get_recommendations(db: Session):
|
|
117
|
+
"""
|
|
118
|
+
Get recommendations for database improvements
|
|
119
|
+
"""
|
|
120
|
+
recommendations = []
|
|
121
|
+
|
|
122
|
+
# Check for pgstattuple extension
|
|
123
|
+
pgstattuple_query = text("""
|
|
124
|
+
SELECT COUNT(*) AS count
|
|
125
|
+
FROM pg_extension
|
|
126
|
+
WHERE extname = 'pgstattuple'
|
|
127
|
+
""")
|
|
128
|
+
|
|
129
|
+
# Check for pg_stat_statements extension
|
|
130
|
+
pg_stat_statements_query = text("""
|
|
131
|
+
SELECT COUNT(*) AS count
|
|
132
|
+
FROM pg_extension
|
|
133
|
+
WHERE extname = 'pg_stat_statements'
|
|
134
|
+
""")
|
|
135
|
+
|
|
136
|
+
# Check shared_preload_libraries for pg_stat_statements
|
|
137
|
+
shared_preload_check = text("""
|
|
138
|
+
SELECT current_setting('shared_preload_libraries', true) AS shared_preload_libraries
|
|
139
|
+
""")
|
|
140
|
+
|
|
141
|
+
# Check autovacuum settings
|
|
142
|
+
autovacuum_query = text("""
|
|
143
|
+
SELECT setting::int AS threshold
|
|
144
|
+
FROM pg_settings
|
|
145
|
+
WHERE name = 'autovacuum_vacuum_threshold'
|
|
146
|
+
""")
|
|
147
|
+
|
|
148
|
+
# Check max_connections usage
|
|
149
|
+
connections_query = text("""
|
|
150
|
+
SELECT
|
|
151
|
+
COUNT(*) AS current_connections,
|
|
152
|
+
(SELECT setting::int FROM pg_settings WHERE name = 'max_connections') AS max_connections
|
|
153
|
+
""")
|
|
154
|
+
|
|
155
|
+
try:
|
|
156
|
+
|
|
157
|
+
# Check pgstattuple extension
|
|
158
|
+
pgstattuple_result = db.execute(pgstattuple_query).mappings().first()
|
|
159
|
+
if pgstattuple_result and pgstattuple_result["count"] == 0:
|
|
160
|
+
recommendations.append({
|
|
161
|
+
"type": "extension",
|
|
162
|
+
"message": "Install pgstattuple for accurate bloat metrics",
|
|
163
|
+
"action": "CREATE EXTENSION pgstattuple;"
|
|
164
|
+
})
|
|
165
|
+
|
|
166
|
+
# Check pg_stat_statements extension
|
|
167
|
+
pg_stat_statements_result = db.execute(pg_stat_statements_query).mappings().first()
|
|
168
|
+
if pg_stat_statements_result:
|
|
169
|
+
if pg_stat_statements_result["count"] == 0:
|
|
170
|
+
recommendations.append({
|
|
171
|
+
"type": "extension",
|
|
172
|
+
"message": "Enable pg_stat_statements for query analysis",
|
|
173
|
+
"action": "CREATE EXTENSION pg_stat_statements;"
|
|
174
|
+
})
|
|
175
|
+
else:
|
|
176
|
+
# Extension installed; check if preloaded
|
|
177
|
+
preload_row = db.execute(shared_preload_check).mappings().first()
|
|
178
|
+
preload_val = (preload_row or {}).get("shared_preload_libraries") or ""
|
|
179
|
+
if "pg_stat_statements" not in preload_val:
|
|
180
|
+
recommendations.append({
|
|
181
|
+
"type": "extension",
|
|
182
|
+
"message": "pg_stat_statements installed but not loaded at startup",
|
|
183
|
+
"action": "Set shared_preload_libraries to include pg_stat_statements and restart PostgreSQL; then ensure CREATE EXTENSION pg_stat_statements; is run in each database as needed."
|
|
184
|
+
})
|
|
185
|
+
|
|
186
|
+
# Check autovacuum settings
|
|
187
|
+
autovacuum_result = db.execute(autovacuum_query).mappings().first()
|
|
188
|
+
if autovacuum_result and autovacuum_result["threshold"] > 50:
|
|
189
|
+
recommendations.append({
|
|
190
|
+
"type": "setting",
|
|
191
|
+
"message": "Lower autovacuum_vacuum_threshold for more frequent vacuuming",
|
|
192
|
+
"action": "ALTER SYSTEM SET autovacuum_vacuum_threshold = 50;"
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
# Check max_connections usage
|
|
196
|
+
connections_result = db.execute(connections_query).mappings().first()
|
|
197
|
+
if connections_result:
|
|
198
|
+
current = connections_result["current_connections"]
|
|
199
|
+
maximum = connections_result["max_connections"]
|
|
200
|
+
if current > 0.8 * maximum:
|
|
201
|
+
recommendations.append({
|
|
202
|
+
"type": "setting",
|
|
203
|
+
"message": f"Current connections ({current}) approaching max_connections ({maximum})",
|
|
204
|
+
"action": "ALTER SYSTEM SET max_connections = 200; # Adjust as needed"
|
|
205
|
+
})
|
|
206
|
+
except Exception as e:
|
|
207
|
+
recommendations.append({
|
|
208
|
+
"type": "error",
|
|
209
|
+
"message": f"Error generating recommendations: {str(e)}",
|
|
210
|
+
"action": "Check database connection and permissions"
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
return recommendations
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def get_metrics_summary(db: Session):
|
|
217
|
+
"""
|
|
218
|
+
Get summary metrics for the dashboard
|
|
219
|
+
"""
|
|
220
|
+
metrics = {}
|
|
221
|
+
|
|
222
|
+
# Get average table bloat
|
|
223
|
+
bloat_query = text("""
|
|
224
|
+
SELECT AVG(ROUND(100.0 * n_dead_tup / GREATEST(n_live_tup + n_dead_tup, 1), 2)) AS avg_bloat
|
|
225
|
+
FROM pg_stat_user_tables
|
|
226
|
+
WHERE n_live_tup + n_dead_tup > 0
|
|
227
|
+
""")
|
|
228
|
+
|
|
229
|
+
# Check if pg_stat_statements is available
|
|
230
|
+
latency_ext_check = text("""
|
|
231
|
+
SELECT EXISTS (
|
|
232
|
+
SELECT 1 FROM pg_extension WHERE extname = 'pg_stat_statements'
|
|
233
|
+
) AS has_pg_stat_statements
|
|
234
|
+
""")
|
|
235
|
+
|
|
236
|
+
# Check if pg_stat_statements is preloaded via shared_preload_libraries
|
|
237
|
+
shared_preload_check = text("""
|
|
238
|
+
SELECT current_setting('shared_preload_libraries', true) AS shared_preload_libraries
|
|
239
|
+
""")
|
|
240
|
+
|
|
241
|
+
# Column detection queries for pg_stat_statements (version-agnostic)
|
|
242
|
+
has_total_exec_col_q = text("""
|
|
243
|
+
SELECT EXISTS (
|
|
244
|
+
SELECT 1
|
|
245
|
+
FROM information_schema.columns
|
|
246
|
+
WHERE table_name = 'pg_stat_statements' AND column_name = 'total_exec_time'
|
|
247
|
+
) AS has_total_exec
|
|
248
|
+
""")
|
|
249
|
+
has_total_time_col_q = text("""
|
|
250
|
+
SELECT EXISTS (
|
|
251
|
+
SELECT 1
|
|
252
|
+
FROM information_schema.columns
|
|
253
|
+
WHERE table_name = 'pg_stat_statements' AND column_name = 'total_time'
|
|
254
|
+
) AS has_total_time
|
|
255
|
+
""")
|
|
256
|
+
|
|
257
|
+
# Get autovacuum runs per day (estimated)
|
|
258
|
+
vacuum_query = text("""
|
|
259
|
+
SELECT COUNT(*) AS table_count
|
|
260
|
+
FROM pg_stat_user_tables
|
|
261
|
+
WHERE last_autovacuum > NOW() - INTERVAL '24 hours'
|
|
262
|
+
""")
|
|
263
|
+
|
|
264
|
+
# Always isolate each metric to avoid one failure breaking all
|
|
265
|
+
try:
|
|
266
|
+
bloat_result = db.execute(bloat_query).mappings().first()
|
|
267
|
+
if bloat_result:
|
|
268
|
+
metrics["bloat_avg"] = round(bloat_result["avg_bloat"] or 0, 2)
|
|
269
|
+
except Exception as e:
|
|
270
|
+
metrics.setdefault("errors", []).append(f"bloat: {e}")
|
|
271
|
+
|
|
272
|
+
try:
|
|
273
|
+
latency_check = db.execute(latency_ext_check).mappings().first()
|
|
274
|
+
if latency_check and latency_check["has_pg_stat_statements"]:
|
|
275
|
+
# Check if it's preloaded; if not, warn and recommend enabling
|
|
276
|
+
preload_row = db.execute(shared_preload_check).mappings().first()
|
|
277
|
+
preload_val = (preload_row or {}).get("shared_preload_libraries") or ""
|
|
278
|
+
is_preloaded = "pg_stat_statements" in preload_val
|
|
279
|
+
if not is_preloaded:
|
|
280
|
+
metrics.setdefault("warnings", []).append(
|
|
281
|
+
"pg_stat_statements extension is installed but not loaded via shared_preload_libraries; latency metrics disabled."
|
|
282
|
+
)
|
|
283
|
+
metrics.setdefault("recommendations", []).append(
|
|
284
|
+
"Add pg_stat_statements to shared_preload_libraries and restart PostgreSQL. Example: shared_preload_libraries = 'pg_stat_statements' (then CREATE EXTENSION pg_stat_statements; if not created)."
|
|
285
|
+
)
|
|
286
|
+
metrics.setdefault("notes", []).append("query_latency_avg set to N/A due to missing preload")
|
|
287
|
+
metrics.setdefault("query_latency_avg", "N/A")
|
|
288
|
+
else:
|
|
289
|
+
# Detect which timing column exists to avoid errors on different versions
|
|
290
|
+
has_total_exec = False
|
|
291
|
+
has_total_time = False
|
|
292
|
+
try:
|
|
293
|
+
r1 = db.execute(has_total_exec_col_q).mappings().first()
|
|
294
|
+
has_total_exec = bool(r1 and r1.get("has_total_exec"))
|
|
295
|
+
except Exception:
|
|
296
|
+
pass
|
|
297
|
+
try:
|
|
298
|
+
r2 = db.execute(has_total_time_col_q).mappings().first()
|
|
299
|
+
has_total_time = bool(r2 and r2.get("has_total_time"))
|
|
300
|
+
except Exception:
|
|
301
|
+
pass
|
|
302
|
+
|
|
303
|
+
latency_sql = None
|
|
304
|
+
if has_total_exec:
|
|
305
|
+
latency_sql = text("""
|
|
306
|
+
SELECT AVG(total_exec_time / GREATEST(calls, 1)) AS avg_latency
|
|
307
|
+
FROM pg_stat_statements
|
|
308
|
+
""")
|
|
309
|
+
elif has_total_time:
|
|
310
|
+
latency_sql = text("""
|
|
311
|
+
SELECT AVG(total_time / GREATEST(calls, 1)) AS avg_latency
|
|
312
|
+
FROM pg_stat_statements
|
|
313
|
+
""")
|
|
314
|
+
|
|
315
|
+
if latency_sql is not None:
|
|
316
|
+
try:
|
|
317
|
+
latency_result = db.execute(latency_sql).mappings().first()
|
|
318
|
+
if latency_result:
|
|
319
|
+
metrics["query_latency_avg"] = round(latency_result["avg_latency"] or 0, 2)
|
|
320
|
+
except Exception as e:
|
|
321
|
+
# Ensure failed query doesn't poison the transaction for later ops
|
|
322
|
+
try:
|
|
323
|
+
db.rollback()
|
|
324
|
+
except Exception:
|
|
325
|
+
pass
|
|
326
|
+
# Convert specific prerequisite error into a warning with recommendation
|
|
327
|
+
msg = str(e)
|
|
328
|
+
if "pg_stat_statements must be loaded via \"shared_preload_libraries\"" in msg or "ObjectNotInPrerequisiteState" in msg:
|
|
329
|
+
metrics.setdefault("warnings", []).append(
|
|
330
|
+
"pg_stat_statements is not preloaded; cannot compute query latency."
|
|
331
|
+
)
|
|
332
|
+
metrics.setdefault("recommendations", []).append(
|
|
333
|
+
"Set shared_preload_libraries to include pg_stat_statements and restart PostgreSQL."
|
|
334
|
+
)
|
|
335
|
+
metrics.setdefault("query_latency_avg", "N/A")
|
|
336
|
+
else:
|
|
337
|
+
metrics.setdefault("errors", []).append(f"latency: {e}")
|
|
338
|
+
else:
|
|
339
|
+
# Extension not installed; provide a gentle recommendation
|
|
340
|
+
metrics.setdefault("warnings", []).append(
|
|
341
|
+
"pg_stat_statements extension is not installed; latency metrics unavailable."
|
|
342
|
+
)
|
|
343
|
+
metrics.setdefault("recommendations", []).append(
|
|
344
|
+
"CREATE EXTENSION pg_stat_statements; and consider adding it to shared_preload_libraries for persistent loading."
|
|
345
|
+
)
|
|
346
|
+
metrics.setdefault("query_latency_avg", "N/A")
|
|
347
|
+
except Exception as e:
|
|
348
|
+
metrics.setdefault("errors", []).append(f"latency_check: {e}")
|
|
349
|
+
|
|
350
|
+
try:
|
|
351
|
+
vacuum_result = db.execute(vacuum_query).mappings().first()
|
|
352
|
+
if vacuum_result:
|
|
353
|
+
metrics["vacuum_runs_24h"] = vacuum_result["table_count"]
|
|
354
|
+
except Exception as e:
|
|
355
|
+
metrics.setdefault("errors", []).append(f"vacuum: {e}")
|
|
356
|
+
|
|
357
|
+
# If there were errors, also keep a human-friendly message
|
|
358
|
+
if "errors" in metrics and metrics["errors"]:
|
|
359
|
+
metrics["error"] = "; ".join(metrics["errors"])
|
|
360
|
+
|
|
361
|
+
return metrics
|
|
362
|
+
|
|
363
|
+
|
|
364
|
+
@dashboard_router.get("/")
|
|
365
|
+
async def get_dashboard_view(
|
|
366
|
+
request: Request,
|
|
367
|
+
db=Depends(get_db),
|
|
368
|
+
view_builder=Depends(get_view_builder)
|
|
369
|
+
):
|
|
370
|
+
"""
|
|
371
|
+
Get the PostgreSQL global dashboard view
|
|
372
|
+
"""
|
|
373
|
+
# Get dashboard data
|
|
374
|
+
health = get_cluster_health(db)
|
|
375
|
+
recommendations = get_recommendations(db)
|
|
376
|
+
metrics = get_metrics_summary(db)
|
|
377
|
+
|
|
378
|
+
# Create widgets
|
|
379
|
+
status_icon = "✅" if health["status"] == "green" else "⚠️" if health["status"] == "yellow" else "❌"
|
|
380
|
+
|
|
381
|
+
health_card = CardWidget(
|
|
382
|
+
title=f"Global Health: {status_icon} {health['status'].capitalize()}",
|
|
383
|
+
content="Your PostgreSQL database is healthy." if health["status"] == "green" else
|
|
384
|
+
"Your PostgreSQL database has some warnings." if health["status"] == "yellow" else
|
|
385
|
+
"Your PostgreSQL database has critical issues that need attention."
|
|
386
|
+
)
|
|
387
|
+
|
|
388
|
+
# Create alerts table if there are any alerts
|
|
389
|
+
widgets = [health_card]
|
|
390
|
+
|
|
391
|
+
if health["alerts"]:
|
|
392
|
+
alerts_table = TableWidget(
|
|
393
|
+
title="Active Alerts",
|
|
394
|
+
endpoint="/postgres/dashboard",
|
|
395
|
+
data=health["alerts"]
|
|
396
|
+
)
|
|
397
|
+
widgets.append(alerts_table)
|
|
398
|
+
|
|
399
|
+
# Create recommendations table
|
|
400
|
+
if recommendations:
|
|
401
|
+
recommendations_table = TableWidget(
|
|
402
|
+
title="Recommendations",
|
|
403
|
+
endpoint="/postgres/dashboard",
|
|
404
|
+
data=recommendations
|
|
405
|
+
)
|
|
406
|
+
widgets.append(recommendations_table)
|
|
407
|
+
|
|
408
|
+
# Create metrics summary card
|
|
409
|
+
metrics_content = "Key Metrics:\n"
|
|
410
|
+
for key, value in metrics.items():
|
|
411
|
+
metrics_content += f"- {key.replace('_', ' ').title()}: {value}\n"
|
|
412
|
+
|
|
413
|
+
metrics_card = CardWidget(
|
|
414
|
+
title="Metrics Summary",
|
|
415
|
+
content=metrics_content
|
|
416
|
+
)
|
|
417
|
+
widgets.append(metrics_card)
|
|
418
|
+
|
|
419
|
+
# Add a cool clickable cards grid for all Postgres Tools features
|
|
420
|
+
cards = [
|
|
421
|
+
{
|
|
422
|
+
"title": "Database Size",
|
|
423
|
+
"icon": "fas fa-database",
|
|
424
|
+
"description": "Explore overall DB size, schemas, tablespaces and largest tables.",
|
|
425
|
+
"url": request.url_for("get_db_size_view")
|
|
426
|
+
},
|
|
427
|
+
{
|
|
428
|
+
"title": "Sequences",
|
|
429
|
+
"icon": "fas fa-list-ol",
|
|
430
|
+
"description": "Check sequence usage and remaining capacity.",
|
|
431
|
+
"url": request.url_for("get_sequences_view")
|
|
432
|
+
},
|
|
433
|
+
{
|
|
434
|
+
"title": "Indexes",
|
|
435
|
+
"icon": "fas fa-layer-group",
|
|
436
|
+
"description": "Find large or unused indexes and scan counts.",
|
|
437
|
+
"url": request.url_for("get_indexes_view")
|
|
438
|
+
},
|
|
439
|
+
{
|
|
440
|
+
"title": "Tables",
|
|
441
|
+
"icon": "fas fa-table",
|
|
442
|
+
"description": "Detect table bloat, last vacuum/analyze and sizes.",
|
|
443
|
+
"url": request.url_for("get_tables_view")
|
|
444
|
+
},
|
|
445
|
+
{
|
|
446
|
+
"title": "Queries",
|
|
447
|
+
"icon": "fas fa-tachometer-alt",
|
|
448
|
+
"description": "Analyze heavy queries with pg_stat_statements.",
|
|
449
|
+
"url": request.url_for("get_queries_view")
|
|
450
|
+
},
|
|
451
|
+
{
|
|
452
|
+
"title": "Connections & Locks",
|
|
453
|
+
"icon": "fas fa-link",
|
|
454
|
+
"description": "Inspect active connections and lock contention.",
|
|
455
|
+
"url": request.url_for("get_connections_view")
|
|
456
|
+
},
|
|
457
|
+
{
|
|
458
|
+
"title": "Vacuum Status",
|
|
459
|
+
"icon": "fas fa-broom",
|
|
460
|
+
"description": "Track vacuum activity and autovacuum settings.",
|
|
461
|
+
"url": request.url_for("get_vacuum_view")
|
|
462
|
+
},
|
|
463
|
+
{
|
|
464
|
+
"title": "Replication & Backups",
|
|
465
|
+
"icon": "fas fa-sync",
|
|
466
|
+
"description": "Monitor replicas, slots and backup history.",
|
|
467
|
+
"url": request.url_for("get_replication_view")
|
|
468
|
+
},
|
|
469
|
+
{
|
|
470
|
+
"title": "Alerts & Webhooks",
|
|
471
|
+
"icon": "fas fa-bell",
|
|
472
|
+
"description": "Configure alert rules and send notifications.",
|
|
473
|
+
"url": request.url_for("get_alerts_view")
|
|
474
|
+
},
|
|
475
|
+
{
|
|
476
|
+
"title": "Extensions",
|
|
477
|
+
"icon": "fas fa-plug",
|
|
478
|
+
"description": "List and manage available PostgreSQL extensions.",
|
|
479
|
+
"url": request.url_for("get_extensions_view")
|
|
480
|
+
},
|
|
481
|
+
]
|
|
482
|
+
|
|
483
|
+
widgets.append(
|
|
484
|
+
CustomTemplateWidget(
|
|
485
|
+
template_name="postgres_tools/dashboard_cards.html.j2",
|
|
486
|
+
context={
|
|
487
|
+
"title": "PostgreSQL Tools",
|
|
488
|
+
"cards": cards
|
|
489
|
+
}
|
|
490
|
+
)
|
|
491
|
+
)
|
|
492
|
+
|
|
493
|
+
# Render the view
|
|
494
|
+
return view_builder.generate(
|
|
495
|
+
request,
|
|
496
|
+
title="PostgreSQL Global Dashboard",
|
|
497
|
+
widgets=widgets
|
|
498
|
+
)
|
{fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/src/routers/db_size.py
RENAMED
|
@@ -83,11 +83,6 @@ def get_largest_tables(db: Session, limit: int = 20, schema: str = None):
|
|
|
83
83
|
|
|
84
84
|
|
|
85
85
|
@db_size_router.get("")
|
|
86
|
-
@menu_entry(
|
|
87
|
-
label="Database Size",
|
|
88
|
-
icon="fa fa-database",
|
|
89
|
-
parent="postgres_tools"
|
|
90
|
-
)
|
|
91
86
|
async def get_db_size_view(
|
|
92
87
|
request: Request,
|
|
93
88
|
db=Depends(get_db),
|
{fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/src/routers/indexes.py
RENAMED
|
@@ -39,11 +39,6 @@ def get_index_usage_statistics(db: Session, schema: str = None, threshold_unused
|
|
|
39
39
|
|
|
40
40
|
|
|
41
41
|
@indexes_router.get("")
|
|
42
|
-
@menu_entry(
|
|
43
|
-
label="Index Usage",
|
|
44
|
-
icon="fa fa-table",
|
|
45
|
-
parent="postgres_tools"
|
|
46
|
-
)
|
|
47
42
|
async def get_indexes_view(
|
|
48
43
|
request: Request,
|
|
49
44
|
db=Depends(get_db),
|
{fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/src/routers/queries.py
RENAMED
|
@@ -65,11 +65,6 @@ def get_query_performance_statistics(db_manager: Session, limit: int = 10, inclu
|
|
|
65
65
|
|
|
66
66
|
|
|
67
67
|
@queries_router.get("")
|
|
68
|
-
@menu_entry(
|
|
69
|
-
label="Query Performance",
|
|
70
|
-
icon="fa fa-tachometer",
|
|
71
|
-
parent="postgres_tools"
|
|
72
|
-
)
|
|
73
68
|
async def get_queries_view(
|
|
74
69
|
request: Request,
|
|
75
70
|
db=Depends(get_db),
|
{fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/src/routers/replication.py
RENAMED
|
@@ -114,11 +114,6 @@ def get_backup_history(db: Session, limit: int = 10):
|
|
|
114
114
|
|
|
115
115
|
|
|
116
116
|
@replication_router.get("")
|
|
117
|
-
@menu_entry(
|
|
118
|
-
label="Replication & Backups",
|
|
119
|
-
icon="fa fa-sync",
|
|
120
|
-
parent="postgres_tools"
|
|
121
|
-
)
|
|
122
117
|
async def get_replication_view(
|
|
123
118
|
request: Request,
|
|
124
119
|
db=Depends(get_db),
|
{fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/src/routers/sequences.py
RENAMED
|
@@ -50,11 +50,6 @@ def get_sequences_data(db: Session):
|
|
|
50
50
|
|
|
51
51
|
|
|
52
52
|
@sequences_router.get("")
|
|
53
|
-
@menu_entry(
|
|
54
|
-
label="Sequences",
|
|
55
|
-
icon="fa fa-list",
|
|
56
|
-
parent="postgres_tools"
|
|
57
|
-
)
|
|
58
53
|
async def get_sequences_view(request: Request, db=Depends(get_db), view_builder=Depends(get_view_builder)):
|
|
59
54
|
"""
|
|
60
55
|
Get the PostgreSQL sequences view
|
|
@@ -42,7 +42,8 @@ def get_table_statistics(db_manager: Session, schema: str = None, threshold_warn
|
|
|
42
42
|
ORDER BY bloat_percent DESC
|
|
43
43
|
"""
|
|
44
44
|
|
|
45
|
-
|
|
45
|
+
result = db_manager.execute(text(query)).mappings()
|
|
46
|
+
return [dict(row) for row in result]
|
|
46
47
|
|
|
47
48
|
|
|
48
49
|
def check_pgstattuple_extension(db_manager: Session):
|
|
@@ -60,11 +61,6 @@ def check_pgstattuple_extension(db_manager: Session):
|
|
|
60
61
|
|
|
61
62
|
|
|
62
63
|
@tables_router.get("")
|
|
63
|
-
@menu_entry(
|
|
64
|
-
label="Table Statistics",
|
|
65
|
-
icon="fa fa-table",
|
|
66
|
-
parent="postgres_tools"
|
|
67
|
-
)
|
|
68
64
|
async def get_tables_view(
|
|
69
65
|
request: Request,
|
|
70
66
|
db=Depends(get_db),
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{#
|
|
2
|
+
PostgreSQL Tools - Dashboard Cards Grid
|
|
3
|
+
Context (prefixed by widget):
|
|
4
|
+
- widget.title: string
|
|
5
|
+
- widget.cards: list of { title, icon, description, url }
|
|
6
|
+
#}
|
|
7
|
+
<div class="card mb-3">
|
|
8
|
+
<div class="card-body">
|
|
9
|
+
<h5 class="card-title mb-0">
|
|
10
|
+
<i class="fas fa-toolbox me-2"></i>{{ widget.title or 'PostgreSQL Tools' }}
|
|
11
|
+
</h5>
|
|
12
|
+
</div>
|
|
13
|
+
</div>
|
|
14
|
+
|
|
15
|
+
<div class="row g-3">
|
|
16
|
+
{% for c in widget.cards %}
|
|
17
|
+
<div class="col-12 col-sm-6 col-lg-4 col-xl-3">
|
|
18
|
+
<a href="{{ c.url }}" class="text-decoration-none text-reset">
|
|
19
|
+
<div class="card h-100 shadow-sm border-0 card-hover">
|
|
20
|
+
<div class="card-body d-flex flex-column">
|
|
21
|
+
<div class="d-flex align-items-center mb-2">
|
|
22
|
+
{% if c.icon %}
|
|
23
|
+
<i class="{{ c.icon }} text-primary me-2" style="font-size: 1.25rem;"></i>
|
|
24
|
+
{% else %}
|
|
25
|
+
<i class="fas fa-circle text-primary me-2" style="font-size: 1.25rem;"></i>
|
|
26
|
+
{% endif %}
|
|
27
|
+
<h6 class="card-title mb-0">{{ c.title }}</h6>
|
|
28
|
+
</div>
|
|
29
|
+
{% if c.description %}
|
|
30
|
+
<p class="card-text text-muted small flex-grow-1">{{ c.description }}</p>
|
|
31
|
+
{% endif %}
|
|
32
|
+
<div class="mt-2 d-flex align-items-center text-primary small fw-semibold">
|
|
33
|
+
<span>Open</span>
|
|
34
|
+
<i class="fas fa-arrow-right ms-2"></i>
|
|
35
|
+
</div>
|
|
36
|
+
</div>
|
|
37
|
+
</div>
|
|
38
|
+
</a>
|
|
39
|
+
</div>
|
|
40
|
+
{% endfor %}
|
|
41
|
+
</div>
|
|
42
|
+
|
|
43
|
+
<style>
|
|
44
|
+
.card-hover { transition: transform .12s ease, box-shadow .12s ease; }
|
|
45
|
+
.card-hover:hover { transform: translateY(-2px); box-shadow: 0 .5rem 1rem rgba(0,0,0,.08) !important; }
|
|
46
|
+
</style>
|
|
@@ -1,336 +0,0 @@
|
|
|
1
|
-
from fastapi import APIRouter, Depends, Request
|
|
2
|
-
from sqlalchemy import text
|
|
3
|
-
from sqlalchemy.orm import Session
|
|
4
|
-
from typing import List, Dict, Any
|
|
5
|
-
|
|
6
|
-
from fastpluggy.core.database import get_db
|
|
7
|
-
from fastpluggy.core.dependency import get_view_builder
|
|
8
|
-
from fastpluggy.core.widgets import TableWidget
|
|
9
|
-
from fastpluggy_plugin.ui_tools.extra_widget.display.card import CardWidget
|
|
10
|
-
from fastpluggy.core.menu.decorator import menu_entry
|
|
11
|
-
|
|
12
|
-
dashboard_router = APIRouter(prefix="/dashboard", tags=["postgres_dashboard"])
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
def get_cluster_health(db: Session):
|
|
16
|
-
"""
|
|
17
|
-
Get overall cluster health status
|
|
18
|
-
"""
|
|
19
|
-
# Check for high table bloat
|
|
20
|
-
bloat_query = text("""
|
|
21
|
-
SELECT
|
|
22
|
-
COUNT(*) AS count,
|
|
23
|
-
MAX(ROUND(100.0 * n_dead_tup / GREATEST(n_live_tup + n_dead_tup, 1), 2)) AS max_bloat
|
|
24
|
-
FROM pg_stat_user_tables
|
|
25
|
-
WHERE ROUND(100.0 * n_dead_tup / GREATEST(n_live_tup + n_dead_tup, 1), 2) > 40
|
|
26
|
-
""")
|
|
27
|
-
|
|
28
|
-
# Check for unused indexes
|
|
29
|
-
index_query = text("""
|
|
30
|
-
SELECT COUNT(*) AS count
|
|
31
|
-
FROM pg_stat_user_indexes
|
|
32
|
-
WHERE idx_scan = 0 AND pg_relation_size(indexrelid) > 10000000
|
|
33
|
-
""")
|
|
34
|
-
|
|
35
|
-
# Check for long-running queries
|
|
36
|
-
query_query = text("""
|
|
37
|
-
SELECT COUNT(*) AS count
|
|
38
|
-
FROM pg_stat_activity
|
|
39
|
-
WHERE state = 'active' AND query_start < NOW() - INTERVAL '5 minutes'
|
|
40
|
-
""")
|
|
41
|
-
|
|
42
|
-
# Check for replication lag
|
|
43
|
-
replication_query = text("""
|
|
44
|
-
SELECT
|
|
45
|
-
COUNT(*) AS count,
|
|
46
|
-
MAX(pg_wal_lsn_diff(pg_current_wal_lsn(), flush_lsn)) AS max_lag
|
|
47
|
-
FROM pg_stat_replication
|
|
48
|
-
WHERE pg_wal_lsn_diff(pg_current_wal_lsn(), flush_lsn) > 50000000
|
|
49
|
-
""")
|
|
50
|
-
|
|
51
|
-
alerts = []
|
|
52
|
-
status = "green"
|
|
53
|
-
|
|
54
|
-
try:
|
|
55
|
-
|
|
56
|
-
# Check table bloat
|
|
57
|
-
bloat_result = db.execute(bloat_query).mappings().first()
|
|
58
|
-
if bloat_result and bloat_result["count"] > 0:
|
|
59
|
-
alerts.append({
|
|
60
|
-
"module": "tables",
|
|
61
|
-
"message": f"{bloat_result['count']} tables with bloat > 40% (max: {bloat_result['max_bloat']}%)",
|
|
62
|
-
"severity": "error"
|
|
63
|
-
})
|
|
64
|
-
status = "red"
|
|
65
|
-
|
|
66
|
-
# Check unused indexes
|
|
67
|
-
index_result = db.execute(index_query).mappings().first()
|
|
68
|
-
if index_result and index_result["count"] > 0:
|
|
69
|
-
alerts.append({
|
|
70
|
-
"module": "indexes",
|
|
71
|
-
"message": f"{index_result['count']} unused indexes larger than 10MB",
|
|
72
|
-
"severity": "warning"
|
|
73
|
-
})
|
|
74
|
-
if status == "green":
|
|
75
|
-
status = "yellow"
|
|
76
|
-
|
|
77
|
-
# Check long-running queries
|
|
78
|
-
query_result = db.execute(query_query).mappings().first()
|
|
79
|
-
if query_result and query_result["count"] > 0:
|
|
80
|
-
alerts.append({
|
|
81
|
-
"module": "queries",
|
|
82
|
-
"message": f"{query_result['count']} queries running for more than 5 minutes",
|
|
83
|
-
"severity": "warning"
|
|
84
|
-
})
|
|
85
|
-
if status == "green":
|
|
86
|
-
status = "yellow"
|
|
87
|
-
|
|
88
|
-
# Check replication lag
|
|
89
|
-
try:
|
|
90
|
-
replication_result = db.execute(replication_query).mappings().first()
|
|
91
|
-
if replication_result and replication_result["count"] > 0:
|
|
92
|
-
alerts.append({
|
|
93
|
-
"module": "replication",
|
|
94
|
-
"message": f"{replication_result['count']} replicas with lag > 50MB",
|
|
95
|
-
"severity": "error"
|
|
96
|
-
})
|
|
97
|
-
status = "red"
|
|
98
|
-
except Exception:
|
|
99
|
-
# This might fail if not running on a primary server
|
|
100
|
-
pass
|
|
101
|
-
except Exception as e:
|
|
102
|
-
alerts.append({
|
|
103
|
-
"module": "system",
|
|
104
|
-
"message": f"Error checking cluster health: {str(e)}",
|
|
105
|
-
"severity": "error"
|
|
106
|
-
})
|
|
107
|
-
status = "red"
|
|
108
|
-
|
|
109
|
-
return {
|
|
110
|
-
"status": status,
|
|
111
|
-
"alerts": alerts
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
def get_recommendations(db: Session):
|
|
116
|
-
"""
|
|
117
|
-
Get recommendations for database improvements
|
|
118
|
-
"""
|
|
119
|
-
recommendations = []
|
|
120
|
-
|
|
121
|
-
# Check for pgstattuple extension
|
|
122
|
-
pgstattuple_query = text("""
|
|
123
|
-
SELECT COUNT(*) AS count
|
|
124
|
-
FROM pg_extension
|
|
125
|
-
WHERE extname = 'pgstattuple'
|
|
126
|
-
""")
|
|
127
|
-
|
|
128
|
-
# Check for pg_stat_statements extension
|
|
129
|
-
pg_stat_statements_query = text("""
|
|
130
|
-
SELECT COUNT(*) AS count
|
|
131
|
-
FROM pg_extension
|
|
132
|
-
WHERE extname = 'pg_stat_statements'
|
|
133
|
-
""")
|
|
134
|
-
|
|
135
|
-
# Check autovacuum settings
|
|
136
|
-
autovacuum_query = text("""
|
|
137
|
-
SELECT setting::int AS threshold
|
|
138
|
-
FROM pg_settings
|
|
139
|
-
WHERE name = 'autovacuum_vacuum_threshold'
|
|
140
|
-
""")
|
|
141
|
-
|
|
142
|
-
# Check max_connections usage
|
|
143
|
-
connections_query = text("""
|
|
144
|
-
SELECT
|
|
145
|
-
COUNT(*) AS current_connections,
|
|
146
|
-
(SELECT setting::int FROM pg_settings WHERE name = 'max_connections') AS max_connections
|
|
147
|
-
""")
|
|
148
|
-
|
|
149
|
-
try:
|
|
150
|
-
|
|
151
|
-
# Check pgstattuple extension
|
|
152
|
-
pgstattuple_result = db.execute(pgstattuple_query).mappings().first()
|
|
153
|
-
if pgstattuple_result and pgstattuple_result["count"] == 0:
|
|
154
|
-
recommendations.append({
|
|
155
|
-
"type": "extension",
|
|
156
|
-
"message": "Install pgstattuple for accurate bloat metrics",
|
|
157
|
-
"action": "CREATE EXTENSION pgstattuple;"
|
|
158
|
-
})
|
|
159
|
-
|
|
160
|
-
# Check pg_stat_statements extension
|
|
161
|
-
pg_stat_statements_result = db.execute(pg_stat_statements_query).mappings().first()
|
|
162
|
-
if pg_stat_statements_result and pg_stat_statements_result["count"] == 0:
|
|
163
|
-
recommendations.append({
|
|
164
|
-
"type": "extension",
|
|
165
|
-
"message": "Enable pg_stat_statements for query analysis",
|
|
166
|
-
"action": "CREATE EXTENSION pg_stat_statements;"
|
|
167
|
-
})
|
|
168
|
-
|
|
169
|
-
# Check autovacuum settings
|
|
170
|
-
autovacuum_result = db.execute(autovacuum_query).mappings().first()
|
|
171
|
-
if autovacuum_result and autovacuum_result["threshold"] > 50:
|
|
172
|
-
recommendations.append({
|
|
173
|
-
"type": "setting",
|
|
174
|
-
"message": "Lower autovacuum_vacuum_threshold for more frequent vacuuming",
|
|
175
|
-
"action": "ALTER SYSTEM SET autovacuum_vacuum_threshold = 50;"
|
|
176
|
-
})
|
|
177
|
-
|
|
178
|
-
# Check max_connections usage
|
|
179
|
-
connections_result = db.execute(connections_query).mappings().first()
|
|
180
|
-
if connections_result:
|
|
181
|
-
current = connections_result["current_connections"]
|
|
182
|
-
maximum = connections_result["max_connections"]
|
|
183
|
-
if current > 0.8 * maximum:
|
|
184
|
-
recommendations.append({
|
|
185
|
-
"type": "setting",
|
|
186
|
-
"message": f"Current connections ({current}) approaching max_connections ({maximum})",
|
|
187
|
-
"action": "ALTER SYSTEM SET max_connections = 200; # Adjust as needed"
|
|
188
|
-
})
|
|
189
|
-
except Exception as e:
|
|
190
|
-
recommendations.append({
|
|
191
|
-
"type": "error",
|
|
192
|
-
"message": f"Error generating recommendations: {str(e)}",
|
|
193
|
-
"action": "Check database connection and permissions"
|
|
194
|
-
})
|
|
195
|
-
|
|
196
|
-
return recommendations
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
def get_metrics_summary(db: Session):
|
|
200
|
-
"""
|
|
201
|
-
Get summary metrics for the dashboard
|
|
202
|
-
"""
|
|
203
|
-
metrics = {}
|
|
204
|
-
|
|
205
|
-
# Get average table bloat
|
|
206
|
-
bloat_query = text("""
|
|
207
|
-
SELECT AVG(ROUND(100.0 * n_dead_tup / GREATEST(n_live_tup + n_dead_tup, 1), 2)) AS avg_bloat
|
|
208
|
-
FROM pg_stat_user_tables
|
|
209
|
-
WHERE n_live_tup + n_dead_tup > 0
|
|
210
|
-
""")
|
|
211
|
-
|
|
212
|
-
# Get average query latency if pg_stat_statements is available
|
|
213
|
-
latency_query = text("""
|
|
214
|
-
SELECT EXISTS (
|
|
215
|
-
SELECT 1 FROM pg_extension WHERE extname = 'pg_stat_statements'
|
|
216
|
-
) AS has_pg_stat_statements
|
|
217
|
-
""")
|
|
218
|
-
|
|
219
|
-
# Try new schema first using derived average (total_exec_time/calls), fallback to legacy (total_time/calls)
|
|
220
|
-
# We avoid relying on mean_* columns which may not exist depending on pg_stat_statements version
|
|
221
|
-
latency_data_query_new = text("""
|
|
222
|
-
SELECT AVG(total_exec_time / GREATEST(calls, 1)) AS avg_latency
|
|
223
|
-
FROM pg_stat_statements
|
|
224
|
-
""")
|
|
225
|
-
latency_data_query_legacy = text("""
|
|
226
|
-
SELECT AVG(total_time / GREATEST(calls, 1)) AS avg_latency
|
|
227
|
-
FROM pg_stat_statements
|
|
228
|
-
""")
|
|
229
|
-
|
|
230
|
-
# Get autovacuum runs per day (estimated)
|
|
231
|
-
vacuum_query = text("""
|
|
232
|
-
SELECT COUNT(*) AS table_count
|
|
233
|
-
FROM pg_stat_user_tables
|
|
234
|
-
WHERE last_autovacuum > NOW() - INTERVAL '24 hours'
|
|
235
|
-
""")
|
|
236
|
-
|
|
237
|
-
try:
|
|
238
|
-
|
|
239
|
-
# Get average table bloat
|
|
240
|
-
bloat_result = db.execute(bloat_query).mappings().first()
|
|
241
|
-
if bloat_result:
|
|
242
|
-
metrics["bloat_avg"] = round(bloat_result["avg_bloat"] or 0, 2)
|
|
243
|
-
|
|
244
|
-
# Check if pg_stat_statements is available
|
|
245
|
-
latency_check = db.execute(latency_query).mappings().first()
|
|
246
|
-
if latency_check and latency_check["has_pg_stat_statements"]:
|
|
247
|
-
# Get average query latency (try new schema first)
|
|
248
|
-
try:
|
|
249
|
-
latency_result = db.execute(latency_data_query_new).mappings().first()
|
|
250
|
-
except Exception:
|
|
251
|
-
# Rollback aborted transaction before retrying with legacy columns
|
|
252
|
-
try:
|
|
253
|
-
db.rollback()
|
|
254
|
-
except Exception:
|
|
255
|
-
pass
|
|
256
|
-
latency_result = db.execute(latency_data_query_legacy).mappings().first()
|
|
257
|
-
if latency_result:
|
|
258
|
-
metrics["query_latency_avg"] = round(latency_result["avg_latency"] or 0, 2)
|
|
259
|
-
|
|
260
|
-
# Get autovacuum runs per day
|
|
261
|
-
vacuum_result = db.execute(vacuum_query).mappings().first()
|
|
262
|
-
if vacuum_result:
|
|
263
|
-
metrics["vacuum_runs_24h"] = vacuum_result["table_count"]
|
|
264
|
-
except Exception as e:
|
|
265
|
-
metrics["error"] = str(e)
|
|
266
|
-
|
|
267
|
-
return metrics
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
@dashboard_router.get("")
|
|
271
|
-
@menu_entry(
|
|
272
|
-
label="Dashboard",
|
|
273
|
-
icon="fa fa-tachometer-alt",
|
|
274
|
-
parent="postgres_tools",
|
|
275
|
-
position=0
|
|
276
|
-
)
|
|
277
|
-
async def get_dashboard_view(
|
|
278
|
-
request: Request,
|
|
279
|
-
db=Depends(get_db),
|
|
280
|
-
view_builder=Depends(get_view_builder)
|
|
281
|
-
):
|
|
282
|
-
"""
|
|
283
|
-
Get the PostgreSQL global dashboard view
|
|
284
|
-
"""
|
|
285
|
-
# Get dashboard data
|
|
286
|
-
health = get_cluster_health(db)
|
|
287
|
-
recommendations = get_recommendations(db)
|
|
288
|
-
metrics = get_metrics_summary(db)
|
|
289
|
-
|
|
290
|
-
# Create widgets
|
|
291
|
-
status_icon = "✅" if health["status"] == "green" else "⚠️" if health["status"] == "yellow" else "❌"
|
|
292
|
-
|
|
293
|
-
health_card = CardWidget(
|
|
294
|
-
title=f"Global Health: {status_icon} {health['status'].capitalize()}",
|
|
295
|
-
content="Your PostgreSQL database is healthy." if health["status"] == "green" else
|
|
296
|
-
"Your PostgreSQL database has some warnings." if health["status"] == "yellow" else
|
|
297
|
-
"Your PostgreSQL database has critical issues that need attention."
|
|
298
|
-
)
|
|
299
|
-
|
|
300
|
-
# Create alerts table if there are any alerts
|
|
301
|
-
widgets = [health_card]
|
|
302
|
-
|
|
303
|
-
if health["alerts"]:
|
|
304
|
-
alerts_table = TableWidget(
|
|
305
|
-
title="Active Alerts",
|
|
306
|
-
endpoint="/postgres/dashboard",
|
|
307
|
-
data=health["alerts"]
|
|
308
|
-
)
|
|
309
|
-
widgets.append(alerts_table)
|
|
310
|
-
|
|
311
|
-
# Create recommendations table
|
|
312
|
-
if recommendations:
|
|
313
|
-
recommendations_table = TableWidget(
|
|
314
|
-
title="Recommendations",
|
|
315
|
-
endpoint="/postgres/dashboard",
|
|
316
|
-
data=recommendations
|
|
317
|
-
)
|
|
318
|
-
widgets.append(recommendations_table)
|
|
319
|
-
|
|
320
|
-
# Create metrics summary card
|
|
321
|
-
metrics_content = "Key Metrics:\n"
|
|
322
|
-
for key, value in metrics.items():
|
|
323
|
-
metrics_content += f"- {key.replace('_', ' ').title()}: {value}\n"
|
|
324
|
-
|
|
325
|
-
metrics_card = CardWidget(
|
|
326
|
-
title="Metrics Summary",
|
|
327
|
-
content=metrics_content
|
|
328
|
-
)
|
|
329
|
-
widgets.append(metrics_card)
|
|
330
|
-
|
|
331
|
-
# Render the view
|
|
332
|
-
return view_builder.generate(
|
|
333
|
-
request,
|
|
334
|
-
title="PostgreSQL Global Dashboard",
|
|
335
|
-
widgets=widgets
|
|
336
|
-
)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{fastpluggy_postgres_tools-0.1.26 → fastpluggy_postgres_tools-0.1.34}/src/routers/__init__.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|