|
| 1 | +/** |
| 2 | + * DAG Explorer API Module |
| 3 | + * |
| 4 | + * Handles all backend API calls with proper error handling and debouncing. |
| 5 | + */ |
| 6 | + |
| 7 | +const DAGAPI = (function() { |
| 8 | + 'use strict'; |
| 9 | + |
| 10 | + // Track pending requests for cancellation |
| 11 | + let pendingRequests = []; |
| 12 | + |
| 13 | + /** |
| 14 | + * Make an AJAX request with error handling |
| 15 | + * @param {Object} options - jQuery AJAX options |
| 16 | + * @returns {Promise} Promise resolving to response data |
| 17 | + */ |
| 18 | + function request(options) { |
| 19 | + return new Promise((resolve, reject) => { |
| 20 | + const defaults = { |
| 21 | + dataType: 'json', |
| 22 | + timeout: 30000 // 30 second timeout |
| 23 | + }; |
| 24 | + |
| 25 | + const ajaxOptions = Object.assign({}, defaults, options, { |
| 26 | + success: function(response) { |
| 27 | + resolve(response); |
| 28 | + }, |
| 29 | + error: function(xhr, status, error) { |
| 30 | + console.error(`API Error [${options.url}]:`, status, error); |
| 31 | + reject({ |
| 32 | + status: xhr.status, |
| 33 | + statusText: status, |
| 34 | + error: error, |
| 35 | + response: xhr.responseText |
| 36 | + }); |
| 37 | + } |
| 38 | + }); |
| 39 | + |
| 40 | + const xhr = $.ajax(ajaxOptions); |
| 41 | + pendingRequests.push(xhr); |
| 42 | + |
| 43 | + // Clean up completed request |
| 44 | + xhr.always(function() { |
| 45 | + const index = pendingRequests.indexOf(xhr); |
| 46 | + if (index > -1) { |
| 47 | + pendingRequests.splice(index, 1); |
| 48 | + } |
| 49 | + }); |
| 50 | + }); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Cancel all pending requests |
| 55 | + */ |
| 56 | + function cancelPendingRequests() { |
| 57 | + pendingRequests.forEach(xhr => xhr.abort()); |
| 58 | + pendingRequests = []; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Fetch DAG data for a node at specified depth |
| 63 | + * @param {string} euid - Starting node EUID |
| 64 | + * @param {number} depth - Traversal depth |
| 65 | + * @returns {Promise} Promise resolving to graph data |
| 66 | + */ |
| 67 | + function fetchDAGData(euid, depth) { |
| 68 | + return request({ |
| 69 | + url: '/get_dagv2', |
| 70 | + method: 'GET', |
| 71 | + data: { euid: euid, depth: depth } |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + // Debounced version of fetchDAGData |
| 76 | + let debouncedFetchTimeout = null; |
| 77 | + function debouncedFetchDAGData(euid, depth, callback) { |
| 78 | + if (debouncedFetchTimeout) { |
| 79 | + clearTimeout(debouncedFetchTimeout); |
| 80 | + } |
| 81 | + debouncedFetchTimeout = setTimeout(function() { |
| 82 | + fetchDAGData(euid, depth) |
| 83 | + .then(callback) |
| 84 | + .catch(function(error) { |
| 85 | + console.error('Failed to fetch DAG data:', error); |
| 86 | + }); |
| 87 | + }, DAGConfig.TIMING.DEBOUNCE_DELAY); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Get node information |
| 92 | + * @param {string} euid - Node EUID |
| 93 | + * @returns {Promise} Promise resolving to node info |
| 94 | + */ |
| 95 | + function getNodeInfo(euid) { |
| 96 | + return request({ |
| 97 | + url: '/get_node_info', |
| 98 | + method: 'GET', |
| 99 | + data: { euid: euid } |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Get a specific property of a node |
| 105 | + * @param {string} euid - Node EUID |
| 106 | + * @param {string} key - Property key |
| 107 | + * @returns {Promise} Promise resolving to property value |
| 108 | + */ |
| 109 | + function getNodeProperty(euid, key) { |
| 110 | + return request({ |
| 111 | + url: '/get_node_property', |
| 112 | + method: 'GET', |
| 113 | + data: { euid: euid, key: key } |
| 114 | + }); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Calculate COGS for parent nodes |
| 119 | + * @param {string} euid - Node EUID |
| 120 | + * @returns {Promise} Promise resolving to COGS value |
| 121 | + */ |
| 122 | + function calculateCogsParents(euid) { |
| 123 | + return request({ |
| 124 | + url: '/calculate_cogs_parents', |
| 125 | + method: 'GET', |
| 126 | + data: { euid: euid } |
| 127 | + }).then(function(response) { |
| 128 | + // Handle response that might be a string |
| 129 | + if (typeof response === 'string') { |
| 130 | + return DAGUtils.safeJSONParse(response, { cogs_value: 'N/A' }); |
| 131 | + } |
| 132 | + return response; |
| 133 | + }); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Calculate COGS for child nodes |
| 138 | + * @param {string} euid - Node EUID |
| 139 | + * @returns {Promise} Promise resolving to COGS value |
| 140 | + */ |
| 141 | + function calculateCogsChildren(euid) { |
| 142 | + return request({ |
| 143 | + url: '/calculate_cogs_children', |
| 144 | + method: 'GET', |
| 145 | + data: { euid: euid } |
| 146 | + }).then(function(response) { |
| 147 | + if (typeof response === 'string') { |
| 148 | + return DAGUtils.safeJSONParse(response, { cogs_value: 'N/A' }); |
| 149 | + } |
| 150 | + return response; |
| 151 | + }); |
| 152 | + } |
| 153 | + |
| 154 | + /** |
| 155 | + * Add a new edge between nodes |
| 156 | + * @param {string} parentUuid - Parent node UUID |
| 157 | + * @param {string} childUuid - Child node UUID |
| 158 | + * @returns {Promise} Promise resolving to new edge data |
| 159 | + */ |
| 160 | + function addNewEdge(parentUuid, childUuid) { |
| 161 | + return request({ |
| 162 | + url: '/add_new_edge', |
| 163 | + method: 'POST', |
| 164 | + contentType: 'application/json', |
| 165 | + data: JSON.stringify({ parent_uuid: parentUuid, child_uuid: childUuid }) |
| 166 | + }); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Delete an object (node or edge) |
| 171 | + * @param {string} euid - Object EUID |
| 172 | + * @returns {Promise} Promise resolving to deletion result |
| 173 | + */ |
| 174 | + function deleteObject(euid) { |
| 175 | + return request({ |
| 176 | + url: '/delete_object', |
| 177 | + method: 'POST', |
| 178 | + contentType: 'application/json', |
| 179 | + data: JSON.stringify({ euid: euid }) |
| 180 | + }); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * Update the DAG with new graph data |
| 185 | + * @param {Object} graphData - Cytoscape graph JSON |
| 186 | + * @returns {Promise} Promise resolving to update result |
| 187 | + */ |
| 188 | + function updateDAG(graphData) { |
| 189 | + return request({ |
| 190 | + url: '/update_dag', |
| 191 | + method: 'POST', |
| 192 | + contentType: 'application/json', |
| 193 | + data: JSON.stringify(graphData) |
| 194 | + }); |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * Add a new node |
| 199 | + * @returns {Promise} Promise resolving to new node data |
| 200 | + */ |
| 201 | + function addNewNode() { |
| 202 | + return request({ |
| 203 | + url: '/add_new_node', |
| 204 | + method: 'GET' |
| 205 | + }); |
| 206 | + } |
| 207 | + |
| 208 | + // Public API |
| 209 | + return { |
| 210 | + request: request, |
| 211 | + cancelPendingRequests: cancelPendingRequests, |
| 212 | + fetchDAGData: fetchDAGData, |
| 213 | + debouncedFetchDAGData: debouncedFetchDAGData, |
| 214 | + getNodeInfo: getNodeInfo, |
| 215 | + getNodeProperty: getNodeProperty, |
| 216 | + calculateCogsParents: calculateCogsParents, |
| 217 | + calculateCogsChildren: calculateCogsChildren, |
| 218 | + addNewEdge: addNewEdge, |
| 219 | + deleteObject: deleteObject, |
| 220 | + updateDAG: updateDAG, |
| 221 | + addNewNode: addNewNode |
| 222 | + }; |
| 223 | +})(); |
| 224 | + |
| 225 | +// Export for use in other modules |
| 226 | +if (typeof module !== 'undefined' && module.exports) { |
| 227 | + module.exports = DAGAPI; |
| 228 | +} |
| 229 | + |
0 commit comments