2025-06-11 18:36:34 +08:00
|
|
|
import json
|
|
|
|
|
import os
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2025-06-11 19:33:08 +08:00
|
|
|
DEFAULT_TIME_RANGE_OPTIONS = [
|
|
|
|
|
{"label": "🕐 Last 1 Hour", "value": "1h"},
|
|
|
|
|
{"label": "🕐 Last 4 Hours", "value": "4h"},
|
|
|
|
|
{"label": "🕐 Last 6 Hours", "value": "6h"},
|
|
|
|
|
{"label": "🕐 Last 12 Hours", "value": "12h"},
|
|
|
|
|
{"label": "📅 Last 1 Day", "value": "1d"},
|
|
|
|
|
{"label": "📅 Last 3 Days", "value": "3d"},
|
|
|
|
|
{"label": "📅 Last 7 Days", "value": "7d"},
|
|
|
|
|
{"label": "📅 Last 30 Days", "value": "30d"},
|
|
|
|
|
{"label": "📅 Custom Range", "value": "custom"},
|
|
|
|
|
{"label": "🔴 Real-time", "value": "realtime"}
|
|
|
|
|
]
|
|
|
|
|
|
2025-06-11 18:36:34 +08:00
|
|
|
def load_time_range_options():
|
|
|
|
|
"""Loads time range options from a JSON file.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
list: A list of dictionaries, each representing a time range option.
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
# Construct path relative to the workspace root
|
|
|
|
|
# Assuming utils is at TCPDashboard/utils
|
|
|
|
|
# and config/options is at TCPDashboard/config/options
|
|
|
|
|
base_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
|
|
|
|
|
file_path = os.path.join(base_dir, 'config/options/time_range_options.json')
|
|
|
|
|
|
|
|
|
|
with open(file_path, 'r', encoding='utf-8') as f:
|
|
|
|
|
return json.load(f)
|
|
|
|
|
except FileNotFoundError:
|
|
|
|
|
logger.error(f"Time range options file not found at {file_path}. Using default.")
|
2025-06-11 19:33:08 +08:00
|
|
|
return DEFAULT_TIME_RANGE_OPTIONS
|
2025-06-11 18:36:34 +08:00
|
|
|
except json.JSONDecodeError:
|
|
|
|
|
logger.error(f"Error decoding JSON from {file_path}. Using default.")
|
2025-06-11 19:33:08 +08:00
|
|
|
return DEFAULT_TIME_RANGE_OPTIONS
|
2025-06-11 18:36:34 +08:00
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"An unexpected error occurred while loading time range options: {e}. Using default.")
|
2025-06-11 19:33:08 +08:00
|
|
|
return DEFAULT_TIME_RANGE_OPTIONS
|