{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e5f6a7b8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "500 events, 50 users\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'user': 'user_020', 'action': 'navigate', 'page': '/home', 'ts': '2025-03-07T10:14:09', 'duration_ms': 5765}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import json\n",
    "from collections import Counter\n",
    "from datetime import datetime, timedelta\n",
    "import random\n",
    "\n",
    "random.seed(42)\n",
    "\n",
    "# generate some fake session data\n",
    "users = [f\"user_{i:03d}\" for i in range(50)]\n",
    "actions = [\"view\", \"click\", \"scroll\", \"submit\", \"navigate\"]\n",
    "pages = [\"/home\", \"/products\", \"/cart\", \"/checkout\", \"/account\"]\n",
    "\n",
    "events = []\n",
    "base = datetime(2025, 3, 1)\n",
    "for _ in range(500):\n",
    "    events.append({\n",
    "        \"user\": random.choice(users),\n",
    "        \"action\": random.choice(actions),\n",
    "        \"page\": random.choice(pages),\n",
    "        \"ts\": (base + timedelta(seconds=random.randint(0, 86400*7))).isoformat(),\n",
    "        \"duration_ms\": random.randint(100, 12000)\n",
    "    })\n",
    "\n",
    "print(f\"{len(events)} events, {len(users)} users\")\n",
    "events[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "c9d0e1f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Actions:\n",
      "  navigate   112  ██████████████████████\n",
      "  scroll     107  █████████████████████\n",
      "  view       100  ████████████████████\n",
      "  click       96  ███████████████████\n",
      "  submit      85  █████████████████\n",
      "\n",
      "Pages:\n",
      "  /checkout    115  ███████████████████████\n",
      "  /home        104  ████████████████████\n",
      "  /cart        100  ████████████████████\n",
      "  /products     96  ███████████████████\n",
      "  /account      85  █████████████████\n"
     ]
    }
   ],
   "source": [
    "action_counts = Counter(e[\"action\"] for e in events)\n",
    "page_counts = Counter(e[\"page\"] for e in events)\n",
    "\n",
    "print(\"Actions:\")\n",
    "for action, n in action_counts.most_common():\n",
    "    bar = \"\\u2588\" * (n // 5)\n",
    "    print(f\"  {action:<10} {n:>3}  {bar}\")\n",
    "\n",
    "print(\"\\nPages:\")\n",
    "for page, n in page_counts.most_common():\n",
    "    bar = \"\\u2588\" * (n // 5)\n",
    "    print(f\"  {page:<12} {n:>3}  {bar}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "a3b4c5d6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Page         Mean   Median  Count\n",
      "--------------------------------------\n",
      "/account     6173ms  6271ms    85\n",
      "/cart        5765ms  5544ms   100\n",
      "/checkout    6238ms  6482ms   115\n",
      "/home        5765ms  5429ms   104\n",
      "/products    6162ms  6320ms    96\n"
     ]
    }
   ],
   "source": [
    "# avg duration per page\n",
    "from statistics import mean, median\n",
    "\n",
    "durations_by_page = {}\n",
    "for e in events:\n",
    "    durations_by_page.setdefault(e[\"page\"], []).append(e[\"duration_ms\"])\n",
    "\n",
    "print(f\"{'Page':<12} {'Mean':>8} {'Median':>8} {'Count':>6}\")\n",
    "print(\"-\" * 38)\n",
    "for page in sorted(durations_by_page):\n",
    "    vals = durations_by_page[page]\n",
    "    print(f\"{page:<12} {mean(vals):>7.0f}ms {median(vals):>7.0f}ms {len(vals):>5}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e7f8a9b0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Top 10 submitters:\n",
      "  user_012: 5\n",
      "  user_048: 4\n",
      "  user_034: 3\n",
      "  user_003: 3\n",
      "  user_038: 3\n",
      "  user_044: 3\n",
      "  user_001: 3\n",
      "  user_026: 2\n",
      "  user_006: 2\n",
      "  user_028: 2\n"
     ]
    }
   ],
   "source": [
    "# users with most submits\n",
    "submits = [e for e in events if e[\"action\"] == \"submit\"]\n",
    "top_submitters = Counter(e[\"user\"] for e in submits).most_common(10)\n",
    "\n",
    "print(\"Top 10 submitters:\")\n",
    "for user, count in top_submitters:\n",
    "    print(f\"  {user}: {count}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c1d2e3f4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hourly distribution:\n",
      "  00:00   22  ▓▓▓▓▓▓▓▓▓▓▓\n",
      "  01:00   23  ▓▓▓▓▓▓▓▓▓▓▓\n",
      "  02:00   16  ▓▓▓▓▓▓▓▓\n",
      "  03:00   21  ▓▓▓▓▓▓▓▓▓▓\n",
      "  04:00   17  ▓▓▓▓▓▓▓▓\n",
      "  05:00   23  ▓▓▓▓▓▓▓▓▓▓▓\n",
      "  06:00   24  ▓▓▓▓▓▓▓▓▓▓▓▓\n",
      "  07:00   22  ▓▓▓▓▓▓▓▓▓▓▓\n",
      "  08:00   20  ▓▓▓▓▓▓▓▓▓▓\n",
      "  09:00   23  ▓▓▓▓▓▓▓▓▓▓▓\n",
      "  10:00   19  ▓▓▓▓▓▓▓▓▓\n",
      "  11:00   22  ▓▓▓▓▓▓▓▓▓▓▓\n",
      "  12:00   22  ▓▓▓▓▓▓▓▓▓▓▓\n",
      "  13:00   21  ▓▓▓▓▓▓▓▓▓▓\n",
      "  14:00   26  ▓▓▓▓▓▓▓▓▓▓▓▓▓\n",
      "  15:00   17  ▓▓▓▓▓▓▓▓\n",
      "  16:00   19  ▓▓▓▓▓▓▓▓▓\n",
      "  17:00   20  ▓▓▓▓▓▓▓▓▓▓\n",
      "  18:00   22  ▓▓▓▓▓▓▓▓▓▓▓\n",
      "  19:00   24  ▓▓▓▓▓▓▓▓▓▓▓▓\n",
      "  20:00   18  ▓▓▓▓▓▓▓▓▓\n",
      "  21:00   19  ▓▓▓▓▓▓▓▓▓\n",
      "  22:00   21  ▓▓▓▓▓▓▓▓▓▓\n",
      "  23:00   19  ▓▓▓▓▓▓▓▓▓\n"
     ]
    }
   ],
   "source": [
    "# hourly distribution\n",
    "hours = [datetime.fromisoformat(e[\"ts\"]).hour for e in events]\n",
    "hour_counts = Counter(hours)\n",
    "\n",
    "print(\"Hourly distribution:\")\n",
    "for h in range(24):\n",
    "    n = hour_counts.get(h, 0)\n",
    "    bar = \"\\u2593\" * (n // 2)\n",
    "    print(f\"  {h:02d}:00  {n:>3}  {bar}\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.12.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
