All files / apps/web/src/backoffice/pages CrawlerJobs.jsx

0% Statements 0/34
0% Branches 0/30
0% Functions 0/13
0% Lines 0/33

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
import { useState } from 'react';
import { useSearchParams } from 'react-router-dom';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import {
  Search,
  Filter,
  Play,
  Square,
  Trash2,
  RefreshCw,
  Eye,
  Download,
  Clock,
  CheckCircle,
  XCircle,
  AlertCircle,
  Bug,
  Globe
} from 'lucide-react';
import api from '../../services/api';
import toast from 'react-hot-toast';
 
const StatusBadge = ({ status }) => {
  const config = {
    pending: { color: 'bg-blue-100 text-blue-800', icon: Clock },
    running: { color: 'bg-yellow-100 text-yellow-800', icon: RefreshCw },
    completed: { color: 'bg-green-100 text-green-800', icon: CheckCircle },
    failed: { color: 'bg-red-100 text-red-800', icon: XCircle },
    cancelled: { color: 'bg-gray-100 text-gray-800', icon: AlertCircle }
  };
 
  const { color, icon: Icon } = config[status] || config.pending;
 
  return (
    <span className={`inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium ${color}`}>
      <Icon className="w-3 h-3 mr-1" />
      {status}
    </span>
  );
};
 
export default function CrawlerJobs() {
  const [searchParams] = useSearchParams();
  const configId = searchParams.get('configId');
  const queryClient = useQueryClient();
  const [searchTerm, setSearchTerm] = useState('');
  const [statusFilter, setStatusFilter] = useState('all');
 
  const { data: jobs, isLoading, refetch } = useQuery({
    queryKey: ['crawler-jobs', configId],
    queryFn: async () => {
      const params = new URLSearchParams();
      if (configId) params.append('configId', configId);
      params.append('limit', '100');
      
      const response = await api.get(`/jobs?${params.toString()}`);
      return response.data.data?.jobs || [];
    },
    refetchInterval: 5000
  });
 
  const cancelJob = useMutation({
    mutationFn: async (jobId) => {
      return api.delete(`/jobs/${jobId}`);
    },
    onSuccess: () => {
      toast.success('Job annulé');
      queryClient.invalidateQueries(['crawler-jobs']);
    },
    onError: (error) => {
      toast.error(error.response?.data?.message || 'Erreur lors de l\'annulation');
    }
  });
 
  const filteredJobs = jobs?.filter(job => {
    const matchesSearch = 
      job.workflowId?.toLowerCase().includes(searchTerm.toLowerCase()) ||
      job.type?.toLowerCase().includes(searchTerm.toLowerCase());
    
    const matchesStatus = statusFilter === 'all' || job.status?.toLowerCase() === statusFilter;
    
    return matchesSearch && matchesStatus;
  });
 
  if (isLoading) {
    return (
      <div className="flex items-center justify-center h-64">
        <div className="animate-spin rounded-full h-12 w-12 border-b-2 border-indigo-600"></div>
      </div>
    );
  }
 
  return (
    <div className="space-y-6">
      <div className="flex items-center justify-between">
        <div>
          <h2 className="text-2xl font-bold text-gray-900">Jobs de Crawl</h2>
          <p className="text-gray-600">{filteredJobs?.length || 0} jobs (total: {jobs?.length || 0})</p>
        </div>
        <button
          onClick={() => refetch()}
          className="flex items-center px-4 py-2 bg-gray-100 text-gray-700 rounded-lg hover:bg-gray-200"
        >
          <RefreshCw className="w-5 h-5 mr-2" />
          Actualiser
        </button>
      </div>
 
      <div className="flex flex-col sm:flex-row gap-4">
        <div className="relative flex-1">
          <Search className="absolute left-3 top-1/2 transform -translate-y-1/2 text-gray-400 w-5 h-5" />
          <input
            type="text"
            placeholder="Rechercher un job..."
            value={searchTerm}
            onChange={(e) => setSearchTerm(e.target.value)}
            className="w-full pl-10 pr-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-indigo-500"
          />
        </div>
        <div className="flex items-center space-x-2">
          <Filter className="w-5 h-5 text-gray-400" />
          <select
            value={statusFilter}
            onChange={(e) => setStatusFilter(e.target.value)}
            className="border border-gray-300 rounded-lg px-4 py-2 focus:ring-2 focus:ring-indigo-500"
          >
            <option value="all">Tous</option>
            <option value="pending">En attente</option>
            <option value="running">En cours</option>
            <option value="completed">Terminés</option>
            <option value="failed">Échoués</option>
            <option value="cancelled">Annulés</option>
            <option value="terminated">Terminés</option>
          </select>
        </div>
      </div>
 
      <div className="bg-white rounded-lg shadow overflow-hidden">
        <div className="overflow-x-auto">
          <table className="min-w-full divide-y divide-gray-200">
            <thead className="bg-gray-50">
              <tr>
                <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">
                  Job ID
                </th>
                <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">
                  Config
                </th>
                <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">
                  Statut
                </th>
                <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">
                  Progression
                </th>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">
                   Début
                 </th>
                 <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">
                   Fin
                 </th>
                <th className="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">
                  Actions
                </th>
              </tr>
            </thead>
            <tbody className="bg-white divide-y divide-gray-200">
              {filteredJobs?.map((job) => (
                <tr key={job.workflowId} className="hover:bg-gray-50">
                  <td className="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">
                    {job.workflowId?.substring(0, 12)}...
                  </td>
                  <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                    <div className="flex items-center">
                      <Bug className="w-4 h-4 mr-1 text-indigo-400" />
                      {job.taskQueue || job.type}
                    </div>
                  </td>
                  <td className="px-6 py-4 whitespace-nowrap">
                    <StatusBadge status={job.status} />
                  </td>
                  <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                    -
                  </td>
                  <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                    {job.startTime ? new Date(job.startTime).toLocaleString() : '-'}
                  </td>
                  <td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
                    {job.closeTime ? new Date(job.closeTime).toLocaleString() : '-'}
                  </td>
                  <td className="px-6 py-4 whitespace-nowrap text-sm font-medium">
                    <div className="flex items-center space-x-2">
                      <button
                        onClick={() => {
                          toast.success('Voir les résultats via /backoffice/jobs')
                        }}
                        className="text-indigo-600 hover:text-indigo-900 p-1"
                        title="Voir"
                      >
                        <Eye className="w-4 h-4" />
                      </button>
                      {job.status === 'RUNNING' && (
                        <button
                          onClick={() => cancelJob.mutate(job.workflowId)}
                          className="text-yellow-600 hover:text-yellow-900 p-1"
                          title="Arrêter"
                        >
                          <Square className="w-4 h-4" />
                        </button>
                      )}
                    </div>
                  </td>
                </tr>
              ))}
            </tbody>
          </table>
        </div>
      </div>
 
      {filteredJobs?.length === 0 && (
        <div className="text-center py-12 bg-gray-50 rounded-lg">
          <Globe className="w-12 h-12 text-gray-400 mx-auto mb-4" />
          <p className="text-gray-500">Aucun job de crawl trouvé</p>
        </div>
      )}
    </div>
  );
}