-- PostgreSQL schema for SMART data logging -- Main table for SMART metrics snapshots CREATE TABLE IF NOT EXISTS smart_metrics ( id BIGSERIAL PRIMARY KEY, timestamp TIMESTAMPTZ NOT NULL DEFAULT NOW(), device_path VARCHAR(255) NOT NULL, serial_number VARCHAR(255) NOT NULL, model VARCHAR(255), disk_role VARCHAR(50), -- e.g., 'd1', 'd2', 'parity', '2-parity', or '-' for unused -- Basic metrics (from SnapRAID smart output) temperature_celsius INTEGER, power_on_days INTEGER, error_count INTEGER, size_tb DECIMAL(10, 2), -- Calculated failure probability failure_probability_pct DECIMAL(5, 2), -- 0.00 to 100.00 -- Raw SMART attributes (key-value pairs as JSONB) smart_attributes JSONB, -- Indexes for efficient querying CONSTRAINT unique_device_timestamp UNIQUE (device_path, timestamp) ); -- Index on serial number for device history queries CREATE INDEX IF NOT EXISTS idx_smart_metrics_serial ON smart_metrics(serial_number); -- Index on timestamp for time-range queries CREATE INDEX IF NOT EXISTS idx_smart_metrics_timestamp ON smart_metrics(timestamp DESC); -- Index on device_path for per-device queries CREATE INDEX IF NOT EXISTS idx_smart_metrics_device ON smart_metrics(device_path); -- Index on JSONB attributes for querying specific SMART values CREATE INDEX IF NOT EXISTS idx_smart_attributes_gin ON smart_metrics USING gin (smart_attributes); -- View for latest metrics per device CREATE OR REPLACE VIEW smart_latest AS SELECT DISTINCT ON (device_path) * FROM smart_metrics ORDER BY device_path, timestamp DESC; -- View for high-risk devices (failure probability > 50%) CREATE OR REPLACE VIEW smart_high_risk AS SELECT * FROM smart_latest WHERE failure_probability_pct > 50.0 ORDER BY failure_probability_pct DESC; COMMENT ON TABLE smart_metrics IS 'Historical SMART metrics collected from Scrutiny InfluxDB'; COMMENT ON COLUMN smart_metrics.failure_probability_pct IS 'BackBlaze-based 1-year failure probability (0-100%)'; COMMENT ON COLUMN smart_metrics.smart_attributes IS 'Raw SMART attributes as JSON (attribute_id -> value)'; COMMENT ON VIEW smart_latest IS 'Latest SMART metrics for each device'; COMMENT ON VIEW smart_high_risk IS 'Devices with >50% annual failure probability';