2021-10-27 18:16:13 +02:00
|
|
|
mod mock_analytics;
|
|
|
|
// if we are in release mode and the feature analytics was enabled
|
|
|
|
#[cfg(all(not(debug_assertions), feature = "analytics"))]
|
|
|
|
mod segment_analytics;
|
|
|
|
|
|
|
|
use std::fs;
|
|
|
|
use std::path::{Path, PathBuf};
|
2022-10-13 15:02:59 +02:00
|
|
|
use std::str::FromStr;
|
2021-10-27 18:16:13 +02:00
|
|
|
|
|
|
|
use actix_web::HttpRequest;
|
2022-10-13 15:02:59 +02:00
|
|
|
use meilisearch_types::InstanceUid;
|
2022-10-20 18:00:07 +02:00
|
|
|
pub use mock_analytics::MockAnalytics;
|
2021-10-27 18:16:13 +02:00
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
use platform_dirs::AppDirs;
|
|
|
|
use serde_json::Value;
|
|
|
|
|
|
|
|
use crate::routes::indexes::documents::UpdateDocumentsQuery;
|
2023-01-11 20:33:07 +01:00
|
|
|
use crate::routes::tasks::TasksFilterQuery;
|
2021-10-27 18:16:13 +02:00
|
|
|
|
|
|
|
// if we are in debug mode OR the analytics feature is disabled
|
|
|
|
// the `SegmentAnalytics` point to the mock instead of the real analytics
|
|
|
|
#[cfg(any(debug_assertions, not(feature = "analytics")))]
|
2021-10-28 16:28:41 +02:00
|
|
|
pub type SegmentAnalytics = mock_analytics::MockAnalytics;
|
|
|
|
#[cfg(any(debug_assertions, not(feature = "analytics")))]
|
|
|
|
pub type SearchAggregator = mock_analytics::SearchAggregator;
|
2021-10-27 18:16:13 +02:00
|
|
|
|
|
|
|
// if we are in release mode and the feature analytics was enabled
|
|
|
|
// we use the real analytics
|
|
|
|
#[cfg(all(not(debug_assertions), feature = "analytics"))]
|
|
|
|
pub type SegmentAnalytics = segment_analytics::SegmentAnalytics;
|
2021-10-28 16:28:41 +02:00
|
|
|
#[cfg(all(not(debug_assertions), feature = "analytics"))]
|
|
|
|
pub type SearchAggregator = segment_analytics::SearchAggregator;
|
2021-10-27 18:16:13 +02:00
|
|
|
|
2022-01-26 17:43:16 +01:00
|
|
|
/// The Meilisearch config dir:
|
|
|
|
/// `~/.config/Meilisearch` on *NIX or *BSD.
|
2021-10-27 18:16:13 +02:00
|
|
|
/// `~/Library/ApplicationSupport` on macOS.
|
|
|
|
/// `%APPDATA` (= `C:\Users%USERNAME%\AppData\Roaming`) on windows.
|
|
|
|
static MEILISEARCH_CONFIG_PATH: Lazy<Option<PathBuf>> =
|
2022-01-26 17:43:16 +01:00
|
|
|
Lazy::new(|| AppDirs::new(Some("Meilisearch"), false).map(|appdir| appdir.config_dir));
|
2021-10-27 18:16:13 +02:00
|
|
|
|
|
|
|
fn config_user_id_path(db_path: &Path) -> Option<PathBuf> {
|
|
|
|
db_path
|
|
|
|
.canonicalize()
|
|
|
|
.ok()
|
2022-10-20 18:00:07 +02:00
|
|
|
.map(|path| path.join("instance-uid").display().to_string().replace('/', "-"))
|
2021-10-27 18:16:13 +02:00
|
|
|
.zip(MEILISEARCH_CONFIG_PATH.as_ref())
|
|
|
|
.map(|(filename, config_path)| config_path.join(filename.trim_start_matches('-')))
|
|
|
|
}
|
|
|
|
|
2022-01-26 17:43:16 +01:00
|
|
|
/// Look for the instance-uid in the `data.ms` or in `~/.config/Meilisearch/path-to-db-instance-uid`
|
2022-10-13 15:02:59 +02:00
|
|
|
fn find_user_id(db_path: &Path) -> Option<InstanceUid> {
|
2021-10-27 18:16:13 +02:00
|
|
|
fs::read_to_string(db_path.join("instance-uid"))
|
|
|
|
.ok()
|
2022-12-19 14:12:26 +01:00
|
|
|
.or_else(|| fs::read_to_string(config_user_id_path(db_path)?).ok())
|
2022-10-13 15:02:59 +02:00
|
|
|
.and_then(|uid| InstanceUid::from_str(&uid).ok())
|
2021-10-27 18:16:13 +02:00
|
|
|
}
|
|
|
|
|
2022-11-28 16:27:41 +01:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
|
|
|
pub enum DocumentDeletionKind {
|
|
|
|
PerDocumentId,
|
|
|
|
ClearAll,
|
|
|
|
PerBatch,
|
|
|
|
}
|
|
|
|
|
2021-10-29 15:58:06 +02:00
|
|
|
pub trait Analytics: Sync + Send {
|
2022-10-13 15:02:59 +02:00
|
|
|
fn instance_uid(&self) -> Option<&InstanceUid>;
|
|
|
|
|
2021-10-27 18:16:13 +02:00
|
|
|
/// The method used to publish most analytics that do not need to be batched every hours
|
2021-10-29 15:58:06 +02:00
|
|
|
fn publish(&self, event_name: String, send: Value, request: Option<&HttpRequest>);
|
2021-10-27 18:16:13 +02:00
|
|
|
|
2022-06-11 17:21:05 +02:00
|
|
|
/// This method should be called to aggregate a get search
|
2021-10-29 15:58:06 +02:00
|
|
|
fn get_search(&self, aggregate: SearchAggregator);
|
2021-10-27 18:16:13 +02:00
|
|
|
|
2021-10-28 16:28:41 +02:00
|
|
|
/// This method should be called to aggregate a post search
|
2021-10-29 15:58:06 +02:00
|
|
|
fn post_search(&self, aggregate: SearchAggregator);
|
2021-10-27 18:16:13 +02:00
|
|
|
|
2021-10-28 16:28:41 +02:00
|
|
|
// this method should be called to aggregate a add documents request
|
2021-10-27 18:16:13 +02:00
|
|
|
fn add_documents(
|
2021-10-29 15:58:06 +02:00
|
|
|
&self,
|
2021-10-27 18:16:13 +02:00
|
|
|
documents_query: &UpdateDocumentsQuery,
|
|
|
|
index_creation: bool,
|
|
|
|
request: &HttpRequest,
|
|
|
|
);
|
2022-11-28 16:27:41 +01:00
|
|
|
|
|
|
|
// this method should be called to aggregate a add documents request
|
|
|
|
fn delete_documents(&self, kind: DocumentDeletionKind, request: &HttpRequest);
|
|
|
|
|
2021-10-27 18:16:13 +02:00
|
|
|
// this method should be called to batch a update documents request
|
|
|
|
fn update_documents(
|
2021-10-29 15:58:06 +02:00
|
|
|
&self,
|
2021-10-27 18:16:13 +02:00
|
|
|
documents_query: &UpdateDocumentsQuery,
|
|
|
|
index_creation: bool,
|
|
|
|
request: &HttpRequest,
|
|
|
|
);
|
2022-11-28 16:27:41 +01:00
|
|
|
|
|
|
|
// this method should be called to aggregate the get tasks requests.
|
2023-01-11 20:33:07 +01:00
|
|
|
fn get_tasks(&self, query: &TasksFilterQuery, request: &HttpRequest);
|
2022-11-28 16:27:41 +01:00
|
|
|
|
|
|
|
// this method should be called to aggregate a add documents request
|
|
|
|
fn health_seen(&self, request: &HttpRequest);
|
2021-10-27 18:16:13 +02:00
|
|
|
}
|