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

234 lines
5.0 KiB
Rust
Raw Normal View History

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