Zie619/n8n-workflows · error · HTTPException

Trend analysis error: {str(e)}

Error message

Trend analysis error: {str(e)}

What it means

A generic 500 from GET /analytics/trends?days=N. The handler delegates straight to analytics_engine.get_trend_analysis(days); any exception is re-raised as 500 with the original message. The days parameter is already validated by FastAPI (1..365), so failures come from the engine's date bucketing or data access, not from input validation.

Source

Thrown at src/analytics_engine.py:375

        return AnalyticsResponse(
            overview=analytics_data["overview"],
            trends=trends,
            patterns=analytics_data["patterns"],
            recommendations=analytics_data["recommendations"],
            generated_at=analytics_data["generated_at"],
        )
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Analytics error: {str(e)}")


@analytics_app.get("/analytics/trends")
async def get_trend_analysis(days: int = Query(30, ge=1, le=365)):
    """Get trend analysis for specified period."""
    try:
        return analytics_engine.get_trend_analysis(days)
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Trend analysis error: {str(e)}")


@analytics_app.get("/analytics/insights")
async def get_usage_insights():
    """Get usage insights and patterns."""
    try:
        return analytics_engine.get_usage_insights()
    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Insights error: {str(e)}")


@analytics_app.get("/analytics/dashboard")
async def get_analytics_dashboard():
    """Get analytics dashboard HTML."""
    html_content = """
    <!DOCTYPE html>
    <html lang="en">
    <head>

View on GitHub (pinned to 94007c1445)

Solutions

  1. Check the appended str(e) — 'no such table' means init the DB; 'database is locked' means serialize writers or enable WAL.
  2. Initialize the analytics storage and record at least a few events, then retry.
  3. Try a default days=30 call first to rule out edge-value bugs with days=1.
  4. If timestamps are the issue, backfill/repair NULL created_at values in the events table.
Defensive patterns

Strategy: try-catch

Validate before calling

def valid_days(days: int) -> bool:
    return isinstance(days, int) and 1 <= days <= 365

Try / catch

try:
    trends = client.get("/analytics/trends", params={"days": days}).json()
except HTTPError as e:
    if e.response.status_code == 500:
        trends = {"trends": [], "period_days": days}  # neutral empty result
    else:
        raise

Prevention

When it happens

Trigger: Calling /analytics/trends when the underlying events table is missing/locked, or when trend bucketing divides by zero / parses bad timestamps on empty or malformed event rows. days=1 edge cases with sparse data can also expose math errors in per-day aggregation.

Common situations: Analytics DB not yet created on a new deployment; events table exists but rows have NULL timestamps from an older writer; concurrent write during read causing 'database is locked'.

Related errors


AI-assisted analysis of Zie619/n8n-workflows@94007c1445 (2026-08-15). Data as JSON: /api/errors/a00b97f5098eb9b3. Report an issue: GitHub.