Spaces:
Sleeping
Sleeping
Download src/utils/streaming_state.py from MikeZhang09/marketwise: direct link, hf CLI and curl.
- Browser
- Download file 4.04 kB
-
https://huggingface.co/spaces/MikeZhang09/marketwise/resolve/main/src/utils/streaming_state.py
- Command line
-
hf download hf://spaces/MikeZhang09/marketwise/src/utils/streaming_state.py
-
curl -L -o streaming_state.py https://huggingface.co/spaces/MikeZhang09/marketwise/resolve/main/src/utils/streaming_state.py
4.04 kB
| """ | |
| Streaming state management utilities for MarketWise real-time data streaming. | |
| This module provides session state management for streaming configuration, | |
| data buffers, and connection status. | |
| """ | |
| import streamlit as st | |
| import logging | |
| from typing import Dict, List, Any, Optional | |
| from datetime import datetime, timedelta | |
| import pandas as pd | |
| logger = logging.getLogger(__name__) | |
| def init_streaming_state() -> None: | |
| """Initialize streaming state variables with proper defaults.""" | |
| # Streaming configuration | |
| if 'streaming_active' not in st.session_state: | |
| st.session_state.streaming_active = False | |
| if 'selected_symbols' not in st.session_state: | |
| st.session_state.selected_symbols = ['^GSPC'] | |
| if 'streaming_period' not in st.session_state: | |
| st.session_state.streaming_period = 5 # Default to 5 years | |
| if 'refresh_interval' not in st.session_state: | |
| st.session_state.refresh_interval = 30 # seconds | |
| if 'auto_refresh_enabled' not in st.session_state: | |
| st.session_state.auto_refresh_enabled = False | |
| # Connection status | |
| if 'connection_status' not in st.session_state: | |
| st.session_state.connection_status = 'disconnected' | |
| if 'last_update' not in st.session_state: | |
| st.session_state.last_update = None | |
| if 'retry_count' not in st.session_state: | |
| st.session_state.retry_count = 0 | |
| # Data buffers | |
| if 'streaming_data_buffer' not in st.session_state: | |
| st.session_state.streaming_data_buffer = {} | |
| if 'max_buffer_size' not in st.session_state: | |
| st.session_state.max_buffer_size = 1000 | |
| if 'streaming_service' not in st.session_state: | |
| st.session_state.streaming_service = None | |
| logger.info("Streaming state initialized") | |
| def get_streaming_data(symbol: str) -> Optional[pd.DataFrame]: | |
| """Get streaming data for a specific symbol.""" | |
| if 'streaming_data_buffer' not in st.session_state: | |
| return None | |
| return st.session_state.streaming_data_buffer.get(symbol, None) | |
| def is_streaming_active() -> bool: | |
| """Check if streaming is currently active.""" | |
| return st.session_state.get('streaming_active', False) | |
| def update_streaming_data(symbol: str, data: pd.DataFrame) -> None: | |
| """Update streaming data buffer for a symbol.""" | |
| if 'streaming_data_buffer' not in st.session_state: | |
| st.session_state.streaming_data_buffer = {} | |
| # Validate data type | |
| if not isinstance(data, pd.DataFrame): | |
| logger.error(f"Invalid data type for {symbol}: expected DataFrame, got {type(data)}") | |
| return | |
| st.session_state.streaming_data_buffer[symbol] = data | |
| st.session_state.last_update = datetime.now() | |
| logger.info(f"Updated streaming data for {symbol}: {len(data)} records") | |
| def manage_data_buffer() -> None: | |
| """Manage data buffer size to prevent memory leaks.""" | |
| if 'streaming_data_buffer' not in st.session_state: | |
| return | |
| max_size = st.session_state.get('max_buffer_size', 1000) | |
| for symbol, data in st.session_state.streaming_data_buffer.items(): | |
| if isinstance(data, pd.DataFrame) and len(data) > max_size: | |
| # Keep only the most recent records | |
| st.session_state.streaming_data_buffer[symbol] = data.tail(max_size) | |
| logger.info(f"Trimmed buffer for {symbol} to {max_size} records") | |
| def clear_streaming_data() -> None: | |
| """Clear all streaming data buffers.""" | |
| st.session_state.streaming_data_buffer = {} | |
| st.session_state.last_update = None | |
| logger.info("Streaming data buffers cleared") | |
| def get_connection_status() -> str: | |
| """Get current connection status.""" | |
| return st.session_state.get('connection_status', 'disconnected') | |
| def update_connection_status(status: str) -> None: | |
| """Update connection status.""" | |
| st.session_state.connection_status = status | |
| if status == 'connected': | |
| st.session_state.retry_count = 0 | |
| logger.info(f"Connection status updated: {status}") | |