|
| 1 | +use serde::{Deserialize, Serialize}; |
| 2 | +use time::OffsetDateTime; |
| 3 | + |
| 4 | +use crate::{client::Client, errors::Error, request::HttpClient}; |
| 5 | + |
| 6 | +/// Types and queries for the Meilisearch Batches API. |
| 7 | +/// |
| 8 | +/// See: https://www.meilisearch.com/docs/reference/api/batches |
| 9 | +#[derive(Debug, Clone, Deserialize)] |
| 10 | +#[serde(rename_all = "camelCase")] |
| 11 | +pub struct Batch { |
| 12 | + /// Unique identifier of the batch. |
| 13 | + #[serde(default)] |
| 14 | + pub uid: u32, |
| 15 | + /// When the batch was enqueued. |
| 16 | + #[serde(default, with = "time::serde::rfc3339::option")] |
| 17 | + pub enqueued_at: Option<OffsetDateTime>, |
| 18 | + /// When the batch started processing. |
| 19 | + #[serde(default, with = "time::serde::rfc3339::option")] |
| 20 | + pub started_at: Option<OffsetDateTime>, |
| 21 | + /// When the batch finished processing. |
| 22 | + #[serde(default, with = "time::serde::rfc3339::option")] |
| 23 | + pub finished_at: Option<OffsetDateTime>, |
| 24 | + /// Index uid related to this batch (if applicable). |
| 25 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 26 | + pub index_uid: Option<String>, |
| 27 | + /// The task uids that are part of this batch. |
| 28 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 29 | + pub task_uids: Option<Vec<u32>>, |
| 30 | + /// The strategy that caused the autobatcher to stop batching tasks. |
| 31 | + /// |
| 32 | + /// Introduced in Meilisearch v1.15. |
| 33 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 34 | + pub batch_strategy: Option<String>, |
| 35 | +} |
| 36 | + |
| 37 | +#[derive(Debug, Clone, Deserialize)] |
| 38 | +#[serde(rename_all = "camelCase")] |
| 39 | +pub struct BatchesResults { |
| 40 | + pub results: Vec<Batch>, |
| 41 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 42 | + pub total: Option<u64>, |
| 43 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 44 | + pub limit: Option<u32>, |
| 45 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 46 | + pub from: Option<u32>, |
| 47 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 48 | + pub next: Option<u32>, |
| 49 | +} |
| 50 | + |
| 51 | +/// Query builder for listing batches. |
| 52 | +#[derive(Debug, Serialize, Clone)] |
| 53 | +#[serde(rename_all = "camelCase")] |
| 54 | +pub struct BatchesQuery<'a, Http: HttpClient> { |
| 55 | + #[serde(skip_serializing)] |
| 56 | + client: &'a Client<Http>, |
| 57 | + /// Maximum number of batches to return. |
| 58 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 59 | + limit: Option<u32>, |
| 60 | + /// The first batch uid that should be returned. |
| 61 | + #[serde(skip_serializing_if = "Option::is_none")] |
| 62 | + from: Option<u32>, |
| 63 | +} |
| 64 | + |
| 65 | +impl<'a, Http: HttpClient> BatchesQuery<'a, Http> { |
| 66 | + #[must_use] |
| 67 | + pub fn new(client: &'a Client<Http>) -> BatchesQuery<'a, Http> { |
| 68 | + BatchesQuery { |
| 69 | + client, |
| 70 | + limit: None, |
| 71 | + from: None, |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + #[must_use] |
| 76 | + pub fn with_limit(&mut self, limit: u32) -> &mut Self { |
| 77 | + self.limit = Some(limit); |
| 78 | + self |
| 79 | + } |
| 80 | + |
| 81 | + #[must_use] |
| 82 | + pub fn with_from(&mut self, from: u32) -> &mut Self { |
| 83 | + self.from = Some(from); |
| 84 | + self |
| 85 | + } |
| 86 | + |
| 87 | + /// Execute the query and list batches. |
| 88 | + pub async fn execute(&self) -> Result<BatchesResults, Error> { |
| 89 | + self.client.get_batches_with(self).await |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +#[cfg(test)] |
| 94 | +mod tests { |
| 95 | + use crate::client::Client; |
| 96 | + |
| 97 | + #[tokio::test] |
| 98 | + async fn test_get_batches_parses_batch_strategy() { |
| 99 | + let mut s = mockito::Server::new_async().await; |
| 100 | + let base = s.url(); |
| 101 | + |
| 102 | + let response_body = serde_json::json!({ |
| 103 | + "results": [ |
| 104 | + { |
| 105 | + "uid": 42, |
| 106 | + "enqueuedAt": "2024-10-11T11:49:53.000Z", |
| 107 | + "startedAt": "2024-10-11T11:49:54.000Z", |
| 108 | + "finishedAt": "2024-10-11T11:49:55.000Z", |
| 109 | + "indexUid": "movies", |
| 110 | + "taskUids": [1, 2, 3], |
| 111 | + "batchStrategy": "time_limit_reached" |
| 112 | + } |
| 113 | + ], |
| 114 | + "limit": 20, |
| 115 | + "from": null, |
| 116 | + "next": null, |
| 117 | + "total": 1 |
| 118 | + }) |
| 119 | + .to_string(); |
| 120 | + |
| 121 | + let _m = s |
| 122 | + .mock("GET", "/batches") |
| 123 | + .with_status(200) |
| 124 | + .with_header("content-type", "application/json") |
| 125 | + .with_body(response_body) |
| 126 | + .create_async() |
| 127 | + .await; |
| 128 | + |
| 129 | + let client = Client::new(base, None::<String>).unwrap(); |
| 130 | + let batches = client.get_batches().await.expect("list batches failed"); |
| 131 | + assert_eq!(batches.results.len(), 1); |
| 132 | + let b = &batches.results[0]; |
| 133 | + assert_eq!(b.uid, 42); |
| 134 | + assert_eq!(b.batch_strategy.as_deref(), Some("time_limit_reached")); |
| 135 | + } |
| 136 | + |
| 137 | + #[tokio::test] |
| 138 | + async fn test_get_batch_by_uid_parses_batch_strategy() { |
| 139 | + let mut s = mockito::Server::new_async().await; |
| 140 | + let base = s.url(); |
| 141 | + |
| 142 | + let response_body = serde_json::json!({ |
| 143 | + "uid": 99, |
| 144 | + "batchStrategy": "size_limit_reached", |
| 145 | + "taskUids": [10, 11] |
| 146 | + }) |
| 147 | + .to_string(); |
| 148 | + |
| 149 | + let _m = s |
| 150 | + .mock("GET", "/batches/99") |
| 151 | + .with_status(200) |
| 152 | + .with_header("content-type", "application/json") |
| 153 | + .with_body(response_body) |
| 154 | + .create_async() |
| 155 | + .await; |
| 156 | + |
| 157 | + let client = Client::new(base, None::<String>).unwrap(); |
| 158 | + let batch = client.get_batch(99).await.expect("get batch failed"); |
| 159 | + assert_eq!(batch.uid, 99); |
| 160 | + assert_eq!(batch.batch_strategy.as_deref(), Some("size_limit_reached")); |
| 161 | + } |
| 162 | +} |
0 commit comments