From 05b59722d76e9cbd060b8dae204546e635849fb0 Mon Sep 17 00:00:00 2001 From: maheshsattala <59285563+maheshsattala@users.noreply.github.com> Date: Wed, 9 Oct 2024 17:16:43 +0530 Subject: [PATCH] Updated code to adding location_info into kwargs related conversation_feedback issue. (#1568) --- kairon/api/app/routers/bot/metric.py | 17 --------- kairon/shared/metering/metering_processor.py | 6 ++-- tests/integration_test/services_test.py | 37 +++++++++++--------- 3 files changed, 25 insertions(+), 35 deletions(-) diff --git a/kairon/api/app/routers/bot/metric.py b/kairon/api/app/routers/bot/metric.py index 5b4215b09..0f426b314 100644 --- a/kairon/api/app/routers/bot/metric.py +++ b/kairon/api/app/routers/bot/metric.py @@ -66,23 +66,6 @@ async def add_end_user_metrics( return Response(message='Metrics added', data={"id": id}) -@router.post("/user/logs/{metric_type}", response_model=Response) -async def add_end_user_metrics( - request_data: DictData, request: Request, - metric_type: MetricType = Path(description="metric type", examples=[MetricType.user_metrics]), - current_user: User = Security(Authentication.get_current_user_and_bot, scopes=CHAT_ACCESS) -): - """ - Stores End User Metrics - """ - data = request_data.dict()["data"] - id = MeteringProcessor.add_log_with_geo_location( - metric_type=metric_type.value, request=request, bot=current_user.get_bot(), user=current_user.get_user(), - account_id=current_user.bot_account, **data - ) - return Response(message='Metrics added', data={"id": id}) - - @router.put("/user/logs/{metric_type}/{id}", response_model=Response) async def update_end_user_metrics( id: str, diff --git a/kairon/shared/metering/metering_processor.py b/kairon/shared/metering/metering_processor.py index 995c9bf96..755fc2bae 100644 --- a/kairon/shared/metering/metering_processor.py +++ b/kairon/shared/metering/metering_processor.py @@ -89,8 +89,10 @@ def add_log_with_geo_location(metric_type: MetricType, account_id: int, request: ip = Utility.get_client_ip(request) if not Utility.check_empty_string(ip): location_info = PluginFactory.get_instance(PluginTypes.ip_info).execute(ip=ip) - if location_info: - kwargs.update(location_info) + if location_info and ip: + data = list(location_info.values()) + if data and isinstance(data[0], dict): + kwargs.update(data[0]) return MeteringProcessor.add_metrics(bot, account_id, metric_type, **kwargs) @staticmethod diff --git a/tests/integration_test/services_test.py b/tests/integration_test/services_test.py index 54f646a11..ffcb82431 100644 --- a/tests/integration_test/services_test.py +++ b/tests/integration_test/services_test.py @@ -25983,14 +25983,16 @@ def test_add_end_user_metrics_with_ip(monkeypatch): url = f"https://ipinfo.io/batch?token={token}" payload = json.dumps(ip_list) expected = { - "ip": "140.82.201.129", - "city": "Mumbai", - "region": "Maharashtra", - "country": "IN", - "loc": "19.0728,72.8826", - "org": "AS13150 CATO NETWORKS LTD", - "postal": "400070", - "timezone": "Asia/Kolkata", + "140.82.201.129": { + "ip": "140.82.201.129", + "city": "Mumbai", + "region": "Maharashtra", + "country": "IN", + "loc": "19.0728,72.8826", + "org": "AS13150 CATO NETWORKS LTD", + "postal": "400070", + "timezone": "Asia/Kolkata", + } } responses.add("POST", url, json=expected) response = client.post( @@ -26064,6 +26066,7 @@ def test_add_and_update_conversation_feedback(monkeypatch): assert actual["message"] == "Metrics updated" +@responses.activate def test_get_end_user_metrics(monkeypatch): token = "abcgd563" enable = True @@ -26072,14 +26075,16 @@ def test_get_end_user_metrics(monkeypatch): url = f"https://ipinfo.io/batch?token={token}" expected = { - "ip": "140.82.201.129", - "city": "Mumbai", - "region": "Maharashtra", - "country": "IN", - "loc": "19.0728,72.8826", - "org": "AS13150 CATO NETWORKS LTD", - "postal": "400070", - "timezone": "Asia/Kolkata", + "140.82.201.129": { + "ip": "140.82.201.129", + "city": "Mumbai", + "region": "Maharashtra", + "country": "IN", + "loc": "19.0728,72.8826", + "org": "AS13150 CATO NETWORKS LTD", + "postal": "400070", + "timezone": "Asia/Kolkata", + } } responses.add("POST", url, json=expected) for i in range(5):