All files / frontend/src/services/api session.ts

17.8% Statements 13/73
100% Branches 0/0
0% Functions 0/9
17.8% Lines 13/73

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                        1x                                                                                                                                                                                                                                                                                                                                                                   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x  
/**
 * Session API Service
 * 
 * Handles recording session operations:
 * - List sessions with pagination
 * - Create new sessions
 * - Get session details
 * - Update session status
 * - Get analytics
 * - Manage known sources
 */
 
import api from '@/lib/api';
 
export interface KnownSource {
    id: string;
    name: string;
    description?: string;
    frequency_hz: number;
    latitude: number;
    longitude: number;
    power_dbm?: number;
    source_type?: string;
    is_validated: boolean;
    created_at: string;
    updated_at: string;
}
 
export interface KnownSourceCreate {
    name: string;
    description?: string;
    frequency_hz: number;
    latitude: number;
    longitude: number;
    power_dbm?: number;
    source_type?: string;
    is_validated?: boolean;
}
 
export interface RecordingSession {
    id: number;
    session_name: string;
    frequency_mhz: number;
    duration_seconds: number;
    status: 'pending' | 'in_progress' | 'processing' | 'completed' | 'failed';
    celery_task_id?: string | null;
    result_metadata?: Record<string, unknown> | null;
    minio_path?: string | null;
    error_message?: string | null;
    created_at: string;
    started_at?: string | null;
    completed_at?: string | null;
    websdrs_enabled?: number;
}
 
export interface RecordingSessionWithDetails extends RecordingSession {
    source_name?: string;
    source_frequency?: number;
    source_latitude?: number;
    source_longitude?: number;
    measurements_count?: number;
    approval_status?: 'pending' | 'approved' | 'rejected';
    notes?: string;
}
 
export interface RecordingSessionCreate {
    session_name: string;
    frequency_mhz: number;
    duration_seconds: number;
    notes?: string;
}
 
export interface SessionListResponse {
    sessions: RecordingSessionWithDetails[];
    total: number;
    page: number;
    per_page: number;
}
 
export interface SessionAnalytics {
    total_sessions: number;
    completed_sessions: number;
    failed_sessions: number;
    pending_sessions: number;
    success_rate: number;
    total_measurements: number;
    average_duration_seconds?: number;
    average_accuracy_meters?: number;
}
 
/**
 * List recording sessions with pagination and filters
 */
export async function listSessions(params: {
    page?: number;
    per_page?: number;
    status?: string;
    approval_status?: string;
}): Promise<SessionListResponse> {
    const response = await api.get<SessionListResponse>('/api/v1/sessions', { params });
    return response.data;
}
 
/**
 * Get a specific session by ID
 */
export async function getSession(sessionId: number): Promise<RecordingSessionWithDetails> {
    const response = await api.get<RecordingSessionWithDetails>(`/api/v1/sessions/${sessionId}`);
    return response.data;
}
 
/**
 * Create a new recording session
 */
export async function createSession(session: RecordingSessionCreate): Promise<RecordingSession> {
    const response = await api.post<RecordingSession>('/api/v1/sessions', session);
    return response.data;
}
 
/**
 * Update session status
 */
export async function updateSessionStatus(
    sessionId: number,
    status: string,
    celeryTaskId?: string
): Promise<RecordingSession> {
    const response = await api.patch<RecordingSession>(
        `/api/v1/sessions/${sessionId}/status`,
        null,
        {
            params: {
                status,
                celery_task_id: celeryTaskId,
            },
        }
    );
    return response.data;
}
 
/**
 * Update session approval status
 */
export async function updateSessionApproval(
    sessionId: number,
    approvalStatus: 'pending' | 'approved' | 'rejected'
): Promise<RecordingSession> {
    const response = await api.patch<RecordingSession>(
        `/api/v1/sessions/${sessionId}/approval`,
        null,
        {
            params: {
                approval_status: approvalStatus,
            },
        }
    );
    return response.data;
}
 
/**
 * Delete a recording session
 */
export async function deleteSession(sessionId: number): Promise<void> {
    await api.delete(`/api/v1/sessions/${sessionId}`);
}
 
/**
 * Get session analytics
 */
export async function getSessionAnalytics(): Promise<SessionAnalytics> {
    const response = await api.get<SessionAnalytics>('/api/v1/sessions/analytics');
    return response.data;
}
 
/**
 * List all known RF sources
 */
export async function listKnownSources(): Promise<KnownSource[]> {
    const response = await api.get<KnownSource[]>('/api/v1/sessions/known-sources');
    return response.data;
}
 
/**
 * Create a new known RF source
 */
export async function createKnownSource(source: KnownSourceCreate): Promise<KnownSource> {
    const response = await api.post<KnownSource>('/api/v1/sessions/known-sources', source);
    return response.data;
}
 
const sessionService = {
    listSessions,
    getSession,
    createSession,
    updateSessionStatus,
    updateSessionApproval,
    deleteSession,
    getSessionAnalytics,
    listKnownSources,
    createKnownSource,
};
 
export default sessionService;