Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Fx endpoint and unused vars for events API (#1849)

Krunal Gandhib0359000b10a
diff --git a/include/http_server.h b/include/http_server.h
index 4ce21181..75a1c9d0 100644
--- a/include/http_server.h
+++ b/include/http_server.h
@@ -196,7 +196,8 @@ private:
static int async_req_cb(void *ctx, int is_end_stream);
- static bool is_write_request(const std::string& root_resource, const std::string& http_method);
+ static bool is_write_request(const std::string& root_resource, const std::string& http_method,
+ bool (*handler)(const std::shared_ptr<http_req>&, const std::shared_ptr<http_res>&));
static void handle_gzip(const std::shared_ptr<http_req>& request);
diff --git a/src/analytics_manager.cpp b/src/analytics_manager.cpp
index f2b8ac91..2279ccac 100644
--- a/src/analytics_manager.cpp
+++ b/src/analytics_manager.cpp
@@ -85,10 +85,6 @@ Option<bool> AnalyticsManager::create_index(nlohmann::json &payload, bool upsert
suggestion_config.rule_type = payload["type"];
if(payload["type"] == LOG_TYPE) {
- if(!params.contains("name") || params["name"].empty()) {
- return Option<bool>(400, "Bad or missing name in params");
- }
-
if(!params["source"].contains("collections") || !params["source"]["collections"].is_array()) {
return Option<bool>(400, "Must contain a valid list of source collections.");
}
@@ -104,10 +100,6 @@ Option<bool> AnalyticsManager::create_index(nlohmann::json &payload, bool upsert
suggestion_collection = params["source"]["collections"][0].get<std::string>();
suggestion_config.suggestion_collection = suggestion_collection;
-
- if(event_collection_map.count(params["name"]) != 0) {
- return Option<bool>(400, "Event name already exists.");
- }
} else {
if(payload["type"] == COUNTER_TYPE) {
if(!params["source"].contains("events") || (params["source"].contains("events") &&
diff --git a/src/core_api.cpp b/src/core_api.cpp
index ec2b7772..4331f4b9 100644
--- a/src/core_api.cpp
+++ b/src/core_api.cpp
@@ -2902,8 +2902,12 @@ bool post_write_analytics_to_db(const std::shared_ptr<http_req>& req, const std:
return false;
}
- AnalyticsManager::get_instance().write_to_db(req_json);
+ if(!AnalyticsManager::get_instance().write_to_db(req_json)) {
+ res->set_500(R"({"ok": false})");
+ return false;
+ }
+ res->set_200(R"({"ok": true})");
return true;
}
diff --git a/src/http_server.cpp b/src/http_server.cpp
index 32a344c9..92e5ffef 100644
--- a/src/http_server.cpp
+++ b/src/http_server.cpp
@@ -12,6 +12,7 @@
#include "logger.h"
#include "ratelimit_manager.h"
#include "sole.hpp"
+#include "core_api.h"
HttpServer::HttpServer(const std::string & version, const std::string & listen_address,
uint32_t listen_port, const std::string & ssl_cert_path, const std::string & ssl_cert_key_path,
@@ -436,7 +437,7 @@ int HttpServer::catch_all_handler(h2o_handler_t *_h2o_handler, h2o_req_t *req) {
bool use_meta_thread_pool = (root_resource == "status");
if(needs_readiness_check) {
- bool write_op = is_write_request(root_resource, http_method);
+ bool write_op = is_write_request(root_resource, http_method, rpath->handler);
bool read_op = !write_op;
std::string message = "{ \"message\": \"Not Ready or Lagging\"}";
@@ -580,11 +581,16 @@ int HttpServer::catch_all_handler(h2o_handler_t *_h2o_handler, h2o_req_t *req) {
}
-bool HttpServer::is_write_request(const std::string& root_resource, const std::string& http_method) {
+bool HttpServer::is_write_request(const std::string& root_resource, const std::string& http_method,
+ bool (*rpath_handler)(const std::shared_ptr<http_req>&, const std::shared_ptr<http_res>&)) {
if(http_method == "GET") {
return false;
}
+ if(rpath_handler == post_create_event) {
+ return false;
+ }
+
bool write_free_request = (root_resource == "multi_search" || root_resource == "proxy" ||
root_resource == "operations");
@@ -701,7 +707,7 @@ int HttpServer::process_request(const std::shared_ptr<http_req>& request, const
}
}
- bool is_write = is_write_request(root_resource, rpath->http_method);
+ bool is_write = is_write_request(root_resource, rpath->http_method, rpath->handler);
if(is_write) {
handler->http_server->get_replication_state()->write(request, response);
diff --git a/src/main/typesense_server.cpp b/src/main/typesense_server.cpp
index e95a01a2..93e4263c 100644
--- a/src/main/typesense_server.cpp
+++ b/src/main/typesense_server.cpp
@@ -80,7 +80,7 @@ void master_server_routes() {
server->put("/analytics/rules/:name", put_upsert_analytics_rules);
server->del("/analytics/rules/:name", del_analytics_rules);
server->post("/analytics/events", post_create_event);
- server->post("analytics/aggregate_events", post_write_analytics_to_db);
+ server->post("/analytics/aggregate_events", post_write_analytics_to_db);
// meta
server->get("/metrics.json", get_metrics_json);
diff --git a/test/analytics_manager_test.cpp b/test/analytics_manager_test.cpp
index 2834035b..c8d552cc 100644
--- a/test/analytics_manager_test.cpp
+++ b/test/analytics_manager_test.cpp
@@ -302,7 +302,6 @@ TEST_F(AnalyticsManagerTest, EventsValidation) {
"name": "product_events",
"type": "log",
"params": {
- "name": "product_events_logging",
"source": {
"collections": ["titles"],
"events": [{"type": "click", "name": "AP"}, {"type": "visit", "name": "VP"}]
@@ -363,7 +362,6 @@ TEST_F(AnalyticsManagerTest, EventsValidation) {
"name": "product_click_events2",
"type": "log",
"params": {
- "name": "product_events_logging2",
"source": {
"collections": ["titles"],
"events": [{"type": "click", "name": "AP"}]
@@ -435,7 +433,6 @@ TEST_F(AnalyticsManagerTest, EventsValidation) {
"name": "product_events2",
"type": "log",
"params": {
- "name": "custom_events_logging",
"source": {
"collections": ["titles"],
"events": [{"type": "custom", "name": "CP"}]
@@ -468,7 +465,6 @@ TEST_F(AnalyticsManagerTest, EventsValidation) {
"name": "product_events2",
"type": "log",
"params": {
- "name": "custom_events_logging",
"source": {
"collections": ["titles"],
"events": [{"type": "custom", "name": "CP"}]
@@ -500,7 +496,6 @@ TEST_F(AnalyticsManagerTest, EventsPersist) {
"name": "product_click_events",
"type": "log",
"params": {
- "name": "product_click_events_logging",
"source": {
"collections": ["titles"],
"events": [{"type": "click", "name": "APC"}]
@@ -617,7 +612,6 @@ TEST_F(AnalyticsManagerTest, EventsRateLimitTest) {
"name": "product_events2",
"type": "log",
"params": {
- "name": "product_events_logging2",
"source": {
"collections": ["titles"],
"events": [{"type": "click", "name": "AB"}]
@@ -1319,7 +1313,6 @@ TEST_F(AnalyticsManagerTest, PopularityScoreValidation) {
"name": "click_event_rule",
"type": "log",
"params": {
- "name": "click_event_log",
"source": {
"collections": ["books"],
"events": [{"type": "click", "name": "APC2"}]
@@ -1470,7 +1463,6 @@ TEST_F(AnalyticsManagerTest, AnalyticsStoreTTL) {
"name": "product_events2",
"type": "log",
"params": {
- "name": "product_events_logging2",
"source": {
"collections": ["titles"],
"events": [{"type": "click", "name": "AB"}]
@@ -1550,7 +1542,6 @@ TEST_F(AnalyticsManagerTest, AnalyticsStoreGetLastN) {
"name": "product_events2",
"type": "log",
"params": {
- "name": "product_events_logging2",
"source": {
"collections": ["titles"],
"events": [{"type": "click", "name": "AB"}, {"type": "visit", "name": "AV"}]