MeiliSearch/meilisearch-lib/src/index_controller/updates/status.rs

252 lines
5.2 KiB
Rust
Raw Normal View History

use std::{error::Error, fmt::Display};
2021-03-15 18:11:10 +01:00
use chrono::{DateTime, Utc};
use meilisearch_error::{Code, ErrorCode};
2021-09-14 18:39:02 +02:00
use milli::update::{DocumentAdditionResult, IndexDocumentsMethod};
2021-03-15 18:11:10 +01:00
use serde::{Deserialize, Serialize};
2021-01-28 14:12:34 +01:00
2021-09-28 22:22:59 +02:00
use crate::{
index::{Settings, Unchecked},
Update,
};
2021-09-14 18:39:02 +02:00
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,
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-09-28 20:20:13 +02:00
pub meta: Update,
2021-01-28 20:55:29 +01:00
pub enqueued_at: DateTime<Utc>,
2021-01-28 14:12:34 +01:00
}
2021-04-22 10:14:29 +02:00
impl Enqueued {
2021-09-28 20:20:13 +02:00
pub fn new(meta: Update, update_id: u64) -> 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
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-09-28 20:20:13 +02:00
pub fn meta(&self) -> &Update {
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()
}
2021-09-28 20:20:13 +02:00
pub fn meta(&self) -> &Update {
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-09-28 20:20:13 +02:00
pub fn meta(&self) -> &Update {
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(),
}
}
pub fn fail(self, error: impl ErrorCode) -> Failed {
let msg = error.to_string();
let code = error.error_code();
2021-01-28 14:12:34 +01:00
Failed {
from: self,
msg,
code,
2021-01-28 14:12:34 +01:00
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-09-29 15:41:25 +02:00
pub from: Enqueued,
pub aborted_at: DateTime<Utc>,
2021-01-28 14:12:34 +01:00
}
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()
}
2021-09-28 20:20:13 +02:00
pub fn meta(&self) -> &Update {
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,
pub msg: String,
pub code: Code,
pub failed_at: DateTime<Utc>,
2021-01-28 14:12:34 +01:00
}
impl Display for Failed {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.msg.fmt(f)
}
}
2021-09-28 22:22:59 +02:00
impl Error for Failed {}
impl ErrorCode for Failed {
fn error_code(&self) -> Code {
self.code
}
}
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()
}
2021-09-28 20:20:13 +02:00
pub fn meta(&self) -> &Update {
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
2021-09-28 20:20:13 +02:00
pub fn meta(&self) -> &Update {
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)
}
}