DavidLiu 4 месяцев назад
Родитель
Сommit
b5e3c86a31

+ 0 - 2
src/core/callcenter/__init__.py

@@ -7,7 +7,6 @@ from .config import BaseConfig
 from flask import Flask, request, render_template_string
 from flask_sqlalchemy import SQLAlchemy
 from flask_cors import CORS
-# from prometheus_flask_exporter import PrometheusMetrics
 from .constant import error_response
 from .exception import BizException
 
@@ -17,7 +16,6 @@ logging.getLogger('apscheduler').setLevel(logging.CRITICAL)
 
 def create_app():
     _app = Flask(__name__)
-    # _metrics = PrometheusMetrics(_app)
     # 添加配置文件
     _app.config.from_object(BaseConfig)
 

+ 6 - 8
src/core/callcenter/esl/client.py

@@ -1,14 +1,11 @@
 #!/usr/bin/env python3
 # encoding:utf-8
 
-import json
 import random
 import socketserver
-import sys
 
 import ESL
 import time
-import mmh3
 import uuid
 import threading
 import traceback
@@ -20,16 +17,15 @@ from src.core.callcenter import BizException
 from src.core.callcenter.cache import Cache
 from src.core.callcenter.api import MakeCallContext, DelayAction, CallInfo, DeviceInfo, NextCommand
 from src.core.callcenter.callback import Callback
-from src.core.callcenter.constant import SK, EMPTY, CTI_ENGINE_DELAY_ACTION_LOCK, HOLD_MUSIC_PATH, WaitingHangupMusicPath, saasId
+from src.core.callcenter.constant import SK, EMPTY, WaitingHangupMusicPath, saasId
 from src.core.callcenter.esl.constant.esl_constant import BRIDGE_VARIABLES, BRIDGE, HANGUP, NORMAL_CLEARING, SIP_HEADER, \
-    SPACE, SPLIT, SOFIA, \
+    SPACE, SOFIA, \
     ORIGINATE, PARK, SET, EAVESDROP, SMF_ALEG, EXECUTE, PLAYBACK, PAUSE, TRANSFER, UUID_TRANSFER, UUID_BROADCAST, \
     UUID_BREAK, UUID_HOLD, \
     UUID_RECORD, UUID_SETVAR, UUID_GETVAR, UUID_KILL
-import src.core.registry as Registry
 import src.core.callcenter.esl.utils.esl_event_util as EslEventUtil
 import src.core.callcenter.esl.handler as event_handler
-from src.core.callcenter.esl.constant.sip_header_constant import sipHeaderHoldMusic, profile1, profile2, sipHeaderCallId
+from src.core.callcenter.esl.constant.sip_header_constant import sipHeaderHoldMusic, profile1, profile2
 from src.core.callcenter.enumeration import CallCause, DeviceType, DelayActionEnum, HangupDir, CallType, NextType, \
     Direction, CdrType, BizErrorCode
 from src.core.callcenter.esl.handler.default_esl_event_handler import DefaultEslEventHandler
@@ -37,6 +33,8 @@ from src.core.callcenter.snowflake import Snowflake
 from src.core.datasource import SERVE_HOST
 from src.core.voip.constant import *
 from src.core.callcenter.data_handler import *
+from src.core.callcenter import registry
+
 class InboundClient:
 
     def __init__(self, agent, app):
@@ -545,7 +543,7 @@ class OutboundClient:
                         self.build_call_info(call_id, device_id, new_device_id, destination=None, bucket_type=_bucket_call_type,  **kwargs)
                         self.server.agent.acd_service.transfer_to_agent(call_id, new_device_id)
 
-                    Registry.counter('call_incoming_requests', '呼入流量统计', ['bucket_type', str(_bucket_call_type)]).inc()
+                    registry.counter('call_incoming_requests', '呼入流量统计', {'bucket_type': f"{_bucket_call_type}"}).inc()
                     self.server.dataHandleServer.create_record(call_id, caller_number, _bucket_call_type, service_category=None, destination=destination)
 
                     # # 检查白名单

+ 5 - 10
src/core/callcenter/esl/handler/channel_answer_handler.py

@@ -1,12 +1,7 @@
 #!/usr/bin/env python3
 # encoding:utf-8
-import json
-import time
-from datetime import datetime
-import src.core.registry as Registry
-from src.core.callcenter.constant import saasId, get_record_prefix, get_record_file_name
-from src.core.callcenter.enumeration import NextType, AnswerFlag, Direction, DeviceType, AgentScene, CdrType, CallType, \
-    CallStage, CallType
+from src.core.callcenter.constant import get_record_prefix, get_record_file_name
+from src.core.callcenter.enumeration import NextType, AnswerFlag, Direction, DeviceType, CdrType, CallStage, CallType
 from src.core.callcenter.esl.annotation import EslEventName
 import src.core.callcenter.esl.utils.esl_event_util as EslEventUtil
 from src.core.callcenter.esl.constant.event_names import CHANNEL_ANSWER
@@ -14,7 +9,7 @@ from src.core.callcenter.esl.handler.esl_event_handler import EslEventHandler
 from src.core.callcenter.api import CallInfo, DeviceInfo, NextCommand, MakeCallContext
 from src.core.callcenter.push import PushHandler
 from src.core.callcenter.snowflake import Snowflake
-
+from src.core.callcenter import registry
 from src.core.callcenter.data_handler import *
 
 @EslEventName(CHANNEL_ANSWER)
@@ -114,7 +109,7 @@ class ChannelAnswerHandler(EslEventHandler):
                          call.caller, call.called, next_command.device_id, next_command.next_value))
         if CallType.BOT_CALL.code == call.call_type:
             call.answer_flag = AnswerFlag.ROBOT_ANSWER.code
-            Registry.counter('call_incoming_bot_answer_requests', '呼入机器人接听量',['bucket_type', str(call.bucket_type)]).inc()
+            registry.counter('call_incoming_bot_answer_requests', '呼入机器人接听量', {'bucket_type': f"{call.bucket_type}"}).inc()
 
         self.record(event, device_id=device.device_id)
         device1 = call.device_info_map.get(next_command.device_id)
@@ -134,7 +129,7 @@ class ChannelAnswerHandler(EslEventHandler):
         device_id = EslEventUtil.getDeviceId(event)
         call.answer_flag = AnswerFlag.AGENT_ANSWER.code
         if CallType.BOT_CALL.code == call.call_type:
-            Registry.counter('call_incoming_transfer_requests', '呼入机器人转人量',['bucket_type', str(call.bucket_type)]).inc()
+            registry.counter('call_incoming_transfer_requests', '呼入机器人转人量', {'bucket_type': f"{call.bucket_type}"}).inc()
         self.record(event, device_id)
         call.next_commands.append(NextCommand(device_id, NextType.NEXT_TRANSFER_SUCCESS.code, call.device_list[1]))
         self.logger.info("转接电话中 callId:%s, from:%s, to:%s "% (call.call_id, from_device_id, device_id))

+ 24 - 0
src/core/callcenter/registry.py

@@ -0,0 +1,24 @@
+#!/usr/bin/env python3
+# encoding:utf-8
+
+# import prometheus_client
+from . import app
+from typing import Iterable
+# from prometheus_client import Counter, Gauge, Histogram, Summary, start_http_server, generate_latest
+# from prometheus_client.core import CollectorRegistry
+from prometheus_flask_exporter import PrometheusMetrics
+
+metrics = PrometheusMetrics(app)
+
+
+def counter(name: str, documentation: str, labels=None):
+    return metrics.counter(name, documentation, labels)
+
+def gauge(name: str, documentation: str, labels=None):
+    return metrics.gauge(name, documentation, labels)
+
+def histogram(name: str, documentation: str, labels=None):
+    return metrics.histogram(name, documentation, labels)
+
+def summary(name: str, documentation: str, labels=None):
+    return metrics.summary(name, documentation, labels)

+ 4 - 12
src/core/callcenter/views.py

@@ -2,17 +2,15 @@
 # encoding:utf-8
 from . import app
 from src.core.callcenter.agent import AgentService, AgentOperService
-from src.core.callcenter.constant import success_response, error_response
+from src.core.callcenter.constant import success_response
 from src.core.callcenter.enumeration import CallType
 from src.core.callcenter.esl.client import InboundClient, OutboundClient
-from flask import Flask, request, render_template_string, Response
+from flask import request, render_template_string, Response
 
 from src.core.callcenter.call import CallService
-from src.core.callcenter.api import AgentCallRequest, AgentActionRequest, HangupCallRequest, \
-    HumanServiceQueryRequest
+from src.core.callcenter.api import AgentCallRequest, AgentActionRequest, HangupCallRequest
 from src.core.voip.bot import BotAgent
 from .acd import AcdService
-from src.core.registry import generate_latest
 
 agent = BotAgent(app)
 inbound_client = InboundClient(agent,app)
@@ -190,10 +188,4 @@ def member_active():
 def num_generate():
     """获取 cti 流程 ID"""
     flow_id = call_service.snowflake.next_id()
-    return success_response(flow_id)
-
-
-@app.route('/metrics', methods=['GET'])
-def get_data():
-    """Returns all data as plaintext."""
-    return Response(generate_latest(), mimetype=CONTENT_TYPE_LATEST)
+    return success_response(flow_id)

+ 0 - 21
src/core/registry.py

@@ -1,21 +0,0 @@
-#!/usr/bin/env python3
-# encoding:utf-8
-
-import prometheus_client
-from typing import Iterable
-from prometheus_client import Counter, Gauge, Histogram, Summary, start_http_server, generate_latest
-
-from prometheus_client.core import CollectorRegistry
-
-
-def counter(name: str, documentation: str, labelnames: Iterable[str] = ()):
-    return Counter(name, documentation, labelnames)
-
-def gauge(name: str, documentation: str, labelnames: Iterable[str] = ()):
-    return Gauge(name, documentation, labelnames)
-
-def histogram(name: str, documentation: str, labelnames: Iterable[str] = ()):
-    return Histogram(name, documentation, labelnames)
-
-def summary(name: str, documentation: str, labelnames: Iterable[str] = ()):
-    return Summary(name, documentation, labelnames)