mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-07-26 06:16:09 +00:00
feat: autoclean monitoring events
This commit is contained in:
@@ -16,6 +16,57 @@ class MonitoringService:
|
|||||||
def __init__(self, ap: app.Application) -> None:
|
def __init__(self, ap: app.Application) -> None:
|
||||||
self.ap = ap
|
self.ap = ap
|
||||||
|
|
||||||
|
# ========== Cleanup Methods ==========
|
||||||
|
|
||||||
|
async def cleanup_expired_records(self, retention_days: int) -> dict[str, int]:
|
||||||
|
"""Delete monitoring records older than the specified retention period.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
retention_days: Number of days to retain records.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
A dict mapping table name to the number of deleted rows.
|
||||||
|
"""
|
||||||
|
cutoff = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None) - datetime.timedelta(
|
||||||
|
days=retention_days
|
||||||
|
)
|
||||||
|
|
||||||
|
tables_and_columns: list[tuple[str, type, sqlalchemy.Column]] = [
|
||||||
|
(
|
||||||
|
'monitoring_messages',
|
||||||
|
persistence_monitoring.MonitoringMessage,
|
||||||
|
persistence_monitoring.MonitoringMessage.timestamp,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
'monitoring_llm_calls',
|
||||||
|
persistence_monitoring.MonitoringLLMCall,
|
||||||
|
persistence_monitoring.MonitoringLLMCall.timestamp,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
'monitoring_embedding_calls',
|
||||||
|
persistence_monitoring.MonitoringEmbeddingCall,
|
||||||
|
persistence_monitoring.MonitoringEmbeddingCall.timestamp,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
'monitoring_errors',
|
||||||
|
persistence_monitoring.MonitoringError,
|
||||||
|
persistence_monitoring.MonitoringError.timestamp,
|
||||||
|
),
|
||||||
|
(
|
||||||
|
'monitoring_sessions',
|
||||||
|
persistence_monitoring.MonitoringSession,
|
||||||
|
persistence_monitoring.MonitoringSession.last_activity,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
||||||
|
deleted_counts: dict[str, int] = {}
|
||||||
|
|
||||||
|
for table_name, model_cls, ts_column in tables_and_columns:
|
||||||
|
result = await self.ap.persistence_mgr.execute_async(sqlalchemy.delete(model_cls).where(ts_column < cutoff))
|
||||||
|
deleted_counts[table_name] = result.rowcount
|
||||||
|
|
||||||
|
return deleted_counts
|
||||||
|
|
||||||
# ========== Recording Methods ==========
|
# ========== Recording Methods ==========
|
||||||
|
|
||||||
async def record_message(
|
async def record_message(
|
||||||
|
|||||||
@@ -188,6 +188,34 @@ class Application:
|
|||||||
scopes=[core_entities.LifecycleControlScope.APPLICATION],
|
scopes=[core_entities.LifecycleControlScope.APPLICATION],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Start monitoring data cleanup task if enabled
|
||||||
|
monitoring_cfg = self.instance_config.data.get('monitoring', {})
|
||||||
|
auto_cleanup_cfg = monitoring_cfg.get('auto_cleanup', {})
|
||||||
|
if auto_cleanup_cfg.get('enabled', True):
|
||||||
|
retention_days = auto_cleanup_cfg.get('retention_days', 30)
|
||||||
|
check_interval_hours = auto_cleanup_cfg.get('check_interval_hours', 1)
|
||||||
|
|
||||||
|
async def monitoring_cleanup_loop():
|
||||||
|
check_interval_seconds = check_interval_hours * 3600
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
deleted = await self.monitoring_service.cleanup_expired_records(retention_days)
|
||||||
|
total_deleted = sum(deleted.values())
|
||||||
|
if total_deleted > 0:
|
||||||
|
self.logger.info(
|
||||||
|
f'Monitoring auto-cleanup: deleted {total_deleted} expired records '
|
||||||
|
f'(retention={retention_days}d): {deleted}'
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
self.logger.warning(f'Monitoring auto-cleanup error: {e}')
|
||||||
|
await asyncio.sleep(check_interval_seconds)
|
||||||
|
|
||||||
|
self.task_mgr.create_task(
|
||||||
|
monitoring_cleanup_loop(),
|
||||||
|
name='monitoring-cleanup',
|
||||||
|
scopes=[core_entities.LifecycleControlScope.APPLICATION],
|
||||||
|
)
|
||||||
|
|
||||||
self.task_mgr.create_task(
|
self.task_mgr.create_task(
|
||||||
never_ending(),
|
never_ending(),
|
||||||
name='never-ending-task',
|
name='never-ending-task',
|
||||||
|
|||||||
@@ -78,6 +78,14 @@ plugin:
|
|||||||
runtime_ws_url: 'ws://langbot_plugin_runtime:5400/control/ws'
|
runtime_ws_url: 'ws://langbot_plugin_runtime:5400/control/ws'
|
||||||
enable_marketplace: true
|
enable_marketplace: true
|
||||||
display_plugin_debug_url: 'ws://localhost:5401/plugin/debug/ws'
|
display_plugin_debug_url: 'ws://localhost:5401/plugin/debug/ws'
|
||||||
|
monitoring:
|
||||||
|
auto_cleanup:
|
||||||
|
# Enable automatic cleanup of expired monitoring records
|
||||||
|
enabled: true
|
||||||
|
# Retention period in days, records older than this will be deleted
|
||||||
|
retention_days: 30
|
||||||
|
# Cleanup check interval in hours
|
||||||
|
check_interval_hours: 1
|
||||||
space:
|
space:
|
||||||
# Space service URL for OAuth and API
|
# Space service URL for OAuth and API
|
||||||
url: 'https://space.langbot.app'
|
url: 'https://space.langbot.app'
|
||||||
|
|||||||
Reference in New Issue
Block a user