File size: 1,462 Bytes
e227a15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import sqlite3
import os
from datetime import datetime
from config.config import Config

def get_db_connection():
    conn = sqlite3.connect(Config.DB_PATH)
    conn.row_factory = sqlite3.Row
    return conn

def init_db():
    conn = get_db_connection()
    c = conn.cursor()
    c.execute('''CREATE TABLE IF NOT EXISTS logs (

        id INTEGER PRIMARY KEY AUTOINCREMENT,

        user_query TEXT NOT NULL,

        ai_response TEXT NOT NULL,

        timestamp TEXT NOT NULL

    )''')
    c.execute('''CREATE TABLE IF NOT EXISTS feedback (

        id INTEGER PRIMARY KEY AUTOINCREMENT,

        log_id INTEGER,

        rating TEXT,

        timestamp TEXT NOT NULL,

        FOREIGN KEY (log_id) REFERENCES logs(id)

    )''')
    conn.commit()
    conn.close()

def insert_log(user_query, ai_response):
    conn = get_db_connection()
    c = conn.cursor()
    timestamp = datetime.utcnow().isoformat()
    c.execute('INSERT INTO logs (user_query, ai_response, timestamp) VALUES (?, ?, ?)',
              (user_query, ai_response, timestamp))
    log_id = c.lastrowid
    conn.commit()
    conn.close()
    return log_id

def insert_feedback(log_id, rating):
    conn = get_db_connection()
    c = conn.cursor()
    timestamp = datetime.utcnow().isoformat()
    c.execute('INSERT INTO feedback (log_id, rating, timestamp) VALUES (?, ?, ?)',
              (log_id, rating, timestamp))
    conn.commit()
    conn.close()