Explorar o código

日志上报优化:
从点击进入云机开始到推流成功的耗时,毫秒单位
新增字段进入云机耗时字段:timeConsuming
原字段linkStartTime、linkEndTime 格式调整为 2025-02-14 13:56:52.000

tangdehang hai 2 meses
pai
achega
0c3e495801
Modificáronse 2 ficheiros con 73 adicións e 16 borrados
  1. 12 11
      static/rtcEngine/js/WXtrialInterface.js
  2. 61 5
      static/rtcEngine/js/logReport.js

+ 12 - 11
static/rtcEngine/js/WXtrialInterface.js

@@ -246,7 +246,7 @@ const app = new Vue({
                     const status = ["TIMEOUT", "failed"]
                     if (status.includes(event.val)) {
                         // 设置日志 推流状态为失败
-                        logReportObj.setParams({plugFowStatus: 2, linkWay: 0});
+                        logReportObj.setParams({plugFowStatus: 2, linkWay: 0, linkEndTime: logReportObj.formatDate(new Date())});
                         // 日志上报
                         logReportObj.collectLog(
 `信令服务地址nats失败:
@@ -281,8 +281,9 @@ url: ${this.engine.options.url}
                         // this.pushflowPopup()
                         // this.getResidueTime()
 
-                        // 设置日志 推流状态为失败
-                        logReportObj.setParams({plugFowStatus: 1, linkWay: 1});
+                        // 设置日志 推流状态为成功
+                        let now = new Date();
+                        logReportObj.setParams({plugFowStatus: 1, linkWay: 1, timeConsuming: now.getTime() - logReportObj.timeStartTime, linkEndTime: logReportObj.formatDate(now)});
                         // 日志上报
                         logReportObj.collectLog(`推流成功`);
                         playOnBtn.bind(this)()
@@ -291,7 +292,7 @@ url: ${this.engine.options.url}
                     if (event.val === "connecting") return
                     if (event.val === 'failed') {
                         // 设置日志 推流状态为失败
-                        logReportObj.setParams({plugFowStatus: 2, linkWay: 1});
+                        logReportObj.setParams({plugFowStatus: 2, linkWay: 1, linkEndTime: logReportObj.formatDate(new Date())});
                         // 日志上报
                         logReportObj.collectLog(
 `rtc链接失败:
@@ -300,7 +301,7 @@ url: ${this.engine.options.url}
                     }
                     if(event.val === 'disconnected') {
                         // 设置日志 推流状态为失败
-                        logReportObj.setParams({plugFowStatus: 2, linkWay: 1});
+                        logReportObj.setParams({plugFowStatus: 2, linkWay: 1, linkEndTime: logReportObj.formatDate(new Date())});
                         // 日志上报
                         logReportObj.collectLog(
 `rtc链接超时:
@@ -481,7 +482,7 @@ url: ${this.engine.options.url}
             // 链接报错的回调
             this.doConnectDirectivesWs.onerror = res => {
                 // 设置日志 推流状态为失败
-                logReportObj.setParams({plugFowStatus: 1, linkWay: 0});
+                logReportObj.setParams({plugFowStatus: 1, linkWay: 0, linkEndTime: logReportObj.formatDate(new Date())});
                 // 日志上报
                 logReportObj.collectLog(
 `业务指令通道报错:
@@ -640,7 +641,7 @@ type: ${res.type}
                 try {
                     if (!res.success) {
                         // 设置日志 推流状态为失败,和链接状态
-                        logReportObj.setParams({plugFowStatus: 2, linkWay: logReportObj.RESPONSE_CODE[res.status] || 0});
+                        logReportObj.setParams({plugFowStatus: 2, linkWay: logReportObj.RESPONSE_CODE[res.status] || 0, linkEndTime: logReportObj.formatDate(new Date())});
 
                         // 日志上报
                         logReportObj.collectLog(
@@ -672,7 +673,7 @@ method: post
     
                             if (!this.isSupportRtc) {
                                 // 设置日志 推流状态为失败
-                                logReportObj.setParams({plugFowStatus: 2});
+                                logReportObj.setParams({plugFowStatus: 2, linkEndTime: logReportObj.formatDate(new Date())});
                                 // 日志上报
                                 logReportObj.collectLog(`${+isWeixin ? '微信小程序' : ''}当前版本暂不支持使用`);
     
@@ -715,7 +716,7 @@ method: get
                                     }
                                 }else{
                                     // 设置上报参数
-                                    logReportObj.setParams({linkWay: 4, plugFowStatus: 2});
+                                    logReportObj.setParams({linkWay: 4, plugFowStatus: 2, linkEndTime: logReportObj.formatDate(new Date())});
                                     // 日志上报
                                     logReportObj.collectLog(
 `webRtc连接,获取中转地址失败:
@@ -769,7 +770,7 @@ method: post
                             break
                         default:
                             // 设置上报参数
-                            logReportObj.setParams({linkWay: logReportObj.RESPONSE_CODE[res.status] || 0});
+                            logReportObj.setParams({linkWay: logReportObj.RESPONSE_CODE[res.status] || 0, linkEndTime: logReportObj.formatDate(new Date())});
                             
                             // 日志上报
                             logReportObj.collectLog(statusTips[res.status] || 
@@ -792,7 +793,7 @@ method: post
                 }
             }).catch((error) => {
                 // 设置上报参数
-                logReportObj.setParams({linkWay: 4, plugFowStatus: 2});
+                logReportObj.setParams({linkWay: 4, plugFowStatus: 2, linkEndTime: logReportObj.formatDate(new Date())});
                 // 日志上报
                 logReportObj.collectLog(
 `接口获取数据失败:

+ 61 - 5
static/rtcEngine/js/logReport.js

@@ -15,6 +15,7 @@ export default class LogReport {
 
   // 上报参数
   paramsJson = {
+    'timeConsuming': '', // 进入云机耗时字段 毫秒单位
     'clientVersion': '', // 客户端版本号 // 通过$Request版本获取
     'clientType': '', // 客户端类型 // 目前判断出wx或h5
     'phoneModel': '', // 手机型号 // 无法获取
@@ -85,6 +86,8 @@ export default class LogReport {
   timer = null; // 定时器
   timerTime = 6000; // // 日志上报间隔时间
 
+  timeStartTime = 0; // 链接开始时间
+
 	/**
 	 * 构造函数,初始化 LogReport 类的实例
 	 * @param {Object} $Request - 用于发送 HTTP 请求的对象
@@ -120,6 +123,12 @@ export default class LogReport {
 
   // 初始化
   async init() {
+    const now = new Date();
+    // 开始连接的时间戳
+    this.timeStartTime = now.getTime();
+    // 设置本次日志上报时间
+    this.paramsJson.linkStartTime = this.formatDate(now);
+
     // 调用 checkSwitch 方法,检查后端上报日志开关是否打开
 		await this.checkSwitch();
 
@@ -195,11 +204,6 @@ export default class LogReport {
       let logData = this.combinativeParam();
 
       logData.logContent = log;
-
-      // 设置本次日志上报时间
-      const date = new Date();
-      let formattedDate = `${date.getFullYear()}-${('0' + (date.getMonth() + 1)).slice(-2)}-${('0' + date.getDate()).slice(-2)} ${('0' + date.getHours()).slice(-2)}:${('0' + date.getMinutes()).slice(-2)}:${('0' + date.getSeconds()).slice(-2)}`;
-      logData.linkStartTime = formattedDate;
   
       this.logs.push(logData);
   
@@ -320,4 +324,56 @@ export default class LogReport {
     // 移除事件监听
     this.netWorkChange(true);
   }
+
+  // 格式化时间
+  formattedDate(date) {
+    const date = new Date();
+    // 开始连接的时间戳
+    this.timeStartTime = date.getTime();
+    // 设置本次日志上报时间
+    let formattedDate = `${date.getFullYear()}-${('0' + (date.getMonth() + 1)).slice(-2)}-${('0' + date.getDate()).slice(-2)} ${('0' + date.getHours()).slice(-2)}:${('0' + date.getMinutes()).slice(-2)}:${('0' + date.getSeconds()).slice(-2)}`;
+  }
+
+  /**
+   * 格式化日期
+   * @param {Date} date - 要格式化的日期对象
+   * @param {string} format - 格式化字符串,使用以下占位符:
+   *   - 'Y' 表示年份(4 位数)
+   *   - 'm' 表示月份(2 位数)
+   *   - 'd' 表示日期(2 位数)
+   *   - 'H' 表示小时(24 小时制)
+   *   - 'h' 表示小时(12 小时制)
+   *   - 'i' 表示分钟
+   *   - 's' 表示秒数
+   *   - 'u' 表示毫秒
+   *   - 'a' 表示上午/下午标识(小写)
+   *   - 'A' 表示上午/下午标识(大写)
+   * @returns {string} 格式化后的日期字符串 
+  */
+  formatDate(date, format='Y-m-d H:i:s.u') {
+    // 判断输入是否为 Date 对象
+    if (!(date instanceof Date)) {
+        throw new TypeError('The first parameter must be a Date object.');
+    }
+
+    // 定义时间单位的获取方法
+    const formatObj = {
+        'Y': date.getFullYear(), // 年份
+        'm': date.getMonth() + 1, // 月份(+1 是因为 getMonth() 返回的月份是从 0 开始的)
+        'd': date.getDate(), // 日期
+        'H': date.getHours(), // 小时(24 小时制)
+        'h': date.getHours() % 12 || 12, // 小时(12 小时制)
+        'i': date.getMinutes(), // 分钟
+        's': date.getSeconds(), // 秒数
+        'u': date.getMilliseconds(), // 毫秒
+        'a': date.getHours() >= 12 ? 'pm' : 'am', // 上午/下午标识
+        'A': date.getHours() >= 12 ? 'PM' : 'AM' // 上午/下午标识(大写)
+    };
+
+    // 替换格式字符串中的占位符
+    return format.replace(/Y|m|d|H|h|i|s|u|a|A/g, (match) => {
+        return formatObj[match];
+    });
+}
+
 }