File size: 7,974 Bytes
cd6f412
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import sqlite3
import logging
import json
from contextlib import contextmanager
from datetime import datetime
from pathlib import Path
from urllib.parse import urlparse
from typing import Optional, Dict, Any

from ..config import settings

# Configure logging
logging.basicConfig(level=settings.LOG_LEVEL)
logger = logging.getLogger(__name__)

@contextmanager
def get_db_connection():
    """Context manager for handling database connections."""
    conn = None
    try:
        conn = sqlite3.connect(settings.DATABASE_URL)
        conn.row_factory = sqlite3.Row
        logger.debug("Database connection established.")
        yield conn
    except sqlite3.Error as e:
        logger.error(f"Database connection error: {e}")
        raise
    finally:
        if conn:
            conn.close()
            logger.debug("Database connection closed.")

def setup_database():
    """Creates/updates all necessary tables in the database."""
    logger.info("Setting up database schema...")
    ddl_statements = [
        """
        CREATE TABLE IF NOT EXISTS data_sources (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            name TEXT NOT NULL,
            url TEXT NOT NULL UNIQUE,
            data_type TEXT NOT NULL,
            category TEXT,
            local_path TEXT,
            content_hash TEXT,
            status TEXT DEFAULT 'pending',
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            updated_at TIMESTAMP
        );
        """,
        """
        CREATE TABLE IF NOT EXISTS knowledge_chunks (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            source_id INTEGER NOT NULL,
            chunk_text TEXT NOT NULL,
            metadata_json TEXT,
            vector_id TEXT,
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            FOREIGN KEY (source_id) REFERENCES data_sources (id)
        );
        """,
        """
        CREATE TABLE IF NOT EXISTS users (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            username TEXT NOT NULL UNIQUE,
            hashed_password TEXT NOT NULL,
            role TEXT NOT NULL DEFAULT 'user',
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
        );
        """,
        """
        CREATE TABLE IF NOT EXISTS user_profiles (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            user_id INTEGER NOT NULL UNIQUE,
            profile_data_json TEXT,
            updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            FOREIGN KEY (user_id) REFERENCES users (id)
        );
        """
    ]
    with get_db_connection() as conn:
        for statement in ddl_statements:
            conn.cursor().execute(statement)
        conn.commit()
    logger.info("Database schema setup complete.")

# --- Crawler-related Helpers ---

def initialize_crawl_jobs():
    """Populates the data_sources table with the initial crawl job start URLs."""
    logger.info("Initializing crawl jobs from config...")
    query = "INSERT OR IGNORE INTO data_sources (name, url, data_type, category, status, created_at) VALUES (?, ?, ?, ?, ?, ?)"
    with get_db_connection() as conn:
        for job in settings.CRAWLING_JOBS:
            conn.cursor().execute(query, (
                job['name'],
                job['start_url'],
                'start_url',
                job.get('domain_lock', 'General'),
                'pending',
                datetime.now()
            ))
        conn.commit()
    logger.info("Crawl jobs initialized successfully.")


def find_or_create_web_source(url: str, name: str) -> int:
    """
    Finds an existing data source by URL or creates a new one if it doesn't exist.
    Used for dynamically ingested web search results.

    Returns:
        The ID of the data source.
    """
    with get_db_connection() as conn:
        cursor = conn.cursor()
        
        # 1. Check if it exists
        cursor.execute("SELECT id FROM data_sources WHERE url = ?", (url,))
        row = cursor.fetchone()
        if row:
            return row['id']
        
        # 2. If not, create it
        logger.info(f"Registering new dynamic web source: {url}")
        insert_query = """
        INSERT INTO data_sources (name, url, data_type, category, status, created_at, updated_at)
        VALUES (?, ?, ?, ?, ?, ?, ?)
        """
        cursor.execute(insert_query, (
            name, url, 'web_search_result', 'Dynamic', 'ingested', datetime.now(), datetime.now()
        ))
        conn.commit()
        return cursor.lastrowid

    
def add_discovered_source(url: str, category: str, data_type: str) -> int:
    """Adds a newly discovered URL to the database if it doesn't exist."""
    with get_db_connection() as conn:
        cursor = conn.cursor()
        cursor.execute("SELECT id FROM data_sources WHERE url = ?", (url,))
        row = cursor.fetchone()
        if row:
            return row['id']
        
        path_name = Path(urlparse(url).path).name
        if not path_name:
            path_name = urlparse(url).path.strip('/').replace('/', '_') or urlparse(url).netloc
        
        insert_query = "INSERT INTO data_sources (name, url, data_type, category, status, created_at) VALUES (?, ?, ?, ?, ?, ?)"
        cursor.execute(insert_query, (path_name, url, data_type, category, 'pending', datetime.now()))
        conn.commit()
        logger.debug(f"Discovered and added new source: {url}")
        return cursor.lastrowid

# --- User Management Helpers ---

def create_user(username: str, hashed_password: str, role: str = 'user') -> Optional[int]:
    """Creates a new user in the database."""
    query = "INSERT INTO users (username, hashed_password, role) VALUES (?, ?, ?)"
    try:
        with get_db_connection() as conn:
            cursor = conn.cursor()
            cursor.execute(query, (username, hashed_password, role))
            conn.commit()
            logger.info(f"User '{username}' created successfully.")
            return cursor.lastrowid
    except sqlite3.IntegrityError:
        logger.warning(f"Attempted to create a user that already exists: {username}")
        return None
    except sqlite3.Error as e:
        logger.error(f"Database error while creating user {username}: {e}")
        return None

def get_user_by_username(username: str) -> Optional[Dict[str, Any]]:
    """Retrieves a user by their username."""
    query = "SELECT * FROM users WHERE username = ?"
    with get_db_connection() as conn:
        cursor = conn.cursor()
        cursor.execute(query, (username,))
        row = cursor.fetchone()
        return dict(row) if row else None

# --- User Profile Helpers ---

def create_or_update_user_profile(user_id: int, profile_data: Dict[str, Any]) -> bool:
    """Creates or updates a user's 360-degree profile."""
    profile_json = json.dumps(profile_data)
    current_time = datetime.now()
    
    # Using INSERT OR REPLACE for simplicity (UPSERT)
    query = """
    INSERT INTO user_profiles (user_id, profile_data_json, updated_at)
    VALUES (?, ?, ?)
    ON CONFLICT(user_id) DO UPDATE SET
        profile_data_json = excluded.profile_data_json,
        updated_at = excluded.updated_at;
    """
    try:
        with get_db_connection() as conn:
            conn.cursor().execute(query, (user_id, profile_json, current_time))
            conn.commit()
        logger.info(f"Profile for user_id {user_id} created/updated.")
        return True
    except sqlite3.Error as e:
        logger.error(f"Database error while updating profile for user_id {user_id}: {e}")
        return False

def get_user_profile(user_id: int) -> Optional[Dict[str, Any]]:
    """Retrieves a user's profile."""
    query = "SELECT profile_data_json FROM user_profiles WHERE user_id = ?"
    with get_db_connection() as conn:
        cursor = conn.cursor()
        cursor.execute(query, (user_id,))
        row = cursor.fetchone()
        if row and row['profile_data_json']:
            return json.loads(row['profile_data_json'])
    return None