shanghui 3 месяцев назад
Родитель
Сommit
0f35d1af72
2 измененных файлов с 35 добавлено и 5 удалено
  1. 17 1
      src/core/callcenter/registry.py
  2. 18 4
      src/core/voip/bot.py

+ 17 - 1
src/core/callcenter/registry.py

@@ -16,9 +16,25 @@ CALL_BOT_ANSWER_REQUESTS = Counter('call_bot_answer_requests', '机器人接听
 CALL_BOT_TRANSFER_REQUESTS = Counter('call_bot_transfer_requests', '机器转人量', ['bucket'])
 CALL_BOT_HANGUP_REQUESTS = Counter('call_bot_hangup_requests', '机器挂机量', ['bucket'])
 
+BOT_INTERACTION_ROUNDS = Counter('bot_interaction_rounds',  '机器人交互轮次',['taskId'] )
+BOT_REQUEST_COUNT = Counter('bot_request_count',  'Total number of bot requests')
+BOT_ASR_408 = Counter('bot_asr_408', 'ASR408 超时次数',['taskId'])
+
+
+
+
 # esl时间耗时
 ESL_EVENT_LATENCY = Histogram('esl_event_latency', 'Esl Event latency in seconds', ['eventName'])
 ESL_EVENT_CALLBACK_LATENCY = Histogram('esl_event_callback_latency', 'Esl Event callback latency in seconds', ['eventName','callType'])
 
+
+
+BOT_CALL_DURATION = Histogram('bot_call_duration', '通话时长',['taskId'])
+BOT_REQUEST_LATENCY = Histogram('bot_request_latency','请求机器人耗时',['taskId'])
+
+
+
 FLASK_ACTIVE_THREADS = Gauge('flask_active_threads', 'Number of active threads')
-BOT_AGENT_LIVES = Gauge('bot_agent_lives', 'Number of active agents')
+BOT_AGENT_LIVES = Gauge('bot_agent_lives', 'Number of active agents')
+
+

+ 18 - 4
src/core/voip/bot.py

@@ -188,6 +188,7 @@ class MyCall(pj.Call):
         self.device_id = kwargs.get('variable_sip_h_P-LIBRA-DeviceId')
         self.call_phone = kwargs.get("Caller-Caller-ID-Number")
         self.logger.info("self.session_id:%s, self.call_phone:%s", self.session_id,self.call_phone)
+        self.taskId = "10001"
         # self.scripts = build_demo_script()
         self.user_asr_text_queue = queue.Queue(maxsize=100)
         self.message_queue = queue.Queue(maxsize=3)
@@ -205,6 +206,7 @@ class MyCall(pj.Call):
         self.asr = TestSt(call_id, message_receiver=self.on_receiver_asr_result)  # 创建ASR实例
         self.asr.start()  # 启动ASR线程
 
+        self.start_time = time.time()  # 当前机器人对话开始时间
 
         # 超时设置
         self.play_start_time = time.time()  # 倒计时开始时间
@@ -213,6 +215,7 @@ class MyCall(pj.Call):
         self.txtLock = False
         self.inputLongStart = time.time()    #长按键开始时间
 
+        self.inter_action_total = 0
 
 
     def wait_time_check(self, current_time, wait_time):
@@ -229,6 +232,7 @@ class MyCall(pj.Call):
             if elapsed_time > wait_time:
                 # self.user_asr_text_queue.put("ASR408error")
                 self.chat('ASR408error')
+                registry.BOT_ASR_408.labels(self.taskId).inc()
         except ValueError as e:
             self.logger.info(f"无效的等待时间参数: {wait_time}, 错误: {e}")
             self.reset_wait_time()
@@ -340,9 +344,7 @@ class MyCall(pj.Call):
         self.say_end_action(self.action)
 
     def bot_say_hello(self):
-        # print('bot_say_hello, come in ')
         self.chat(user_asr_text="start")
-
     def chat(self, user_asr_text=None):
         # 调用文本机器人接口
         ToTextBotAgent(user_asr_text,self)
@@ -352,10 +354,16 @@ class MyCall(pj.Call):
         action_code = action.action_code
         if action_code == 'hang':  # 挂断
             self.agent.hangup(self.user_part)
+            self.end_statistics()
         elif action_code == 'transfer':  # 转人工
             self.agent.transfer(user_part=self.user_part, call_id=self.session_id, device_id=self.device_id)
+            self.end_statistics()
             #更新通话记录
             self.agent.dataHandleServer.update_record(self.session_id, service_category=2)
+    def end_statistics(self):
+        latency = (time.time() - self.start_time)
+        registry.BOT_CALL_DURATION.labels(self.taskId).observe(latency)
+        registry.BOT_INTERACTION_ROUNDS.labels(self.taskId).inc(self.inter_action_total)
 
 class ToTextBotAgent:
     def __init__(self, user_asr_text, call_agent):
@@ -368,11 +376,13 @@ class ToTextBotAgent:
             userId=self.call_agent.call_phone,
             sessionId= self.call_agent.session_id,
             recordId="",
-            taskId="10001",
+            taskId=self.call_agent.taskId,
             asrText=user_asr_text,
             ext= None
         )
         self.call_agent.logger.info("user_asr_text发送结果: %s", user_asr_text)
+        if user_asr_text != 'ASR408error':
+            self.call_agent.inter_action_total += 1
         # 发送请求并处理响应
         # self.test_request(self.request_data)
         self.to_quest(self.request_data)
@@ -382,7 +392,7 @@ class ToTextBotAgent:
         headers = {'Content-Type': 'application/json'}
         request_data = request.to_json_string()
         url = f"http://{SERVE_HOST}:40072/botservice"
-
+        start_time = time.time()
         self.call_agent.logger.info(f"请求数据:{request_data},url:{url}")
         try:
             response = requests.post(url=url,  json=json.loads(request_data), headers=headers, timeout=10)  # 使用占位URL
@@ -400,6 +410,10 @@ class ToTextBotAgent:
                 self.call_agent.logger.info(f"请求失败,状态码: {response.status_code}, 响应内容: {response.text}")
         except requests.RequestException as e:
             self.call_agent.logger.info(f"请求发生异常: {e}")
+        finally:
+            latency = (time.time() - start_time)
+            registry.BOT_REQUEST_COUNT.inc()
+            registry.BOT_REQUEST_LATENCY.labels(request.taskId).observe(latency)
 
 
 # 模拟接口请求返回