adding test for events expiry

This commit is contained in:
krunal 2023-12-05 14:34:28 +05:30
parent aff1f3b9fa
commit cc83b6697e
2 changed files with 94 additions and 0 deletions

View File

@ -10,7 +10,11 @@
LRU::Cache<std::string, event_cache_t> events_cache;
#define CLICK_EVENTS_RATE_LIMIT_SEC 60
#define CLICK_EVENTS_RATE_LIMIT_COUNT 5
#ifdef TEST_BUILD
#define EVENTS_TTL_INTERVAL_US 30000000 //30sec
#else
#define EVENTS_TTL_INTERVAL_US 2592000000000 //30days
#endif
Option<bool> AnalyticsManager::create_rule(nlohmann::json& payload, bool upsert, bool write_to_disk) {
/*

View File

@ -500,4 +500,94 @@ TEST_F(AnalyticsManagerTest, QueryHitsCount) {
ASSERT_EQ("funky", result[1]["query"]);
ASSERT_EQ(1, result[1]["hits_count"]);
ASSERT_EQ(1625365616, result[1]["timestamp"]);
}
TEST_F(AnalyticsManagerTest, EventsExpiry) {
//delete the analytic store db and reinit
delete analytics_store;
std::string analytics_db_path = "/tmp/typesense_test/analytics_db2";
analytics_store = new Store(analytics_db_path);
analyticsManager.init(store, analytics_store);
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();
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"] = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
events.push_back(event);
event["doc_id"] = "12";
event["position"] = 3;
event["timestamp"] = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
events.push_back(event);
ASSERT_TRUE(analyticsManager.write_events_to_store(events).ok());
nlohmann::json resp = analyticsManager.get_click_events();
ASSERT_EQ(2, 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"]);
LOG(INFO) << "waiting for TTL to pass";
sleep(35);//wait till ttl passes which is set to 30 seconds
analyticsManager.checkEventsExpiry();
resp = analyticsManager.get_click_events();
ASSERT_EQ(0, resp.size());
//add query hits events with click events
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"] = std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::system_clock::now().time_since_epoch()).count();
events.push_back(event2);
ASSERT_TRUE(analyticsManager.write_events_to_store(events).ok());
resp = analyticsManager.get_query_hits_counts();
ASSERT_EQ(1, resp.size());
ASSERT_EQ("technology", resp[0]["q"]);
ASSERT_EQ(13, resp[0]["user_id"]);
ASSERT_EQ(124, resp[0]["hits_count"]);
//now old click events will be deleted on checking expiry but query hits events will be remaining
analyticsManager.checkEventsExpiry();
resp = analyticsManager.get_click_events();
ASSERT_EQ(0, resp.size());
resp = analyticsManager.get_query_hits_counts();
ASSERT_EQ(1, resp.size());
ASSERT_EQ("technology", resp[0]["q"]);
ASSERT_EQ(13, resp[0]["user_id"]);
ASSERT_EQ(124, resp[0]["hits_count"]);
}