Agent_week_6 / backend /database.py
Mohanp76's picture
Deploy project
5545e4d
Raw
History Blame Contribute Delete
2.77 kB
import sqlite3
import json
from datetime import datetime
from pathlib import Path
from dotenv import load_dotenv
load_dotenv(override=True)
# Resolve the database from the project directory, rather than the process
# working directory. The dashboard and the scheduler are often started from
# different directories; a relative path makes each process create/read a
# different database and produces an apparently empty dashboard.
DB = str(Path(__file__).resolve().parents[1] / "accounts.db")
with sqlite3.connect(DB) as conn:
cursor = conn.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS accounts (name TEXT PRIMARY KEY, account TEXT)')
cursor.execute('''
CREATE TABLE IF NOT EXISTS logs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
datetime DATETIME,
type TEXT,
message TEXT
)
''')
conn.commit()
def write_account(name, account_dict):
json_data = json.dumps(account_dict)
with sqlite3.connect(DB) as conn:
cursor = conn.cursor()
cursor.execute('''
INSERT INTO accounts (name, account)
VALUES (?, ?)
ON CONFLICT(name) DO UPDATE SET account=excluded.account
''', (name.lower(), json_data))
conn.commit()
def read_account(name):
with sqlite3.connect(DB) as conn:
cursor = conn.cursor()
cursor.execute('SELECT account FROM accounts WHERE name = ?', (name.lower(),))
row = cursor.fetchone()
return json.loads(row[0]) if row else None
def write_log(name: str, type: str, message: str):
"""
Write a log entry to the logs table.
Args:
name (str): The name associated with the log
type (str): The type of log entry
message (str): The log message
"""
now = datetime.now().isoformat()
with sqlite3.connect(DB) as conn:
cursor = conn.cursor()
cursor.execute('''
INSERT INTO logs (name, datetime, type, message)
VALUES (?, datetime('now'), ?, ?)
''', (name.lower(), type, message))
conn.commit()
def read_log(name: str, last_n=10):
"""
Read the most recent log entries for a given name.
Args:
name (str): The name to retrieve logs for
last_n (int): Number of most recent entries to retrieve
Returns:
list: A list of tuples containing (datetime, type, message)
"""
with sqlite3.connect(DB) as conn:
cursor = conn.cursor()
cursor.execute('''
SELECT datetime, type, message FROM logs
WHERE name = ?
ORDER BY id DESC
LIMIT ?
''', (name.lower(), last_n))
return reversed(cursor.fetchall())