adding test for partial events expiring

This commit is contained in:
krunal 2023-12-06 20:31:28 +05:30
parent 4fc0482875
commit 406775c1c3

View File

@ -446,6 +446,9 @@ TEST_F(AnalyticsManagerTest, NoresultsQueries) {
}
TEST_F(AnalyticsManagerTest, QueryHitsCount) {
//flush all events from analytics store
analyticsManager.resetAnalyticsStore();
nlohmann::json titles_schema = R"({
"name": "titles",
"fields": [
@ -655,4 +658,113 @@ TEST_F(AnalyticsManagerTest, EventsExpiryAll) {
resp = analyticsManager.get_query_hits_counts();
ASSERT_EQ(0, resp.size());
}
TEST_F(AnalyticsManagerTest, EventsExpiryPartial) {
//flush all events from analytics store
analyticsManager.resetAnalyticsStore();
nlohmann::json titles_schema = R"({
"name": "titles",
"fields": [
{"name": "title", "type": "string"}
]
})"_json;
Collection *titles_coll = collectionManager.create_collection(titles_schema).get();
nlohmann::json events = nlohmann::json::array();
auto ts = 1701851341000000;
nlohmann::json event;
event["event_type"] = "click_events";
event["q"] = "technology";
event["collection_id"] = "0";
event["doc_id"] = "21";
event["position"] = 2;
event["user_id"] = 13;
event["timestamp"] = ts;
events.push_back(event);
event["doc_id"] = "12";
event["position"] = 3;
event["timestamp"] = ts + 1000000; //after 1s
events.push_back(event);
event["doc_id"] = "19";
event["position"] = 1;
event["timestamp"] = ts + 6000000; //after 6s
events.push_back(event);
nlohmann::json event2;
event2["event_type"] = "query_hits_counts";
event2["q"] = "technology";
event2["collection_id"] = "0";
event2["user_id"] = 13;
event2["hits_count"] = 124;
event2["timestamp"] = ts;
events.push_back(event2);
event2["q"] = "industry";
event2["user_id"] = 13;
event2["hits_count"] = 214;
event2["timestamp"] = ts + 2000000; //after 2s
events.push_back(event2);
event2["q"] = "management";
event2["user_id"] = 13;
event2["hits_count"] = 834;
event2["timestamp"] = ts + 8000000; //after 8s
events.push_back(event2);
ASSERT_TRUE(analyticsManager.write_events_to_store(events).ok());
nlohmann::json resp = analyticsManager.get_click_events();
ASSERT_EQ(3, resp.size());
ASSERT_EQ("technology", resp[0]["q"]);
ASSERT_EQ("21", resp[0]["doc_id"]);
ASSERT_EQ(2, resp[0]["position"]);
ASSERT_EQ(13, resp[0]["user_id"]);
ASSERT_EQ("technology", resp[1]["q"]);
ASSERT_EQ("12", resp[1]["doc_id"]);
ASSERT_EQ(3, resp[1]["position"]);
ASSERT_EQ(13, resp[1]["user_id"]);
ASSERT_EQ("technology", resp[2]["q"]);
ASSERT_EQ("19", resp[2]["doc_id"]);
ASSERT_EQ(1, resp[2]["position"]);
ASSERT_EQ(13, resp[2]["user_id"]);
resp = analyticsManager.get_query_hits_counts();
ASSERT_EQ(3, resp.size());
ASSERT_EQ("technology", resp[0]["q"]);
ASSERT_EQ(13, resp[0]["user_id"]);
ASSERT_EQ(124, resp[0]["hits_count"]);
ASSERT_EQ("industry", resp[1]["q"]);
ASSERT_EQ(13, resp[1]["user_id"]);
ASSERT_EQ(214, resp[1]["hits_count"]);
ASSERT_EQ("management", resp[2]["q"]);
ASSERT_EQ(13, resp[2]["user_id"]);
ASSERT_EQ(834, resp[2]["hits_count"]);
//check for events expiry
// assuming ttl is ts + 5 sec
//only events in ttl interval will be removed
analyticsManager.checkEventsExpiry();
resp = analyticsManager.get_click_events();
ASSERT_EQ(1, resp.size());
ASSERT_EQ("technology", resp[0]["q"]);
ASSERT_EQ("19", resp[0]["doc_id"]);
ASSERT_EQ(1, resp[0]["position"]);
ASSERT_EQ(13, resp[0]["user_id"]);
resp = analyticsManager.get_query_hits_counts();
ASSERT_EQ(1, resp.size());
ASSERT_EQ("management", resp[0]["q"]);
ASSERT_EQ(13, resp[0]["user_id"]);
ASSERT_EQ(834, resp[0]["hits_count"]);
}