2022-06-05 06:49:36 +02:00
|
|
|
use std::error::Error;
|
|
|
|
use std::fmt::{self, Write};
|
2022-05-17 16:08:23 +02:00
|
|
|
use std::str::FromStr;
|
2022-02-14 15:32:41 +01:00
|
|
|
use std::write;
|
|
|
|
|
2021-12-02 16:03:26 +01:00
|
|
|
use meilisearch_lib::index::{Settings, Unchecked};
|
2022-01-19 11:21:19 +01:00
|
|
|
use meilisearch_lib::tasks::batch::BatchId;
|
2021-12-02 16:03:26 +01:00
|
|
|
use meilisearch_lib::tasks::task::{
|
|
|
|
DocumentDeletion, Task, TaskContent, TaskEvent, TaskId, TaskResult,
|
|
|
|
};
|
2022-06-06 12:38:46 +02:00
|
|
|
use meilisearch_types::error::ResponseError;
|
2022-05-17 16:08:23 +02:00
|
|
|
use serde::{Deserialize, Serialize, Serializer};
|
2022-02-14 15:32:41 +01:00
|
|
|
use time::{Duration, OffsetDateTime};
|
2021-12-02 16:03:26 +01:00
|
|
|
|
2022-01-19 11:21:19 +01:00
|
|
|
use crate::AUTOBATCHING_ENABLED;
|
|
|
|
|
2022-05-17 16:08:23 +02:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2021-12-02 16:03:26 +01:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2022-05-17 16:08:23 +02:00
|
|
|
pub enum TaskType {
|
2021-12-02 16:03:26 +01:00
|
|
|
IndexCreation,
|
|
|
|
IndexUpdate,
|
|
|
|
IndexDeletion,
|
2022-05-25 12:05:24 +02:00
|
|
|
DocumentAdditionOrUpdate,
|
2021-12-15 09:49:39 +01:00
|
|
|
DocumentDeletion,
|
2021-12-02 16:03:26 +01:00
|
|
|
SettingsUpdate,
|
2022-05-23 10:54:49 +02:00
|
|
|
DumpCreation,
|
2021-12-02 16:03:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<TaskContent> for TaskType {
|
|
|
|
fn from(other: TaskContent) -> Self {
|
|
|
|
match other {
|
|
|
|
TaskContent::IndexCreation { .. } => TaskType::IndexCreation,
|
|
|
|
TaskContent::IndexUpdate { .. } => TaskType::IndexUpdate,
|
2022-05-31 17:18:40 +02:00
|
|
|
TaskContent::IndexDeletion { .. } => TaskType::IndexDeletion,
|
2022-05-25 12:05:24 +02:00
|
|
|
TaskContent::DocumentAddition { .. } => TaskType::DocumentAdditionOrUpdate,
|
2022-05-31 17:18:40 +02:00
|
|
|
TaskContent::DocumentDeletion { .. } => TaskType::DocumentDeletion,
|
2022-05-25 12:05:24 +02:00
|
|
|
TaskContent::SettingsUpdate { .. } => TaskType::SettingsUpdate,
|
2022-05-23 10:54:49 +02:00
|
|
|
TaskContent::Dump { .. } => TaskType::DumpCreation,
|
2021-12-02 16:03:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-05 06:49:36 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct TaskTypeError {
|
|
|
|
invalid_type: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for TaskTypeError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"invalid task type `{}`, expecting one of: \
|
|
|
|
indexCreation, indexUpdate, indexDeletion, documentAdditionOrUpdate, \
|
|
|
|
documentDeletion, settingsUpdate, dumpCreation",
|
|
|
|
self.invalid_type
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error for TaskTypeError {}
|
|
|
|
|
2022-05-17 16:08:23 +02:00
|
|
|
impl FromStr for TaskType {
|
2022-06-05 06:49:36 +02:00
|
|
|
type Err = TaskTypeError;
|
2022-05-17 16:08:23 +02:00
|
|
|
|
2022-06-09 01:57:45 +02:00
|
|
|
fn from_str(type_: &str) -> Result<Self, TaskTypeError> {
|
|
|
|
if type_.eq_ignore_ascii_case("indexCreation") {
|
2022-05-25 12:05:24 +02:00
|
|
|
Ok(TaskType::IndexCreation)
|
2022-06-09 01:57:45 +02:00
|
|
|
} else if type_.eq_ignore_ascii_case("indexUpdate") {
|
2022-05-25 12:05:24 +02:00
|
|
|
Ok(TaskType::IndexUpdate)
|
2022-06-09 01:57:45 +02:00
|
|
|
} else if type_.eq_ignore_ascii_case("indexDeletion") {
|
2022-05-25 12:05:24 +02:00
|
|
|
Ok(TaskType::IndexDeletion)
|
2022-06-09 01:57:45 +02:00
|
|
|
} else if type_.eq_ignore_ascii_case("documentAdditionOrUpdate") {
|
2022-05-25 12:05:24 +02:00
|
|
|
Ok(TaskType::DocumentAdditionOrUpdate)
|
2022-06-09 01:57:45 +02:00
|
|
|
} else if type_.eq_ignore_ascii_case("documentDeletion") {
|
2022-05-25 12:05:24 +02:00
|
|
|
Ok(TaskType::DocumentDeletion)
|
2022-06-09 01:57:45 +02:00
|
|
|
} else if type_.eq_ignore_ascii_case("settingsUpdate") {
|
2022-05-25 12:05:24 +02:00
|
|
|
Ok(TaskType::SettingsUpdate)
|
2022-06-09 01:57:45 +02:00
|
|
|
} else if type_.eq_ignore_ascii_case("dumpCreation") {
|
2022-05-25 12:05:24 +02:00
|
|
|
Ok(TaskType::DumpCreation)
|
|
|
|
} else {
|
2022-06-05 06:49:36 +02:00
|
|
|
Err(TaskTypeError {
|
2022-06-09 01:57:45 +02:00
|
|
|
invalid_type: type_.to_string(),
|
2022-06-05 06:49:36 +02:00
|
|
|
})
|
2022-05-17 16:08:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
2021-12-02 16:03:26 +01:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2022-05-17 16:08:23 +02:00
|
|
|
pub enum TaskStatus {
|
2021-12-02 16:03:26 +01:00
|
|
|
Enqueued,
|
|
|
|
Processing,
|
|
|
|
Succeeded,
|
|
|
|
Failed,
|
|
|
|
}
|
|
|
|
|
2022-06-05 06:49:57 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct TaskStatusError {
|
|
|
|
invalid_status: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for TaskStatusError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"invalid task status `{}`, expecting one of: \
|
|
|
|
enqueued, processing, succeeded, or failed",
|
|
|
|
self.invalid_status,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error for TaskStatusError {}
|
|
|
|
|
2022-05-17 16:08:23 +02:00
|
|
|
impl FromStr for TaskStatus {
|
2022-06-05 06:49:57 +02:00
|
|
|
type Err = TaskStatusError;
|
2022-05-17 16:08:23 +02:00
|
|
|
|
2022-06-05 06:49:57 +02:00
|
|
|
fn from_str(status: &str) -> Result<Self, TaskStatusError> {
|
2022-05-25 12:05:24 +02:00
|
|
|
if status.eq_ignore_ascii_case("enqueued") {
|
|
|
|
Ok(TaskStatus::Enqueued)
|
|
|
|
} else if status.eq_ignore_ascii_case("processing") {
|
|
|
|
Ok(TaskStatus::Processing)
|
|
|
|
} else if status.eq_ignore_ascii_case("succeeded") {
|
|
|
|
Ok(TaskStatus::Succeeded)
|
|
|
|
} else if status.eq_ignore_ascii_case("failed") {
|
|
|
|
Ok(TaskStatus::Failed)
|
|
|
|
} else {
|
2022-06-05 06:49:57 +02:00
|
|
|
Err(TaskStatusError {
|
|
|
|
invalid_status: status.to_string(),
|
|
|
|
})
|
2022-05-17 16:08:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-02 16:03:26 +01:00
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
#[allow(clippy::large_enum_variant)]
|
|
|
|
enum TaskDetails {
|
|
|
|
#[serde(rename_all = "camelCase")]
|
2021-12-15 09:49:39 +01:00
|
|
|
DocumentAddition {
|
2021-12-02 16:03:26 +01:00
|
|
|
received_documents: usize,
|
|
|
|
indexed_documents: Option<u64>,
|
|
|
|
},
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
Settings {
|
|
|
|
#[serde(flatten)]
|
|
|
|
settings: Settings<Unchecked>,
|
|
|
|
},
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
IndexInfo { primary_key: Option<String> },
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
DocumentDeletion {
|
|
|
|
received_document_ids: usize,
|
|
|
|
deleted_documents: Option<u64>,
|
|
|
|
},
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
ClearAll { deleted_documents: Option<u64> },
|
2022-05-19 20:18:43 +02:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
Dump { dump_uid: String },
|
2021-12-02 16:03:26 +01:00
|
|
|
}
|
|
|
|
|
2022-02-14 15:32:41 +01:00
|
|
|
/// Serialize a `time::Duration` as a best effort ISO 8601 while waiting for
|
|
|
|
/// https://github.com/time-rs/time/issues/378.
|
|
|
|
/// This code is a port of the old code of time that was removed in 0.2.
|
2021-12-02 16:03:26 +01:00
|
|
|
fn serialize_duration<S: Serializer>(
|
|
|
|
duration: &Option<Duration>,
|
|
|
|
serializer: S,
|
|
|
|
) -> Result<S::Ok, S::Error> {
|
|
|
|
match duration {
|
|
|
|
Some(duration) => {
|
2022-02-14 15:32:41 +01:00
|
|
|
// technically speaking, negative duration is not valid ISO 8601
|
|
|
|
if duration.is_negative() {
|
|
|
|
return serializer.serialize_none();
|
|
|
|
}
|
|
|
|
|
|
|
|
const SECS_PER_DAY: i64 = Duration::DAY.whole_seconds();
|
|
|
|
let secs = duration.whole_seconds();
|
|
|
|
let days = secs / SECS_PER_DAY;
|
|
|
|
let secs = secs - days * SECS_PER_DAY;
|
|
|
|
let hasdate = days != 0;
|
|
|
|
let nanos = duration.subsec_nanoseconds();
|
|
|
|
let hastime = (secs != 0 || nanos != 0) || !hasdate;
|
|
|
|
|
|
|
|
// all the following unwrap can't fail
|
|
|
|
let mut res = String::new();
|
|
|
|
write!(&mut res, "P").unwrap();
|
|
|
|
|
|
|
|
if hasdate {
|
|
|
|
write!(&mut res, "{}D", days).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
const NANOS_PER_MILLI: i32 = Duration::MILLISECOND.subsec_nanoseconds();
|
|
|
|
const NANOS_PER_MICRO: i32 = Duration::MICROSECOND.subsec_nanoseconds();
|
|
|
|
|
|
|
|
if hastime {
|
|
|
|
if nanos == 0 {
|
|
|
|
write!(&mut res, "T{}S", secs).unwrap();
|
|
|
|
} else if nanos % NANOS_PER_MILLI == 0 {
|
|
|
|
write!(&mut res, "T{}.{:03}S", secs, nanos / NANOS_PER_MILLI).unwrap();
|
|
|
|
} else if nanos % NANOS_PER_MICRO == 0 {
|
|
|
|
write!(&mut res, "T{}.{:06}S", secs, nanos / NANOS_PER_MICRO).unwrap();
|
|
|
|
} else {
|
|
|
|
write!(&mut res, "T{}.{:09}S", secs, nanos).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
serializer.serialize_str(&res)
|
2021-12-02 16:03:26 +01:00
|
|
|
}
|
|
|
|
None => serializer.serialize_none(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct TaskView {
|
2022-05-31 11:56:51 +02:00
|
|
|
pub uid: TaskId,
|
2022-05-16 19:50:45 +02:00
|
|
|
index_uid: Option<String>,
|
2021-12-02 16:03:26 +01:00
|
|
|
status: TaskStatus,
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
task_type: TaskType,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
details: Option<TaskDetails>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
error: Option<ResponseError>,
|
|
|
|
#[serde(serialize_with = "serialize_duration")]
|
|
|
|
duration: Option<Duration>,
|
2022-02-14 15:32:41 +01:00
|
|
|
#[serde(serialize_with = "time::serde::rfc3339::serialize")]
|
|
|
|
enqueued_at: OffsetDateTime,
|
|
|
|
#[serde(serialize_with = "time::serde::rfc3339::option::serialize")]
|
|
|
|
started_at: Option<OffsetDateTime>,
|
|
|
|
#[serde(serialize_with = "time::serde::rfc3339::option::serialize")]
|
|
|
|
finished_at: Option<OffsetDateTime>,
|
2022-01-19 11:21:19 +01:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
batch_uid: Option<Option<BatchId>>,
|
2021-12-02 16:03:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Task> for TaskView {
|
|
|
|
fn from(task: Task) -> Self {
|
2022-05-31 17:18:40 +02:00
|
|
|
let index_uid = task.index_uid().map(String::from);
|
2021-12-02 16:03:26 +01:00
|
|
|
let Task {
|
|
|
|
id,
|
|
|
|
content,
|
|
|
|
events,
|
|
|
|
} = task;
|
|
|
|
|
|
|
|
let (task_type, mut details) = match content {
|
|
|
|
TaskContent::DocumentAddition {
|
2022-05-25 12:05:24 +02:00
|
|
|
documents_count, ..
|
2021-12-02 16:03:26 +01:00
|
|
|
} => {
|
2021-12-15 09:49:39 +01:00
|
|
|
let details = TaskDetails::DocumentAddition {
|
2021-12-02 16:03:26 +01:00
|
|
|
received_documents: documents_count,
|
|
|
|
indexed_documents: None,
|
|
|
|
};
|
|
|
|
|
2022-05-25 12:05:24 +02:00
|
|
|
(TaskType::DocumentAdditionOrUpdate, Some(details))
|
2021-12-02 16:03:26 +01:00
|
|
|
}
|
2022-05-31 17:18:40 +02:00
|
|
|
TaskContent::DocumentDeletion {
|
|
|
|
deletion: DocumentDeletion::Ids(ids),
|
|
|
|
..
|
|
|
|
} => (
|
2021-12-15 09:49:39 +01:00
|
|
|
TaskType::DocumentDeletion,
|
2021-12-02 16:03:26 +01:00
|
|
|
Some(TaskDetails::DocumentDeletion {
|
|
|
|
received_document_ids: ids.len(),
|
|
|
|
deleted_documents: None,
|
|
|
|
}),
|
|
|
|
),
|
2022-05-31 17:18:40 +02:00
|
|
|
TaskContent::DocumentDeletion {
|
|
|
|
deletion: DocumentDeletion::Clear,
|
|
|
|
..
|
|
|
|
} => (
|
2022-05-25 12:05:24 +02:00
|
|
|
TaskType::DocumentDeletion,
|
2021-12-02 16:03:26 +01:00
|
|
|
Some(TaskDetails::ClearAll {
|
|
|
|
deleted_documents: None,
|
|
|
|
}),
|
|
|
|
),
|
2022-05-31 17:18:40 +02:00
|
|
|
TaskContent::IndexDeletion { .. } => (
|
2021-12-02 16:03:26 +01:00
|
|
|
TaskType::IndexDeletion,
|
|
|
|
Some(TaskDetails::ClearAll {
|
|
|
|
deleted_documents: None,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
TaskContent::SettingsUpdate { settings, .. } => (
|
|
|
|
TaskType::SettingsUpdate,
|
|
|
|
Some(TaskDetails::Settings { settings }),
|
|
|
|
),
|
2022-05-31 17:18:40 +02:00
|
|
|
TaskContent::IndexCreation { primary_key, .. } => (
|
2021-12-02 16:03:26 +01:00
|
|
|
TaskType::IndexCreation,
|
|
|
|
Some(TaskDetails::IndexInfo { primary_key }),
|
|
|
|
),
|
2022-05-31 17:18:40 +02:00
|
|
|
TaskContent::IndexUpdate { primary_key, .. } => (
|
2021-12-02 16:03:26 +01:00
|
|
|
TaskType::IndexUpdate,
|
|
|
|
Some(TaskDetails::IndexInfo { primary_key }),
|
|
|
|
),
|
2022-05-23 10:54:49 +02:00
|
|
|
TaskContent::Dump { uid } => (
|
|
|
|
TaskType::DumpCreation,
|
|
|
|
Some(TaskDetails::Dump { dump_uid: uid }),
|
|
|
|
),
|
2021-12-02 16:03:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// An event always has at least one event: "Created"
|
|
|
|
let (status, error, finished_at) = match events.last().unwrap() {
|
|
|
|
TaskEvent::Created(_) => (TaskStatus::Enqueued, None, None),
|
|
|
|
TaskEvent::Batched { .. } => (TaskStatus::Enqueued, None, None),
|
|
|
|
TaskEvent::Processing(_) => (TaskStatus::Processing, None, None),
|
2022-05-30 16:42:51 +02:00
|
|
|
TaskEvent::Succeeded { timestamp, result } => {
|
2021-12-02 16:03:26 +01:00
|
|
|
match (result, &mut details) {
|
|
|
|
(
|
|
|
|
TaskResult::DocumentAddition {
|
|
|
|
indexed_documents: num,
|
|
|
|
..
|
|
|
|
},
|
2021-12-15 09:49:39 +01:00
|
|
|
Some(TaskDetails::DocumentAddition {
|
2021-12-02 16:03:26 +01:00
|
|
|
ref mut indexed_documents,
|
|
|
|
..
|
|
|
|
}),
|
|
|
|
) => {
|
|
|
|
indexed_documents.replace(*num);
|
|
|
|
}
|
|
|
|
(
|
|
|
|
TaskResult::DocumentDeletion {
|
|
|
|
deleted_documents: docs,
|
|
|
|
..
|
|
|
|
},
|
|
|
|
Some(TaskDetails::DocumentDeletion {
|
|
|
|
ref mut deleted_documents,
|
|
|
|
..
|
|
|
|
}),
|
|
|
|
) => {
|
|
|
|
deleted_documents.replace(*docs);
|
|
|
|
}
|
|
|
|
(
|
|
|
|
TaskResult::ClearAll {
|
|
|
|
deleted_documents: docs,
|
|
|
|
},
|
|
|
|
Some(TaskDetails::ClearAll {
|
|
|
|
ref mut deleted_documents,
|
|
|
|
}),
|
|
|
|
) => {
|
|
|
|
deleted_documents.replace(*docs);
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
(TaskStatus::Succeeded, None, Some(*timestamp))
|
|
|
|
}
|
|
|
|
TaskEvent::Failed { timestamp, error } => {
|
2022-01-06 13:18:47 +01:00
|
|
|
match details {
|
|
|
|
Some(TaskDetails::DocumentDeletion {
|
|
|
|
ref mut deleted_documents,
|
|
|
|
..
|
|
|
|
}) => {
|
|
|
|
deleted_documents.replace(0);
|
|
|
|
}
|
|
|
|
Some(TaskDetails::ClearAll {
|
|
|
|
ref mut deleted_documents,
|
|
|
|
..
|
|
|
|
}) => {
|
|
|
|
deleted_documents.replace(0);
|
|
|
|
}
|
|
|
|
Some(TaskDetails::DocumentAddition {
|
|
|
|
ref mut indexed_documents,
|
|
|
|
..
|
|
|
|
}) => {
|
|
|
|
indexed_documents.replace(0);
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
2021-12-02 16:03:26 +01:00
|
|
|
(TaskStatus::Failed, Some(error.clone()), Some(*timestamp))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let enqueued_at = match events.first() {
|
|
|
|
Some(TaskEvent::Created(ts)) => *ts,
|
|
|
|
_ => unreachable!("A task must always have a creation event."),
|
|
|
|
};
|
|
|
|
|
|
|
|
let started_at = events.iter().find_map(|e| match e {
|
|
|
|
TaskEvent::Processing(ts) => Some(*ts),
|
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
|
2022-01-11 18:01:25 +01:00
|
|
|
let duration = finished_at.zip(started_at).map(|(tf, ts)| (tf - ts));
|
|
|
|
|
2022-01-19 11:21:19 +01:00
|
|
|
let batch_uid = if AUTOBATCHING_ENABLED.load(std::sync::atomic::Ordering::Relaxed) {
|
|
|
|
let id = events.iter().find_map(|e| match e {
|
|
|
|
TaskEvent::Batched { batch_id, .. } => Some(*batch_id),
|
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
Some(id)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2021-12-02 16:03:26 +01:00
|
|
|
Self {
|
|
|
|
uid: id,
|
2022-05-31 17:18:40 +02:00
|
|
|
index_uid,
|
2021-12-02 16:03:26 +01:00
|
|
|
status,
|
|
|
|
task_type,
|
|
|
|
details,
|
|
|
|
error,
|
|
|
|
duration,
|
|
|
|
enqueued_at,
|
|
|
|
started_at,
|
|
|
|
finished_at,
|
2022-01-19 11:21:19 +01:00
|
|
|
batch_uid,
|
2021-12-02 16:03:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
pub struct TaskListView {
|
2022-05-31 11:56:51 +02:00
|
|
|
pub results: Vec<TaskView>,
|
|
|
|
pub limit: usize,
|
2022-06-01 15:30:39 +02:00
|
|
|
pub from: Option<TaskId>,
|
|
|
|
pub next: Option<TaskId>,
|
2021-12-02 16:03:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct SummarizedTaskView {
|
2022-05-17 11:17:32 +02:00
|
|
|
task_uid: TaskId,
|
2022-05-16 19:50:45 +02:00
|
|
|
index_uid: Option<String>,
|
2021-12-02 16:03:26 +01:00
|
|
|
status: TaskStatus,
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
task_type: TaskType,
|
2022-02-14 15:32:41 +01:00
|
|
|
#[serde(serialize_with = "time::serde::rfc3339::serialize")]
|
|
|
|
enqueued_at: OffsetDateTime,
|
2021-12-02 16:03:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Task> for SummarizedTaskView {
|
|
|
|
fn from(mut other: Task) -> Self {
|
|
|
|
let created_event = other
|
|
|
|
.events
|
|
|
|
.drain(..1)
|
|
|
|
.next()
|
|
|
|
.expect("Task must have an enqueued event.");
|
|
|
|
|
|
|
|
let enqueued_at = match created_event {
|
|
|
|
TaskEvent::Created(ts) => ts,
|
|
|
|
_ => unreachable!("The first event of a task must always be 'Created'"),
|
|
|
|
};
|
|
|
|
|
|
|
|
Self {
|
2022-05-17 11:17:32 +02:00
|
|
|
task_uid: other.id,
|
2022-05-31 17:18:40 +02:00
|
|
|
index_uid: other.index_uid().map(String::from),
|
2021-12-02 16:03:26 +01:00
|
|
|
status: TaskStatus::Enqueued,
|
|
|
|
task_type: other.content.into(),
|
|
|
|
enqueued_at,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|