Skip to content

Commit

Permalink
feat(performance): add missing methods (#505)
Browse files Browse the repository at this point in the history
Implement all methods on ios.
  • Loading branch information
ebarooni committed Dec 7, 2024
1 parent f5db8be commit 284d70b
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 0 deletions.
45 changes: 45 additions & 0 deletions packages/performance/ios/Plugin/FirebasePerformance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,49 @@ import FirebasePerformance
@objc public func isEnabled() -> Bool {
return Performance.sharedInstance().isDataCollectionEnabled
}

@objc public static func putAttribute(_ trace: Trace, _ attribute: String, _ value: String) {
trace.setValue(value, forAttribute: attribute)
}

@objc public static func getAttribute(_ trace: Trace, _ attribute: String) -> String? {
return trace.value(forAttribute: attribute)
}

@objc public static func getAttributes(_ trace: Trace) -> [String: String] {
return trace.attributes
}

@objc public static func removeAttribute(_ trace: Trace, _ attribute: String) {
trace.removeAttribute(attribute)
}

@objc public static func putMetric(_ trace: Trace, _ metricName: String, _ num: Double) {
trace.setValue(Int64(floor(num)), forMetric: metricName)
}

@objc public static func getMetric(_ trace: Trace, _ metricName: String) -> Int64 {
return trace.valueForMetric(metricName)
}

@objc public func record(_ traceName: String, _ startTime: Double, _ duration: Double, _ attributes: [String: String], _ metrics: [String: Double]) {
let trace = getTraceByName(traceName)
let currentTime = Date().timeIntervalSince1970 * 1000
let startDelay = max(0, (startTime - currentTime) / 1000)

DispatchQueue.global().asyncAfter(deadline: .now() + startDelay) {
for (key, value) in attributes {
FirebasePerformance.putAttribute(trace!, key, value)
}
for (key, value) in metrics {
FirebasePerformance.putMetric(trace!, key, value)
}
self.startTrace(traceName)

DispatchQueue.global().asyncAfter(deadline: .now() + duration / 1000) {
self.stopTrace(traceName)
}
}
return
}
}
7 changes: 7 additions & 0 deletions packages/performance/ios/Plugin/FirebasePerformancePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@
CAP_PLUGIN_METHOD(incrementMetric, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(setEnabled, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(isEnabled, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(putAttribute, CAPPluginReturnNone);
CAP_PLUGIN_METHOD(getAttribute, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(getAttributes, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(removeAttribute, CAPPluginReturnNone);
CAP_PLUGIN_METHOD(putMetric, CAPPluginReturnNone);
CAP_PLUGIN_METHOD(getMetric, CAPPluginReturnPromise);
CAP_PLUGIN_METHOD(record, CAPPluginReturnNone);
)
142 changes: 142 additions & 0 deletions packages/performance/ios/Plugin/FirebasePerformancePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public class FirebasePerformancePlugin: CAPPlugin {
public let errorEnabledMissing = "enabled must be provided."
public let errorTraceNameAlreadyAssigned = "traceName already assigned."
public let errorTraceNotFound = "No trace was found with the provided traceName."
public let errorAttributeMissing = "attribute must be provided."
public let errorValueMissing = "value must be provided."
public let errorNumMissing = "num must be provided."
public let errorStartTimeMissing = "startTime must be provided."
public let errorDurationMissing = "duration must be provided."
private var implementation: FirebasePerformance?

override public func load() {
Expand Down Expand Up @@ -80,4 +85,141 @@ public class FirebasePerformancePlugin: CAPPlugin {
result["enabled"] = enabled
call.resolve(result)
}

@objc func putAttribute(_ call: CAPPluginCall) {
guard let traceName = call.getString("traceName") else {
call.reject(errorTraceNameMissing)
return
}
guard let attribute = call.getString("attribute") else {
call.reject(errorAttributeMissing)
return
}
guard let value = call.getString("value") else {
call.reject(errorValueMissing)
return
}
let trace = implementation?.getTraceByName(traceName)
guard trace != nil else {
call.reject(errorTraceNotFound)
return
}
FirebasePerformance.putAttribute(trace!, attribute, value)
call.resolve()
}

@objc func getAttribute(_ call: CAPPluginCall) {
guard let traceName = call.getString("traceName") else {
call.reject(errorTraceNameMissing)
return
}
guard let attribute = call.getString("attribute") else {
call.reject(errorAttributeMissing)
return
}
let trace = implementation?.getTraceByName(traceName)
guard trace != nil else {
call.reject(errorTraceNotFound)
return
}
var result = JSObject()
result["value"] = FirebasePerformance.getAttribute(trace!, attribute) ?? NSNull()
call.resolve(result)
}

@objc func getAttributes(_ call: CAPPluginCall) {
guard let traceName = call.getString("traceName") else {
call.reject(errorTraceNameMissing)
return
}
let trace = implementation?.getTraceByName(traceName)
guard trace != nil else {
call.reject(errorTraceNotFound)
return
}
call.resolve(["result": FirebasePerformance.getAttributes(trace!)])
}

@objc func removeAttribute(_ call: CAPPluginCall) {
guard let traceName = call.getString("traceName") else {
call.reject(errorTraceNameMissing)
return
}
guard let attribute = call.getString("attribute") else {
call.reject(errorAttributeMissing)
return
}
let trace = implementation?.getTraceByName(traceName)
guard trace != nil else {
call.reject(errorTraceNotFound)
return
}
FirebasePerformance.removeAttribute(trace!, attribute)
call.resolve()
}

@objc func putMetric(_ call: CAPPluginCall) {
guard let traceName = call.getString("traceName") else {
call.reject(errorTraceNameMissing)
return
}
guard let metricName = call.getString("metricName") else {
call.reject(errorMetricNameMissing)
return
}
guard let num = call.getDouble("num") else {
call.reject(errorNumMissing)
return
}
let trace = implementation?.getTraceByName(traceName)
guard trace != nil else {
call.reject(errorTraceNotFound)
return
}
FirebasePerformance.putMetric(trace!, metricName, num)
call.resolve()
}

@objc func getMetric(_ call: CAPPluginCall) {
guard let traceName = call.getString("traceName") else {
call.reject(errorTraceNameMissing)
return
}
guard let metricName = call.getString("metricName") else {
call.reject(errorMetricNameMissing)
return
}
let trace = implementation?.getTraceByName(traceName)
guard trace != nil else {
call.reject(errorTraceNotFound)
return
}
let value = FirebasePerformance.getMetric(trace!, metricName)
call.resolve(["value": value == 0 ? NSNull() : value])
}

@objc func record(_ call: CAPPluginCall) {
guard let traceName = call.getString("traceName") else {
call.reject(errorTraceNameMissing)
return
}
guard let startTime = call.getDouble("startTime") else {
call.reject(errorStartTimeMissing)
return
}
guard let duration = call.getDouble("duration") else {
call.reject(errorDurationMissing)
return
}
let options = call.getObject("options")
let metrics = options?["metrics"] as? [String: Double] ?? [:]
let attributes = options?["attributes"] as? [String: String] ?? [:]
let trace = implementation?.getTraceByName(traceName)
guard trace != nil else {
call.reject(errorTraceNotFound)
return
}
implementation?.record(traceName, startTime, duration, attributes, metrics)
call.resolve()
}
}

0 comments on commit 284d70b

Please sign in to comment.