File size: 2,950 Bytes
199bfa3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
CSH2 Web Analytics Dashboard — DELPHI
Main entry point with multi-page navigation
"""
import streamlit as st
import base64
from pathlib import Path

st.set_page_config(
    page_title="CSH2 // DELPHI",
    page_icon="⚙️",
    layout="wide",
    initial_sidebar_state="expanded",
)

from ui.styles import inject_custom_css
from core.db_connector import get_db_connector
from ui.components import system_status_footer

# Inject HUD theme CSS
inject_custom_css()

# Initialize DB connection (cached as singleton)
db = get_db_connector()

# Store DB connector in session state for access from pages
if 'db' not in st.session_state:
    st.session_state.db = db


@st.cache_data
def _get_logo_base64():
    """Load logo and encode as base64 for inline display"""
    logo_path = Path(__file__).parent / "assets" / "csh2_logo.jpeg"
    if not logo_path.exists():
        return None
    with open(logo_path, "rb") as f:
        return base64.b64encode(f.read()).decode()


@st.cache_data(ttl=3600)
def _cached_time_range():
    """Cache the data time range (quasi-static, 1hr TTL)"""
    _db = get_db_connector()
    return _db.get_time_range()


# Sidebar branding
with st.sidebar:
    # Logo with CSS filter for dark background
    logo_b64 = _get_logo_base64()
    if logo_b64:
        st.markdown(f"""
        <div style="text-align: center; margin-bottom: 0.5rem; padding-top: 0.5rem;">
            <img src="data:image/jpeg;base64,{logo_b64}"
                 style="width: 120px; filter: invert(0.85) sepia(0.3) saturate(2) hue-rotate(15deg) brightness(1.1);" />
        </div>
        """, unsafe_allow_html=True)

    st.markdown(
        '<div style="text-align: center; color: #D4A04A; font-size: 0.7rem; letter-spacing: 2px; '
        'text-transform: uppercase; margin-bottom: 12px;">PUMP ANALYTICS</div>',
        unsafe_allow_html=True,
    )

    # System status
    earliest, latest = _cached_time_range()
    system_status_footer(
        sensor_count=len(db.tag_cache),
        data_start=earliest.strftime('%Y-%m-%d') if earliest else 'N/A',
        data_end=latest.strftime('%Y-%m-%d') if latest else 'N/A',
        connected=True,
    )

    st.markdown('<div style="height: 8px;"></div>', unsafe_allow_html=True)

# Page navigation
data_explorer = st.Page("pages/1_data_explorer.py", title="Data Explorer", default=True)
pump_cycles = st.Page("pages/2_pump_cycles.py", title="Testing Cycles")
cycle_detail = st.Page("pages/3_cycle_detail.py", title="Cycle Detail")
variation_analysis = st.Page("pages/4_variation_analysis.py", title="Variation Analysis")
cycle_comparison = st.Page("pages/5_cycle_comparison.py", title="Cycle Comparison")
advanced_explorer = st.Page("pages/6_advanced_explorer.py", title="Advanced Data Explorer")
search = st.Page("pages/7_search.py", title="Search")

pg = st.navigation([data_explorer, advanced_explorer, pump_cycles, cycle_detail, variation_analysis, cycle_comparison, search])
pg.run()