File size: 12,589 Bytes
ea9303b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
"""
SysCRED Storage Module - SQLite + Supabase
==========================================
Stocke les triplets RDF et résultats d'analyse.
Utilise SQLite localement, avec option de sync vers Supabase.
"""

import os
import sqlite3
import hashlib
import json
from datetime import datetime
from typing import Optional, Dict, Any, List, Tuple
from urllib.parse import urlparse
from pathlib import Path

# Chemins
BASE_DIR = Path(__file__).parent
DB_PATH = BASE_DIR / "syscred_local.db"

class SysCREDStore:
    """
    Gestionnaire de stockage pour SysCRED.
    SQLite local avec option Supabase.
    """
    
    def __init__(self, db_path: str = None, supabase_url: str = None):
        self.db_path = db_path or str(DB_PATH)
        self.supabase_url = supabase_url or os.getenv("DATABASE_URL")
        self.conn = None
        self._init_local_db()
        
    def _init_local_db(self):
        """Initialise la base SQLite locale."""
        self.conn = sqlite3.connect(self.db_path, check_same_thread=False)
        self.conn.row_factory = sqlite3.Row
        
        # Créer les tables
        self.conn.executescript("""
            -- Résultats d'analyse
            CREATE TABLE IF NOT EXISTS analysis_results (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                url TEXT NOT NULL,
                credibility_score REAL NOT NULL,
                summary TEXT,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                source_reputation TEXT,
                fact_check_count INTEGER DEFAULT 0,
                score_details TEXT,
                domain TEXT
            );
            
            -- Triplets RDF
            CREATE TABLE IF NOT EXISTS rdf_triples (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                subject TEXT NOT NULL,
                predicate TEXT NOT NULL,
                object TEXT NOT NULL,
                object_type TEXT DEFAULT 'uri',
                graph_name TEXT DEFAULT 'data',
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                UNIQUE(subject, predicate, object, graph_name)
            );
            
            -- Sources
            CREATE TABLE IF NOT EXISTS sources (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                domain TEXT UNIQUE NOT NULL,
                reputation_score REAL,
                domain_age_years REAL,
                is_fact_checker INTEGER DEFAULT 0,
                analysis_count INTEGER DEFAULT 0,
                last_analyzed TIMESTAMP,
                metadata TEXT,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
            );
            
            -- Claims
            CREATE TABLE IF NOT EXISTS claims (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                claim_text TEXT NOT NULL,
                claim_hash TEXT UNIQUE,
                source_url TEXT,
                extracted_entities TEXT,
                credibility_score REAL,
                verification_status TEXT DEFAULT 'unverified',
                evidence_count INTEGER DEFAULT 0,
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
            );
            
            -- Evidence
            CREATE TABLE IF NOT EXISTS evidence (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                claim_id INTEGER,
                doc_id TEXT,
                doc_text TEXT,
                relevance_score REAL,
                retrieval_method TEXT DEFAULT 'bm25',
                created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                FOREIGN KEY (claim_id) REFERENCES claims(id)
            );
            
            -- Index
            CREATE INDEX IF NOT EXISTS idx_analysis_url ON analysis_results(url);
            CREATE INDEX IF NOT EXISTS idx_triple_subject ON rdf_triples(subject);
            CREATE INDEX IF NOT EXISTS idx_triple_graph ON rdf_triples(graph_name);
            CREATE INDEX IF NOT EXISTS idx_sources_domain ON sources(domain);
        """)
        self.conn.commit()
        print(f"[SysCREDStore] SQLite initialisé: {self.db_path}")
    
    # =========================================================================
    # ONTOLOGY / RDF TRIPLES
    # =========================================================================
    
    def sync_ontology(self, ontology_manager) -> Dict[str, int]:
        """
        Synchronise les graphes RDFLib vers SQLite.
        
        Args:
            ontology_manager: Instance avec base_graph et data_graph
        """
        result = {'base_synced': 0, 'data_synced': 0}
        
        try:
            # Sync base ontology
            if hasattr(ontology_manager, 'base_graph') and ontology_manager.base_graph:
                result['base_synced'] = self._sync_graph(
                    ontology_manager.base_graph, 
                    graph_name='base'
                )
            
            # Sync data graph
            if hasattr(ontology_manager, 'data_graph') and ontology_manager.data_graph:
                result['data_synced'] = self._sync_graph(
                    ontology_manager.data_graph,
                    graph_name='data'
                )
            
            self.conn.commit()
            print(f"[SysCREDStore] Synced {result['base_synced']} base + {result['data_synced']} data triples")
            
        except Exception as e:
            result['error'] = str(e)
            print(f"[SysCREDStore] Sync error: {e}")
        
        return result
    
    def _sync_graph(self, graph, graph_name: str) -> int:
        """Sync un graphe RDFLib vers SQLite."""
        from rdflib import Literal
        
        count = 0
        cursor = self.conn.cursor()
        
        for s, p, o in graph:
            subject = str(s)
            predicate = str(p)
            obj_value = str(o)
            obj_type = 'literal' if isinstance(o, Literal) else 'uri'
            
            try:
                cursor.execute("""
                    INSERT OR IGNORE INTO rdf_triples 
                    (subject, predicate, object, object_type, graph_name)
                    VALUES (?, ?, ?, ?, ?)
                """, (subject, predicate, obj_value, obj_type, graph_name))
                count += 1
            except:
                pass
        
        return count
    
    def get_triple_stats(self) -> Dict[str, int]:
        """Statistiques des triplets."""
        cursor = self.conn.cursor()
        
        cursor.execute("SELECT COUNT(*) FROM rdf_triples WHERE graph_name = 'base'")
        base = cursor.fetchone()[0]
        
        cursor.execute("SELECT COUNT(*) FROM rdf_triples WHERE graph_name = 'data'")
        data = cursor.fetchone()[0]
        
        return {
            'base_triples': base,
            'data_triples': data,
            'total_triples': base + data
        }
    
    # =========================================================================
    # ANALYSIS RESULTS
    # =========================================================================
    
    def save_analysis(self, url: str, credibility_score: float,
                      summary: str = None, score_details: Dict = None,
                      source_reputation: str = None, fact_check_count: int = 0) -> int:
        """Sauvegarde un résultat d'analyse."""
        domain = urlparse(url).netloc
        
        cursor = self.conn.cursor()
        cursor.execute("""
            INSERT INTO analysis_results 
            (url, credibility_score, summary, score_details, source_reputation, 
             fact_check_count, domain)
            VALUES (?, ?, ?, ?, ?, ?, ?)
        """, (
            url, credibility_score, summary,
            json.dumps(score_details) if score_details else None,
            source_reputation, fact_check_count, domain
        ))
        self.conn.commit()
        
        result_id = cursor.lastrowid
        print(f"[SysCREDStore] Saved analysis #{result_id} for {domain}")
        
        # Update source stats
        self._update_source(domain, credibility_score)
        
        return result_id
    
    def get_history(self, url: str = None, limit: int = 50) -> List[Dict]:
        """Récupère l'historique des analyses."""
        cursor = self.conn.cursor()
        
        if url:
            cursor.execute("""
                SELECT * FROM analysis_results
                WHERE url = ? ORDER BY created_at DESC LIMIT ?
            """, (url, limit))
        else:
            cursor.execute("""
                SELECT * FROM analysis_results
                ORDER BY created_at DESC LIMIT ?
            """, (limit,))
        
        return [dict(row) for row in cursor.fetchall()]
    
    # =========================================================================
    # SOURCES
    # =========================================================================
    
    def _update_source(self, domain: str, score: float = None):
        """Met à jour les stats d'une source."""
        cursor = self.conn.cursor()
        
        cursor.execute("SELECT id, analysis_count FROM sources WHERE domain = ?", (domain,))
        row = cursor.fetchone()
        
        if row:
            cursor.execute("""
                UPDATE sources SET 
                    analysis_count = analysis_count + 1,
                    last_analyzed = CURRENT_TIMESTAMP,
                    reputation_score = COALESCE(?, reputation_score)
                WHERE domain = ?
            """, (score, domain))
        else:
            cursor.execute("""
                INSERT INTO sources (domain, reputation_score, analysis_count, last_analyzed)
                VALUES (?, ?, 1, CURRENT_TIMESTAMP)
            """, (domain, score))
        
        self.conn.commit()
    
    def get_source(self, domain: str) -> Optional[Dict]:
        """Récupère les infos d'une source."""
        cursor = self.conn.cursor()
        cursor.execute("SELECT * FROM sources WHERE domain = ?", (domain,))
        row = cursor.fetchone()
        return dict(row) if row else None
    
    # =========================================================================
    # GLOBAL STATS
    # =========================================================================
    
    def get_stats(self) -> Dict[str, Any]:
        """Statistiques globales."""
        cursor = self.conn.cursor()
        
        cursor.execute("SELECT COUNT(*) FROM analysis_results")
        total_analyses = cursor.fetchone()[0]
        
        cursor.execute("SELECT COUNT(*) FROM sources")
        unique_domains = cursor.fetchone()[0]
        
        cursor.execute("SELECT AVG(credibility_score) FROM analysis_results")
        avg_score = cursor.fetchone()[0]
        
        triple_stats = self.get_triple_stats()
        
        return {
            'total_analyses': total_analyses,
            'unique_domains': unique_domains,
            'avg_credibility': round(avg_score, 2) if avg_score else None,
            **triple_stats
        }
    
    def close(self):
        """Ferme la connexion."""
        if self.conn:
            self.conn.close()


# ============================================================================
# INTEGRATION
# ============================================================================

def sync_ontology_to_db():
    """Synchronise l'ontologie vers la base de données."""
    import sys
    sys.path.insert(0, str(BASE_DIR))
    
    try:
        from ontology_manager import OntologyManager
        from config import Config
        
        # Init ontology
        onto = OntologyManager(
            base_ontology_path=str(Config.ONTOLOGY_BASE_PATH),
            data_path=str(Config.ONTOLOGY_DATA_PATH)
        )
        
        # Init store
        store = SysCREDStore()
        
        # Sync
        result = store.sync_ontology(onto)
        print(f"\n✅ Sync complete: {result}")
        
        # Stats
        stats = store.get_stats()
        print(f"📊 Stats: {stats}")
        
        return store
        
    except ImportError as e:
        print(f"Import error: {e}")
        return None


# ============================================================================
# CLI
# ============================================================================

if __name__ == "__main__":
    print("=" * 60)
    print("SysCRED Storage - Synchronisation des triplets")
    print("=" * 60)
    
    store = sync_ontology_to_db()
    
    if store:
        print("\n✅ Base de données prête!")
        print(f"   Fichier: {store.db_path}")