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

234 lines
5.0 KiB
Rust
Raw Normal View History

2021-03-16 01:11:10 +08:00
use chrono::{DateTime, Utc};
2021-04-22 16:14:29 +08:00
use milli::update::{DocumentAdditionResult, IndexDocumentsMethod, UpdateFormat};
2021-03-16 01:11:10 +08:00
use serde::{Deserialize, Serialize};
2021-05-29 06:08:17 +08:00
use uuid::Uuid;
2021-01-28 21:12:34 +08:00
2021-06-23 20:48:33 +08:00
use crate::{
error::ResponseError,
index::{Settings, Unchecked},
};
2021-04-22 16:14:29 +08:00
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum UpdateResult {
DocumentsAddition(DocumentAdditionResult),
DocumentDeletion { deleted: u64 },
Other,
}
2021-06-14 16:34:16 +08:00
#[allow(clippy::large_enum_variant)]
2021-04-22 16:14:29 +08:00
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum UpdateMeta {
DocumentsAddition {
method: IndexDocumentsMethod,
format: UpdateFormat,
primary_key: Option<String>,
},
ClearDocuments,
2021-06-09 23:10:10 +08:00
DeleteDocuments {
2021-06-15 23:39:07 +08:00
ids: Vec<String>,
2021-06-09 23:10:10 +08:00
},
Settings(Settings<Unchecked>),
2021-04-22 16:14:29 +08:00
}
#[derive(Debug, Serialize, Deserialize, Clone)]
2021-02-19 00:48:37 +08:00
#[serde(rename_all = "camelCase")]
2021-04-22 16:14:29 +08:00
pub struct Enqueued {
2021-01-29 03:55:29 +08:00
pub update_id: u64,
2021-04-22 16:14:29 +08:00
pub meta: UpdateMeta,
2021-01-29 03:55:29 +08:00
pub enqueued_at: DateTime<Utc>,
2021-05-29 06:08:17 +08:00
pub content: Option<Uuid>,
2021-01-28 21:12:34 +08:00
}
2021-04-22 16:14:29 +08:00
impl Enqueued {
2021-05-29 06:08:17 +08:00
pub fn new(meta: UpdateMeta, update_id: u64, content: Option<Uuid>) -> Self {
2021-01-28 21:12:34 +08:00
Self {
enqueued_at: Utc::now(),
meta,
update_id,
2021-04-22 16:14:29 +08:00
content,
2021-01-28 21:12:34 +08:00
}
}
2021-04-22 16:14:29 +08:00
pub fn processing(self) -> Processing {
2021-01-28 21:12:34 +08:00
Processing {
from: self,
started_processing_at: Utc::now(),
}
}
2021-04-22 16:14:29 +08:00
pub fn abort(self) -> Aborted {
2021-01-28 21:12:34 +08:00
Aborted {
from: self,
aborted_at: Utc::now(),
}
}
2021-04-22 16:14:29 +08:00
pub fn meta(&self) -> &UpdateMeta {
2021-01-28 21:12:34 +08:00
&self.meta
}
pub fn id(&self) -> u64 {
self.update_id
}
}
2021-04-22 16:14:29 +08:00
#[derive(Debug, Serialize, Deserialize, Clone)]
2021-02-19 05:10:50 +08:00
#[serde(rename_all = "camelCase")]
2021-04-22 16:14:29 +08:00
pub struct Processed {
pub success: UpdateResult,
2021-01-29 03:55:29 +08:00
pub processed_at: DateTime<Utc>,
2021-01-28 21:12:34 +08:00
#[serde(flatten)]
2021-04-22 16:14:29 +08:00
pub from: Processing,
2021-01-28 21:12:34 +08:00
}
2021-04-22 16:14:29 +08:00
impl Processed {
2021-01-28 21:12:34 +08:00
pub fn id(&self) -> u64 {
self.from.id()
}
pub fn meta(&self) -> &UpdateMeta {
self.from.meta()
}
2021-01-28 21:12:34 +08:00
}
2021-04-22 16:14:29 +08:00
#[derive(Debug, Serialize, Deserialize, Clone)]
2021-02-19 05:10:50 +08:00
#[serde(rename_all = "camelCase")]
2021-04-22 16:14:29 +08:00
pub struct Processing {
2021-01-28 21:12:34 +08:00
#[serde(flatten)]
2021-04-22 16:14:29 +08:00
pub from: Enqueued,
2021-01-29 03:55:29 +08:00
pub started_processing_at: DateTime<Utc>,
2021-01-28 21:12:34 +08:00
}
2021-04-22 16:14:29 +08:00
impl Processing {
2021-01-28 21:12:34 +08:00
pub fn id(&self) -> u64 {
self.from.id()
}
2021-04-22 16:14:29 +08:00
pub fn meta(&self) -> &UpdateMeta {
2021-01-28 21:12:34 +08:00
self.from.meta()
}
2021-04-22 16:14:29 +08:00
pub fn process(self, success: UpdateResult) -> Processed {
2021-01-28 21:12:34 +08:00
Processed {
2021-04-22 16:14:29 +08:00
success,
2021-01-28 21:12:34 +08:00
from: self,
processed_at: Utc::now(),
}
}
2021-06-22 00:42:47 +08:00
pub fn fail(self, error: ResponseError) -> Failed {
2021-01-28 21:12:34 +08:00
Failed {
from: self,
error,
failed_at: Utc::now(),
}
}
}
2021-04-22 16:14:29 +08:00
#[derive(Debug, Serialize, Deserialize, Clone)]
2021-02-19 05:10:50 +08:00
#[serde(rename_all = "camelCase")]
2021-04-22 16:14:29 +08:00
pub struct Aborted {
2021-01-28 21:12:34 +08:00
#[serde(flatten)]
2021-04-22 16:14:29 +08:00
from: Enqueued,
2021-01-28 21:12:34 +08:00
aborted_at: DateTime<Utc>,
}
2021-04-22 16:14:29 +08:00
impl Aborted {
2021-01-28 21:12:34 +08:00
pub fn id(&self) -> u64 {
self.from.id()
}
pub fn meta(&self) -> &UpdateMeta {
self.from.meta()
}
2021-01-28 21:12:34 +08:00
}
2021-06-22 00:42:47 +08:00
#[derive(Debug, Serialize, Deserialize)]
2021-02-19 05:10:50 +08:00
#[serde(rename_all = "camelCase")]
2021-04-22 16:14:29 +08:00
pub struct Failed {
2021-01-28 21:12:34 +08:00
#[serde(flatten)]
pub from: Processing,
2021-06-22 00:42:47 +08:00
pub error: ResponseError,
pub failed_at: DateTime<Utc>,
2021-01-28 21:12:34 +08:00
}
2021-04-22 16:14:29 +08:00
impl Failed {
2021-01-28 21:12:34 +08:00
pub fn id(&self) -> u64 {
self.from.id()
}
pub fn meta(&self) -> &UpdateMeta {
self.from.meta()
}
2021-01-28 21:12:34 +08:00
}
2021-06-22 00:42:47 +08:00
#[derive(Debug, Serialize, Deserialize)]
2021-02-19 00:48:37 +08:00
#[serde(tag = "status", rename_all = "camelCase")]
2021-04-22 16:14:29 +08:00
pub enum UpdateStatus {
Processing(Processing),
Enqueued(Enqueued),
Processed(Processed),
Aborted(Aborted),
Failed(Failed),
2021-01-28 21:12:34 +08:00
}
2021-04-22 16:14:29 +08:00
impl UpdateStatus {
2021-01-28 21:12:34 +08:00
pub fn id(&self) -> u64 {
match self {
UpdateStatus::Processing(u) => u.id(),
2021-04-08 01:46:36 +08:00
UpdateStatus::Enqueued(u) => u.id(),
2021-01-28 21:12:34 +08:00
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 meta(&self) -> &UpdateMeta {
match self {
UpdateStatus::Processing(u) => u.meta(),
UpdateStatus::Enqueued(u) => u.meta(),
UpdateStatus::Processed(u) => u.meta(),
UpdateStatus::Aborted(u) => u.meta(),
UpdateStatus::Failed(u) => u.meta(),
}
}
2021-04-22 16:14:29 +08:00
pub fn processed(&self) -> Option<&Processed> {
2021-02-04 00:44:20 +08:00
match self {
UpdateStatus::Processed(p) => Some(p),
_ => None,
}
}
2021-01-28 21:12:34 +08:00
}
2021-04-22 16:14:29 +08:00
impl From<Enqueued> for UpdateStatus {
fn from(other: Enqueued) -> Self {
2021-04-08 01:46:36 +08:00
Self::Enqueued(other)
2021-01-28 21:12:34 +08:00
}
}
2021-04-22 16:14:29 +08:00
impl From<Aborted> for UpdateStatus {
fn from(other: Aborted) -> Self {
2021-01-28 21:12:34 +08:00
Self::Aborted(other)
}
}
2021-04-22 16:14:29 +08:00
impl From<Processed> for UpdateStatus {
fn from(other: Processed) -> Self {
2021-01-28 21:12:34 +08:00
Self::Processed(other)
}
}
2021-04-22 16:14:29 +08:00
impl From<Processing> for UpdateStatus {
fn from(other: Processing) -> Self {
2021-01-28 21:12:34 +08:00
Self::Processing(other)
}
}
2021-04-22 16:14:29 +08:00
impl From<Failed> for UpdateStatus {
fn from(other: Failed) -> Self {
2021-01-28 21:12:34 +08:00
Self::Failed(other)
}
}