File size: 14,549 Bytes
75f48fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8a3c0e
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
"""

Prisma Client Integration for NAVADA 2.0

Provides enhanced database operations with Prisma ORM

"""

import asyncio
import json
import base64
import logging
from typing import List, Dict, Optional, Any
from datetime import datetime
import numpy as np
import cv2

logger = logging.getLogger(__name__)

class PrismaManager:
    """Enhanced database manager using Prisma ORM"""

    def __init__(self):
        self.client = None
        self._init_client()

    def _init_client(self):
        """Initialize Prisma client"""
        try:
            # Import Prisma client (needs to be generated first)
            # from prisma import Prisma
            # self.client = Prisma()
            logger.info("Prisma client initialized")
        except ImportError:
            logger.warning("Prisma client not available - run 'npm run prisma:generate'")
            self.client = None
        except Exception as e:
            logger.error(f"Failed to initialize Prisma client: {e}")
            self.client = None

    async def connect(self):
        """Connect to database"""
        if self.client:
            try:
                await self.client.connect()
                logger.info("Connected to database via Prisma")
                return True
            except Exception as e:
                logger.error(f"Failed to connect to database: {e}")
                return False
        return False

    async def disconnect(self):
        """Disconnect from database"""
        if self.client:
            try:
                await self.client.disconnect()
                logger.info("Disconnected from database")
            except Exception as e:
                logger.error(f"Error disconnecting: {e}")

    # Document Management for Knowledge Retrieval
    async def add_document(self, title: str, content: str, content_type: str = "text",

                          tags: List[str] = None, category: str = None,

                          image_data: bytes = None, image_url: str = None) -> Optional[int]:
        """

        Add document for knowledge retrieval



        Args:

            title: Document title

            content: Document content (text)

            content_type: "text", "image", "mixed"

            tags: List of tags

            category: Document category

            image_data: Binary image data

            image_url: URL to image



        Returns:

            Document ID if successful

        """
        if not self.client:
            return None

        try:
            tags_str = json.dumps(tags) if tags else None

            document = await self.client.document.create(
                data={
                    'title': title,
                    'content': content,
                    'contentType': content_type,
                    'tags': tags_str,
                    'category': category,
                    'imageData': image_data,
                    'imageUrl': image_url
                }
            )

            # Create document chunks for better retrieval
            await self._create_document_chunks(document.id, content)

            logger.info(f"Added document: {title} (ID: {document.id})")
            return document.id

        except Exception as e:
            logger.error(f"Failed to add document: {e}")
            return None

    async def _create_document_chunks(self, document_id: int, content: str, chunk_size: int = 500):
        """Create chunks from document content for better retrieval"""
        if not self.client:
            return

        try:
            # Split content into chunks
            chunks = [content[i:i+chunk_size] for i in range(0, len(content), chunk_size)]

            for i, chunk in enumerate(chunks):
                await self.client.documentchunk.create(
                    data={
                        'documentId': document_id,
                        'chunkIndex': i,
                        'content': chunk
                    }
                )

        except Exception as e:
            logger.error(f"Failed to create document chunks: {e}")

    async def search_documents(self, query: str, content_type: str = None,

                             category: str = None, limit: int = 10) -> List[Dict]:
        """

        Search documents by content, tags, or category



        Args:

            query: Search query

            content_type: Filter by content type

            category: Filter by category

            limit: Maximum results



        Returns:

            List of matching documents

        """
        if not self.client:
            return []

        try:
            where_clause = {
                'isActive': True,
                'OR': [
                    {'title': {'contains': query}},
                    {'content': {'contains': query}},
                    {'tags': {'contains': query}}
                ]
            }

            if content_type:
                where_clause['contentType'] = content_type
            if category:
                where_clause['category'] = category

            documents = await self.client.document.find_many(
                where=where_clause,
                take=limit,
                order_by={'createdAt': 'desc'}
            )

            return [self._document_to_dict(doc) for doc in documents]

        except Exception as e:
            logger.error(f"Document search failed: {e}")
            return []

    def _document_to_dict(self, document) -> Dict:
        """Convert Prisma document to dictionary"""
        return {
            'id': document.id,
            'title': document.title,
            'content': document.content,
            'content_type': document.contentType,
            'tags': json.loads(document.tags) if document.tags else [],
            'category': document.category,
            'image_url': document.imageUrl,
            'created_at': document.createdAt,
            'updated_at': document.updatedAt
        }

    # Media File Management
    async def add_media_file(self, filename: str, filepath: str, mime_type: str,

                           file_size: int, image_data: bytes = None,

                           description: str = None, tags: List[str] = None) -> Optional[int]:
        """Add media file to database"""
        if not self.client:
            return None

        try:
            tags_str = json.dumps(tags) if tags else None

            media_file = await self.client.mediafile.create(
                data={
                    'filename': filename,
                    'filepath': filepath,
                    'mimeType': mime_type,
                    'fileSize': file_size,
                    'imageData': image_data,
                    'description': description,
                    'tags': tags_str
                }
            )

            logger.info(f"Added media file: {filename} (ID: {media_file.id})")
            return media_file.id

        except Exception as e:
            logger.error(f"Failed to add media file: {e}")
            return None

    async def get_media_files(self, tags: List[str] = None, mime_type: str = None,

                            limit: int = 50) -> List[Dict]:
        """Get media files with optional filtering"""
        if not self.client:
            return []

        try:
            where_clause = {'isActive': True}

            if mime_type:
                where_clause['mimeType'] = {'contains': mime_type}

            if tags:
                # Search for any of the provided tags
                tag_conditions = [{'tags': {'contains': tag}} for tag in tags]
                where_clause['OR'] = tag_conditions

            media_files = await self.client.mediafile.find_many(
                where=where_clause,
                take=limit,
                order_by={'createdAt': 'desc'}
            )

            return [self._media_file_to_dict(file) for file in media_files]

        except Exception as e:
            logger.error(f"Failed to get media files: {e}")
            return []

    def _media_file_to_dict(self, media_file) -> Dict:
        """Convert Prisma media file to dictionary"""
        return {
            'id': media_file.id,
            'filename': media_file.filename,
            'filepath': media_file.filepath,
            'mime_type': media_file.mimeType,
            'file_size': media_file.fileSize,
            'description': media_file.description,
            'tags': json.loads(media_file.tags) if media_file.tags else [],
            'created_at': media_file.createdAt
        }

    # Enhanced Knowledge Base Operations
    async def add_knowledge_entry(self, entity_type: str, entity_id: int, content: str,

                                title: str = None, description: str = None,

                                tags: List[str] = None, category: str = None,

                                image_url: str = None, text_content: str = None) -> Optional[int]:
        """Add enhanced knowledge base entry"""
        if not self.client:
            return None

        try:
            keywords_str = json.dumps(tags) if tags else None

            knowledge_entry = await self.client.knowledgebase.create(
                data={
                    'entityType': entity_type,
                    'entityId': entity_id,
                    'content': content,
                    'title': title,
                    'description': description,
                    'keywords': keywords_str,
                    'category': category,
                    'imageUrl': image_url,
                    'textContent': text_content
                }
            )

            logger.info(f"Added knowledge entry: {title or content[:50]}")
            return knowledge_entry.id

        except Exception as e:
            logger.error(f"Failed to add knowledge entry: {e}")
            return None

    async def search_knowledge(self, query: str, entity_type: str = None,

                             category: str = None, limit: int = 10) -> List[Dict]:
        """Enhanced knowledge search"""
        if not self.client:
            return []

        try:
            where_clause = {
                'OR': [
                    {'content': {'contains': query}},
                    {'title': {'contains': query}},
                    {'description': {'contains': query}},
                    {'keywords': {'contains': query}},
                    {'textContent': {'contains': query}}
                ]
            }

            if entity_type:
                where_clause['entityType'] = entity_type
            if category:
                where_clause['category'] = category

            entries = await self.client.knowledgebase.find_many(
                where=where_clause,
                take=limit,
                order_by={'createdAt': 'desc'}
            )

            return [self._knowledge_to_dict(entry) for entry in entries]

        except Exception as e:
            logger.error(f"Knowledge search failed: {e}")
            return []

    def _knowledge_to_dict(self, entry) -> Dict:
        """Convert Prisma knowledge entry to dictionary"""
        return {
            'id': entry.id,
            'entity_type': entry.entityType,
            'entity_id': entry.entityId,
            'content': entry.content,
            'title': entry.title,
            'description': entry.description,
            'keywords': json.loads(entry.keywords) if entry.keywords else [],
            'category': entry.category,
            'image_url': entry.imageUrl,
            'text_content': entry.textContent,
            'created_at': entry.createdAt,
            'updated_at': entry.updatedAt
        }

    # Statistics and Analytics
    async def get_enhanced_stats(self) -> Dict:
        """Get comprehensive database statistics"""
        if not self.client:
            return {}

        try:
            stats = {}

            # Basic counts
            stats['faces'] = await self.client.face.count(where={'isActive': True})
            stats['objects'] = await self.client.object.count(where={'isActive': True})
            stats['documents'] = await self.client.document.count(where={'isActive': True})
            stats['media_files'] = await self.client.mediafile.count(where={'isActive': True})
            stats['knowledge_entries'] = await self.client.knowledgebase.count()
            stats['training_corrections'] = await self.client.trainingcorrection.count()

            # Recent activity (last 7 days)
            seven_days_ago = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
            stats['recent_detections'] = await self.client.detectionhistory.count(
                where={'createdAt': {'gte': seven_days_ago}}
            )

            return stats

        except Exception as e:
            logger.error(f"Failed to get enhanced stats: {e}")
            return {}

# Global Prisma manager instance
prisma_manager = PrismaManager()

# Helper functions for async operations in Streamlit
def run_async(coro):
    """Run async function in Streamlit"""
    try:
        loop = asyncio.get_event_loop()
    except RuntimeError:
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)

    return loop.run_until_complete(coro)

# Convenience functions
def add_document_sync(title: str, content: str, **kwargs) -> Optional[int]:
    """Synchronous wrapper for adding documents"""
    return run_async(prisma_manager.add_document(title, content, **kwargs))

def search_documents_sync(query: str, **kwargs) -> List[Dict]:
    """Synchronous wrapper for searching documents"""
    return run_async(prisma_manager.search_documents(query, **kwargs))

def add_media_file_sync(filename: str, filepath: str, mime_type: str,

                       file_size: int, **kwargs) -> Optional[int]:
    """Synchronous wrapper for adding media files"""
    return run_async(prisma_manager.add_media_file(filename, filepath, mime_type, file_size, **kwargs))

def get_enhanced_stats_sync() -> Dict:
    """Synchronous wrapper for getting stats"""
    return run_async(prisma_manager.get_enhanced_stats())