{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "7b57ccd0-a7f5-43a6-9938-68c775cf86f9",
      "metadata": {},
      "outputs": [],
      "source": [
        "# Follows the DBNL Quickstart at https://docs.dbnl.com/get-started/quickstart\n",
        "!pip install --upgrade dbnl"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "e99228e1-aeff-4d58-807a-eae4c94685f6",
      "metadata": {},
      "outputs": [],
      "source": [
        "import dbnl\n",
        "import io, json, zstandard, pandas\n",
        "from datetime import datetime, timedelta, timezone\n",
        "from urllib.request import urlopen\n",
        "\n",
        "# Make sure your version matches the docs at https://docs.dbnl.com/\n",
        "print(\"dbnl version:\", dbnl.__version__)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "26a736c9-a755-4e02-b4bb-fe8238498f6a",
      "metadata": {},
      "outputs": [],
      "source": [
        "# Login to DBNL (using default Sandbox url)\n",
        "dbnl.login(\n",
        "    api_url=\"http://localhost:8080/api\",\n",
        "    api_token=\"\",  # found at http://localhost:8080/tokens\n",
        ")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "1961d4ef-f120-4c98-bb26-c0e5b3119421",
      "metadata": {},
      "outputs": [],
      "source": [
        "# Create a new project\n",
        "project = dbnl.get_or_create_project(\n",
        "    name=\"Quickstart Demo\",\n",
        "    default_llm_model_name=\"quickstart_model\",  # from step (2) in quickstart\n",
        ")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "22dcdc85-02ec-4837-9736-a7e1a365af7c",
      "metadata": {},
      "outputs": [],
      "source": [
        "# Load 14 days of OTEL traces from public S3 and upload to DBNL\n",
        "BASE = \"https://dbnl-demo-public.s3.us-east-1.amazonaws.com/outing_agent_log_data\"\n",
        "today = datetime.now(timezone.utc).replace(hour=0, minute=0, second=0, microsecond=0)\n",
        "dctx = zstandard.ZstdDecompressor()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cfc03866-28c4-437c-bd96-3b06c9525415",
      "metadata": {},
      "outputs": [],
      "source": [
        "# Upload the data, DBNL needs at least 7 days to establish behavioral baselines\n",
        "print(\"Uploading data...\")\n",
        "print(f\"See status at: {dbnl.config.app_url()}/ns/{project.namespace_id}/projects/{project.id}/status\")\n",
        "for i in range(14):\n",
        "    data_start = today - timedelta(days=14 - i)\n",
        "    data_end = data_start + timedelta(days=1)\n",
        "    day = data_start.strftime(\"%Y-%m-%d\")\n",
        "    try:\n",
        "        raw = dctx.stream_reader(io.BytesIO(urlopen(f\"{BASE}/traces_{day}.jsonl.zst\").read())).read()\n",
        "        data = pandas.Series([json.loads(l) for l in raw.decode().splitlines()])\n",
        "        print(f\"[{i+1}/14] {day}: uploading {len(data)} records\")\n",
        "    except Exception as e:\n",
        "        if \"Not Found\" in str(e):\n",
        "            print(f\"[{i+1}/14] {day}: no data\")\n",
        "            continue\n",
        "        raise\n",
        "    try:\n",
        "        dbnl.log(\n",
        "            project_id=project.id,\n",
        "            data_start_time=data_start,\n",
        "            data_end_time=data_end,\n",
        "            otlp_data=data,\n",
        "            wait_timeout=60 * 30,\n",
        "        )\n",
        "    except Exception as e:\n",
        "        if \"Data already exists\" in str(e):\n",
        "            print(f\"[{i+1}/14] {day}: data already exists\")\n",
        "            continue\n",
        "        raise\n",
        "\n",
        "print(\"You can now explore your data in DBNL!\")\n",
        "print(f\"{dbnl.config.app_url()}/ns/{project.namespace_id}/projects/{project.id}\")"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3 (ipykernel)",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.12.7"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
