Skip to content

Commit 10e471d

Browse files
committed
Refactor SQL query formatting in various scripts
- Updated multiple scripts to improve SQL query formatting by ensuring consistent line breaks and indentation, enhancing readability. - Adjusted queries in `alertManager.sh`, `generateMetrics.sh`, `updateDashboard.sh`, `metricsFunctions.sh`, `monitoringFunctions.sh`, and others to follow a uniform style. - Improved overall code clarity and maintainability by applying consistent formatting practices across the codebase.
1 parent caeb984 commit 10e471d

File tree

58 files changed

+1710
-1308
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1710
-1308
lines changed

bin/alerts/alertManager.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ show_stats() {
405405
local dbport="${DBPORT:-5432}"
406406
local dbuser="${DBUSER:-postgres}"
407407

408-
local query="SELECT
408+
local query="SELECT
409409
component,
410410
alert_level,
411411
status,

bin/dashboard/generateMetrics.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ generate_component_metrics() {
103103
local component_lower
104104
component_lower=$(echo "${component}" | tr '[:upper:]' '[:lower:]')
105105

106-
local query="SELECT
106+
local query="SELECT
107107
metric_name,
108108
metric_value,
109109
metadata,
@@ -215,7 +215,7 @@ generate_dashboard_metrics() {
215215
component_lower=$(echo "${component}" | tr '[:upper:]' '[:lower:]')
216216

217217
# Aggregate metrics by name for dashboard
218-
local dashboard_query="SELECT
218+
local dashboard_query="SELECT
219219
metric_name,
220220
COUNT(*) as count,
221221
AVG(metric_value::numeric) as avg_value,

bin/dashboard/updateDashboard.sh

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ update_component_health() {
308308
SELECT DISTINCT ON (component)
309309
component,
310310
MAX(timestamp) as latest_timestamp,
311-
CASE
311+
CASE
312312
WHEN COUNT(*) FILTER (WHERE (metric_name LIKE '%error%' OR metric_name LIKE '%failure%') AND metric_value::numeric > 0) > 0 THEN 'degraded'
313313
WHEN COUNT(*) FILTER (WHERE metric_name LIKE '%availability%' AND metric_value::numeric < 1) > 0 THEN 'down'
314314
WHEN MAX(timestamp) < CURRENT_TIMESTAMP - INTERVAL '1 hour' THEN 'unknown'
@@ -320,7 +320,7 @@ update_component_health() {
320320
ORDER BY component, MAX(timestamp) DESC
321321
)
322322
INSERT INTO component_health (component, status, last_check, last_success)
323-
SELECT
323+
SELECT
324324
lm.component,
325325
lm.status,
326326
CURRENT_TIMESTAMP,
@@ -331,7 +331,7 @@ update_component_health() {
331331
status = EXCLUDED.status,
332332
last_check = EXCLUDED.last_check,
333333
last_success = CASE WHEN EXCLUDED.status = 'healthy' THEN EXCLUDED.last_success ELSE component_health.last_success END,
334-
error_count = CASE
334+
error_count = CASE
335335
WHEN EXCLUDED.status != 'healthy' THEN component_health.error_count + 1
336336
ELSE 0
337337
END;
@@ -356,37 +356,37 @@ update_component_health() {
356356

357357
# Update daemon health separately (daemon metrics are under 'ingestion' component)
358358
local daemon_query="
359-
UPDATE component_health
360-
SET status = CASE
359+
UPDATE component_health
360+
SET status = CASE
361361
WHEN EXISTS (
362-
SELECT 1 FROM metrics
363-
WHERE component = 'ingestion'
364-
AND metric_name LIKE 'daemon%'
362+
SELECT 1 FROM metrics
363+
WHERE component = 'ingestion'
364+
AND metric_name LIKE 'daemon%'
365365
AND timestamp > CURRENT_TIMESTAMP - INTERVAL '5 minutes'
366366
) THEN 'healthy'
367367
WHEN EXISTS (
368-
SELECT 1 FROM metrics
369-
WHERE component = 'ingestion'
370-
AND metric_name LIKE 'daemon%'
368+
SELECT 1 FROM metrics
369+
WHERE component = 'ingestion'
370+
AND metric_name LIKE 'daemon%'
371371
AND timestamp > CURRENT_TIMESTAMP - INTERVAL '1 hour'
372372
) THEN 'degraded'
373373
ELSE 'down'
374374
END,
375375
last_check = CURRENT_TIMESTAMP,
376-
last_success = CASE
376+
last_success = CASE
377377
WHEN EXISTS (
378-
SELECT 1 FROM metrics
379-
WHERE component = 'ingestion'
380-
AND metric_name LIKE 'daemon%'
378+
SELECT 1 FROM metrics
379+
WHERE component = 'ingestion'
380+
AND metric_name LIKE 'daemon%'
381381
AND timestamp > CURRENT_TIMESTAMP - INTERVAL '5 minutes'
382382
) THEN CURRENT_TIMESTAMP
383383
ELSE last_success
384384
END,
385-
error_count = CASE
385+
error_count = CASE
386386
WHEN EXISTS (
387-
SELECT 1 FROM metrics
388-
WHERE component = 'ingestion'
389-
AND metric_name LIKE 'daemon%'
387+
SELECT 1 FROM metrics
388+
WHERE component = 'ingestion'
389+
AND metric_name LIKE 'daemon%'
390390
AND timestamp > CURRENT_TIMESTAMP - INTERVAL '5 minutes'
391391
) THEN 0
392392
ELSE error_count + 1

bin/lib/metricsFunctions.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ get_metrics_summary() {
4646
local dbuser="${DBUSER:-postgres}"
4747

4848
local query
49-
query="SELECT
49+
query="SELECT
5050
metric_name,
5151
AVG(metric_value) as avg_value,
5252
MIN(metric_value) as min_value,
@@ -377,7 +377,7 @@ aggregate_metrics() {
377377
esac
378378

379379
local query
380-
query="SELECT
380+
query="SELECT
381381
${time_group} as period,
382382
AVG(metric_value) as avg_value,
383383
MIN(metric_value) as min_value,

bin/lib/monitoringFunctions.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -362,15 +362,15 @@ update_component_health() {
362362

363363
local query
364364
if [[ "${status}" == "healthy" ]]; then
365-
query="UPDATE component_health
366-
SET status = '${status}',
365+
query="UPDATE component_health
366+
SET status = '${status}',
367367
last_check = CURRENT_TIMESTAMP,
368368
last_success = CURRENT_TIMESTAMP,
369369
error_count = ${error_count}
370370
WHERE component = '${component}';"
371371
else
372-
query="UPDATE component_health
373-
SET status = '${status}',
372+
query="UPDATE component_health
373+
SET status = '${status}',
374374
last_check = CURRENT_TIMESTAMP,
375375
error_count = ${error_count}
376376
WHERE component = '${component}';"

bin/monitor/collectBoundaryMetrics.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ detect_notes_out_of_bounds() {
156156
log_info "${COMPONENT}: Detecting notes with coordinates out of bounds"
157157

158158
# Check for notes with invalid coordinates (outside valid lat/lon ranges)
159-
local query="SELECT COUNT(*) FROM notes
160-
WHERE latitude < -90 OR latitude > 90
159+
local query="SELECT COUNT(*) FROM notes
160+
WHERE latitude < -90 OR latitude > 90
161161
OR longitude < -180 OR longitude > 180;"
162162
local result
163163
result=$(execute_sql_query "${query}" 2> /dev/null | tr -d '[:space:]' || echo "0")
@@ -181,8 +181,8 @@ detect_wrong_country_assignments() {
181181
log_info "${COMPONENT}: Detecting notes with potentially wrong country assignment"
182182

183183
# Check 1: Notes with country_id that doesn't exist in countries table
184-
local query_invalid_ref="SELECT COUNT(*) FROM notes n
185-
WHERE n.country_id IS NOT NULL
184+
local query_invalid_ref="SELECT COUNT(*) FROM notes n
185+
WHERE n.country_id IS NOT NULL
186186
AND NOT EXISTS (
187187
SELECT 1 FROM countries c WHERE c.id = n.country_id
188188
);"
@@ -201,7 +201,7 @@ detect_wrong_country_assignments() {
201201
AND n.latitude IS NOT NULL
202202
AND n.longitude IS NOT NULL
203203
AND EXISTS (
204-
SELECT 1 FROM countries c
204+
SELECT 1 FROM countries c
205205
WHERE c.id = n.country_id
206206
AND (
207207
-- Check if note coordinates are outside country bounding box
@@ -225,7 +225,7 @@ detect_wrong_country_assignments() {
225225
AND n.latitude IS NOT NULL
226226
AND n.longitude IS NOT NULL
227227
AND EXISTS (
228-
SELECT 1 FROM countries c
228+
SELECT 1 FROM countries c
229229
WHERE c.id = n.country_id
230230
AND c.geometry IS NOT NULL
231231
AND NOT ST_Contains(
@@ -297,7 +297,7 @@ detect_notes_affected_by_boundary_changes() {
297297
AND n.longitude IS NOT NULL
298298
AND n.updated_at < (SELECT MAX(updated_at) FROM countries WHERE updated_at IS NOT NULL)
299299
AND EXISTS (
300-
SELECT 1 FROM countries c
300+
SELECT 1 FROM countries c
301301
WHERE c.id = n.country_id
302302
AND c.updated_at > n.updated_at
303303
);"

bin/monitor/collectDatabaseMetrics.sh

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ collect_table_sizes() {
4545
log_info "${COMPONENT}: Collecting table sizes"
4646

4747
local query="
48-
SELECT
48+
SELECT
4949
tablename,
5050
pg_total_relation_size('public.'||tablename) AS total_size_bytes,
5151
pg_relation_size('public.'||tablename) AS table_size_bytes,
@@ -91,7 +91,7 @@ collect_table_bloat() {
9191
log_info "${COMPONENT}: Collecting table bloat metrics"
9292

9393
local query="
94-
SELECT
94+
SELECT
9595
tablename,
9696
n_live_tup AS live_tuples,
9797
n_dead_tup AS dead_tuples,
@@ -148,12 +148,12 @@ collect_index_usage() {
148148
log_info "${COMPONENT}: Collecting index usage metrics"
149149

150150
local query="
151-
SELECT
151+
SELECT
152152
t.tablename,
153153
COALESCE(SUM(i.idx_scan), 0) AS total_index_scans,
154154
t.seq_scan AS sequential_scans,
155-
CASE
156-
WHEN t.seq_scan + COALESCE(SUM(i.idx_scan), 0) > 0
155+
CASE
156+
WHEN t.seq_scan + COALESCE(SUM(i.idx_scan), 0) > 0
157157
THEN ROUND(COALESCE(SUM(i.idx_scan), 0) * 100.0 / (t.seq_scan + COALESCE(SUM(i.idx_scan), 0)), 2)
158158
ELSE 0
159159
END AS index_scan_ratio_percent
@@ -211,7 +211,7 @@ collect_unused_indexes() {
211211
log_info "${COMPONENT}: Collecting unused indexes metrics"
212212

213213
local query="
214-
SELECT
214+
SELECT
215215
COUNT(*) AS unused_index_count,
216216
SUM(pg_relation_size(indexrelid)) AS unused_index_size_bytes
217217
FROM pg_stat_user_indexes
@@ -263,7 +263,7 @@ collect_slow_queries() {
263263
fi
264264

265265
local query="
266-
SELECT
266+
SELECT
267267
COUNT(*) AS slow_query_count
268268
FROM pg_stat_statements
269269
WHERE mean_exec_time > 1000
@@ -288,7 +288,7 @@ collect_cache_hit_ratio() {
288288
log_info "${COMPONENT}: Collecting cache hit ratio"
289289

290290
local query="
291-
SELECT
291+
SELECT
292292
ROUND(blks_hit * 100.0 / NULLIF(blks_hit + blks_read, 0), 2) AS cache_hit_ratio_percent
293293
FROM pg_stat_database
294294
WHERE datname = current_database();
@@ -313,7 +313,7 @@ collect_connection_stats() {
313313

314314
# Active connections by application
315315
local app_query="
316-
SELECT
316+
SELECT
317317
application_name,
318318
COUNT(*) AS connection_count
319319
FROM pg_stat_activity
@@ -339,7 +339,7 @@ collect_connection_stats() {
339339

340340
# Overall connection statistics
341341
local conn_query="
342-
SELECT
342+
SELECT
343343
COUNT(*) AS total_connections,
344344
COUNT(*) FILTER (WHERE state = 'active') AS active_connections,
345345
COUNT(*) FILTER (WHERE state = 'idle') AS idle_connections,
@@ -375,7 +375,7 @@ collect_connection_stats() {
375375

376376
# Connection usage percentage
377377
local usage_query="
378-
SELECT
378+
SELECT
379379
COUNT(*) AS current_connections,
380380
current_setting('max_connections')::integer AS max_connections,
381381
ROUND(COUNT(*) * 100.0 / current_setting('max_connections')::integer, 2) AS connection_usage_percent
@@ -413,7 +413,7 @@ collect_lock_stats() {
413413

414414
# Active locks summary
415415
local lock_query="
416-
SELECT
416+
SELECT
417417
COUNT(*) AS total_locks,
418418
COUNT(*) FILTER (WHERE granted = true) AS granted_locks,
419419
COUNT(*) FILTER (WHERE granted = false) AS waiting_locks
@@ -441,7 +441,7 @@ collect_lock_stats() {
441441

442442
# Deadlocks count
443443
local deadlock_query="
444-
SELECT
444+
SELECT
445445
deadlocks AS deadlocks_count
446446
FROM pg_stat_database
447447
WHERE datname = current_database();

bin/monitor/monitorAPI.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Options:
5757
-q, --quiet Suppress non-error output
5858
-c, --config FILE Use specific configuration file
5959
--check CHECK Run specific check only
60-
Available checks: availability, rate_limiting,
60+
Available checks: availability, rate_limiting,
6161
ddos_protection, abuse_detection
6262
6363
Examples:

0 commit comments

Comments
 (0)