-- DataSource 문제 진단 SQL
-- 10.26.252.51과 10.29.17.90 양쪽에서 실행하여 비교

-- ============================================
-- 1. 현재 활성 연결 확인
-- ============================================
SELECT
    pid,
    usename,
    application_name,
    client_addr,
    backend_start,
    state,
    query_start,
    LEFT(query, 100) as current_query
FROM pg_stat_activity
WHERE datname IN ('mdadb', 'mpcdb2')
AND application_name LIKE '%vessel%'
ORDER BY backend_start DESC;

-- ============================================
-- 2. 최근 INSERT/UPDATE 통계 확인
-- ============================================
SELECT
    schemaname,
    tablename,
    n_tup_ins as total_inserts,
    n_tup_upd as total_updates,
    n_tup_del as total_deletes,
    n_live_tup as live_rows,
    last_autoanalyze,
    last_autovacuum
FROM pg_stat_user_tables
WHERE schemaname = 'signal'
AND tablename IN (
    't_vessel_tracks_5min',
    't_vessel_tracks_hourly',
    't_vessel_tracks_daily',
    't_abnormal_tracks',
    't_vessel_latest_position'
)
ORDER BY n_tup_ins DESC;

-- ============================================
-- 3. 최근 데이터 확인 (마지막 INSERT 시간)
-- ============================================

-- 5분 집계
SELECT
    'tracks_5min' as table_name,
    COUNT(*) as total_rows,
    MAX(time_bucket) as last_time_bucket,
    NOW() - MAX(time_bucket) as data_delay
FROM signal.t_vessel_tracks_5min;

-- 시간 집계
SELECT
    'tracks_hourly' as table_name,
    COUNT(*) as total_rows,
    MAX(time_bucket) as last_time_bucket,
    NOW() - MAX(time_bucket) as data_delay
FROM signal.t_vessel_tracks_hourly;

-- 일 집계
SELECT
    'tracks_daily' as table_name,
    COUNT(*) as total_rows,
    MAX(time_bucket) as last_time_bucket,
    NOW() - MAX(time_bucket) as data_delay
FROM signal.t_vessel_tracks_daily;

-- 비정상 궤적
SELECT
    'abnormal_tracks' as table_name,
    COUNT(*) as total_rows,
    MAX(time_bucket) as last_time_bucket,
    NOW() - MAX(time_bucket) as data_delay
FROM signal.t_abnormal_tracks;

-- 최신 위치
SELECT
    'latest_position' as table_name,
    COUNT(*) as total_rows,
    MAX(last_update) as last_update,
    NOW() - MAX(last_update) as data_delay
FROM signal.t_vessel_latest_position;

-- ============================================
-- 4. 특정 시간대 데이터 확인 (지난 1시간)
-- ============================================
SELECT
    '5min_last_hour' as category,
    COUNT(*) as count,
    COUNT(DISTINCT sig_src_cd) as source_count,
    COUNT(DISTINCT target_id) as vessel_count
FROM signal.t_vessel_tracks_5min
WHERE time_bucket >= NOW() - INTERVAL '1 hour';

SELECT
    'hourly_last_day' as category,
    COUNT(*) as count,
    COUNT(DISTINCT sig_src_cd) as source_count,
    COUNT(DISTINCT target_id) as vessel_count
FROM signal.t_vessel_tracks_hourly
WHERE time_bucket >= NOW() - INTERVAL '1 day';

-- ============================================
-- 5. 테이블 크기 확인
-- ============================================
SELECT
    schemaname,
    tablename,
    pg_size_pretty(pg_total_relation_size(schemaname||'.'||tablename)) AS total_size,
    pg_size_pretty(pg_relation_size(schemaname||'.'||tablename)) AS table_size,
    pg_size_pretty(pg_total_relation_size(schemaname||'.'||tablename) - pg_relation_size(schemaname||'.'||tablename)) AS indexes_size
FROM pg_tables
WHERE schemaname = 'signal'
AND tablename IN (
    't_vessel_tracks_5min',
    't_vessel_tracks_hourly',
    't_vessel_tracks_daily',
    't_abnormal_tracks',
    't_vessel_latest_position'
)
ORDER BY pg_total_relation_size(schemaname||'.'||tablename) DESC;

-- ============================================
-- 6. 샘플 데이터 확인 (최근 10개)
-- ============================================
SELECT
    sig_src_cd,
    target_id,
    time_bucket,
    point_count,
    avg_speed,
    max_speed
FROM signal.t_vessel_tracks_5min
ORDER BY time_bucket DESC
LIMIT 10;
