All files / frontend/src/pages RecordingSession.tsx

60.13% Statements 184/306
56.25% Branches 9/16
37.5% Functions 3/8
60.13% Lines 184/306

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 3581x 1x 1x 1x     1x 19x 19x 19x 19x 19x 19x   19x   19x 19x 19x 19x 19x 19x 19x   19x 19x 19x   19x 17x 17x 19x   19x 19x   19x                                                                                   19x                       19x 19x   19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x     19x             19x   19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 1x 1x 1x 1x 1x 1x 1x 19x   19x 19x 38x 38x 38x 19x 19x 19x   19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x   19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x   19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x   19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x   19x                                                 19x 19x 19x 19x 19x 19x 19x   19x           19x 19x   19x   19x 19x 19x 19x 19x   19x   19x 19x 19x 19x     19x   19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x     19x                                                                                                                 19x 19x 19x   19x   1x  
import React, { useEffect, useState } from 'react';
import { useSessionStore } from '../store/sessionStore';
import { useWebSDRStore } from '../store/websdrStore';
import { acquisitionService } from '../services/api';
import type { AcquisitionStatusResponse } from '../services/api/types';
 
const RecordingSession: React.FC = () => {
    const {
        knownSources,
        fetchKnownSources,
        createSession,
        error: sessionError,
    } = useSessionStore();
 
    const { healthStatus, fetchWebSDRs } = useWebSDRStore();
 
    const [formData, setFormData] = useState({
        knownSourceId: '',
        sessionName: '',
        frequency: '',
        duration: 60,
        notes: '',
    });
 
    const [acquisitionStatus, setAcquisitionStatus] = useState<AcquisitionStatusResponse | null>(null);
    const [isAcquiring, setIsAcquiring] = useState(false);
    const [currentTaskId, setCurrentTaskId] = useState<string | null>(null);
 
    useEffect(() => {
        fetchKnownSources();
        fetchWebSDRs();
    }, [fetchKnownSources, fetchWebSDRs]);
 
    const selectedSource = knownSources.find(s => s.id === formData.knownSourceId);
    const onlineWebSDRs = Object.values(healthStatus).filter(h => h.status === 'online').length;
 
    const handleStartAcquisition = async () => {
        if (!formData.knownSourceId || !formData.sessionName) {
            alert('Please select a source and enter a session name');
            return;
        }
 
        try {
            setIsAcquiring(true);
 
            // Create session in database
            await createSession({
                session_name: formData.sessionName,
                frequency_mhz: parseFloat(formData.frequency),
                duration_seconds: formData.duration,
                notes: formData.notes,
            });
 
            // Trigger acquisition
            const acquisitionResponse = await acquisitionService.triggerAcquisition({
                frequency_mhz: parseFloat(formData.frequency),
                duration_seconds: formData.duration,
            });
 
            setCurrentTaskId(acquisitionResponse.task_id);
 
            // Poll status
            const finalStatus = await acquisitionService.pollAcquisitionStatus(
                acquisitionResponse.task_id,
                (status) => {
                    setAcquisitionStatus(status);
                }
            );
 
            setAcquisitionStatus(finalStatus);
        } catch (error) {
            console.error('Acquisition failed:', error);
            alert('Failed to start acquisition');
        } finally {
            setIsAcquiring(false);
        }
    };
 
    const handleReset = () => {
        setFormData({
            knownSourceId: '',
            sessionName: '',
            frequency: '',
            duration: 60,
            notes: '',
        });
        setAcquisitionStatus(null);
        setCurrentTaskId(null);
    };
 
    return (
        <>
            {/* Breadcrumb */}
            <div className="page-header">
                <div className="page-block">
                    <div className="row align-items-center">
                        <div className="col-md-12">
                            <ul className="breadcrumb">
                                <li className="breadcrumb-item"><a href="/dashboard">Home</a></li>
                                <li className="breadcrumb-item"><a href="#">RF Operations</a></li>
                                <li className="breadcrumb-item" aria-current="page">Recording Session</li>
                            </ul>
                        </div>
                        <div className="col-md-12">
                            <div className="page-header-title">
                                <h2 className="mb-0">Create Recording Session</h2>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
 
            {/* Error Alert */}
            {sessionError && (
                <div className="alert alert-danger alert-dismissible fade show" role="alert">
                    <strong>Error!</strong> {sessionError}
                    <button type="button" className="btn-close" data-bs-dismiss="alert"></button>
                </div>
            )}
 
            <div className="row">
                {/* Session Configuration */}
                <div className="col-lg-8">
                    <div className="card">
                        <div className="card-header">
                            <h5 className="mb-0">Session Configuration</h5>
                        </div>
                        <div className="card-body">
                            <div className="row g-3">
                                <div className="col-12">
                                    <label className="form-label">Known Source *</label>
                                    <select
                                        className="form-select"
                                        value={formData.knownSourceId}
                                        onChange={(e) => {
                                            const source = knownSources.find(s => s.id === e.target.value);
                                            setFormData({
                                                ...formData,
                                                knownSourceId: e.target.value,
                                                frequency: source ? (source.frequency_hz / 1e6).toString() : '',
                                            });
                                        }}
                                        disabled={isAcquiring}
                                    >
                                        <option value="">Select a source...</option>
                                        {knownSources.map((source) => (
                                            <option key={source.id} value={source.id}>
                                                {source.name} - {(source.frequency_hz / 1e6).toFixed(3)} MHz
                                            </option>
                                        ))}
                                    </select>
                                </div>
 
                                <div className="col-md-6">
                                    <label className="form-label">Session Name *</label>
                                    <input
                                        type="text"
                                        className="form-control"
                                        value={formData.sessionName}
                                        onChange={(e) => setFormData({ ...formData, sessionName: e.target.value })}
                                        placeholder="e.g., Beacon Recording 2024-10-23"
                                        disabled={isAcquiring}
                                    />
                                </div>
 
                                <div className="col-md-6">
                                    <label className="form-label">Frequency (MHz) *</label>
                                    <input
                                        type="number"
                                        className="form-control"
                                        value={formData.frequency}
                                        onChange={(e) => setFormData({ ...formData, frequency: e.target.value })}
                                        step="0.001"
                                        min="144"
                                        max="148"
                                        disabled={isAcquiring}
                                    />
                                </div>
 
                                <div className="col-12">
                                    <label className="form-label">Duration (seconds)</label>
                                    <input
                                        type="range"
                                        className="form-range"
                                        min="10"
                                        max="300"
                                        step="10"
                                        value={formData.duration}
                                        onChange={(e) => setFormData({ ...formData, duration: parseInt(e.target.value) })}
                                        disabled={isAcquiring}
                                    />
                                    <div className="d-flex justify-content-between">
                                        <span className="f-12 text-muted">10s</span>
                                        <span className="badge bg-primary">{formData.duration}s</span>
                                        <span className="f-12 text-muted">300s (5min)</span>
                                    </div>
                                </div>
 
                                <div className="col-12">
                                    <label className="form-label">Notes (Optional)</label>
                                    <textarea
                                        className="form-control"
                                        rows={3}
                                        value={formData.notes}
                                        onChange={(e) => setFormData({ ...formData, notes: e.target.value })}
                                        placeholder="Additional notes about this recording session..."
                                        disabled={isAcquiring}
                                    ></textarea>
                                </div>
 
                                {selectedSource && (
                                    <div className="col-12">
                                        <div className="alert alert-info">
                                            <h6 className="mb-2">Source Information</h6>
                                            <table className="table table-sm table-borderless mb-0">
                                                <tbody>
                                                    <tr>
                                                        <td className="text-muted">Name:</td>
                                                        <td>{selectedSource.name}</td>
                                                    </tr>
                                                    <tr>
                                                        <td className="text-muted">Location:</td>
                                                        <td>
                                                            {selectedSource.latitude.toFixed(4)}, {selectedSource.longitude.toFixed(4)}
                                                        </td>
                                                    </tr>
                                                    <tr>
                                                        <td className="text-muted">Power:</td>
                                                        <td>{selectedSource.power_dbm ? `${selectedSource.power_dbm} dBm` : 'N/A'}</td>
                                                    </tr>
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                )}
                            </div>
                        </div>
                        <div className="card-footer">
                            <button
                                className="btn btn-primary"
                                onClick={handleStartAcquisition}
                                disabled={isAcquiring || !formData.knownSourceId || !formData.sessionName}
                            >
                                {isAcquiring ? (
                                    <>
                                        <span className="spinner-border spinner-border-sm me-2"></span>
                                        Acquiring...
                                    </>
                                ) : (
                                    <>
                                        <i className="ph ph-record me-2"></i>
                                        Start Acquisition
                                    </>
                                )}
                            </button>
                            <button
                                className="btn btn-outline-secondary ms-2"
                                onClick={handleReset}
                                disabled={isAcquiring}
                            >
                                <i className="ph ph-arrow-counter-clockwise me-2"></i>
                                Reset
                            </button>
                        </div>
                    </div>
                </div>
 
                {/* Status Panel */}
                <div className="col-lg-4">
                    {/* System Status */}
                    <div className="card">
                        <div className="card-header">
                            <h5 className="mb-0">System Status</h5>
                        </div>
                        <div className="card-body">
                            <div className="d-flex justify-content-between align-items-center mb-3">
                                <span>WebSDR Receivers</span>
                                <span className="badge bg-light-primary">{onlineWebSDRs}/7 Online</span>
                            </div>
                            <div className="d-flex justify-content-between align-items-center mb-3">
                                <span>Known Sources</span>
                                <span className="badge bg-light-info">{knownSources.length}</span>
                            </div>
                            <div className="d-flex justify-content-between align-items-center">
                                <span>System Ready</span>
                                <span className={`badge ${onlineWebSDRs > 0 ? 'bg-light-success' : 'bg-light-danger'}`}>
                                    {onlineWebSDRs > 0 ? 'Yes' : 'No'}
                                </span>
                            </div>
                        </div>
                    </div>
 
                    {/* Acquisition Status */}
                    {acquisitionStatus && (
                        <div className="card">
                            <div className="card-header">
                                <h5 className="mb-0">Acquisition Status</h5>
                            </div>
                            <div className="card-body">
                                <div className="d-flex align-items-center mb-3">
                                    <div className={`avtar avtar-s ${acquisitionStatus.status === 'SUCCESS'
                                        ? 'bg-light-success'
                                        : acquisitionStatus.status === 'FAILURE'
                                            ? 'bg-light-danger'
                                            : 'bg-light-primary'
                                        }`}>
                                        <i className={`ph ${acquisitionStatus.status === 'SUCCESS'
                                            ? 'ph-check-circle'
                                            : acquisitionStatus.status === 'FAILURE'
                                                ? 'ph-x-circle'
                                                : 'ph-hourglass'
                                            }`}></i>
                                    </div>
                                    <div className="ms-3 flex-grow-1">
                                        <h6 className="mb-0">
                                            {acquisitionStatus.status === 'SUCCESS'
                                                ? 'Completed'
                                                : acquisitionStatus.status === 'FAILURE'
                                                    ? 'Failed'
                                                    : 'In Progress'}
                                        </h6>
                                        <p className="text-muted f-12 mb-0">
                                            {acquisitionStatus.message || 'Processing...'}
                                        </p>
                                    </div>
                                </div>
 
                                {acquisitionStatus.progress !== undefined && (
                                    <div className="mb-3">
                                        <div className="d-flex justify-content-between mb-1">
                                            <span className="f-12">Progress</span>
                                            <span className="f-12">{Math.round(acquisitionStatus.progress * 100)}%</span>
                                        </div>
                                        <div className="progress" style={{ height: '6px' }}>
                                            <div
                                                className="progress-bar bg-primary"
                                                style={{ width: `${acquisitionStatus.progress * 100}%` }}
                                            ></div>
                                        </div>
                                    </div>
                                )}
 
                                {currentTaskId && (
                                    <div className="f-12 text-muted">
                                        Task ID: <code>{currentTaskId}</code>
                                    </div>
                                )}
                            </div>
                        </div>
                    )}
                </div>
            </div>
        </>
    );
};
 
export default RecordingSession;