pass anaytics into Arc instead of static ref

This commit is contained in:
marin postma 2021-10-29 15:58:06 +02:00
parent 66d87761b7
commit d65f055030
No known key found for this signature in database
GPG key ID: 6088B7721C3E39F9
5 changed files with 57 additions and 69 deletions

View file

@ -1,4 +1,4 @@
use std::{any::Any, fmt::Display};
use std::{any::Any, sync::Arc};
use actix_web::HttpRequest;
use serde_json::Value;
@ -7,9 +7,7 @@ use crate::{routes::indexes::documents::UpdateDocumentsQuery, Opt};
use super::{find_user_id, Analytics};
pub struct MockAnalytics {
user: String,
}
pub struct MockAnalytics;
#[derive(Default)]
pub struct SearchAggregator {}
@ -24,36 +22,29 @@ impl SearchAggregator {
}
impl MockAnalytics {
pub fn new(opt: &Opt) -> &'static Self {
pub fn new(opt: &Opt) -> (Arc<dyn Analytics>, String) {
let user = find_user_id(&opt.db_path).unwrap_or_default();
let analytics = Box::new(Self { user });
Box::leak(analytics)
(Arc::new(Self), user)
}
}
impl Analytics for MockAnalytics {
// These methods are noop and should be optimized out
fn publish(&'static self, _event_name: String, _send: Value, _request: Option<&HttpRequest>) {}
fn get_search(&'static self, _aggregate: super::SearchAggregator) {}
fn post_search(&'static self, _aggregate: super::SearchAggregator) {}
fn publish(&self, _event_name: String, _send: Value, _request: Option<&HttpRequest>) {}
fn get_search(&self, _aggregate: super::SearchAggregator) {}
fn post_search(&self, _aggregate: super::SearchAggregator) {}
fn add_documents(
&'static self,
&self,
_documents_query: &UpdateDocumentsQuery,
_index_creation: bool,
_request: &HttpRequest,
) {
}
fn update_documents(
&'static self,
&self,
_documents_query: &UpdateDocumentsQuery,
_index_creation: bool,
_request: &HttpRequest,
) {
}
}
impl Display for MockAnalytics {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.user)
}
}