All files / apps/web/src/backoffice/components CrawlerResultsModal.jsx

0% Statements 0/54
0% Branches 0/113
0% Functions 0/18
0% Lines 0/46

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
import { useState, useEffect } from 'react'
import { 
  X, Download, Copy, Globe, Link2, Brain, 
  CheckCircle, AlertCircle, Clock, Loader2, MapPin, ExternalLink
} from 'lucide-react'
import toast from 'react-hot-toast'
 
function CrawlerStatsModal({ isOpen, onClose, job }) {
  const [activeTab, setActiveTab] = useState('overview')
  
  const stats = job.stats || {}
  const outputs = job.outputs || []
  const facts = stats.factsExtracted || []
 
  useEffect(() => {
    if (!isOpen) return
    
    const handleEscape = (e) => {
      if (e.key === 'Escape') onClose()
    }
    window.addEventListener('keydown', handleEscape)
    return () => window.removeEventListener('keydown', handleEscape)
  }, [isOpen, onClose])
 
  if (!isOpen || !job) return null
 
  const copyAllFacts = () => {
    const text = facts.map(f => {
      if (typeof f === 'string') return f
      if (f.fact) return f.fact
      return JSON.stringify(f)
    }).join('\n')
    navigator.clipboard.writeText(text)
    toast.success('Faits copiés !')
  }
 
  const downloadFacts = () => {
    const factsOutput = outputs.find(o => o.type === 'facts')
    if (factsOutput?.url) {
      const a = document.createElement('a')
      a.href = factsOutput.url
      a.download = `${job.workflowId}-facts.json`
      a.click()
    }
  }
 
  const getMinioUrl = (url) => {
    if (!url) return url;
    return url.replace('http://minio:9000', 'http://localhost:9000');
  };
 
  const getStatusIcon = (status) => {
    switch (status?.toLowerCase()) {
      case 'completed': return <CheckCircle className="w-5 h-5 text-green-500" />
      case 'failed': return <AlertCircle className="w-5 h-5 text-red-500" />
      case 'running': return <Loader2 className="w-5 h-5 text-yellow-500 animate-spin" />
      default: return <Clock className="w-5 h-5 text-gray-400" />
    }
  }
 
  return (
    <div className="fixed inset-0 z-50 flex items-center justify-center bg-black/60 backdrop-blur-sm p-4">
      <div className="bg-white rounded-xl shadow-2xl max-w-4xl w-full max-h-[85vh] overflow-hidden flex flex-col">
        {/* Header */}
        <div className="flex items-center justify-between p-4 border-b">
          <div className="flex items-center gap-3">
            <div className="w-10 h-10 rounded-full bg-blue-100 flex items-center justify-center">
              <Globe className="w-5 h-5 text-blue-600" />
            </div>
            <div>
              <h3 className="text-lg font-semibold text-gray-900">Résultats du Crawl</h3>
              <p className="text-sm text-gray-500 flex items-center gap-2">
                {getStatusIcon(job.status)}
                <span className="font-mono text-xs">{(job.jobId || job.workflowId)?.substring(0, 20)}...</span>
              </p>
            </div>
          </div>
          <div className="flex items-center gap-2">
            <button 
              onClick={onClose}
              className="p-2 hover:bg-gray-100 rounded-full transition-colors"
            >
              <X className="w-5 h-5 text-gray-500" />
            </button>
          </div>
        </div>
 
        {/* Tabs */}
        <div className="flex border-b">
          <button
            onClick={() => setActiveTab('overview')}
            className={`px-6 py-3 text-sm font-medium transition-colors ${
              activeTab === 'overview'
                ? 'border-b-2 border-blue-500 text-blue-600 bg-blue-50'
                : 'text-gray-500 hover:text-gray-700'
            }`}
          >
            Aperçu
          </button>
          <button
            onClick={() => setActiveTab('facts')}
            className={`px-6 py-3 text-sm font-medium transition-colors ${
              activeTab === 'facts'
                ? 'border-b-2 border-blue-500 text-blue-600 bg-blue-50'
                : 'text-gray-500 hover:text-gray-700'
            }`}
          >
            Faits ({facts.length})
          </button>
          <button
            onClick={() => setActiveTab('outputs')}
            className={`px-6 py-3 text-sm font-medium transition-colors ${
              activeTab === 'outputs'
                ? 'border-b-2 border-blue-500 text-blue-600 bg-blue-50'
                : 'text-gray-500 hover:text-gray-700'
            }`}
          >
            Fichiers
          </button>
        </div>
 
        {/* Content */}
        <div className="flex-1 overflow-auto p-6">
          {activeTab === 'overview' && (
            <div className="space-y-6">
              {/* Input Parameters */}
              {job.input && (
                <div className="bg-blue-50 rounded-lg p-4">
                  <h4 className="font-medium text-blue-900 mb-3 flex items-center gap-2">
                    <Globe className="w-4 h-4" />
                    Paramètres d'entrée
                  </h4>
                  <div className="grid grid-cols-1 gap-3 text-sm">
                    {job.input.startUrls && job.input.startUrls.length > 0 && (
                      <div>
                        <span className="text-blue-700 font-medium">URLs de départ:</span>
                        <div className="mt-1 space-y-1">
                          {job.input.startUrls.map((url, i) => (
                            <a key={i} href={url} target="_blank" rel="noopener noreferrer" 
                               className="block text-blue-900 hover:underline font-mono text-xs">
                              {url}
                            </a>
                          ))}
                        </div>
                      </div>
                    )}
                    {job.input.prompt && (
                      <div>
                        <span className="text-blue-700 font-medium">Prompt:</span>
                        <p className="mt-1 text-blue-900 bg-blue-100 rounded p-2 text-xs">
                          {job.input.prompt}
                        </p>
                      </div>
                    )}
                    {job.input.config && (
                      <div>
                        <span className="text-blue-700 font-medium">Configuration:</span>
                        <div className="mt-1 grid grid-cols-2 gap-2 text-xs bg-blue-100 rounded p-2">
                          {job.input.config.maxPages && (
                            <div>Max pages: <span className="font-medium">{job.input.config.maxPages}</span></div>
                          )}
                          {job.input.config.maxDepth && (
                            <div>Max depth: <span className="font-medium">{job.input.config.maxDepth}</span></div>
                          )}
                          {job.input.config.pageThreshold && (
                            <div>Page threshold: <span className="font-medium">{job.input.config.pageThreshold}</span></div>
                          )}
                          {job.input.config.linkThreshold && (
                            <div>Link threshold: <span className="font-medium">{job.input.config.linkThreshold}</span></div>
                          )}
                        </div>
                      </div>
                    )}
                  </div>
                </div>
              )}
 
              {/* Stats Grid */}
              <div className="grid grid-cols-2 md:grid-cols-4 gap-4">
                <div className="bg-gray-50 rounded-lg p-4">
                  <div className="text-2xl font-bold text-gray-900">{stats.pagesCrawled || 0}</div>
                  <div className="text-sm text-gray-500">Pages crawllées</div>
                </div>
                <div className="bg-green-50 rounded-lg p-4">
                  <div className="text-2xl font-bold text-green-600">{stats.pagesRelevant || 0}</div>
                  <div className="text-sm text-gray-500">Pages pertinentes</div>
                </div>
                <div className="bg-red-50 rounded-lg p-4">
                  <div className="text-2xl font-bold text-red-600">{stats.pagesFailed || 0}</div>
                  <div className="text-sm text-gray-500">Échecs</div>
                </div>
                <div className="bg-blue-50 rounded-lg p-4">
                  <div className="text-2xl font-bold text-blue-600">{stats.linksQueued || 0}</div>
                  <div className="text-sm text-gray-500">Liens en attente</div>
                </div>
              </div>
 
              {/* Additional Stats */}
              <div className="bg-gray-50 rounded-lg p-4">
                <h4 className="font-medium text-gray-900 mb-3">Détails</h4>
                <div className="grid grid-cols-2 gap-4 text-sm">
                  <div>
                    <span className="text-gray-500">Faits extraits:</span>
                    <span className="ml-2 font-medium">{facts.length}</span>
                  </div>
                  <div>
                    <span className="text-gray-500">Synthèses déclenchées:</span>
                    <span className="ml-2 font-medium">{stats.synthesisTriggered || 0}</span>
                  </div>
                  <div>
                    <span className="text-gray-500">Router utilisé:</span>
                    <span className={`ml-2 font-medium ${stats.routerUsed ? 'text-green-600' : 'text-gray-400'}`}>
                      {stats.routerUsed ? 'Oui' : 'Non'}
                    </span>
                  </div>
                  <div>
                    <span className="text-gray-500">Taille de la queue:</span>
                    <span className="ml-2 font-medium">{stats.queueSize || 0}</span>
                  </div>
                  {stats.startedAt && (
                    <div>
                      <span className="text-gray-500">Démarré:</span>
                      <span className="ml-2 font-medium">{new Date(stats.startedAt).toLocaleString()}</span>
                    </div>
                  )}
                  {stats.stopReason && (
                    <div className="col-span-2">
                      <span className="text-gray-500">Raison d'arrêt:</span>
                      <span className={`ml-2 font-medium ${['max_pages_reached', 'queue_empty'].includes(stats.stopReason) ? 'text-green-600' : 'text-red-600'}`}>{stats.stopReason}</span>
                    </div>
                  )}
                </div>
              </div>
 
              {/* Routing Info */}
              {stats.routedTo && stats.routedTo.length > 0 && (
                <div className="bg-purple-50 rounded-lg p-4">
                  <h4 className="font-medium text-purple-900 mb-3 flex items-center gap-2">
                    <Brain className="w-4 h-4" />
                    Routing vers bases de connaissances
                  </h4>
                  <div className="flex flex-wrap gap-2">
                    {stats.routedTo.map((id, i) => (
                      <span key={i} className="px-3 py-1 bg-purple-100 text-purple-800 rounded-full text-sm font-mono">
                        {id}
                      </span>
                    ))}
                  </div>
                </div>
              )}
            </div>
          )}
 
          {activeTab === 'facts' && (
            <div className="space-y-4">
              <div className="flex justify-between items-center">
                <h4 className="font-medium text-gray-900">
                  {facts.length} fait{facts.length !== 1 ? 's' : ''} extrait{facts.length !== 1 ? 's' : ''}
                </h4>
                <div className="flex gap-2">
                  <button 
                    onClick={copyAllFacts}
                    className="px-3 py-2 text-sm text-gray-600 hover:bg-gray-100 rounded-lg flex items-center gap-1"
                  >
                    <Copy className="w-4 h-4" />
                    Copier
                  </button>
                  <button 
                    onClick={downloadFacts}
                    className="px-3 py-2 text-sm text-gray-600 hover:bg-gray-100 rounded-lg flex items-center gap-1"
                  >
                    <Download className="w-4 h-4" />
                    Télécharger
                  </button>
                </div>
              </div>
 
              {facts.length === 0 ? (
                <div className="text-center text-gray-500 py-8">
                  Aucun fait à afficher
                </div>
              ) : (
                <div className="space-y-2 max-h-[50vh] overflow-y-auto">
                  {facts.map((fact, index) => {
                    const factText = typeof fact === 'string' 
                      ? fact 
                      : fact.fact || fact.content || JSON.stringify(fact)
                    const source = typeof fact === 'object' ? fact.source : null
                    
                    return (
                      <div 
                        key={index}
                        className="p-4 bg-gray-50 rounded-lg border-l-4 border-blue-500 hover:bg-gray-100 transition-colors"
                      >
                        <p className="text-gray-800 whitespace-pre-wrap">{factText}</p>
                        {source && (
                          <div className="mt-2 flex items-center gap-1 text-xs text-gray-500">
                            <Link2 className="w-3 h-3" />
                            <span className="truncate max-w-md">{source}</span>
                          </div>
                        )}
                      </div>
                    )
                  })}
                </div>
              )}
            </div>
          )}
 
          {activeTab === 'outputs' && (
            <div className="space-y-4">
              <h4 className="font-medium text-gray-900">Fichiers générés</h4>
              
              {outputs.length === 0 ? (
                <div className="text-center text-gray-500 py-8">
                  Aucun fichier disponible
                </div>
              ) : (
                <div className="space-y-3">
                  {outputs.map((output, index) => (
                    <div 
                      key={index}
                      className="flex items-center justify-between p-4 bg-gray-50 rounded-lg hover:bg-gray-100 transition-colors"
                    >
                      <div className="flex items-center gap-3">
                        <div className={`w-10 h-10 rounded-lg flex items-center justify-center ${
                          output.type === 'facts' ? 'bg-purple-100 text-purple-600' :
                          output.type === 'routing' ? 'bg-orange-100 text-orange-600' :
                          'bg-blue-100 text-blue-600'
                        }`}>
                          {output.type === 'facts' ? <Brain className="w-5 h-5" /> :
                           output.type === 'routing' ? <MapPin className="w-5 h-5" /> :
                           <Download className="w-5 h-5" />}
                        </div>
                        <div>
                          <div className="font-medium text-gray-900 capitalize">{output.type}</div>
                          <div className="text-xs text-gray-500">
                            {output.format?.toUpperCase()} • {output.bucket}
                          </div>
                        </div>
                      </div>
                      <div className="flex items-center gap-2">
                        {output.url && (
                          <a
                            href={getMinioUrl(output.url)}
                            target="_blank"
                            rel="noopener noreferrer"
                            className="p-2 hover:bg-gray-200 rounded-lg transition-colors"
                            title="Ouvrir"
                          >
                            <ExternalLink className="w-4 h-4 text-gray-600" />
                          </a>
                        )}
                        {output.url && (
                          <button
                            onClick={() => {
                              navigator.clipboard.writeText(getMinioUrl(output.url))
                              toast.success('URL copiée !')
                            }}
                            className="p-2 hover:bg-gray-200 rounded-lg transition-colors"
                            title="Copier l'URL"
                          >
                            <Copy className="w-4 h-4 text-gray-600" />
                          </button>
                        )}
                        {output.url && (
                          <a
                            href={getMinioUrl(output.url)}
                            download
                            className="p-2 hover:bg-gray-200 rounded-lg transition-colors"
                            title="Télécharger"
                          >
                            <Download className="w-4 h-4 text-gray-600" />
                          </a>
                        )}
                      </div>
                    </div>
                  ))}
                </div>
              )}
            </div>
          )}
        </div>
 
        {/* Footer */}
        <div className="p-3 bg-gray-50 border-t text-center text-sm text-gray-500">
          Appuyez sur Echap pour fermer
        </div>
      </div>
    </div>
  )
}
 
export default CrawlerStatsModal