meilisearch/meilisearch-http/src/index_controller/updates.rs

187 lines
4.1 KiB
Rust
Raw Normal View History

2021-03-16 01:11:10 +08:00
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
2021-02-27 01:11:43 +08:00
use uuid::Uuid;
2021-01-28 21:12:34 +08:00
#[derive(Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Clone)]
2021-02-19 00:48:37 +08:00
#[serde(rename_all = "camelCase")]
2021-01-28 21:12:34 +08:00
pub struct Pending<M> {
2021-01-29 03:55:29 +08:00
pub update_id: u64,
pub meta: M,
pub enqueued_at: DateTime<Utc>,
2021-02-27 01:11:43 +08:00
pub index_uuid: Uuid,
2021-01-28 21:12:34 +08:00
}
impl<M> Pending<M> {
2021-02-27 01:11:43 +08:00
pub fn new(meta: M, update_id: u64, index_uuid: Uuid) -> Self {
2021-01-28 21:12:34 +08:00
Self {
enqueued_at: Utc::now(),
meta,
update_id,
2021-02-27 01:11:43 +08:00
index_uuid,
2021-01-28 21:12:34 +08:00
}
}
pub fn processing(self) -> Processing<M> {
Processing {
from: self,
started_processing_at: Utc::now(),
}
}
pub fn abort(self) -> Aborted<M> {
Aborted {
from: self,
aborted_at: Utc::now(),
}
}
pub fn meta(&self) -> &M {
&self.meta
}
pub fn id(&self) -> u64 {
self.update_id
}
}
#[derive(Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Clone)]
2021-02-19 05:10:50 +08:00
#[serde(rename_all = "camelCase")]
2021-01-28 21:12:34 +08:00
pub struct Processed<M, N> {
2021-01-29 03:55:29 +08:00
pub success: N,
pub processed_at: DateTime<Utc>,
2021-01-28 21:12:34 +08:00
#[serde(flatten)]
2021-01-29 03:55:29 +08:00
pub from: Processing<M>,
2021-01-28 21:12:34 +08:00
}
impl<M, N> Processed<M, N> {
pub fn id(&self) -> u64 {
self.from.id()
}
}
#[derive(Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Clone)]
2021-02-19 05:10:50 +08:00
#[serde(rename_all = "camelCase")]
2021-01-28 21:12:34 +08:00
pub struct Processing<M> {
#[serde(flatten)]
2021-01-29 03:55:29 +08:00
pub from: Pending<M>,
pub started_processing_at: DateTime<Utc>,
2021-01-28 21:12:34 +08:00
}
impl<M> Processing<M> {
pub fn id(&self) -> u64 {
self.from.id()
}
pub fn meta(&self) -> &M {
self.from.meta()
}
2021-02-27 01:11:43 +08:00
pub fn index_uuid(&self) -> &Uuid {
&self.from.index_uuid
}
2021-01-28 21:12:34 +08:00
pub fn process<N>(self, meta: N) -> Processed<M, N> {
Processed {
success: meta,
from: self,
processed_at: Utc::now(),
}
}
pub fn fail<E>(self, error: E) -> Failed<M, E> {
Failed {
from: self,
error,
failed_at: Utc::now(),
}
}
}
#[derive(Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Clone)]
2021-02-19 05:10:50 +08:00
#[serde(rename_all = "camelCase")]
2021-01-28 21:12:34 +08:00
pub struct Aborted<M> {
#[serde(flatten)]
from: Pending<M>,
aborted_at: DateTime<Utc>,
}
impl<M> Aborted<M> {
pub fn id(&self) -> u64 {
self.from.id()
}
}
#[derive(Debug, PartialEq, Eq, Hash, Serialize, Deserialize, Clone)]
2021-02-19 05:10:50 +08:00
#[serde(rename_all = "camelCase")]
2021-01-28 21:12:34 +08:00
pub struct Failed<M, E> {
#[serde(flatten)]
from: Processing<M>,
error: E,
failed_at: DateTime<Utc>,
}
impl<M, E> Failed<M, E> {
pub fn id(&self) -> u64 {
self.from.id()
}
}
#[derive(Debug, PartialEq, Eq, Hash, Serialize)]
2021-02-19 00:48:37 +08:00
#[serde(tag = "status", rename_all = "camelCase")]
2021-01-28 21:12:34 +08:00
pub enum UpdateStatus<M, N, E> {
Processing(Processing<M>),
Pending(Pending<M>),
Processed(Processed<M, N>),
Aborted(Aborted<M>),
Failed(Failed<M, E>),
}
impl<M, N, E> UpdateStatus<M, N, E> {
pub fn id(&self) -> u64 {
match self {
UpdateStatus::Processing(u) => u.id(),
UpdateStatus::Pending(u) => u.id(),
UpdateStatus::Processed(u) => u.id(),
UpdateStatus::Aborted(u) => u.id(),
UpdateStatus::Failed(u) => u.id(),
}
}
2021-02-04 00:44:20 +08:00
pub fn processed(&self) -> Option<&Processed<M, N>> {
match self {
UpdateStatus::Processed(p) => Some(p),
_ => None,
}
}
2021-01-28 21:12:34 +08:00
}
impl<M, N, E> From<Pending<M>> for UpdateStatus<M, N, E> {
fn from(other: Pending<M>) -> Self {
Self::Pending(other)
}
}
impl<M, N, E> From<Aborted<M>> for UpdateStatus<M, N, E> {
fn from(other: Aborted<M>) -> Self {
Self::Aborted(other)
}
}
impl<M, N, E> From<Processed<M, N>> for UpdateStatus<M, N, E> {
fn from(other: Processed<M, N>) -> Self {
Self::Processed(other)
}
}
impl<M, N, E> From<Processing<M>> for UpdateStatus<M, N, E> {
fn from(other: Processing<M>) -> Self {
Self::Processing(other)
}
}
impl<M, N, E> From<Failed<M, E>> for UpdateStatus<M, N, E> {
fn from(other: Failed<M, E>) -> Self {
Self::Failed(other)
}
}