Skip to content

Commit

Permalink
Updated code to adding location_info into kwargs related conversation…
Browse files Browse the repository at this point in the history
…_feedback issue. (#1568)
  • Loading branch information
maheshsattala authored Oct 9, 2024
1 parent 981b92a commit 05b5972
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 35 deletions.
17 changes: 0 additions & 17 deletions kairon/api/app/routers/bot/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions kairon/shared/metering/metering_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 21 additions & 16 deletions tests/integration_test/services_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down

0 comments on commit 05b5972

Please sign in to comment.