Merge pull request #1453 from krunal1313/event_anaylytics

Store analytic rule in suggestion config
This commit is contained in:
Kishore Nallan 2023-12-26 16:06:07 +05:30 committed by GitHub
commit 9e705ffa47
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 75 additions and 1 deletions

View File

@ -119,10 +119,11 @@ private:
std::string suggestion_collection;
std::vector<std::string> query_collections;
size_t limit;
std::string rule_type;
void to_json(nlohmann::json& obj) const {
obj["name"] = name;
obj["type"] = POPULAR_QUERIES_TYPE;
obj["type"] = rule_type;
obj["params"] = nlohmann::json::object();
obj["params"]["limit"] = limit;
obj["params"]["source"]["collections"] = query_collections;

View File

@ -99,6 +99,7 @@ Option<bool> AnalyticsManager::create_queries_index(nlohmann::json &payload, boo
suggestion_config.name = suggestion_config_name;
suggestion_config.suggestion_collection = suggestion_collection;
suggestion_config.limit = limit;
suggestion_config.rule_type = payload["type"];
if(payload["type"] == POPULAR_QUERIES_TYPE) {
if (!upsert && popular_queries.count(suggestion_collection) != 0) {

View File

@ -445,6 +445,78 @@ TEST_F(AnalyticsManagerTest, NoresultsQueries) {
ASSERT_EQ(0, noresults_queries.size());
}
TEST_F(AnalyticsManagerTest, SuggestionConfigRule) {
nlohmann::json titles_schema = R"({
"name": "titles",
"fields": [
{"name": "title", "type": "string"}
]
})"_json;
Collection* titles_coll = collectionManager.create_collection(titles_schema).get();
// create a collection to store suggestions
nlohmann::json suggestions_schema = R"({
"name": "top_queries",
"fields": [
{"name": "q", "type": "string" },
{"name": "count", "type": "int32" }
]
})"_json;
Collection* suggestions_coll = collectionManager.create_collection(suggestions_schema).get();
//add popular quries rule
nlohmann::json analytics_rule = R"({
"name": "top_search_queries",
"type": "popular_queries",
"params": {
"limit": 100,
"source": {
"collections": ["titles"]
},
"destination": {
"collection": "top_queries"
}
}
})"_json;
auto create_op = analyticsManager.create_rule(analytics_rule, false, true);
ASSERT_TRUE(create_op.ok());
//add nohits rule
analytics_rule = R"({
"name": "search_queries",
"type": "nohits_queries",
"params": {
"limit": 100,
"source": {
"collections": ["titles"]
},
"destination": {
"collection": "top_queries"
}
}
})"_json;
create_op = analyticsManager.create_rule(analytics_rule, false, true);
ASSERT_TRUE(create_op.ok());
auto rules = analyticsManager.list_rules().get()["rules"];
ASSERT_EQ(2, rules.size());
ASSERT_EQ("search_queries", rules[0]["name"]);
ASSERT_EQ("nohits_queries", rules[0]["type"]);
ASSERT_EQ("top_search_queries", rules[1]["name"]);
ASSERT_EQ("popular_queries", rules[1]["type"]);
//try deleting rules
ASSERT_TRUE(analyticsManager.remove_rule("search_queries").ok());
ASSERT_TRUE(analyticsManager.remove_rule("top_search_queries").ok());
rules = analyticsManager.list_rules().get()["rules"];
ASSERT_EQ(0, rules.size());
}
TEST_F(AnalyticsManagerTest, QueryHitsCount) {
//flush all events from analytics store
analyticsManager.resetAnalyticsStore();