|
| 1 | +// |
| 2 | +// APIReporter.swift |
| 3 | +// CallAPI |
| 4 | +// |
| 5 | +// Created by wushengtao on 2024/4/8. |
| 6 | +// |
| 7 | + |
| 8 | +import AgoraRtcKit |
| 9 | + |
| 10 | + |
| 11 | +/// 场景化类型 |
| 12 | +public enum APIType: Int { |
| 13 | + case ktv = 1 //K歌 |
| 14 | + case call = 2 //呼叫 |
| 15 | + case beauty = 3 //美颜 |
| 16 | + case videoLoader = 4 //秒开/秒切 |
| 17 | + case pk = 5 //团战 |
| 18 | + case vitualSpace = 6 // |
| 19 | + case screenSpace = 7 //屏幕共享 |
| 20 | + case audioScenario = 8 //音频scenario |
| 21 | +} |
| 22 | + |
| 23 | +enum APIEventType: Int { |
| 24 | + case api = 0 //api事件 |
| 25 | + case cost //耗时事件 |
| 26 | + case custom //自定义事件 |
| 27 | +} |
| 28 | + |
| 29 | +struct ApiEventKey { |
| 30 | + static let type = "type" |
| 31 | + static let desc = "desc" |
| 32 | + static let apiValue = "apiValue" |
| 33 | + static let ts = "ts" |
| 34 | + static let ext = "ext" |
| 35 | +} |
| 36 | + |
| 37 | +struct APICostEvent { |
| 38 | + static let channelUsage = "channelUsage" //频道使用耗时 |
| 39 | + static let firstFrameActual = "firstFrameActual" //首帧实际耗时 |
| 40 | + static let firstFramePerceived = "firstFramePerceived" //首帧感官耗时 |
| 41 | +} |
| 42 | + |
| 43 | +let formatter = DateFormatter() |
| 44 | +func debugApiPrint(_ message: String) { |
| 45 | +#if DEBUG |
| 46 | + formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS" |
| 47 | + let timeString = formatter.string(from: Date()) |
| 48 | + print("\(timeString) \(message)") |
| 49 | +#endif |
| 50 | +} |
| 51 | + |
| 52 | +@objcMembers |
| 53 | +public class APIReporter: NSObject { |
| 54 | + private var engine: AgoraRtcEngineKit |
| 55 | + private let messsageId: String = "agora:scenarioAPI" |
| 56 | + private var category: String |
| 57 | + private var durationEventStartMap: [String: Int64] = [:] |
| 58 | + |
| 59 | + //MARK: public |
| 60 | + public init(type: APIType, version: String, engine: AgoraRtcEngineKit) { |
| 61 | + self.category = "\(type.rawValue)_iOS_\(version)" |
| 62 | + self.engine = engine |
| 63 | + super.init() |
| 64 | + |
| 65 | + configParameters() |
| 66 | + } |
| 67 | + |
| 68 | + public func reportFuncEvent(name: String, value: [String: Any], ext: [String: Any]) { |
| 69 | + let content = "[APIReporter]reportFuncEvent: \(name) value: \(value) ext: \(ext)" |
| 70 | + debugApiPrint(content) |
| 71 | + let eventMap: [String: Any] = [ApiEventKey.type: APIEventType.api.rawValue, ApiEventKey.desc: name] |
| 72 | + let labelMap: [String: Any] = [ApiEventKey.apiValue: value, ApiEventKey.ts: getCurrentTs(), ApiEventKey.ext: ext] |
| 73 | + let event = convertToJSONString(eventMap) ?? "" |
| 74 | + let label = convertToJSONString(labelMap) ?? "" |
| 75 | + engine.sendCustomReportMessage(messsageId, |
| 76 | + category: category, |
| 77 | + event: event, |
| 78 | + label: label, |
| 79 | + value: 0) |
| 80 | + } |
| 81 | + |
| 82 | + public func startDurationEvent(name: String) { |
| 83 | + durationEventStartMap[name] = getCurrentTs() |
| 84 | + } |
| 85 | + |
| 86 | + public func endDurationEvent(name: String, ext: [String: Any]) { |
| 87 | + guard let beginTs = durationEventStartMap[name] else {return} |
| 88 | + durationEventStartMap.removeValue(forKey: name) |
| 89 | + let ts = getCurrentTs() |
| 90 | + let cost = Int(ts - beginTs) |
| 91 | + |
| 92 | + reportCostEvent(ts: ts, name: name, cost: cost, ext: ext) |
| 93 | + } |
| 94 | + |
| 95 | + public func reportCostEvent(name: String, cost: Int, ext: [String: Any]) { |
| 96 | + durationEventStartMap.removeValue(forKey: name) |
| 97 | + reportCostEvent(ts: getCurrentTs(), name: name, cost: cost, ext: ext) |
| 98 | + } |
| 99 | + |
| 100 | + public func reportCustomEvent(name: String, ext: [String: Any]) { |
| 101 | + let content = "[APIReporter]reportCustomEvent: \(name) ext: \(ext)" |
| 102 | + debugApiPrint(content) |
| 103 | + let eventMap: [String: Any] = [ApiEventKey.type: APIEventType.custom.rawValue, ApiEventKey.desc: name] |
| 104 | + let labelMap: [String: Any] = [ApiEventKey.ts: getCurrentTs(), ApiEventKey.ext: ext] |
| 105 | + let event = convertToJSONString(eventMap) ?? "" |
| 106 | + let label = convertToJSONString(labelMap) ?? "" |
| 107 | + engine.sendCustomReportMessage(messsageId, |
| 108 | + category: category, |
| 109 | + event: event, |
| 110 | + label: label, |
| 111 | + value: 0) |
| 112 | + } |
| 113 | + |
| 114 | + public func writeLog(content: String, level: AgoraLogLevel) { |
| 115 | + engine.writeLog(level, content: content) |
| 116 | + } |
| 117 | + |
| 118 | + public func cleanCache() { |
| 119 | + durationEventStartMap.removeAll() |
| 120 | + } |
| 121 | + |
| 122 | + //MARK: private |
| 123 | + private func reportCostEvent(ts: Int64, name: String, cost: Int, ext: [String: Any]) { |
| 124 | + let content = "[APIReporter]reportCostEvent: \(name) cost: \(cost) ms ext: \(ext)" |
| 125 | + debugApiPrint(content) |
| 126 | + writeLog(content: content, level: .info) |
| 127 | + let eventMap: [String: Any] = [ApiEventKey.type: APIEventType.cost.rawValue, ApiEventKey.desc: name] |
| 128 | + let labelMap: [String: Any] = [ApiEventKey.ts: ts, ApiEventKey.ext: ext] |
| 129 | + let event = convertToJSONString(eventMap) ?? "" |
| 130 | + let label = convertToJSONString(labelMap) ?? "" |
| 131 | + engine.sendCustomReportMessage(messsageId, |
| 132 | + category: category, |
| 133 | + event: event, |
| 134 | + label: label, |
| 135 | + value: cost) |
| 136 | + } |
| 137 | + |
| 138 | + private func configParameters() { |
| 139 | +// engine.setParameters("{\"rtc.qos_for_test_purpose\": true}") |
| 140 | + engine.setParameters("{\"rtc.direct_send_custom_event\": true}") |
| 141 | + engine.setParameters("{\"rtc.log_external_input\": true}") |
| 142 | + } |
| 143 | + |
| 144 | + private func getCurrentTs() -> Int64 { |
| 145 | + return Int64(round(Date().timeIntervalSince1970 * 1000.0)) |
| 146 | + } |
| 147 | + |
| 148 | + private func convertToJSONString(_ dictionary: [String: Any]) -> String? { |
| 149 | + do { |
| 150 | + let jsonData = try JSONSerialization.data(withJSONObject: dictionary, options: []) |
| 151 | + if let jsonString = String(data: jsonData, encoding: .utf8) { |
| 152 | + return jsonString |
| 153 | + } |
| 154 | + } catch { |
| 155 | + writeLog(content: "[APIReporter]convert to json fail: \(error) dictionary: \(dictionary)", level: .warn) |
| 156 | + } |
| 157 | + return nil |
| 158 | + } |
| 159 | +} |
0 commit comments