Skip to content

Instantly share code, notes, and snippets.

@j1fig
Created November 13, 2022 22:26
Show Gist options
  • Save j1fig/0aca72a7d2a23769f766e9bb58137f59 to your computer and use it in GitHub Desktop.
Save j1fig/0aca72a7d2a23769f766e9bb58137f59 to your computer and use it in GitHub Desktop.
Context switch tracking hook
#!/usr/bin/env python3
# Context switch tracking app.
# Stores context switches in a local SQLite DB.
from datetime import datetime
import os
import sqlite3
LOCAL_DIR = os.path.expanduser("~/.swapctx")
LOCAL_DB = os.path.join(LOCAL_DIR, "swapctx.db")
SCHEMA_DDL = """
CREATE TABLE context(switched)
"""
def _ensure_local_dir():
if not os.path.exists(LOCAL_DIR):
os.mkdir(LOCAL_DIR)
def _create_db():
with sqlite3.connect(LOCAL_DB) as conn:
conn.execute(SCHEMA_DDL)
def _ensure_local_db():
_ensure_local_dir()
if not os.path.exists(LOCAL_DB):
_create_db()
def _add_context_switch():
with sqlite3.connect(LOCAL_DB) as conn:
conn.execute(
"INSERT INTO context VALUES(?)",
(datetime.utcnow(),)
)
def _run():
_ensure_local_db()
_add_context_switch()
if __name__=="__main__":
_run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment