typesense/include/query_analytics.h
Krunal Gandhi 631563d1c6
add flag to enable/disable auto-aggregation of search queries (#1865)
* add flag to enable/disable auto-aggregation of search events

* updaitng validation checks
2024-08-02 20:53:03 +05:30

59 lines
1.5 KiB
C++

#pragma once
#include <string>
#include <vector>
#include <tsl/htrie_map.h>
#include <json.hpp>
#include <atomic>
#include <shared_mutex>
class QueryAnalytics {
public:
struct QWithTimestamp {
std::string query;
uint64_t timestamp;
QWithTimestamp(const std::string& query, uint64_t timestamp) : query(query), timestamp(timestamp) {}
};
static const size_t QUERY_FINALIZATION_INTERVAL_MICROS = 4 * 1000 * 1000;
private:
size_t k;
const size_t max_size;
const size_t max_query_length = 1024;
bool expand_query = false;
bool auto_aggregation_enabled;
// counts aggregated within the current node
tsl::htrie_map<char, uint32_t> local_counts;
std::shared_mutex lmutex;
std::unordered_map<std::string, std::vector<QWithTimestamp>> user_prefix_queries;
std::shared_mutex umutex;
public:
QueryAnalytics(size_t k, bool enable_auto_aggregation = true);
void add(const std::string& value, const std::string& expanded_key,
const bool live_query, const std::string& user_id, uint64_t now_ts_us = 0);
void compact_user_queries(uint64_t now_ts_us);
void serialize_as_docs(std::string& docs);
void reset_local_counts();
size_t get_k();
std::unordered_map<std::string, std::vector<QWithTimestamp>> get_user_prefix_queries();
tsl::htrie_map<char, uint32_t> get_local_counts();
void set_expand_query(bool expand_query);
bool is_auto_aggregation_enabled() const;
};