Procházet zdrojové kódy

feat(indexer): sqlite db generating script

iwanhae před 1 rokem
rodič
revize
3bf044618c
2 změnil soubory, kde provedl 306 přidání a 0 odebrání
  1. 163 0
      indexer/.gitignore
  2. 143 0
      indexer/1_gen_sqlite3_db.ipynb

+ 163 - 0
indexer/.gitignore

@@ -0,0 +1,163 @@
+data.jsonl
+kuberian.db
+
+# Byte-compiled / optimized / DLL files
+__pycache__/
+*.py[cod]
+*$py.class
+
+# C extensions
+*.so
+
+# Distribution / packaging
+.Python
+build/
+develop-eggs/
+dist/
+downloads/
+eggs/
+.eggs/
+lib/
+lib64/
+parts/
+sdist/
+var/
+wheels/
+share/python-wheels/
+*.egg-info/
+.installed.cfg
+*.egg
+MANIFEST
+
+# PyInstaller
+#  Usually these files are written by a python script from a template
+#  before PyInstaller builds the exe, so as to inject date/other infos into it.
+*.manifest
+*.spec
+
+# Installer logs
+pip-log.txt
+pip-delete-this-directory.txt
+
+# Unit test / coverage reports
+htmlcov/
+.tox/
+.nox/
+.coverage
+.coverage.*
+.cache
+nosetests.xml
+coverage.xml
+*.cover
+*.py,cover
+.hypothesis/
+.pytest_cache/
+cover/
+
+# Translations
+*.mo
+*.pot
+
+# Django stuff:
+*.log
+local_settings.py
+db.sqlite3
+db.sqlite3-journal
+
+# Flask stuff:
+instance/
+.webassets-cache
+
+# Scrapy stuff:
+.scrapy
+
+# Sphinx documentation
+docs/_build/
+
+# PyBuilder
+.pybuilder/
+target/
+
+# Jupyter Notebook
+.ipynb_checkpoints
+
+# IPython
+profile_default/
+ipython_config.py
+
+# pyenv
+#   For a library or package, you might want to ignore these files since the code is
+#   intended to run in multiple environments; otherwise, check them in:
+# .python-version
+
+# pipenv
+#   According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
+#   However, in case of collaboration, if having platform-specific dependencies or dependencies
+#   having no cross-platform support, pipenv may install dependencies that don't work, or not
+#   install all needed dependencies.
+#Pipfile.lock
+
+# poetry
+#   Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
+#   This is especially recommended for binary packages to ensure reproducibility, and is more
+#   commonly ignored for libraries.
+#   https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
+#poetry.lock
+
+# pdm
+#   Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
+#pdm.lock
+#   pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
+#   in version control.
+#   https://pdm.fming.dev/#use-with-ide
+.pdm.toml
+
+# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
+__pypackages__/
+
+# Celery stuff
+celerybeat-schedule
+celerybeat.pid
+
+# SageMath parsed files
+*.sage.py
+
+# Environments
+.env
+.venv
+env/
+venv/
+ENV/
+env.bak/
+venv.bak/
+
+# Spyder project settings
+.spyderproject
+.spyproject
+
+# Rope project settings
+.ropeproject
+
+# mkdocs documentation
+/site
+
+# mypy
+.mypy_cache/
+.dmypy.json
+dmypy.json
+
+# Pyre type checker
+.pyre/
+
+# pytype static type analyzer
+.pytype/
+
+# Cython debug symbols
+cython_debug/
+
+# PyCharm
+#  JetBrains specific template is maintained in a separate JetBrains.gitignore that can
+#  be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
+#  and can be added to the global gitignore or merged into this file.  For a more nuclear
+#  option (not recommended) you can uncomment the following to ignore the entire idea folder.
+#.idea/

+ 143 - 0
indexer/1_gen_sqlite3_db.ipynb

@@ -0,0 +1,143 @@
+{
+ "cells": [
+  {
+   "cell_type": "code",
+   "execution_count": 1,
+   "metadata": {},
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current\n",
+      "                                 Dload  Upload   Total   Spent    Left  Speed\n",
+      "100 20.8M  100 20.8M    0     0  10.6M      0  0:00:01  0:00:01 --:--:-- 10.6M\n"
+     ]
+    }
+   ],
+   "source": [
+    "RAW_DATA = \"https://s3.iwanhae.kr/kuberian/1_prod_kubernetes_v1_27_4.jsonl.gz\"\n",
+    "RAW_DATA_NAME = \"data.jsonl\"\n",
+    "DB_NAME = \"kuberian.db\"\n",
+    "!curl \"{RAW_DATA}\" -o \"{RAW_DATA_NAME}.gz\"\n",
+    "!rm \"{RAW_DATA_NAME}\" \"{DB_NAME}\"\n",
+    "!gunzip \"{RAW_DATA_NAME}.gz\""
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 2,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "import sqlite3\n",
+    "tables = [\n",
+    "    \"\"\"\n",
+    "CREATE TABLE functions (\n",
+    "\tid INTEGER PRIMARY KEY,\n",
+    "   \tname TEXT NOT NULL,\n",
+    "    signature TEXT NOT NULL,\n",
+    "\tfile TEXT NOT NULL,\n",
+    "\tcode TEXT NOT NULL,\n",
+    "    line_start INTEGER NOT NULL,\n",
+    "    line_end INTEGER NOT NULL\n",
+    ");\"\"\",\n",
+    "    \"\"\"\n",
+    "CREATE TABLE function_analyses (\n",
+    "\tid INTEGER PRIMARY KEY,\n",
+    "    function_id INTEGER,\n",
+    "    summary TEXT NOT NULL,\n",
+    "    background TEXT,\n",
+    "\tanalysis TEXT,\n",
+    "\tpurpose TEXT,\n",
+    "    comment TEXT,\n",
+    "    tldr TEXT\n",
+    ");\n",
+    "\"\"\"]\n",
+    "indexes = [\n",
+    "    \"CREATE INDEX idx_function_analyses_function_id ON function_analyses (function_id);\"\n",
+    "]\n",
+    "\n",
+    "conn = sqlite3.connect(DB_NAME)\n",
+    "cur = conn.cursor()\n",
+    "for table in tables:\n",
+    "    cur.execute(table)\n",
+    "conn.commit()"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 3,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "import json\n",
+    "\n",
+    "sql_function = \"\"\"\n",
+    "INSERT INTO functions(id,name,signature,file,code,line_start,line_end)\n",
+    "    VALUES(?,?,?,?,?,?,?);\n",
+    "\"\"\"\n",
+    "\n",
+    "sql_function_analysis = \"\"\"\n",
+    "INSERT INTO function_analyses(function_id, summary, background, analysis, purpose, comment, tldr)\n",
+    "    VALUES(?,?,?,?,?,?,?);\n",
+    "\"\"\"\n",
+    "\n",
+    "data = []\n",
+    "with open(\"data.jsonl\") as f:\n",
+    "    for line in f:\n",
+    "        d = json.loads(line)\n",
+    "        cur.execute(sql_function, (\n",
+    "            d['id'], d['name'], d['signature'], d['file'], d['code'], d['line']['from'], d['line']['to']\n",
+    "        ))\n",
+    "        if \"parsed\" in d:\n",
+    "            parsed = d['parsed']\n",
+    "            summary = d['result'].split(\n",
+    "                \"In one sentence, this is a function that\")[1].strip()\n",
+    "            cur.execute(sql_function_analysis, (d['id'],\n",
+    "                        summary,\n",
+    "                        parsed['background'] if \"background\" in parsed else None,\n",
+    "                        parsed['analysis'] if \"analysis\" in parsed else None,\n",
+    "                        parsed['purpose'] if \"purpose\" in parsed else None,\n",
+    "                        parsed['comment'] if \"comment\" in parsed else None,\n",
+    "                        parsed['tldr'] if \"tldr\" in parsed else None,\n",
+    "                        ))\n",
+    "conn.commit()"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 4,
+   "metadata": {},
+   "outputs": [],
+   "source": [
+    "for index in indexes:\n",
+    "    cur.execute(index)\n",
+    "conn.commit()\n",
+    "conn.close()"
+   ]
+  }
+ ],
+ "metadata": {
+  "kernelspec": {
+   "display_name": ".venv",
+   "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.11.4"
+  },
+  "orig_nbformat": 4
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}