|
| 1 | +// Copyright 2021 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import Foundation |
| 16 | +import FirebaseCore |
| 17 | +import GoogleDataTransport |
| 18 | + |
| 19 | +/// Extension to set Firebase app info. |
| 20 | +extension SystemInfo { |
| 21 | + mutating func setAppInfo(apiKey: String?, projectID: String?) { |
| 22 | + appID = Bundle.main.bundleIdentifier ?? "unknownBundleID" |
| 23 | + // TODO: Reconsider using app version. |
| 24 | + let appVersionKey = "CFBundleShortVersionString" |
| 25 | + appVersion = Bundle.main.infoDictionary?[appVersionKey] as? String ?? "unknownAppVersion" |
| 26 | + // TODO: May also need to log SDK version. |
| 27 | + self.apiKey = apiKey ?? "unknownAPIKey" |
| 28 | + firebaseProjectID = projectID ?? "unknownProjectID" |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/// Extension to set model options. |
| 33 | +extension ModelOptions { |
| 34 | + mutating func setModelOptions(model: CustomModel, isModelUpdateEnabled: Bool? = nil) { |
| 35 | + if let updateEnabled = isModelUpdateEnabled { |
| 36 | + self.isModelUpdateEnabled = updateEnabled |
| 37 | + } |
| 38 | + modelInfo.name = model.name |
| 39 | + modelInfo.hash = model.hash |
| 40 | + modelInfo.modelType = .custom |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +/// Extension to build model download log event. |
| 45 | +extension ModelDownloadLogEvent { |
| 46 | + mutating func setEvent(status: DownloadStatus, errorCode: ErrorCode? = nil, |
| 47 | + roughDownloadDuration: UInt64? = nil, exactDownloadDuration: UInt64? = nil, |
| 48 | + downloadFailureStatus: Int64? = nil, modelOptions: ModelOptions) { |
| 49 | + downloadStatus = status |
| 50 | + if let code = errorCode { |
| 51 | + self.errorCode = code |
| 52 | + } |
| 53 | + if let roughDuration = roughDownloadDuration { |
| 54 | + roughDownloadDurationMs = roughDuration |
| 55 | + } |
| 56 | + if let exactDuration = exactDownloadDuration { |
| 57 | + exactDownloadDurationMs = exactDuration |
| 58 | + } |
| 59 | + if let failureStatus = downloadFailureStatus { |
| 60 | + self.downloadFailureStatus = failureStatus |
| 61 | + } |
| 62 | + options = modelOptions |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +/// Extension to build Firebase ML log event. |
| 67 | +extension FirebaseMlLogEvent { |
| 68 | + mutating func setEvent(eventName: EventName, systemInfo: SystemInfo, |
| 69 | + modelDownloadLogEvent: ModelDownloadLogEvent) { |
| 70 | + self.eventName = eventName |
| 71 | + self.systemInfo = systemInfo |
| 72 | + self.modelDownloadLogEvent = modelDownloadLogEvent |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/// Data object for Firelog event. |
| 77 | +class FBMLDataObject: NSObject, GDTCOREventDataObject { |
| 78 | + private let event: FirebaseMlLogEvent |
| 79 | + |
| 80 | + init(event: FirebaseMlLogEvent) { |
| 81 | + self.event = event |
| 82 | + } |
| 83 | + |
| 84 | + /// Encode Firelog event for transport. |
| 85 | + func transportBytes() -> Data { |
| 86 | + do { |
| 87 | + // TODO: Should this be binary or json serialized? |
| 88 | + let data = try event.serializedData() |
| 89 | + return data |
| 90 | + } catch { |
| 91 | + DeviceLogger.logEvent( |
| 92 | + level: .debug, |
| 93 | + category: .analytics, |
| 94 | + message: "Unable to encode Firelog event.", |
| 95 | + messageCode: .analyticsEventEncodeError |
| 96 | + ) |
| 97 | + return Data() |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +/// Firelog logger. |
| 103 | +class TelemetryLogger { |
| 104 | + /// Mapping ID for the log source. |
| 105 | + private let mappingID = "1326" |
| 106 | + /// Current Firebase app. |
| 107 | + private let app: FirebaseApp |
| 108 | + /// Transport for Firelog events. |
| 109 | + private let fllTransport: GDTCORTransport |
| 110 | + |
| 111 | + /// Init logger, could be nil if unable to get event transport. |
| 112 | + init?(app: FirebaseApp) { |
| 113 | + self.app = app |
| 114 | + guard let fllTransport = GDTCORTransport( |
| 115 | + mappingID: mappingID, |
| 116 | + transformers: nil, |
| 117 | + target: GDTCORTarget.FLL |
| 118 | + ) else { |
| 119 | + DeviceLogger.logEvent( |
| 120 | + level: .debug, |
| 121 | + category: .analytics, |
| 122 | + message: "Unable to create telemetry logger.", |
| 123 | + messageCode: .telemetryInitError |
| 124 | + ) |
| 125 | + return nil |
| 126 | + } |
| 127 | + self.fllTransport = fllTransport |
| 128 | + } |
| 129 | + |
| 130 | + /// Log events to Firelog. |
| 131 | + private func logModelEvent(event: FirebaseMlLogEvent) { |
| 132 | + let eventForTransport: GDTCOREvent = fllTransport.eventForTransport() |
| 133 | + eventForTransport.dataObject = FBMLDataObject(event: event) |
| 134 | + fllTransport.sendTelemetryEvent(eventForTransport) |
| 135 | + } |
| 136 | + |
| 137 | + /// Log model download event to Firelog. |
| 138 | + func logModelDownloadEvent(eventName: EventName, status: ModelDownloadStatus, |
| 139 | + model: CustomModel? = nil) { |
| 140 | + guard app.isDataCollectionDefaultEnabled else { return } |
| 141 | + var modelOptions = ModelOptions() |
| 142 | + if let model = model { |
| 143 | + modelOptions.setModelOptions(model: model) |
| 144 | + } |
| 145 | + var systemInfo = SystemInfo() |
| 146 | + let apiKey = app.options.apiKey |
| 147 | + let projectID = app.options.projectID |
| 148 | + systemInfo.setAppInfo(apiKey: apiKey, projectID: projectID) |
| 149 | + |
| 150 | + var modelDownloadLogEvent = ModelDownloadLogEvent() |
| 151 | + switch status { |
| 152 | + case .successful: modelDownloadLogEvent.setEvent(status: .succeeded, modelOptions: modelOptions) |
| 153 | + case .failed: modelDownloadLogEvent.setEvent(status: .failed, modelOptions: modelOptions) |
| 154 | + case .notStarted, .inProgress: break |
| 155 | + } |
| 156 | + var fbmlEvent = FirebaseMlLogEvent() |
| 157 | + fbmlEvent.setEvent( |
| 158 | + eventName: eventName, |
| 159 | + systemInfo: systemInfo, |
| 160 | + modelDownloadLogEvent: modelDownloadLogEvent |
| 161 | + ) |
| 162 | + logModelEvent(event: fbmlEvent) |
| 163 | + } |
| 164 | +} |
0 commit comments