Skip to content

Commit 1d352dd

Browse files
committed
log feedback to splunk
1 parent 1137fa8 commit 1d352dd

File tree

5 files changed

+111
-51
lines changed

5 files changed

+111
-51
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import traceback
2+
3+
from slack_sdk import WebClient
4+
from app.core.config import get_logger
5+
6+
7+
logger = get_logger()
8+
9+
10+
def get_friendly_channel_name(channel_id: str, client: WebClient) -> str:
11+
friendly_channel_name = channel_id
12+
try:
13+
conversations_info_response = client.conversations_info(channel=channel_id)
14+
if conversations_info_response["ok"]:
15+
friendly_channel_name = conversations_info_response["channel"]["name"]
16+
else:
17+
logger(
18+
"There was a problem getting the friendly channel name",
19+
extra={"conversations_info_response": conversations_info_response},
20+
)
21+
except Exception:
22+
logger.error("There was an error getting the friendly channel name", extra={"error": traceback.format_exc()})
23+
return friendly_channel_name

packages/slackBotFunction/app/slack/slack_events.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
update_state_information,
2323
)
2424
from app.services.query_reformulator import reformulate_query
25+
from app.services.slack import get_friendly_channel_name
2526

2627
logger = get_logger()
2728

@@ -261,18 +262,7 @@ def log_query_stats(user_query, event, channel, client, thread_ts):
261262
end_time = time.time()
262263
duration = end_time - start_time
263264
is_direct_message = event.get("channel_type") == constants.CHANNEL_TYPE_IM
264-
friendly_channel_name = channel
265-
try:
266-
conversations_info_response = client.conversations_info(channel=channel)
267-
if conversations_info_response["ok"]:
268-
friendly_channel_name = conversations_info_response["channel"]["name"]
269-
else:
270-
logger(
271-
"There was a problem getting the friendly channel name",
272-
extra={"conversations_info_response": conversations_info_response},
273-
)
274-
except Exception:
275-
logger.error("There was an error getting the friendly channel name", extra={"error", traceback.format_exc()})
265+
friendly_channel_name = get_friendly_channel_name(channel_id=channel, client=client)
276266
reporting_info = {
277267
"query_length": query_length,
278268
"start_time": start_time,
@@ -295,6 +285,7 @@ def store_feedback(
295285
feedback_type,
296286
user_id,
297287
channel_id,
288+
client: WebClient,
298289
thread_ts=None,
299290
message_ts=None,
300291
feedback_text=None,
@@ -303,6 +294,14 @@ def store_feedback(
303294
Store user feedback with reference to Q&A record
304295
"""
305296
try:
297+
friendly_channel_name = get_friendly_channel_name(channel_id=channel_id, client=client)
298+
reporting_info = {
299+
"feedback_type": feedback_type,
300+
"feedback_text": feedback_text,
301+
"thread_ts": thread_ts,
302+
"channel": friendly_channel_name,
303+
}
304+
logger.info("REPORTING: feedback_stats", extra={"reporting_info": reporting_info})
306305
now = int(time.time())
307306
ttl = now + constants.TTL_FEEDBACK
308307

packages/slackBotFunction/app/slack/slack_handlers.py

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,14 @@ def mention_handler(event, ack, body, client):
105105
feedback_text = cleaned.split(":", 1)[1].strip() if ":" in cleaned else ""
106106
try:
107107
store_feedback(
108-
conversation_key,
109-
"additional",
110-
user_id,
111-
channel_id,
112-
thread_root,
113-
None,
114-
feedback_text,
108+
conversation_key=conversation_key,
109+
feedback_type="additional",
110+
user_id=user_id,
111+
channel_id=channel_id,
112+
thread_ts=thread_root,
113+
message_ts=None,
114+
feedback_text=feedback_text,
115+
client=client,
115116
)
116117
except Exception as e:
117118
logger.error(f"Failed to store channel feedback via mention: {e}", extra={"error": traceback.format_exc()})
@@ -149,13 +150,14 @@ def dm_message_handler(event, event_id, client):
149150
feedback_text = text.split(":", 1)[1].strip() if ":" in text else ""
150151
try:
151152
store_feedback(
152-
conversation_key,
153-
"additional",
154-
user_id,
155-
channel_id,
156-
thread_root,
157-
None,
158-
feedback_text,
153+
conversation_key=conversation_key,
154+
feedback_type="additional",
155+
user_id=user_id,
156+
channel_id=channel_id,
157+
thread_ts=thread_root,
158+
message_ts=None,
159+
feedback_text=feedback_text,
160+
client=client,
159161
)
160162
except Exception as e:
161163
logger.error(f"Failed to store DM additional feedback: {e}", extra={"error": traceback.format_exc()})
@@ -208,13 +210,14 @@ def thread_message_handler(event, event_id, client):
208210
user_id = event.get("user", "unknown")
209211
try:
210212
store_feedback(
211-
conversation_key,
212-
"additional",
213-
user_id,
214-
channel_id,
215-
thread_root,
216-
None,
217-
feedback_text,
213+
conversation_key=conversation_key,
214+
feedback_type="additional",
215+
user_id=user_id,
216+
channel_id=channel_id,
217+
thread_ts=thread_root,
218+
message_ts=None,
219+
feedback_text=feedback_text,
220+
client=client,
218221
)
219222
except Exception as e:
220223
logger.error(f"Failed to store channel additional feedback: {e}", extra={"error": traceback.format_exc()})
@@ -285,12 +288,13 @@ def feedback_handler(ack, body, client):
285288

286289
try:
287290
store_feedback(
288-
feedback_data["ck"],
289-
feedback_type,
290-
body["user"]["id"],
291-
feedback_data["ch"],
292-
feedback_data.get("tt"),
293-
feedback_data.get("mt"),
291+
conversation_key=feedback_data["ck"],
292+
feedback_type=feedback_type,
293+
user_id=body["user"]["id"],
294+
channel_id=feedback_data["ch"],
295+
thread_ts=feedback_data.get("tt"),
296+
feedback_text=feedback_data.get("mt"),
297+
client=client,
294298
)
295299
# Only post message if storage succeeded
296300
post_params = {"channel": feedback_data["ch"], "text": response_message}

packages/slackBotFunction/tests/test_feedback.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
import sys
22
import pytest
3-
from unittest.mock import patch
3+
from unittest.mock import Mock, patch
44
from botocore.exceptions import ClientError
55

66

77
@patch("app.services.dynamo.store_state_information")
88
@patch("app.services.dynamo.get_state_information")
9-
def test_store_feedback(mock_get_state_information, mock_store_state_information, mock_env):
9+
@patch("app.services.slack.get_friendly_channel_name")
10+
def test_store_feedback(
11+
mock_get_friendly_channel_name, mock_get_state_information, mock_store_state_information, mock_env
12+
):
1013
"""Test feedback storage functionality"""
1114
# set up mocks
15+
mock_client = Mock()
1216

1317
# delete and import module to test
1418
if "app.slack.slack_events" in sys.modules:
1519
del sys.modules["app.slack.slack_events"]
1620
from app.slack.slack_events import store_feedback
1721

1822
# perform operation
19-
store_feedback("test-conversation", "positive", "U123", "C123")
23+
store_feedback("test-conversation", "positive", "U123", "C123", mock_client)
2024

2125
# assertions
2226
mock_store_state_information.assert_called_once()
@@ -27,17 +31,23 @@ def test_store_feedback(mock_get_state_information, mock_store_state_information
2731

2832
@patch("app.services.dynamo.store_state_information")
2933
@patch("app.services.dynamo.get_state_information")
30-
def test_feedback_storage_with_additional_text(mock_get_state_information, mock_store_state_information, mock_env):
34+
@patch("app.services.slack.get_friendly_channel_name")
35+
def test_feedback_storage_with_additional_text(
36+
mock_get_friendly_channel_name, mock_get_state_information, mock_store_state_information, mock_env
37+
):
3138
"""Test feedback storage with additional feedback text"""
3239
# set up mocks
40+
mock_client = Mock()
3341

3442
# delete and import module to test
3543
if "app.slack.slack_events" in sys.modules:
3644
del sys.modules["app.slack.slack_events"]
3745
from app.slack.slack_events import store_feedback
3846

3947
# perform operation
40-
store_feedback("test-conversation", "additional", "U123", "C123", feedback_text="This is additional feedback")
48+
store_feedback(
49+
"test-conversation", "additional", "U123", "C123", mock_client, feedback_text="This is additional feedback"
50+
)
4151

4252
# assertions
4353
mock_store_state_information.assert_called_once()
@@ -239,18 +249,22 @@ def test_cleanup_previous_unfeedback_qa_does_not_throw_error(delete_state_inform
239249

240250
@patch("app.services.dynamo.store_state_information")
241251
@patch("app.slack.slack_events.get_latest_message_ts")
242-
def test_store_feedback_no_message_ts_fallback(mock_get_latest_message_ts, mock_store_state_information, mock_env):
252+
@patch("app.services.slack.get_friendly_channel_name")
253+
def test_store_feedback_no_message_ts_fallback(
254+
mock_get_friendly_channel_name, mock_get_latest_message_ts, mock_store_state_information, mock_env
255+
):
243256
"""Test store_feedback fallback path when no message_ts"""
244257
# set up mocks
245258
mock_get_latest_message_ts.return_value = None
259+
mock_client = Mock()
246260

247261
# delete and import module to test
248262
if "app.slack.slack_events" in sys.modules:
249263
del sys.modules["app.slack.slack_events"]
250264
from app.slack.slack_events import store_feedback
251265

252266
# perform operation
253-
store_feedback("conv-key", "positive", "user-id", "channel-id")
267+
store_feedback("conv-key", "positive", "user-id", "channel-id", mock_client)
254268

255269
# assertions
256270
mock_store_state_information.assert_called_once()
@@ -361,12 +375,16 @@ def test_get_latest_message_ts_no_item(mock_get_state_information, mock_env):
361375

362376
@patch("app.services.dynamo.store_state_information")
363377
@patch("app.slack.slack_events.get_latest_message_ts")
364-
def test_store_feedback_client_error_reraise(mock_get_latest_message_ts, mock_store_state_information, mock_env):
378+
@patch("app.services.slack.get_friendly_channel_name")
379+
def test_store_feedback_client_error_reraise(
380+
mock_get_friendly_channel_name, mock_get_latest_message_ts, mock_store_state_information, mock_env
381+
):
365382
"""Test store_feedback re-raises ClientError"""
366383
# set up mocks
367384
error = ClientError({"Error": {"Code": "ValidationException"}}, "PutItem")
368385
mock_store_state_information.side_effect = error
369386
mock_get_latest_message_ts.return_value = "123"
387+
mock_client = Mock()
370388

371389
# delete and import module to test
372390
if "app.slack.slack_events" in sys.modules:
@@ -375,7 +393,7 @@ def test_store_feedback_client_error_reraise(mock_get_latest_message_ts, mock_st
375393

376394
# perform operation
377395
with pytest.raises(ClientError):
378-
store_feedback("conv-key", "positive", "user-id", "channel-id")
396+
store_feedback("conv-key", "positive", "user-id", "channel-id", mock_client)
379397

380398

381399
@patch("app.services.dynamo.store_state_information")

packages/slackBotFunction/tests/test_slack_handlers.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import sys
2-
from unittest.mock import Mock, patch
2+
from unittest.mock import ANY, Mock, patch
33
from botocore.exceptions import ClientError
44

55

@@ -161,7 +161,15 @@ def decorator(func):
161161
yes_handler(mock_ack, mock_body, mock_client)
162162

163163
mock_ack.assert_called_once()
164-
mock_store_feedback.assert_called_once_with("conv-key", "positive", "U123", "C123", "123", "456")
164+
mock_store_feedback.assert_called_once_with(
165+
conversation_key="conv-key",
166+
feedback_type="positive",
167+
user_id="U123",
168+
channel_id="C123",
169+
thread_ts="123",
170+
feedback_text="456",
171+
client=ANY,
172+
)
165173
mock_client.chat_postMessage.assert_called_once()
166174

167175

@@ -217,7 +225,15 @@ def decorator(func):
217225
no_handler(mock_ack, mock_body, mock_client)
218226

219227
mock_ack.assert_called_once()
220-
mock_store_feedback.assert_called_once_with("conv-key", "negative", "U123", "C123", "123", "456")
228+
mock_store_feedback.assert_called_once_with(
229+
conversation_key="conv-key",
230+
feedback_type="negative",
231+
user_id="U123",
232+
channel_id="C123",
233+
thread_ts="123",
234+
feedback_text="456",
235+
client=ANY,
236+
)
221237
mock_client.chat_postMessage.assert_called_once()
222238

223239

0 commit comments

Comments
 (0)