// OpenObserve AI Observability — demo data
// AI inference platform: LLM gateway, embedding service, vector DB, RAG pipeline

function rng(seed) {
  let s = seed;
  return () => { s = (s * 9301 + 49297) % 233280; return s / 233280; };
}

// ─── Histogram (74 bars, ~1.2M inference events/15 min)
// Spike at bin 41 = rate-limit burst, spike at bin 56 = batch embedding job
const HIST_BARS = (() => {
  const r = rng(7);
  return Array.from({ length: 74 }, (_, i) => {
    const base = 14000 + Math.sin(i * 0.32) * 3200 + Math.sin(i * 0.13 + 1) * 4500;
    const noise = (r() - 0.5) * 3800;
    const spike = i === 41 ? 18000 : i === 56 ? 11000 : 0;
    return Math.max(2000, Math.round(base + noise + spike));
  });
})();

const HIST_TIMES = ["14:22","14:24","14:26","14:28","14:30","14:32","14:34"];

// ─── Field list — AI inference stream
const FIELDS = [
  { name: "_timestamp",        type: "ts",  pinned: true },
  { name: "model",             type: "txt", expand: true },
  { name: "service_name",      type: "txt", expand: true },
  { name: "prompt_tokens",     type: "num", expand: true },
  { name: "completion_tokens", type: "num", expand: true },
  { name: "total_tokens",      type: "num", expand: true },
  { name: "latency_ms",        type: "num", expand: true },
  { name: "status",            type: "txt", expand: true },
  { name: "error_type",        type: "txt", expand: true },
  { name: "request_id",        type: "txt", expand: true },
  { name: "pipeline",          type: "txt", expand: true },
  { name: "cost_usd",          type: "num", expand: true },
  { name: "user_id",           type: "txt", expand: true },
  { name: "region",            type: "txt", expand: true },
  { name: "cache_hit",         type: "txt", expand: true },
  { name: "k8s_namespace_name",type: "txt", expand: true },
  { name: "k8s_pod_name",      type: "txt", expand: true },
  { name: "k8s_node_name",     type: "txt", expand: true },
];

// ─── Raw AI inference log events
// Structured so SQL filters work: model = 'gpt-4o', status = 'error',
// service_name = 'embedding-service', pipeline = 'rag-v2'
const AI_EVENTS = [
  { model:"gpt-4o",          service_name:"llm-gateway",        prompt_tokens:1847, completion_tokens:412, total_tokens:2259, latency_ms:2104, status:"success", pipeline:"rag-v2",    cost_usd:0.0226, region:"us-east-1", cache_hit:"miss" },
  { model:"text-embedding-3-large", service_name:"embedding-service", prompt_tokens:512, completion_tokens:0, total_tokens:512, latency_ms:38,  status:"success", pipeline:"rag-v2",    cost_usd:0.0001, region:"us-east-1", cache_hit:"miss" },
  { model:"gpt-4o",          service_name:"llm-gateway",        prompt_tokens:2341, completion_tokens:188, total_tokens:2529, latency_ms:1683, status:"success", pipeline:"agent-v1",   cost_usd:0.0253, region:"us-east-1", cache_hit:"miss" },
  { model:"gpt-4o-mini",     service_name:"llm-gateway",        prompt_tokens:384,  completion_tokens:96,  total_tokens:480,  latency_ms:441,  status:"success", pipeline:"classifier", cost_usd:0.0001, region:"eu-west-1", cache_hit:"hit" },
  { model:"gpt-4o",          service_name:"llm-gateway",        prompt_tokens:3812, completion_tokens:0,   total_tokens:3812, latency_ms:30004, status:"error", error_type:"model_timeout", pipeline:"rag-v2", cost_usd:0.0, region:"us-east-1", cache_hit:"miss" },
  { model:"text-embedding-3-small", service_name:"embedding-service", prompt_tokens:1024, completion_tokens:0, total_tokens:1024, latency_ms:22, status:"success", pipeline:"batch-embed", cost_usd:0.0000, region:"us-west-2", cache_hit:"miss" },
  { model:"gpt-4o",          service_name:"llm-gateway",        prompt_tokens:1124, completion_tokens:632, total_tokens:1756, latency_ms:2891, status:"success", pipeline:"rag-v2",    cost_usd:0.0176, region:"us-east-1", cache_hit:"miss" },
  { model:"claude-3-5-sonnet",service_name:"llm-gateway",       prompt_tokens:2048, completion_tokens:512, total_tokens:2560, latency_ms:3241, status:"success", pipeline:"agent-v1",   cost_usd:0.0205, region:"us-east-1", cache_hit:"miss" },
  { model:"gpt-4o",          service_name:"llm-gateway",        prompt_tokens:8192, completion_tokens:0,   total_tokens:8192, latency_ms:0,    status:"error",   error_type:"token_limit_exceeded", pipeline:"doc-qa", cost_usd:0.0, region:"us-east-1", cache_hit:"miss" },
  { model:"gpt-4o-mini",     service_name:"llm-gateway",        prompt_tokens:201,  completion_tokens:48,  total_tokens:249,  latency_ms:382,  status:"success", pipeline:"classifier", cost_usd:0.00005, region:"eu-west-1", cache_hit:"hit" },
  { model:"text-embedding-3-large", service_name:"embedding-service", prompt_tokens:768, completion_tokens:0, total_tokens:768, latency_ms:44, status:"success", pipeline:"rag-v2",   cost_usd:0.0002, region:"us-east-1", cache_hit:"miss" },
  { model:"gpt-4o",          service_name:"llm-gateway",        prompt_tokens:1502, completion_tokens:384, total_tokens:1886, latency_ms:1922, status:"success", pipeline:"rag-v2",    cost_usd:0.0189, region:"us-east-1", cache_hit:"miss" },
  { model:"gpt-4o",          service_name:"llm-gateway",        prompt_tokens:944,  completion_tokens:211, total_tokens:1155, latency_ms:1544, status:"success", pipeline:"rag-v2",    cost_usd:0.0116, region:"ap-south-1",cache_hit:"miss" },
  { model:"gpt-4o-mini",     service_name:"llm-gateway",        prompt_tokens:512,  completion_tokens:0,   total_tokens:512,  latency_ms:0,    status:"error",   error_type:"rate_limit", pipeline:"classifier", cost_usd:0.0, region:"eu-west-1", cache_hit:"miss" },
  { model:"claude-3-5-sonnet",service_name:"llm-gateway",       prompt_tokens:3241, completion_tokens:824, total_tokens:4065, latency_ms:4128, status:"success", pipeline:"agent-v1",   cost_usd:0.0325, region:"us-east-1", cache_hit:"miss" },
  { model:"text-embedding-3-small", service_name:"embedding-service", prompt_tokens:256, completion_tokens:0, total_tokens:256, latency_ms:18, status:"success", pipeline:"rag-v2",   cost_usd:0.0000, region:"us-west-2", cache_hit:"hit" },
  { model:"gpt-4o",          service_name:"llm-gateway",        prompt_tokens:2180, completion_tokens:592, total_tokens:2772, latency_ms:3014, status:"success", pipeline:"doc-qa",    cost_usd:0.0277, region:"us-east-1", cache_hit:"miss" },
  { model:"gpt-4o",          service_name:"llm-gateway",        prompt_tokens:1344, completion_tokens:312, total_tokens:1656, latency_ms:2012, status:"success", pipeline:"rag-v2",    cost_usd:0.0166, region:"us-east-1", cache_hit:"miss" },
  { model:"gpt-4o",          service_name:"llm-gateway",        prompt_tokens:6144, completion_tokens:0,   total_tokens:6144, latency_ms:0,    status:"error",   error_type:"context_overflow", pipeline:"doc-qa", cost_usd:0.0, region:"ap-south-1", cache_hit:"miss" },
  { model:"text-embedding-3-large", service_name:"embedding-service", prompt_tokens:1536, completion_tokens:0, total_tokens:1536, latency_ms:67, status:"success", pipeline:"batch-embed", cost_usd:0.0003, region:"us-east-1", cache_hit:"miss" },
  { model:"gpt-4o-mini",     service_name:"llm-gateway",        prompt_tokens:319,  completion_tokens:84,  total_tokens:403,  latency_ms:401,  status:"success", pipeline:"classifier", cost_usd:0.0001, region:"eu-west-1", cache_hit:"hit" },
  { model:"gpt-4o",          service_name:"llm-gateway",        prompt_tokens:1088, completion_tokens:287, total_tokens:1375, latency_ms:1738, status:"success", pipeline:"rag-v2",    cost_usd:0.0138, region:"us-east-1", cache_hit:"miss" },
  { model:"claude-3-5-sonnet",service_name:"llm-gateway",       prompt_tokens:1892, completion_tokens:401, total_tokens:2293, latency_ms:2904, status:"success", pipeline:"agent-v1",   cost_usd:0.0184, region:"us-east-1", cache_hit:"miss" },
  { model:"gpt-4o",          service_name:"llm-gateway",        prompt_tokens:0,    completion_tokens:0,   total_tokens:0,    latency_ms:0,    status:"error",   error_type:"rate_limit", pipeline:"rag-v2", cost_usd:0.0, region:"us-east-1", cache_hit:"miss" },
  { model:"text-embedding-3-small", service_name:"embedding-service", prompt_tokens:384, completion_tokens:0, total_tokens:384, latency_ms:21, status:"success", pipeline:"rag-v2",   cost_usd:0.0000, region:"us-east-1", cache_hit:"hit" },
];

function pad(n) { return String(n).padStart(2, "0"); }

function buildLogRows() {
  const rows = [];
  let t = new Date("2026-06-04T14:34:55.000Z").getTime();
  const reqIds = ["req_7f3a9b","req_a2c841","req_d9e02f","req_b5f118","req_e3c724","req_9a18d4","req_c7b293","req_0f64ae","req_852d71","req_4e19bc","req_6d3a08","req_1c89f5","req_f7e42b","req_81d306","req_3b90ca","req_a6f231","req_5e7d14","req_2c4809","req_d1b673","req_8f025e","req_7a41cd","req_0d83b9","req_6c19f4","req_e5280a","req_9f3712"];
  const users = ["usr_4829","usr_1047","usr_8834","usr_2291","usr_6650","usr_3312","usr_9901","usr_7743"];
  const pods  = ["llm-gw-7d8f9b-x2k","llm-gw-7d8f9b-p4m","embed-svc-9a3c2d-r7n","embed-svc-9a3c2d-k1p","llm-gw-7d8f9b-h8j"];
  const nodes = ["ip-10-0-1-47","ip-10-0-1-52","ip-10-0-2-18","ip-10-0-2-34"];

  for (let i = 0; i < 25; i++) {
    const ev = AI_EVENTS[i % AI_EVENTS.length];
    const date = new Date(t);
    const tsStr = `2026-06-04 ${pad(date.getUTCHours())}:${pad(date.getUTCMinutes())}:${pad(date.getUTCSeconds())}.${String(date.getUTCMilliseconds()).padStart(3,"0")}`;
    const source = {
      _timestamp: t * 1000,
      model: ev.model,
      service_name: ev.service_name,
      status: ev.status,
      ...(ev.error_type ? { error_type: ev.error_type } : {}),
      prompt_tokens: ev.prompt_tokens,
      completion_tokens: ev.completion_tokens,
      total_tokens: ev.total_tokens,
      latency_ms: ev.latency_ms,
      cost_usd: ev.cost_usd,
      pipeline: ev.pipeline,
      request_id: reqIds[i % reqIds.length],
      user_id: users[i % users.length],
      region: ev.region,
      cache_hit: ev.cache_hit,
      k8s_namespace_name: ev.service_name === "embedding-service" ? "embedding-service" : "llm-gateway",
      k8s_pod_name: pods[i % pods.length],
      k8s_node_name: nodes[i % nodes.length],
    };
    rows.push({
      ts: tsStr.substring(11, 23),
      date: "2026-06-04",
      source,
      highlight: ev.status === "error",
    });
    t -= 180 + Math.round(Math.random() * 420);
  }
  return rows;
}

const LOG_ROWS_DEFAULT = buildLogRows();
const LOG_ROWS_SQL     = buildLogRows();   // same pool; SQL filter differentiates

// ─── AI service namespaces (for K8s memory dashboard)
const NAMESPACES = [
  { name: "llm-gateway",        color: "#5C5DBC", key: "llmgw" },
  { name: "embedding-service",  color: "#4F9D77", key: "embed" },
  { name: "vector-db",          color: "#E08E3C", key: "vecdb" },
  { name: "rag-pipeline",       color: "#7C5DC4", key: "rag" },
  { name: "agent-runner",       color: "#3A9DD4", key: "agent" },
  { name: "model-router",       color: "#D45AB8", key: "router" },
  { name: "inference-cache",    color: "#5AAE5E", key: "cache" },
  { name: "context-builder",    color: "#D9CE57", key: "ctx" },
  { name: "otel-collector",     color: "#9DC25E", key: "otel" },
  { name: "kube-system",        color: "#F2B83C", key: "kubesys" },
  { name: "monitoring",         color: "#E25264", key: "monitor" },
  { name: "model-registry",     color: "#509FE0", key: "registry" },
  { name: "fine-tuning",        color: "#F09B43", key: "finetune" },
  { name: "prompt-cache",       color: "#3CADD9", key: "promptcache" },
  { name: "guardrails",         color: "#41B5D2", key: "guardrails" },
  { name: "batch-processor",    color: "#5BBB66", key: "batch" },
  { name: "feedback-svc",       color: "#5E6BB5", key: "feedback" },
  { name: "cert-manager",       color: "#EE5466", key: "cert" },
  { name: "argocd",             color: "#5BB561", key: "argocd" },
  { name: "ingress-nginx",      color: "#7866B9", key: "ingress" },
  { name: "postgres",           color: "#F2A04E", key: "postgres" },
  { name: "redis",              color: "#4DAEA3", key: "redis" },
];

// Stacked area data: memory GiB per namespace over 90 time points
function buildAreaData() {
  const r = rng(11);
  return Array.from({ length: 90 }, (_, i) => {
    const min = Math.floor(i / 30);
    const row = { time: `14:${String(20 + min * 4).padStart(2, "0")}` };
    NAMESPACES.forEach((n, idx) => {
      // llm-gateway and embedding-service are the heavyweights
      const base = idx === 0 ? 38   // llm-gateway
        : idx === 1 ? 24            // embedding-service
        : idx === 2 ? 18            // vector-db
        : idx === 3 ? 14            // rag-pipeline
        : Math.max(0.5, 10 - idx * 0.38);
      const wave = Math.sin(i * 0.14 + idx * 0.6) * base * 0.07;
      const spike = (i > 38 && i < 44 && idx < 3) ? 4.5 : 0; // rate-limit burst
      row[n.key] = +(base + wave + spike + (r() - 0.5) * base * 0.05).toFixed(2);
    });
    return row;
  });
}

const AREA_DATA = buildAreaData();
const AREA_X_LABELS = ["14:20","14:24","14:28","14:32","14:36","14:40","14:44"];
const AREA_Y_LABELS = [
  { v: 0,      label: "0.00B" },
  { v: 31.82,  label: "31.82GB" },
  { v: 63.64,  label: "63.64GB" },
  { v: 95.46,  label: "95.46GB" },
  { v: 127.28, label: "127.28GB" },
  { v: 159.10, label: "159.10GB" },
];

// ─── AI observability log patterns (XDrain output)
const PATTERNS = [
  {
    id: 1, count: 8420, pct: 38.1, rare: false, varCount: 6,
    template: 'model=%_% service=llm-gateway status=success prompt_tokens=%_% completion_tokens=%_% latency_ms=%_% pipeline=%_% cost_usd=%_%',
    examples: [
      'model=gpt-4o service=llm-gateway status=success prompt_tokens=1847 completion_tokens=412 latency_ms=2104 pipeline=rag-v2 cost_usd=0.0226',
      'model=gpt-4o service=llm-gateway status=success prompt_tokens=2341 completion_tokens=188 latency_ms=1683 pipeline=agent-v1 cost_usd=0.0253',
      'model=gpt-4o service=llm-gateway status=success prompt_tokens=1124 completion_tokens=632 latency_ms=2891 pipeline=rag-v2 cost_usd=0.0176',
      'model=claude-3-5-sonnet service=llm-gateway status=success prompt_tokens=2048 completion_tokens=512 latency_ms=3241 pipeline=agent-v1 cost_usd=0.0205',
      'model=gpt-4o service=llm-gateway status=success prompt_tokens=1502 completion_tokens=384 latency_ms=1922 pipeline=rag-v2 cost_usd=0.0189',
    ],
  },
  {
    id: 2, count: 3812, pct: 17.2, rare: false, varCount: 4,
    template: 'model=%_% service=embedding-service status=success prompt_tokens=%_% latency_ms=%_% pipeline=%_% cache_hit=%_%',
    examples: [
      'model=text-embedding-3-large service=embedding-service status=success prompt_tokens=512 latency_ms=38 pipeline=rag-v2 cache_hit=miss',
      'model=text-embedding-3-small service=embedding-service status=success prompt_tokens=256 latency_ms=18 pipeline=rag-v2 cache_hit=hit',
      'model=text-embedding-3-large service=embedding-service status=success prompt_tokens=1536 latency_ms=67 pipeline=batch-embed cache_hit=miss',
      'model=text-embedding-3-small service=embedding-service status=success prompt_tokens=1024 latency_ms=22 pipeline=batch-embed cache_hit=miss',
    ],
  },
  {
    id: 3, count: 1204, pct: 5.4, rare: false, varCount: 3,
    template: 'model=gpt-4o-mini service=llm-gateway status=success prompt_tokens=%_% completion_tokens=%_% latency_ms=%_% pipeline=classifier',
    examples: [
      'model=gpt-4o-mini service=llm-gateway status=success prompt_tokens=384 completion_tokens=96 latency_ms=441 pipeline=classifier cost_usd=0.0001',
      'model=gpt-4o-mini service=llm-gateway status=success prompt_tokens=201 completion_tokens=48 latency_ms=382 pipeline=classifier cost_usd=0.00005',
      'model=gpt-4o-mini service=llm-gateway status=success prompt_tokens=319 completion_tokens=84 latency_ms=401 pipeline=classifier cost_usd=0.0001',
    ],
  },
  {
    id: 4, count: 847, pct: 3.8, rare: false, varCount: 2,
    template: 'vector_search index=openobserve-prod-v3 top_k=%_% results=%_% latency_ms=%_% pipeline=rag-v2',
    examples: [],
  },
  {
    id: 5, count: 634, pct: 2.9, rare: false, varCount: 3,
    template: 'cache LOOKUP key=%_% ttl=%_% hit=%_% pipeline=%_%',
    examples: [],
  },
  {
    id: 6, count: 512, pct: 2.3, rare: false, varCount: 2,
    template: 'rag pipeline=%_% stage=%_% latency_ms=%_% context_docs=%_% total_tokens=%_%',
    examples: [],
  },
  {
    id: 7, count: 388, pct: 1.8, rare: false, varCount: 4,
    template: 'agent_step tool=%_% input_len=%_% output_len=%_% status=%_% iteration=%_%',
    examples: [],
  },
  {
    id: 8, count: 284, pct: 1.3, rare: false, varCount: 2,
    template: 'guardrail check=%_% result=%_% model=%_% latency_ms=%_%',
    examples: [],
  },
  {
    id: 9, count: 218, pct: 1.0, rare: false, varCount: 2,
    template: 'model_router model=%_% selected=%_% reason=%_% latency_ms=%_%',
    examples: [],
  },
  {
    id: 10, count: 184, pct: 0.8, rare: false, varCount: 1,
    template: 'batch_embed job_id=%_% chunks=%_% model=%_% latency_ms=%_% cost_usd=%_%',
    examples: [],
  },
  {
    id: 11, count: 141, pct: 0.6, rare: false, varCount: 3,
    template: 'context_builder documents=%_% total_chars=%_% truncated=%_% pipeline=rag-v2',
    examples: [],
  },
  {
    id: 12, count: 98, pct: 0.4, rare: false, varCount: 2,
    template: 'feedback signal=%_% thumbs=%_% pipeline=%_% request_id=%_%',
    examples: [],
  },
  {
    id: 13, count: 72, pct: 0.3, rare: true, varCount: 3,
    template: 'ERROR model=%_% error_type=model_timeout latency_ms=%_% pipeline=%_% retries=%_%',
    examples: [
      'ERROR model=gpt-4o error_type=model_timeout latency_ms=30004 pipeline=rag-v2 retries=3',
      'ERROR model=gpt-4o error_type=model_timeout latency_ms=30001 pipeline=doc-qa retries=3',
    ],
  },
  {
    id: 14, count: 64, pct: 0.3, rare: true, varCount: 2,
    template: 'ERROR model=%_% error_type=token_limit_exceeded prompt_tokens=%_% max_tokens=%_% pipeline=%_%',
    examples: [
      'ERROR model=gpt-4o error_type=token_limit_exceeded prompt_tokens=8192 max_tokens=8192 pipeline=doc-qa',
      'ERROR model=gpt-4o error_type=token_limit_exceeded prompt_tokens=6144 max_tokens=8192 pipeline=doc-qa',
    ],
  },
  {
    id: 15, count: 58, pct: 0.3, rare: true, varCount: 3,
    template: 'ERROR model=%_% error_type=rate_limit retry_after=%_% pipeline=%_% request_id=%_%',
    examples: [
      'ERROR model=gpt-4o error_type=rate_limit retry_after=60 pipeline=rag-v2 request_id=req_e3c724',
      'ERROR model=gpt-4o-mini error_type=rate_limit retry_after=60 pipeline=classifier request_id=req_f7e42b',
    ],
  },
  {
    id: 16, count: 44, pct: 0.2, rare: true, varCount: 2,
    template: 'ERROR model=%_% error_type=context_overflow prompt_tokens=%_% context_window=%_% pipeline=%_%',
    examples: [
      'ERROR model=gpt-4o error_type=context_overflow prompt_tokens=6144 context_window=8192 pipeline=doc-qa',
    ],
  },
  {
    id: 17, count: 29, pct: 0.1, rare: true, varCount: 4,
    template: 'ALERT p99_latency_ms=%_% threshold_ms=%_% model=%_% pipeline=%_% triggered_at=%TS%',
    examples: [
      'ALERT p99_latency_ms=4218 threshold_ms=4000 model=claude-3-5-sonnet pipeline=agent-v1 triggered_at=2026-06-04T14:28:41Z',
      'ALERT p99_latency_ms=5102 threshold_ms=4000 model=gpt-4o pipeline=rag-v2 triggered_at=2026-06-04T14:31:07Z',
    ],
  },
  {
    id: 18, count: 21, pct: 0.1, rare: true, varCount: 3,
    template: 'ALERT gpu_utilization=%_% threshold=%_% node=%_% triggered_at=%TS%',
    examples: [
      'ALERT gpu_utilization=97.4 threshold=95.0 node=ip-10-0-1-47 triggered_at=2026-06-04T14:33:22Z',
    ],
  },
];

// ─── Dashboard metric fields
const DASHBOARD_FIELDS = [
  { name: "__hash__",              type: "txt" },
  { name: "__name__",              type: "txt" },
  { name: "_timestamp",            type: "num" },
  { name: "model",                 type: "txt" },
  { name: "pipeline",              type: "txt" },
  { name: "service_name",          type: "txt" },
  { name: "region",                type: "txt" },
  { name: "prompt_tokens",         type: "num" },
  { name: "completion_tokens",     type: "num" },
  { name: "latency_ms",            type: "num" },
  { name: "cost_usd",              type: "num" },
  { name: "gpu_utilization",       type: "num" },
  { name: "cache_hit_rate",        type: "num" },
  { name: "error_rate",            type: "num" },
  { name: "k8s_namespace_name",    type: "txt" },
  { name: "k8s_pod_name",          type: "txt" },
  { name: "k8s_node_name",         type: "txt" },
  { name: "k8s_node_uid",          type: "txt" },
  { name: "k8s_pod_uid",           type: "txt" },
  { name: "instrumentation_library_name", type: "txt" },
];

// ─── RAG pipeline trace (replaces e-commerce checkout trace)
const TRACE_DETAIL = {
  name: "rag_pipeline_request",
  startedAt: "4 Jun 14:28:41:093",
  ago: "6m ago",
  traceId: "a4f92d18c7b035ef8829e61da14c9b03",
  spanCount: 22,
  errors: 0,
  spans: [
    { id:"s0",  depth:0, service:"llm-gateway",       op:"handle_request",                   kind:"s", status:200, dur:3241,  start:0,    color:"blue" },
    { id:"s1",  depth:1, service:"guardrails",         op:"input_safety_check",               kind:"c", status:200, dur:48,    start:12,   color:"purple" },
    { id:"s2",  depth:2, service:"guardrails",         op:"PII_scan",                         kind:"i", status:200, dur:31,    start:18,   color:"green" },
    { id:"s3",  depth:1, service:"model-router",       op:"select_model",                     kind:"c", status:200, dur:8,     start:62,   color:"purple" },
    { id:"s4",  depth:1, service:"inference-cache",    op:"LOOKUP prompt_hash=a4f92d",        kind:"c", status:200, dur:4,     start:72,   color:"purple" },
    { id:"s5",  depth:1, service:"embedding-service",  op:"embed_query",                      kind:"c", status:200, dur:38,    start:80,   color:"green" },
    { id:"s6",  depth:2, service:"embedding-service",  op:"openai_embedding text-embedding-3-large", kind:"i", status:200, dur:34, start:84, color:"yellow", dot:true },
    { id:"s7",  depth:1, service:"vector-db",          op:"similarity_search top_k=8",        kind:"c", status:200, dur:14,    start:120,  color:"purple" },
    { id:"s8",  depth:2, service:"vector-db",          op:"scan_index openobserve-prod-v3",   kind:"i", status:200, dur:11,    start:124,  color:"green",  dot:true },
    { id:"s9",  depth:1, service:"context-builder",    op:"build_context docs=8",             kind:"c", status:200, dur:22,    start:136,  color:"purple" },
    { id:"s10", depth:2, service:"context-builder",    op:"rerank_docs",                      kind:"i", status:200, dur:18,    start:140,  color:"yellow" },
    { id:"s11", depth:1, service:"llm-gateway",        op:"openai_chat_completion gpt-4o",    kind:"c", status:200, dur:2891,  start:160,  color:"purple", selected:true },
    { id:"s12", depth:2, service:"llm-gateway",        op:"stream_response",                  kind:"i", status:200, dur:2880,  start:168,  color:"yellow" },
    { id:"s13", depth:3, service:"llm-gateway",        op:"chunk_received tokens=128",        kind:"i", status:200, dur:320,   start:320,  color:"yellow", dot:true },
    { id:"s14", depth:3, service:"llm-gateway",        op:"chunk_received tokens=128",        kind:"i", status:200, dur:280,   start:680,  color:"yellow", dot:true },
    { id:"s15", depth:3, service:"llm-gateway",        op:"chunk_received tokens=156",        kind:"i", status:200, dur:310,   start:1020, color:"yellow", dot:true },
    { id:"s16", depth:1, service:"guardrails",         op:"output_safety_check",              kind:"c", status:200, dur:21,    start:3053, color:"purple" },
    { id:"s17", depth:1, service:"inference-cache",    op:"SET prompt_hash=a4f92d ttl=3600",  kind:"c", status:200, dur:6,     start:3076, color:"purple" },
    { id:"s18", depth:1, service:"feedback-svc",       op:"emit_telemetry",                   kind:"c", status:200, dur:8,     start:3084, color:"purple" },
    { id:"s19", depth:2, service:"feedback-svc",       op:"otel_export",                      kind:"i", status:200, dur:6,     start:3086, color:"green",  dot:true },
    { id:"s20", depth:1, service:"postgres",           op:"INSERT request_log",               kind:"c", status:200, dur:12,    start:3094, color:"purple" },
    { id:"s21", depth:1, service:"llm-gateway",        op:"send_response",                    kind:"i", status:200, dur:4,     start:3108, color:"yellow" },
  ],
  associatedLogs: [
    { time: "2026-06-04 14:28:44.334", body: "model=gpt-4o completion_tokens=412 latency…", sev: "INFO" },
    { time: "2026-06-04 14:28:44.320", body: "cache SET prompt_hash=a4f92d ttl=3600…",     sev: "INFO" },
    { time: "2026-06-04 14:28:44.298", body: "output_safety_check passed latency_ms=21…",  sev: "INFO" },
    { time: "2026-06-04 14:28:44.098", body: "openai stream done total_tokens=2259…",       sev: "INFO" },
    { time: "2026-06-04 14:28:41.320", body: "context_builder docs=8 total_chars=14382…",  sev: "INFO" },
    { time: "2026-06-04 14:28:41.296", body: "vector_search top_k=8 results=8 latency…",   sev: "INFO" },
    { time: "2026-06-04 14:28:41.282", body: "embedding done model=text-embedding-3-la…",  sev: "INFO" },
    { time: "2026-06-04 14:28:41.244", body: "cache MISS prompt_hash=a4f92d pipeline=r…",  sev: "INFO" },
    { time: "2026-06-04 14:28:41.238", body: "model_router selected=gpt-4o reason=perf…",  sev: "INFO" },
    { time: "2026-06-04 14:28:41.226", body: "input_safety_check passed PII=none latenc…", sev: "INFO" },
    { time: "2026-06-04 14:28:41.212", body: "handle_request pipeline=rag-v2 user=usr_4…", sev: "INFO" },
    { time: "2026-06-04 14:28:39.108", body: "failed to export span batch retrying…",       sev: "ERROR" },
    { time: "2026-06-04 14:28:38.892", body: "model=gpt-4o completion_tokens=188 latenc…", sev: "INFO" },
    { time: "2026-06-04 14:28:38.441", body: "batch_embed job_id=emb_8821 chunks=64…",     sev: "INFO" },
    { time: "2026-06-04 14:28:36.017", body: "guardrail ALERT p99_latency_ms=4218…",       sev: "INFO" },
    { time: "2026-06-04 14:28:35.774", body: "context_overflow prompt_tokens=6144 max…",   sev: "ERROR" },
    { time: "2026-06-04 14:28:35.102", body: "model=claude-3-5-sonnet completion_tokens…", sev: "INFO" },
    { time: "2026-06-04 14:28:34.690", body: "vector_search top_k=8 results=6 latency…",   sev: "INFO" },
  ],
  selectedSpan: {
    id: "b8e7c3a291d40f56",
    service: "llm-gateway",
    duration: "2.891s",
    start: "160us",
    attributes: {
      _timestamp:       "Jun 4, 2026 14:28:41.253 UTC",
      duration:         "2891000us",
      end_time:         "Jun 4, 2026 14:28:44.144 UTC",
      model:            "gpt-4o",
      prompt_tokens:    "1847",
      completion_tokens:"412",
      total_tokens:     "2259",
      cost_usd:         "0.0226",
      pipeline:         "rag-v2",
      region:           "us-east-1",
      operation_name:   "openai_chat_completion",
      net_peer_name:    "api.openai.com",
      net_peer_port:    "443",
      cache_hit:        "miss",
      reference_parent_span_id:  "f3a90d7b182ec5d4",
      reference_parent_trace_id: "a4f92d18c7b035ef8829e61da14c9b03",
    },
  },
};

window.Data = {
  HIST_BARS, HIST_TIMES, FIELDS,
  LOG_ROWS_DEFAULT, LOG_ROWS_SQL,
  NAMESPACES, AREA_DATA, AREA_X_LABELS, AREA_Y_LABELS,
  DASHBOARD_FIELDS,
  PATTERNS, TRACE_DETAIL,
};
