{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ae430903-55a2-47ed-87c6-677b1fb44198",
   "metadata": {},
   "outputs": [],
   "source": [
    "from enum import Enum\n",
    "import os\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "9d6920fc-8633-4754-a762-ece23af18877",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Types of cache\n",
    "class CacheType(Enum):\n",
    "    LRU = 1\n",
    "    RANDOM_EVICTION = 2\n",
    "\n",
    "\n",
    "DATABASE_OBJECTS = 100 # Number of objects in the database"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9b8c0686-1bab-47a4-9f9f-e5a11167ea8c",
   "metadata": {},
   "outputs": [],
   "source": [
    "configurations = {\n",
    "    \"default\": (DATABASE_OBJECTS, 10, CacheType.LRU, 5),\n",
    "    \"No Refresh\": (DATABASE_OBJECTS, 0, CacheType.LRU, 5),\n",
    "    \"Infinite TTL\": (int(DATABASE_OBJECTS / 2), 0, CacheType.LRU, 0),\n",
    "    \"Random Eviction\": (int(DATABASE_OBJECTS / 2), 10, CacheType.RANDOM_EVICTION, 5),\n",
    "    \"RE without Refresh\": (int(DATABASE_OBJECTS / 2), 0, CacheType.RANDOM_EVICTION, 5),\n",
    "    \"No Refresh (0.5s ttl)\": (DATABASE_OBJECTS, 0, CacheType.LRU, 0.5),\n",
    "    \"No Refresh (1.0s ttl)\": (DATABASE_OBJECTS, 0, CacheType.LRU, 1),\n",
    "    \"No Refresh (2.0s ttl)\": (DATABASE_OBJECTS, 0, CacheType.LRU, 2),\n",
    "    \"No Refresh (3.0s ttl)\": (DATABASE_OBJECTS, 0, CacheType.LRU, 3),\n",
    "    \"No Refresh (4.0s ttl)\": (DATABASE_OBJECTS, 0, CacheType.LRU, 4),\n",
    "    \"No Refresh (5.0s ttl)\": (DATABASE_OBJECTS, 0, CacheType.LRU, 5),\n",
    "}\n",
    "\n",
    "experiments = [k.replace(\" \", \"_\").replace(\"(\", \"\").replace(\")\", \"\").replace(\".\", \"_\") for k in configurations.keys()]\n",
    "experiments = [os.path.join(\"./experiments/\",e, \"overall_hit_age.csv\") for e in experiments if \"_ttl\" in e]\n",
    "experiments = [{'ttl': '.'.join(file.split('/')[2].split(\"_\")[2:4]),'data': pd.read_csv(file)} for file in experiments]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "5be1df01-5dc8-43fe-a597-72ba76eefc0f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'ttl': '0.5s',\n",
       "  'data': {'hit_rate': 0.458669533255314, 'avg_age': 0.1106194370534208}},\n",
       " {'ttl': '1.0s',\n",
       "  'data': {'hit_rate': 0.619673736493892, 'avg_age': 0.3072731620786534}},\n",
       " {'ttl': '2.0s',\n",
       "  'data': {'hit_rate': 0.769815289387402, 'avg_age': 0.7718808361404631}},\n",
       " {'ttl': '3.0s',\n",
       "  'data': {'hit_rate': 0.7906927824364712, 'avg_age': 1.1699029289276106}},\n",
       " {'ttl': '4.0s',\n",
       "  'data': {'hit_rate': 0.8461611168860607, 'avg_age': 1.6633213831159597}},\n",
       " {'ttl': '5.0s',\n",
       "  'data': {'hit_rate': 0.8689161003980432, 'avg_age': 2.145385684680086}}]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stats = [\n",
    "    {'ttl': e['ttl'],\n",
    "     'data':  {\n",
    "         \"hit_rate\": e['data']['hit_rate'][1], \n",
    "         \"avg_age\": e['data']['avg_age'][1]\n",
    "     }\n",
    "    } for e in experiments]\n",
    "stats"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "26030d03-b970-4e88-9c9a-e96ef79181c6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 50496 characters]",
      "text/plain": [
       "<Figure size 1000x600 with 2 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Extract TTL, hit_rate, and avg_age from data\n",
    "ttls = [entry['ttl'] for entry in stats]\n",
    "hit_rates = [entry['data']['hit_rate'] for entry in stats]\n",
    "avg_ages = [entry['data']['avg_age'] for entry in stats]\n",
    "\n",
    "# Plotting\n",
    "plt.figure(figsize=(10, 6))\n",
    "\n",
    "# Plot hit_rate\n",
    "plt.subplot(2, 1, 1)\n",
    "plt.plot(ttls, hit_rates, marker='o', color='b', label='Hit Rate')\n",
    "plt.xlabel('TTL (seconds)')\n",
    "plt.ylabel('Hit Rate')\n",
    "plt.title('Hit Rate vs TTL')\n",
    "plt.grid(True)\n",
    "\n",
    "# Plot avg_age\n",
    "plt.subplot(2, 1, 2)\n",
    "plt.plot(ttls, avg_ages, marker='o', color='r', label='Avg Age')\n",
    "plt.xlabel('TTL (seconds)')\n",
    "plt.ylabel('Avg Age')\n",
    "plt.title('Avg Age vs TTL')\n",
    "plt.grid(True)\n",
    "\n",
    "# Show the plots\n",
    "plt.tight_layout()\n",
    "plt.savefig(\"./experiments/hr_and_age_vs_ttl\")\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c4a8220e-9a21-42db-889a-26d901672f5e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['0.5s', '1.0s', '2.0s', '3.0s', '4.0s', '5.0s']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ttls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "58a7865b-6ce6-4960-8dc5-c926d5463949",
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.DataFrame(hit_rates, columns=[\"hit_rates\"], index=ttls).to_markdown('./experiments/hit_rates.md')\n",
    "pd.DataFrame(avg_ages, columns=[\"avg_ages\"], index=ttls).to_markdown('./experiments/avg_ages.md')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a92dcf86-d0ae-4f5c-937f-aeac45ac0dea",
   "metadata": {},
   "outputs": [],
   "source": [
    "details = [k.replace(\" \", \"_\").replace(\"(\", \"\").replace(\")\", \"\").replace(\".\", \"_\") for k in configurations.keys()]\n",
    "details = [os.path.join(\"./experiments/\",e, \"details.csv\") for e in details if \"_ttl\" in e]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "0ffb1366-83ea-49ab-83ca-945ed993ef3f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['./experiments/No_Refresh_0_5s_ttl/details.csv',\n",
       " './experiments/No_Refresh_1_0s_ttl/details.csv',\n",
       " './experiments/No_Refresh_2_0s_ttl/details.csv',\n",
       " './experiments/No_Refresh_3_0s_ttl/details.csv',\n",
       " './experiments/No_Refresh_4_0s_ttl/details.csv',\n",
       " './experiments/No_Refresh_5_0s_ttl/details.csv']"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "details"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6e48f78f-15a3-4788-8ac9-6ad1fabbee26",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0.5s</th>\n",
       "      <th>1.0s</th>\n",
       "      <th>2.0s</th>\n",
       "      <th>3.0s</th>\n",
       "      <th>4.0s</th>\n",
       "      <th>5.0s</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>obj_id</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>45</td>\n",
       "      <td>1</td>\n",
       "      <td>7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>7</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>37</td>\n",
       "      <td>1</td>\n",
       "      <td>52</td>\n",
       "      <td>2</td>\n",
       "      <td>13</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>18</td>\n",
       "      <td>3</td>\n",
       "      <td>19</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>100</th>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>100 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        0.5s  1.0s  2.0s  3.0s  4.0s  5.0s\n",
       "obj_id                                    \n",
       "1          1     1     5     1     1     1\n",
       "2          3     2     2     1     1     1\n",
       "3          1     3     3     1     1     1\n",
       "4          1     2     6     1     3     3\n",
       "5          2     1     1     1     2     1\n",
       "...      ...   ...   ...   ...   ...   ...\n",
       "96         1     1     2    45     1     7\n",
       "97         1     3     7     1     1     3\n",
       "98        37     1    52     2    13     1\n",
       "99         4     1    18     3    19     2\n",
       "100        2     1     1     1     1     2\n",
       "\n",
       "[100 rows x 6 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.concat([pd.read_csv(file,index_col=\"obj_id\")['lambda'].rename(f\"{'.'.join(file.split('/')[2].split(\"_\")[2:4])}\") for file in details], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "3e0ddc4a-bede-4921-8ce8-427ef028e19e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "43624"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(pd.read_csv(\"./experiments/No_Refresh_0_5s_ttl/details.csv\",index_col=\"obj_id\")['access_count'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e324fbe8-24a6-49e8-96b9-d1a1a49f9644",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "graphs",
   "language": "python",
   "name": "graphs"
  },
  "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
}
