makes the analytics available for all the routes

This commit is contained in:
Tamo 2021-10-12 14:32:44 +02:00 committed by marin postma
parent 664d09e86a
commit d72c887422
No known key found for this signature in database
GPG Key ID: 6088B7721C3E39F9
6 changed files with 69 additions and 21 deletions

View File

@ -151,6 +151,7 @@ mod segment {
impl super::Analytics for SegmentAnalytics {
fn publish(&'static self, event_name: String, send: Value) {
tokio::spawn(async move {
println!("ANALYTICS pushing {} in the batcher", event_name);
let _ = self
.batcher
.lock()
@ -162,6 +163,7 @@ mod segment {
..Default::default()
})
.await;
println!("ANALYTICS {} pushed", event_name);
});
}
}
@ -205,6 +207,6 @@ impl Display for MockAnalytics {
}
#[async_trait::async_trait]
pub trait Analytics: Display {
pub trait Analytics: Display + Sync + Send {
fn publish(&'static self, event_name: String, send: Value);
}

View File

@ -12,6 +12,7 @@ use std::time::Duration;
use crate::error::MeilisearchHttpError;
use crate::extractors::authentication::AuthConfig;
use actix_web::error::JsonPayloadError;
use analytics::Analytics;
use error::PayloadError;
use http::header::CONTENT_TYPE;
pub use option::Opt;
@ -73,10 +74,16 @@ pub fn setup_meilisearch(opt: &Opt) -> anyhow::Result<MeiliSearch> {
meilisearch.build(opt.db_path.clone(), opt.indexer_options.clone())
}
pub fn configure_data(config: &mut web::ServiceConfig, data: MeiliSearch, opt: &Opt) {
pub fn configure_data(
config: &mut web::ServiceConfig,
data: MeiliSearch,
opt: &Opt,
analytics: &'static dyn Analytics,
) {
let http_payload_size_limit = opt.http_payload_size_limit.get_bytes() as usize;
config
.app_data(data)
.app_data(web::Data::new(analytics))
.app_data(
web::JsonConfig::default()
.content_type(|mime| mime == mime::APPLICATION_JSON)
@ -167,7 +174,7 @@ pub fn dashboard(config: &mut web::ServiceConfig, _enable_frontend: bool) {
#[macro_export]
macro_rules! create_app {
($data:expr, $enable_frontend:expr, $opt:expr) => {{
($data:expr, $enable_frontend:expr, $opt:expr, $analytics:expr) => {{
use actix_cors::Cors;
use actix_web::middleware::TrailingSlash;
use actix_web::App;
@ -177,7 +184,7 @@ macro_rules! create_app {
use meilisearch_http::{configure_auth, configure_data, dashboard};
App::new()
.configure(|s| configure_data(s, $data.clone(), &$opt))
.configure(|s| configure_data(s, $data.clone(), &$opt, $analytics))
.configure(|s| configure_auth(s, &$opt))
.configure(routes::configure)
.configure(|s| dashboard(s, $enable_frontend))

View File

@ -56,15 +56,20 @@ async fn main() -> anyhow::Result<()> {
print_launch_resume(&opt, analytics);
run_http(meilisearch, opt).await?;
run_http(meilisearch, opt, analytics).await?;
Ok(())
}
async fn run_http(data: MeiliSearch, opt: Opt) -> anyhow::Result<()> {
async fn run_http(
data: MeiliSearch,
opt: Opt,
analytics: &'static dyn Analytics,
) -> anyhow::Result<()> {
let _enable_dashboard = &opt.env == "development";
let opt_clone = opt.clone();
let http_server = HttpServer::new(move || create_app!(data, _enable_dashboard, opt_clone))
let http_server =
HttpServer::new(move || create_app!(data, _enable_dashboard, opt_clone, analytics))
// Disable signals allows the server to terminate immediately when a user enter CTRL-C
.disable_signals();

View File

@ -2,7 +2,7 @@ use actix_web::{http::StatusCode, test};
use meilisearch_lib::MeiliSearch;
use serde_json::Value;
use meilisearch_http::{create_app, Opt};
use meilisearch_http::{analytics, create_app, Opt};
pub struct Service {
pub meilisearch: MeiliSearch,
@ -11,7 +11,13 @@ pub struct Service {
impl Service {
pub async fn post(&self, url: impl AsRef<str>, body: Value) -> (Value, StatusCode) {
let app = test::init_service(create_app!(&self.meilisearch, true, &self.options)).await;
let app = test::init_service(create_app!(
&self.meilisearch,
true,
&self.options,
analytics::MockAnalytics::new(&self.options)
))
.await;
let req = test::TestRequest::post()
.uri(url.as_ref())
@ -31,7 +37,13 @@ impl Service {
url: impl AsRef<str>,
body: impl AsRef<str>,
) -> (Value, StatusCode) {
let app = test::init_service(create_app!(&self.meilisearch, true, &self.options)).await;
let app = test::init_service(create_app!(
&self.meilisearch,
true,
&self.options,
analytics::MockAnalytics::new(&self.options)
))
.await;
let req = test::TestRequest::post()
.uri(url.as_ref())
@ -47,7 +59,13 @@ impl Service {
}
pub async fn get(&self, url: impl AsRef<str>) -> (Value, StatusCode) {
let app = test::init_service(create_app!(&self.meilisearch, true, &self.options)).await;
let app = test::init_service(create_app!(
&self.meilisearch,
true,
&self.options,
analytics::MockAnalytics::new(&self.options)
))
.await;
let req = test::TestRequest::get().uri(url.as_ref()).to_request();
let res = test::call_service(&app, req).await;
@ -59,7 +77,13 @@ impl Service {
}
pub async fn put(&self, url: impl AsRef<str>, body: Value) -> (Value, StatusCode) {
let app = test::init_service(create_app!(&self.meilisearch, true, &self.options)).await;
let app = test::init_service(create_app!(
&self.meilisearch,
true,
&self.options,
analytics::MockAnalytics::new(&self.options)
))
.await;
let req = test::TestRequest::put()
.uri(url.as_ref())
@ -74,7 +98,13 @@ impl Service {
}
pub async fn delete(&self, url: impl AsRef<str>) -> (Value, StatusCode) {
let app = test::init_service(create_app!(&self.meilisearch, true, &self.options)).await;
let app = test::init_service(create_app!(
&self.meilisearch,
true,
&self.options,
analytics::MockAnalytics::new(&self.options)
))
.await;
let req = test::TestRequest::delete().uri(url.as_ref()).to_request();
let res = test::call_service(&app, req).await;

View File

@ -4,7 +4,7 @@ mod common;
use crate::common::Server;
use actix_web::test;
use meilisearch_http::create_app;
use meilisearch_http::{analytics, create_app};
use serde_json::{json, Value};
#[actix_rt::test]
@ -40,7 +40,8 @@ async fn error_json_bad_content_type() {
let app = test::init_service(create_app!(
&server.service.meilisearch,
true,
&server.service.options
&server.service.options,
analytics::MockAnalytics::new(&server.service.options)
))
.await;
for route in routes {

View File

@ -1,7 +1,7 @@
use crate::common::{GetAllDocumentsOptions, Server};
use actix_web::test;
use chrono::DateTime;
use meilisearch_http::create_app;
use meilisearch_http::{analytics, create_app};
use serde_json::{json, Value};
/// This is the basic usage of our API and every other tests uses the content-type application/json
@ -19,7 +19,8 @@ async fn add_documents_test_json_content_types() {
let app = test::init_service(create_app!(
&server.service.meilisearch,
true,
&server.service.options
&server.service.options,
analytics::MockAnalytics::new(&server.service.options)
))
.await;
// post
@ -63,7 +64,8 @@ async fn error_add_documents_test_bad_content_types() {
let app = test::init_service(create_app!(
&server.service.meilisearch,
true,
&server.service.options
&server.service.options,
analytics::MockAnalytics::new(&server.service.options)
))
.await;
// post
@ -129,7 +131,8 @@ async fn error_add_documents_test_no_content_type() {
let app = test::init_service(create_app!(
&server.service.meilisearch,
true,
&server.service.options
&server.service.options,
analytics::MockAnalytics::new(&server.service.options)
))
.await;
// post