{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "initial_id",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-21T08:17:53.760671Z",
     "start_time": "2025-04-21T08:17:52.614395Z"
    }
   },
   "outputs": [],
   "source": [
    "from collections import defaultdict, Counter\n",
    "import json\n",
    "import re\n",
    "import os\n",
    "\n",
    "from common import *\n",
    "\n",
    "from matplotlib import pyplot as plt\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "f4f962a27d9e4a44",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-21T08:18:14.984780Z",
     "start_time": "2025-04-21T08:17:53.852545Z"
    }
   },
   "outputs": [],
   "source": [
    "# File generated with\n",
    "# python analyze_bundle_dataset.py --total 100000 --worker $(nproc) -s get_urls $DATASETS/bundles-daily/results-*\n",
    "# mv results.json bundlers.json\n",
    "with open(os.path.join(DATASETS, \"cdn-results.json\"), \"r\") as f:\n",
    "    data = json.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4ed7d3f57c77618e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-21T08:18:15.943030Z",
     "start_time": "2025-04-21T08:18:15.062711Z"
    }
   },
   "outputs": [],
   "source": [
    "responsive_data = [d[\"urls\"] for d in data if len(d[\"urls\"]) > 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8250aa41825433f7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-12T12:44:40.398516Z",
     "start_time": "2024-11-12T12:44:40.396426Z"
    }
   },
   "outputs": [],
   "source": [
    "results = defaultdict(dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "ba8f464d5efe7898",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-12T12:44:44.386306Z",
     "start_time": "2024-11-12T12:44:40.446708Z"
    }
   },
   "outputs": [],
   "source": [
    "for host in CDN_HOSTS:\n",
    "    count = 0\n",
    "    for urls in responsive_data:\n",
    "        for url in urls:\n",
    "            if host in url:\n",
    "                count += 1\n",
    "    results[\"occurences\"][host] = count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "80841782bfba56b0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-12T12:44:48.101207Z",
     "start_time": "2024-11-12T12:44:44.394808Z"
    }
   },
   "outputs": [],
   "source": [
    "for host in CDN_HOSTS:\n",
    "    count = 0\n",
    "    for urls in responsive_data:\n",
    "        for url in urls:\n",
    "            if host in url:\n",
    "                count += 1\n",
    "                break\n",
    "    results[\"at_least_one\"][host] = count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "26aba7c384e23184",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-12T12:54:58.864654Z",
     "start_time": "2024-11-12T12:54:54.745501Z"
    }
   },
   "outputs": [],
   "source": [
    "version_regex = re.compile(r\"[0-9]+\\.[0-9]+\\.[0-9]+\")\n",
    "no_match = []\n",
    "for host in CDN_HOSTS:\n",
    "    count = 0\n",
    "    for urls in responsive_data:\n",
    "        for url in urls:\n",
    "            if host in url:\n",
    "                if version_regex.search(url):\n",
    "                    count += 1\n",
    "                else:\n",
    "                    no_match.append(url)\n",
    "    results[\"with_versions\"][host] = count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "6212dba680a6ea3c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-11-12T12:55:01.947101Z",
     "start_time": "2024-11-12T12:55:01.944040Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defaultdict(dict,\n",
       "            {'occurences': {'//cdn.jsdelivr.net': 373251,\n",
       "              '//cdnjs.cloudflare.com': 497213,\n",
       "              '//unpkg.com': 85646,\n",
       "              '//ajax.googleapis.com': 278897,\n",
       "              '//ajax.aspnetcdn.com': 9391,\n",
       "              '//code.jquery.com': 157443},\n",
       "             'at_least_one': {'//cdn.jsdelivr.net': 232982,\n",
       "              '//cdnjs.cloudflare.com': 259470,\n",
       "              '//unpkg.com': 54355,\n",
       "              '//ajax.googleapis.com': 242591,\n",
       "              '//ajax.aspnetcdn.com': 5966,\n",
       "              '//code.jquery.com': 135218},\n",
       "             'with_versions': {'//cdn.jsdelivr.net': 232631,\n",
       "              '//cdnjs.cloudflare.com': 482685,\n",
       "              '//unpkg.com': 14395,\n",
       "              '//ajax.googleapis.com': 264956,\n",
       "              '//ajax.aspnetcdn.com': 6891,\n",
       "              '//code.jquery.com': 153804}})"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "results"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9cc0a3f64c0bd46c",
   "metadata": {},
   "source": [
    "### How old are the jQuery versions?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0e2785fd-df30-41a3-bddc-174e9d35616d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-20T18:26:43.423685Z",
     "start_time": "2025-04-20T18:26:29.008540Z"
    }
   },
   "outputs": [],
   "source": [
    "version_regex = re.compile(r\"([0-9]+\\.[0-9]+\\.[0-9]+)\\.[^?]+$\")\n",
    "host = CDN_HOSTS[-1]\n",
    "versions = Counter()\n",
    "versions_major = Counter()\n",
    "for urls in responsive_data:\n",
    "    for url in urls:\n",
    "        if host in url:\n",
    "            matches = version_regex.search(url)\n",
    "            if matches:\n",
    "                versions.update([matches.groups()[0]])\n",
    "                versions_major.update([matches.groups()[0][0]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "90498412b02855a6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-20T18:27:43.719707Z",
     "start_time": "2025-04-20T18:27:43.711406Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.2427144956479144"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(versions_major[\"1\"] + versions_major[\"2\"]) / (sum(versions_major.values()))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec113bd6",
   "metadata": {},
   "source": [
    "### What about cookielaw?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9c16dc498f8e53c5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-21T08:25:51.465131Z",
     "start_time": "2025-04-21T08:25:41.506442Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3611 78394\n"
     ]
    }
   ],
   "source": [
    "cookielaw_stats = {}\n",
    "for d in data:\n",
    "    if len(d[\"urls\"]) > 0:\n",
    "        cookielaw = cookielaw_stats.get(d[\"domain\"], False)\n",
    "        for url in d[\"urls\"]:\n",
    "            if \"cdn.cookielaw.org\" in url:\n",
    "                cookielaw = True\n",
    "                break\n",
    "        cookielaw_stats[d[\"domain\"]] = cookielaw\n",
    "print(sum(map(int, cookielaw_stats.values())), len(cookielaw_stats))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "fda3147f1e8276e0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-21T08:27:04.447783Z",
     "start_time": "2025-04-21T08:27:04.434339Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.04606219863765084"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "3611 / 78394"
   ]
  }
 ],
 "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.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
