Skip to content

Commit

Permalink
Merge pull request #375 from weni-ai/fix/empty-message-history-and-tag
Browse files Browse the repository at this point in the history
Fix/empty message history and tag
  • Loading branch information
AlisoSouza authored Dec 13, 2024
2 parents f6bfc58 + f09a9b7 commit d17327c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
20 changes: 17 additions & 3 deletions nexus/logs/api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,6 @@ def test_tag_filter(self):
)
response.render()
content = json.loads(response.content)

self.assertEqual(response.status_code, 200)
self.assertEquals(len(content.get("results")), 10)

Expand All @@ -316,6 +315,21 @@ def test_null_reflection_data(self):
self.assertEqual(response.status_code, 200)
self.assertEquals(len(content.get("results")), 0)

def test_empty_logs_data(self):
MessageLog.objects.all().delete()

request = self.factory.get(f"/api/{self.project.uuid}/message_history/?page_size=100&started_day={self.started_day}&ended_day={self.ended_day}")
force_authenticate(request, user=self.user)
response = MessageHistoryViewset.as_view({'get': 'list'})(
request,
project_uuid=str(self.project.uuid),
)
response.render()
content = json.loads(response.content)

self.assertEqual(response.status_code, 200)
self.assertEqual(len(content.get("results")), 0)


class TagPercentageViewSetTestCase(APITestCase):

Expand Down Expand Up @@ -351,8 +365,8 @@ def test_get_tag_percentages_no_logs(self):
response = self.view(request, project_uuid=str(self.project.uuid))
response.render()

self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
self.assertEqual(response.data['error'], "No logs found for the given date range")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, [])

def test_get_tag_percentages_invalid_date(self):
request = self.factory.get(self.url, {'started_day': 'invalid-date', 'ended_day': self.ended_day})
Expand Down
7 changes: 5 additions & 2 deletions nexus/logs/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def count_status(logs, tag):
message_logs = message_logs.exclude(message__status="F")

if not message_logs.exists():
return Response({"error": "No logs found for the given date range"}, status=404)
return Response([], status=200)

tag_counts = message_logs.aggregate(action_count=Count(Case(When(reflection_data__tag='action_started', then=1), output_field=IntegerField())))

Expand All @@ -97,7 +97,7 @@ def count_status(logs, tag):

total_logs = sum(tag_counts.values())
if total_logs == 0:
return Response({"error": "No logs found for the given date range"}, status=404)
return Response([], status=200)

action_percentage = (tag_counts['action_count'] / total_logs) * 100
succeed_percentage = (tag_counts['succeed_count'] / total_logs) * 100
Expand Down Expand Up @@ -177,6 +177,9 @@ def get_queryset(self):
}
logs = [log for log in logs if log.message.response_status == status.get(tag_param) and log.reflection_data.get("tag") != "action_started"]

if not logs:
return MessageLog.objects.none()

return logs


Expand Down

0 comments on commit d17327c

Please sign in to comment.