observation-tools 0.0.13

Observation Tools Client Library
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! Client for communicating with the observation-tools server

use crate::error::Result;
use crate::execution::BeginExecution;
use crate::execution::ExecutionHandle;
use crate::observation_handle::ObservationHandle;
use crate::ObservationWithPayload;
use async_channel;
use log::error;
use log::info;
use log::trace;
use napi_derive::napi;
use observation_tools_shared::models::Execution;
// Re-export constants from shared crate for convenience
pub use observation_tools_shared::BATCH_SIZE;
pub use observation_tools_shared::BLOB_THRESHOLD_BYTES;
use std::sync::Arc;

/// Result type for observation upload completion notifications via watch
/// channel Uses String for error since crate::Error doesn't implement Clone
pub(crate) type ObservationUploadResult = Option<std::result::Result<ObservationHandle, String>>;

/// Result type for execution upload completion notifications via watch channel
/// Uses String for error since crate::Error doesn't implement Clone
pub(crate) type ExecutionUploadResult = Option<std::result::Result<ExecutionHandle, String>>;

/// Message types for the background uploader task
pub(crate) enum UploaderMessage {
  Execution {
    execution: Execution,
    handle: ExecutionHandle,
    uploaded_tx: tokio::sync::watch::Sender<ExecutionUploadResult>,
  },
  Observations {
    observations: Vec<ObservationWithPayload>,
    handle: ObservationHandle,
    uploaded_tx: tokio::sync::watch::Sender<ObservationUploadResult>,
  },
  Flush,
  Shutdown,
}

// Manual Debug implementation since watch::Sender doesn't implement Debug
impl std::fmt::Debug for UploaderMessage {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      Self::Execution { execution, .. } => f
        .debug_struct("Execution")
        .field("execution", execution)
        .finish(),
      Self::Observations {
        observations,
        handle,
        ..
      } => f
        .debug_struct("Observations")
        .field("observations", observations)
        .field("handle", handle)
        .finish(),
      Self::Flush => write!(f, "Flush"),
      Self::Shutdown => write!(f, "Shutdown"),
    }
  }
}

/// Client for observation-tools
#[napi]
#[derive(Clone)]
pub struct Client {
  inner: Arc<ClientInner>,
}

struct ClientInner {
  base_url: String,
  uploader_tx: async_channel::Sender<UploaderMessage>,
  shutdown_rx: std::sync::Mutex<Option<tokio::sync::oneshot::Receiver<()>>>,
  // If we create a runtime for the uploader, we hold it here to keep it alive
  _runtime: Option<Arc<tokio::runtime::Runtime>>,
}

/// Generate a new execution ID (for testing)
///
/// This allows tests to generate an execution ID before creating the execution,
/// enabling navigation to the execution URL before the execution is uploaded.
#[napi(js_name = "generateExecutionId")]
#[allow(unused)]
pub fn generate_execution_id() -> String {
  observation_tools_shared::models::ExecutionId::new().to_string()
}

/// Generate a new observation ID (for testing)
///
/// This allows tests to generate an observation ID before creating the
/// observation, enabling navigation to the observation URL before the
/// observation is uploaded.
#[napi(js_name = "generateObservationId")]
#[allow(unused)]
pub fn generate_observation_id() -> String {
  observation_tools_shared::ObservationId::new().to_string()
}

#[napi]
impl Client {
  #[napi(js_name = "beginExecution")]
  pub fn begin_execution_wasm(&self, name: String) -> napi::Result<ExecutionHandle> {
    self
      .begin_execution(name)
      .map(|begin| begin.into_handle())
      .map_err(|e| napi::Error::from_reason(e.to_string()))
  }

  /// Begin a new execution with a specific ID (for testing)
  ///
  /// This allows tests to create an execution with a known ID, enabling
  /// navigation to the execution URL before the execution is uploaded.
  #[napi(js_name = "beginExecutionWithId")]
  pub fn begin_execution_with_id_wasm(
    &self,
    id: String,
    name: String,
  ) -> napi::Result<ExecutionHandle> {
    let execution_id = observation_tools_shared::models::ExecutionId::parse(&id)
      .map_err(|e| napi::Error::from_reason(e.to_string()))?;
    let execution = Execution::with_id(execution_id, name);
    self
      .begin_execution_internal(execution)
      .map(|begin| begin.into_handle())
      .map_err(|e| napi::Error::from_reason(e.to_string()))
  }
}

impl Client {
  /// Begin a new execution
  ///
  /// Returns a `BeginExecution` which allows you to wait for the execution
  /// to be uploaded before proceeding, or to get the handle immediately.
  pub fn begin_execution(&self, name: impl Into<String>) -> Result<BeginExecution> {
    let execution = Execution::new(name.into());
    self.begin_execution_internal(execution)
  }

  fn begin_execution_internal(&self, execution: Execution) -> Result<BeginExecution> {
    trace!("Beginning new execution with ID {}", execution.id);
    let handle = ExecutionHandle::new(
      execution.id,
      self.inner.uploader_tx.clone(),
      self.inner.base_url.clone(),
    );
    let (uploaded_tx, uploaded_rx) = tokio::sync::watch::channel(None);
    self
      .inner
      .uploader_tx
      .try_send(UploaderMessage::Execution {
        execution: execution.clone(),
        handle: handle.clone(),
        uploaded_tx,
      })?;
    Ok(BeginExecution::new(handle, uploaded_rx))
  }

  /// Shutdown the client and wait for pending uploads
  pub async fn shutdown(&self) -> Result<()> {
    self.inner.uploader_tx.try_send(UploaderMessage::Shutdown)?;
    // Wait for the uploader thread to finish
    if let Some(rx) = self.inner.shutdown_rx.lock().unwrap().take() {
      let _ = rx.await;
    }
    Ok(())
  }
}

impl Drop for ClientInner {
  fn drop(&mut self) {
    // Best effort shutdown notification
    let _ = self.uploader_tx.try_send(UploaderMessage::Shutdown);
  }
}

/// Builder for Client
#[napi]
pub struct ClientBuilder {
  base_url: Option<String>,
  api_key: Option<String>,
}

impl Default for ClientBuilder {
  fn default() -> Self {
    Self {
      base_url: None,
      api_key: None,
    }
  }
}

#[napi]
impl ClientBuilder {
  /// Create a new client builder
  #[napi(constructor)]
  pub fn new() -> Self {
    Self::default()
  }

  /// Set the base URL for the server
  #[napi]
  pub fn set_base_url(&mut self, url: String) {
    self.base_url = Some(url);
  }

  /// Set the API key for authentication
  #[napi]
  pub fn set_api_key(&mut self, api_key: String) {
    self.api_key = Some(api_key);
  }
}

impl ClientBuilder {
  /// Set the base URL for the server
  pub fn base_url(mut self, url: impl Into<String>) -> Self {
    self.base_url = Some(url.into());
    self
  }

  /// Set the API key for authentication
  pub fn api_key(mut self, api_key: impl Into<String>) -> Self {
    self.api_key = Some(api_key.into());
    self
  }
}

#[napi]
impl ClientBuilder {
  /// Build the client
  #[napi]
  pub fn build(&self) -> napi::Result<Client> {
    let base_url = self
      .base_url
      .clone()
      .unwrap_or_else(|| "http://localhost:3000".to_string());
    let (tx, rx) = async_channel::unbounded();
    let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel();
    let timer_tx = tx.clone();
    let uploader_base_url = base_url.clone();
    let api_key = self.api_key.clone();
    let (handle, runtime) = match tokio::runtime::Handle::try_current() {
      Ok(handle) => (handle, None),
      Err(_) => {
        let runtime = Arc::new(
          tokio::runtime::Builder::new_multi_thread()
            .worker_threads(1)
            .enable_all()
            .build()?,
        );
        (runtime.handle().clone(), Some(runtime))
      }
    };
    let api_client = crate::server_client::create_client(&uploader_base_url, api_key.clone())?;
    handle.spawn(async move {
      tokio::spawn(async move {
        let mut interval = tokio::time::interval(tokio::time::Duration::from_millis(100));
        interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
        loop {
          interval.tick().await;
          if timer_tx.send(UploaderMessage::Flush).await.is_err() {
            break; // Channel closed, stop timer
          }
        }
      });
      uploader_task(api_client, rx).await;
      let _ = shutdown_tx.send(());
    });
    Ok(Client {
      inner: Arc::new(ClientInner {
        base_url,
        uploader_tx: tx,
        shutdown_rx: std::sync::Mutex::new(Some(shutdown_rx)),
        _runtime: runtime,
      }),
    })
  }
}

async fn uploader_task(
  api_client: crate::server_client::Client,
  rx: async_channel::Receiver<UploaderMessage>,
) {
  info!("Uploader task started");

  // Buffer type for observation senders: (handle, sender)
  type ObservationSender = (
    ObservationHandle,
    tokio::sync::watch::Sender<ObservationUploadResult>,
  );

  let flush_observations = async |buffer: &mut Vec<ObservationWithPayload>,
                                  senders: &mut Vec<ObservationSender>| {
    if buffer.is_empty() {
      return;
    }
    let result = upload_observations(&api_client, buffer.drain(..).collect()).await;
    match result {
      Ok(()) => {
        // Signal all senders that observations were uploaded successfully
        for (handle, sender) in senders.drain(..) {
          let _ = sender.send(Some(Ok(handle)));
        }
      }
      Err(e) => {
        let error_msg = e.to_string();
        error!("Failed to upload observations: {}", error_msg);
        // Signal all senders with the error (as String for Clone compatibility)
        for (_, sender) in senders.drain(..) {
          let _ = sender.send(Some(Err(error_msg.clone())));
        }
      }
    }
  };
  let mut observation_buffer: Vec<ObservationWithPayload> = Vec::new();
  let mut sender_buffer: Vec<ObservationSender> = Vec::new();
  loop {
    let msg = rx.recv().await.ok();
    match msg {
      Some(UploaderMessage::Execution {
        execution,
        handle,
        uploaded_tx,
      }) => {
        let result = upload_execution(&api_client, execution).await;
        match result {
          Ok(()) => {
            // Signal successful upload with handle
            let _ = uploaded_tx.send(Some(Ok(handle)));
          }
          Err(e) => {
            let error_msg = e.to_string();
            error!("Failed to upload execution: {}", error_msg);
            let _ = uploaded_tx.send(Some(Err(error_msg)));
          }
        }
      }
      Some(UploaderMessage::Observations {
        observations,
        handle,
        uploaded_tx,
      }) => {
        observation_buffer.extend(observations);
        sender_buffer.push((handle, uploaded_tx));
        if observation_buffer.len() >= BATCH_SIZE {
          flush_observations(&mut observation_buffer, &mut sender_buffer).await;
        }
      }
      Some(UploaderMessage::Flush) => {
        flush_observations(&mut observation_buffer, &mut sender_buffer).await;
      }
      Some(UploaderMessage::Shutdown) | None => {
        flush_observations(&mut observation_buffer, &mut sender_buffer).await;
        break;
      }
    }
  }
}

// Async upload functions (used by both native and WASM)
async fn upload_execution(
  client: &crate::server_client::Client,
  execution: Execution,
) -> Result<()> {
  trace!("Uploading execution");

  // Convert from shared type to OpenAPI type via serde
  let execution_json = serde_json::to_value(&execution)?;
  let openapi_execution: crate::server_client::types::Execution =
    serde_json::from_value(execution_json)?;

  client
    .create_execution()
    .body_map(|b| b.execution(openapi_execution))
    .send()
    .await
    .map_err(|e| crate::error::Error::Config(e.to_string()))?;

  Ok(())
}

async fn upload_observations(
  client: &crate::server_client::Client,
  observations: Vec<ObservationWithPayload>,
) -> Result<()> {
  if observations.is_empty() {
    return Ok(());
  }

  // Group by execution_id
  let mut by_execution: std::collections::HashMap<_, Vec<_>> = std::collections::HashMap::new();
  for obs in observations {
    by_execution
      .entry(obs.observation.execution_id)
      .or_default()
      .push(obs);
  }

  // Upload each batch via multipart form
  for (execution_id, observations) in by_execution {
    trace!(
      "Uploading {} observations for execution {}",
      observations.len(),
      execution_id
    );

    client
      .create_observations_multipart(&execution_id.to_string(), observations)
      .await
      .map_err(|e| crate::error::Error::Config(e.to_string()))?;
  }

  Ok(())
}