Repository: meilisearch/meilisearch
File: crates/meilisearch/src/routes/chats/chat_completion_analytics.rs (Rust)

Code before:

impl<Method: AggregateMethod> ChatCompletionAggregator<Method> {
    pub fn from_request(model: &str, message_count: usize, is_stream: bool) -> Self {
        let mut models_used = std::collections::HashMap::new();
        models_used.insert(model.to_string(), 1);

        Self {
            total_received: 1,
            total_messages: message_count,
            total_streamed_requests: if is_stream { 1 } else { 0 },
            total_non_streamed_requests: if is_stream { 0 } else { 1 },
            models_used,
            ..Default::default()
        }
    }

    pub fn succeed(&mut self, time_spent: std::time::Duration) {
        self.total_succeeded += 1;
        self.time_spent.push(time_spent.as_millis() as usize);
    }
}

impl<Method: AggregateMethod> Aggregate for ChatCompletionAggregator<Method> {
    fn event_name(&self) -> &'static str {
        Method::event_name()
    }

    fn aggregate(mut self: Box<Self>, new: Box<Self>) -> Box<Self> {
        let Self {
            total_received,
            total_succeeded,
            mut time_spent,
            total_messages,
            total_streamed_requests,
            total_non_streamed_requests,
            models_used,
            ..
        } = *new;

        // Aggregate time spent
        self.time_spent.append(&mut time_spent);

        // Aggregate counters
        self.total_received = self.total_received.saturating_add(total_received);
        self.total_succeeded = self.total_succeeded.saturating_add(total_succeeded);
        self.total_messages = self.total_messages.saturating_add(total_messages);
        self.total_streamed_requests =
            self.total_streamed_requests.saturating_add(total_streamed_requests);
        self.total_non_streamed_requests =
            self.total_non_streamed_requests.saturating_add(total_non_streamed_requests);


Review comment (suggestion):
```suggestion
impl Aggregate for ChatCompletionAggregator {
    fn event_name(&self) -> &'static str {
        "Chat Completion POST"
    }
```

Code after:
            total_received: 1,
            total_succeeded: 0,
            time_spent: BinaryHeap::new(),

            total_messages: message_count,
            total_streamed_requests: if is_stream { 1 } else { 0 },
            total_non_streamed_requests: if is_stream { 0 } else { 1 },

            models_used,
        }
    }

    pub fn succeed(&mut self, time_spent: std::time::Duration) {
        self.total_succeeded += 1;
        self.time_spent.push(time_spent.as_millis() as usize);
    }
}

impl Aggregate for ChatCompletionAggregator {
    fn event_name(&self) -> &'static str {
        "Chat Completion POST"
    }

    fn aggregate(mut self: Box<Self>, new: Box<Self>) -> Box<Self> {
        let Self {
            total_received,
            total_succeeded,
            mut time_spent,
            total_messages,
            total_streamed_requests,
            total_non_streamed_requests,
            models_used,
            ..
        } = *new;

        // Aggregate time spent
        self.time_spent.append(&mut time_spent);

        // Aggregate counters
        self.total_received = self.total_received.saturating_add(total_received);
        self.total_succeeded = self.total_succeeded.saturating_add(total_succeeded);
        self.total_messages = self.total_messages.saturating_add(total_messages);
        self.total_streamed_requests =
            self.total_streamed_requests.saturating_add(total_streamed_requests);
        self.total_non_streamed_requests =
            self.total_non_streamed_requests.saturating_add(total_non_streamed_requests);

        // Aggregate model usage
        for (model, count) in models_used {
            *self.models_used.entry(model).or_insert(0) += count;
        }