|
| 1 | +/** |
| 2 | + * Data Reader Service |
| 3 | + * |
| 4 | + * Provides a standardized interface for accessing project volumes |
| 5 | + * by taskId, abstracting away file paths and backend specifics. |
| 6 | + */ |
| 7 | + |
| 8 | +import axios from 'axios'; |
| 9 | + |
| 10 | +// Backend configuration |
| 11 | +const API_BASE = process.env.REACT_APP_API_URL || 'http://localhost:4242/api/pm'; |
| 12 | + |
| 13 | +class DataReaderService { |
| 14 | + /** |
| 15 | + * Fetch all pooled volumes (with pagination handled internally if needed) |
| 16 | + * The frontend "pools" the list from the active metadata JSON. |
| 17 | + */ |
| 18 | + async getPooledVolumes(params = {}) { |
| 19 | + try { |
| 20 | + const response = await axios.get(`${API_BASE}/volumes`, { params }); |
| 21 | + return response.data; // { total, page, items, ... } |
| 22 | + } catch (error) { |
| 23 | + console.error('Error fetching pooled volumes:', error); |
| 24 | + throw error; |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Get metadata for a specific volume by taskId |
| 30 | + */ |
| 31 | + async getVolumeByTaskId(taskId) { |
| 32 | + try { |
| 33 | + // Since the backend uses id as taskId (e.g. vol_001_em.h5) |
| 34 | + // we can just use the volumes endpoint with filtering if supported, |
| 35 | + // or we could add a dedicated GET /volumes/{id} if needed. |
| 36 | + // For now, we'll assume the frontend already has the list or we fetch one. |
| 37 | + const response = await axios.get(`${API_BASE}/volumes`, { params: { id: taskId, page_size: 1 } }); |
| 38 | + return response.data.items[0] || null; |
| 39 | + } catch (error) { |
| 40 | + console.error(`Error fetching volume ${taskId}:`, error); |
| 41 | + throw error; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Update volume status by taskId |
| 47 | + */ |
| 48 | + async updateStatus(taskId, status) { |
| 49 | + try { |
| 50 | + const response = await axios.patch(`${API_BASE}/volumes/${taskId}`, { status }); |
| 51 | + return response.data; |
| 52 | + } catch (error) { |
| 53 | + console.error(`Error updating volume ${taskId}:`, error); |
| 54 | + throw error; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Bulk link to an external metadata JSON |
| 60 | + */ |
| 61 | + async linkExternalMetadata(path) { |
| 62 | + try { |
| 63 | + const response = await axios.post(`${API_BASE}/data/link`, { path }); |
| 64 | + return response.data; |
| 65 | + } catch (error) { |
| 66 | + console.error('Error linking external metadata:', error); |
| 67 | + throw error; |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +export const dataReader = new DataReaderService(); |
| 73 | +export default dataReader; |
0 commit comments