From 67c0f3a14188306631fd48ce14d9afbf80ba64a0 Mon Sep 17 00:00:00 2001 From: Ehsan Barooni <66495214+ebarooni@users.noreply.github.com> Date: Fri, 6 Dec 2024 23:35:46 +0100 Subject: [PATCH 01/14] feat(performance): add missing methods (#505) Add method definitions. --- packages/performance/README.md | 213 ++++++++++++++++++++++++ packages/performance/src/definitions.ts | 184 ++++++++++++++++++++ 2 files changed, 397 insertions(+) diff --git a/packages/performance/README.md b/packages/performance/README.md index b499547c..2130df29 100644 --- a/packages/performance/README.md +++ b/packages/performance/README.md @@ -69,7 +69,15 @@ const isEnabled = async () => { * [`incrementMetric(...)`](#incrementmetric) * [`setEnabled(...)`](#setenabled) * [`isEnabled()`](#isenabled) +* [`putAttribute(...)`](#putattribute) +* [`getAttribute(...)`](#getattribute) +* [`getAttributes(...)`](#getattributes) +* [`removeAttribute(...)`](#removeattribute) +* [`putMetric(...)`](#putmetric) +* [`getMetric(...)`](#getmetric) +* [`record(...)`](#record) * [Interfaces](#interfaces) +* [Type Aliases](#type-aliases) @@ -160,6 +168,131 @@ Determines whether performance monitoring is enabled or disabled. -------------------- +### putAttribute(...) + +```typescript +putAttribute(options: PutAttributeOptions) => Promise +``` + +Sets a custom attribute of a trace to a given value. + +| Param | Type | +| ------------- | ------------------------------------------------------------------- | +| **`options`** | PutAttributeOptions | + +**Since:** 6.3.0 + +-------------------- + + +### getAttribute(...) + +```typescript +getAttribute(options: GetAttributeOptions) => Promise +``` + +Returns the value of a custom attribute of a trace. + +| Param | Type | +| ------------- | ------------------------------------------------------------------- | +| **`options`** | GetAttributeOptions | + +**Returns:** Promise<GetAttributeResult> + +**Since:** 6.3.0 + +-------------------- + + +### getAttributes(...) + +```typescript +getAttributes(options: GetAttributesOptions) => Promise +``` + +Gets the all the custom attributes of a trace with their values. + +| Param | Type | +| ------------- | --------------------------------------------------------------------- | +| **`options`** | GetAttributesOptions | + +**Returns:** Promise<GetAttributesResult> + +**Since:** 6.3.0 + +-------------------- + + +### removeAttribute(...) + +```typescript +removeAttribute(options: RemoveAttributeOptions) => Promise +``` + +Removes a custom attribute from a trace given its name. + +| Param | Type | +| ------------- | ------------------------------------------------------------------- | +| **`options`** | GetAttributeOptions | + +**Since:** 6.3.0 + +-------------------- + + +### putMetric(...) + +```typescript +putMetric(options: PutMetricOptions) => Promise +``` + +Sets the value of a custom metric. + +| Param | Type | +| ------------- | ------------------------------------------------------------- | +| **`options`** | PutMetricOptions | + +**Since:** 6.3.0 + +-------------------- + + +### getMetric(...) + +```typescript +getMetric(options: GetMetricOptions) => Promise +``` + +Get the value of a custom metric by name. + +| Param | Type | +| ------------- | ------------------------------------------------------------- | +| **`options`** | GetMetricOptions | + +**Returns:** Promise<GetMetricResult> + +**Since:** 6.3.0 + +-------------------- + + +### record(...) + +```typescript +record(options: RecordOptions) => Promise +``` + +Records a trace given its name and options. + +| Param | Type | +| ------------- | ------------------------------------------------------- | +| **`options`** | RecordOptions | + +**Since:** 6.3.0 + +-------------------- + + ### Interfaces @@ -199,6 +332,86 @@ Determines whether performance monitoring is enabled or disabled. | ------------- | -------------------- | --------------------------------------------------------------- | ----- | | **`enabled`** | boolean | `true` if performance monitoring is enabled, otherwise `false`. | 0.1.0 | + +#### PutAttributeOptions + +| Prop | Type | Description | Since | +| --------------- | ------------------- | --------------------------------------- | ----- | +| **`traceName`** | string | Name of the trace to set its attribute. | 6.3.0 | +| **`attribute`** | string | Name of the attribute to set its value. | 6.3.0 | +| **`value`** | string | The value to set to the attribute. | 6.3.0 | + + +#### GetAttributeResult + +| Prop | Type | Description | Since | +| ----------- | --------------------------- | ---------------------------------- | ----- | +| **`value`** | string \| null | The value of the custom attribute. | 6.3.0 | + + +#### GetAttributeOptions + +| Prop | Type | Description | Since | +| --------------- | ------------------- | -------------------------------------------- | ----- | +| **`traceName`** | string | Name of the trace to set its attribute. | 6.3.0 | +| **`attribute`** | string | Name of the attribute to retrieve its value. | 6.3.0 | + + +#### GetAttributesResult + +| Prop | Type | Description | Since | +| ----------- | ------------------------------------- | ------------------------------------------------------------ | ----- | +| **`value`** | { [k: string]: string; } | A map of all custom attributes of a trace with their values. | 6.3.0 | + + +#### GetAttributesOptions + +| Prop | Type | Description | Since | +| --------------- | ------------------- | ---------------------------------------- | ----- | +| **`traceName`** | string | Name of the trace to get its attributes. | 6.3.0 | + + +#### PutMetricOptions + +| Prop | Type | Description | Since | +| ---------------- | ------------------- | ---------------------------------------------------------------------------------------- | ----- | +| **`traceName`** | string | Name of the trace to set its metric. | 6.3.0 | +| **`metricName`** | string | The metric name. | 6.3.0 | +| **`num`** | number | The value to set for the metric. The given value is floored down to the nearest integer. | 6.3.0 | + + +#### GetMetricResult + +| Prop | Type | Description | Since | +| ----------- | --------------------------- | ---------------------------------------------------------- | ----- | +| **`value`** | number \| null | The value of the metric if exists. If not it will be null. | 6.3.0 | + + +#### GetMetricOptions + +| Prop | Type | Description | Since | +| ---------------- | ------------------- | ------------------------------------ | ----- | +| **`traceName`** | string | Name of the trace to get its metric. | 6.3.0 | +| **`metricName`** | string | The metric name. | 6.3.0 | + + +#### RecordOptions + +| Prop | Type | Description | Since | +| --------------- | ----------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------- | ----- | +| **`traceName`** | string | Name of the trace to record. | 6.3.0 | +| **`startTime`** | number | Start time of the trace since epoch in milliseconds. | 6.3.0 | +| **`duration`** | number | The duration of the trace in milliseconds. | 6.3.0 | +| **`options`** | { metrics?: { [key: string]: number; }; attributes?: { [key: string]: string; }; } | An optional object that hold optional maps of custom metrics and attributes. | 6.3.0 | + + +### Type Aliases + + +#### RemoveAttributeOptions + +GetAttributeOptions + ## Changelog diff --git a/packages/performance/src/definitions.ts b/packages/performance/src/definitions.ts index 883fe31b..07d6261c 100644 --- a/packages/performance/src/definitions.ts +++ b/packages/performance/src/definitions.ts @@ -30,6 +30,49 @@ export interface FirebasePerformancePlugin { * @since 0.1.0 */ isEnabled(): Promise; + /** + * Sets a custom attribute of a trace to a given value. + * + * @since 6.3.0 + */ + putAttribute(options: PutAttributeOptions): Promise; + /** + * Returns the value of a custom attribute of a trace. + * + * @since 6.3.0 + */ + getAttribute(options: GetAttributeOptions): Promise; + /** + * Gets the all the custom attributes of a trace with their values. + * + * @since 6.3.0 + */ + getAttributes(options: GetAttributesOptions): Promise; + /** + * Removes a custom attribute from a trace given its name. + * + * @since 6.3.0 + */ + removeAttribute(options: RemoveAttributeOptions): Promise; + /** + * Sets the value of a custom metric. + * + * @since 6.3.0 + */ + putMetric(options: PutMetricOptions): Promise; + /** + * Get the value of a custom metric by name. + * + * @since 6.3.0 + */ + getMetric(options: GetMetricOptions): Promise; + + /** + * Records a trace given its name and options. + * + * @since 6.3.0 + */ + record(options: RecordOptions): Promise; } /** @@ -105,3 +148,144 @@ export interface IsEnabledResult { */ enabled: boolean; } + +export interface PutAttributeOptions { + /** + * Name of the trace to set its attribute. + * + * @since 6.3.0 + */ + traceName: string; + /** + * Name of the attribute to set its value. + * + * @since 6.3.0 + */ + attribute: string; + /** + * The value to set to the attribute. + * + * @since 6.3.0 + */ + value: string; +} + +export interface GetAttributeOptions { + /** + * Name of the trace to set its attribute. + * + * @since 6.3.0 + */ + traceName: string; + /** + * Name of the attribute to retrieve its value. + * + * @since 6.3.0 + */ + attribute: string; +} + +export interface GetAttributeResult { + /** + * The value of the custom attribute. + * + * @since 6.3.0 + */ + value: string | null; +} + +export interface GetAttributesOptions { + /** + * Name of the trace to get its attributes. + * + * @since 6.3.0 + */ + traceName: string; +} + +export interface GetAttributesResult { + /** + * A map of all custom attributes of a trace with their values. + * + * @since 6.3.0 + */ + value: { [k: string]: string }; +} + +export type RemoveAttributeOptions = GetAttributeOptions; + +export interface PutMetricOptions { + /** + * Name of the trace to set its metric. + * + * @since 6.3.0 + */ + traceName: string; + /** + * The metric name. + * + * @since 6.3.0 + */ + metricName: string; + /** + * The value to set for the metric. + * The given value is floored down to the nearest integer. + * + * @since 6.3.0 + */ + num: number; +} + +export interface GetMetricOptions { + /** + * Name of the trace to get its metric. + * + * @since 6.3.0 + */ + traceName: string; + /** + * The metric name. + * + * @since 6.3.0 + */ + metricName: string; +} + +export interface GetMetricResult { + /** + * The value of the metric if exists. If not it will be null. + * + * @since 6.3.0 + */ + value: number | null; +} + +export interface RecordOptions { + /** + * Name of the trace to record. + * + * @since 6.3.0 + */ + traceName: string; + /** + * Start time of the trace since epoch in milliseconds. + * + * @since 6.3.0 + */ + startTime: number; + /** + * The duration of the trace in milliseconds. + * + * @since 6.3.0 + */ + duration: number; + /** + * An optional object that hold optional maps of custom metrics and attributes. + * + * @since 6.3.0 + */ + options?: { + metrics?: { [key: string]: number }; + attributes?: { [key: string]: string }; + }; +} From a3c5cd33be54c78ee6cecb491cac2406860afaff Mon Sep 17 00:00:00 2001 From: Ehsan Barooni <66495214+ebarooni@users.noreply.github.com> Date: Fri, 6 Dec 2024 23:36:23 +0100 Subject: [PATCH 02/14] feat(performance): add missing methods (#505) Implement all method definitions on the web. --- packages/performance/src/web.ts | 81 +++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/packages/performance/src/web.ts b/packages/performance/src/web.ts index 4ed70e48..2c92611f 100644 --- a/packages/performance/src/web.ts +++ b/packages/performance/src/web.ts @@ -9,8 +9,19 @@ import type { SetEnabledOptions, StartTraceOptions, StopTraceOptions, + PutAttributeOptions, + GetAttributeOptions, + GetAttributeResult, + GetAttributesOptions, + GetAttributesResult, + RemoveAttributeOptions, + PutMetricOptions, + GetMetricResult, + RecordOptions, } from './definitions'; +const traceNotFoundError = 'No trace found for the given name.'; + export class FirebasePerformanceWeb extends WebPlugin implements FirebasePerformancePlugin @@ -54,4 +65,74 @@ export class FirebasePerformanceWeb }; return result; } + + public async putAttribute({ + traceName, + attribute, + value, + }: PutAttributeOptions): Promise { + const trace = this.traces[traceName]; + if (!trace) return; + trace.putAttribute(attribute, value); + return; + } + + public async getAttribute({ + traceName, + attribute, + }: GetAttributeOptions): Promise { + const trace = this.traces[traceName]; + if (!trace) throw new Error(traceNotFoundError); + return { value: trace.getAttribute(attribute) ?? null }; + } + + public async getAttributes({ + traceName, + }: GetAttributesOptions): Promise { + const trace = this.traces[traceName]; + if (!trace) throw new Error(traceNotFoundError); + return { value: trace.getAttributes() }; + } + + public async removeAttribute({ + traceName, + attribute, + }: RemoveAttributeOptions): Promise { + const trace = this.traces[traceName]; + if (!trace) throw new Error(traceNotFoundError); + trace.removeAttribute(attribute); + return; + } + + public async putMetric({ + traceName, + metricName, + num, + }: PutMetricOptions): Promise { + const trace = this.traces[traceName]; + if (!trace) throw new Error(traceNotFoundError); + trace.putMetric(metricName, num); + return; + } + + public async getMetric({ + traceName, + metricName, + }: PutMetricOptions): Promise { + const trace = this.traces[traceName]; + if (!trace) throw new Error(traceNotFoundError); + return { value: trace.getMetric(metricName) ?? null }; + } + + public async record({ + traceName, + startTime, + duration, + options, + }: RecordOptions): Promise { + const trace = this.traces[traceName]; + if (!trace) throw new Error(traceNotFoundError); + trace.record(startTime, duration, options); + return; + } } From b3131850850fc9416bf8d6fd76625414f4da1312 Mon Sep 17 00:00:00 2001 From: Ehsan Barooni <66495214+ebarooni@users.noreply.github.com> Date: Sun, 8 Dec 2024 00:09:01 +0100 Subject: [PATCH 03/14] feat(performance): add missing methods (#505) Update definitions. --- packages/performance/README.md | 12 ++++++------ packages/performance/src/definitions.ts | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/performance/README.md b/packages/performance/README.md index 2130df29..1e5f9e2b 100644 --- a/packages/performance/README.md +++ b/packages/performance/README.md @@ -359,9 +359,9 @@ Records a trace given its name and options. #### GetAttributesResult -| Prop | Type | Description | Since | -| ----------- | ------------------------------------- | ------------------------------------------------------------ | ----- | -| **`value`** | { [k: string]: string; } | A map of all custom attributes of a trace with their values. | 6.3.0 | +| Prop | Type | Description | Since | +| ------------ | ------------------------------------- | ------------------------------------------------------------ | ----- | +| **`result`** | { [k: string]: string; } | A map of all custom attributes of a trace with their values. | 6.3.0 | #### GetAttributesOptions @@ -382,9 +382,9 @@ Records a trace given its name and options. #### GetMetricResult -| Prop | Type | Description | Since | -| ----------- | --------------------------- | ---------------------------------------------------------- | ----- | -| **`value`** | number \| null | The value of the metric if exists. If not it will be null. | 6.3.0 | +| Prop | Type | Description | Since | +| ----------- | --------------------------- | ---------------------------------- | ----- | +| **`value`** | number \| null | The value of the metric if exists. | 6.3.0 | #### GetMetricOptions diff --git a/packages/performance/src/definitions.ts b/packages/performance/src/definitions.ts index 07d6261c..b043fb2f 100644 --- a/packages/performance/src/definitions.ts +++ b/packages/performance/src/definitions.ts @@ -209,7 +209,7 @@ export interface GetAttributesResult { * * @since 6.3.0 */ - value: { [k: string]: string }; + result: { [k: string]: string }; } export type RemoveAttributeOptions = GetAttributeOptions; @@ -253,7 +253,7 @@ export interface GetMetricOptions { export interface GetMetricResult { /** - * The value of the metric if exists. If not it will be null. + * The value of the metric if exists. * * @since 6.3.0 */ From f5db8be041e2e88f2785aa201663ebfd2c4d988c Mon Sep 17 00:00:00 2001 From: Ehsan Barooni <66495214+ebarooni@users.noreply.github.com> Date: Sun, 8 Dec 2024 00:09:30 +0100 Subject: [PATCH 04/14] feat(performance): add missing methods (#505) Change return of getAttributes on the web. --- packages/performance/src/web.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/performance/src/web.ts b/packages/performance/src/web.ts index 2c92611f..b45dc1ab 100644 --- a/packages/performance/src/web.ts +++ b/packages/performance/src/web.ts @@ -91,7 +91,7 @@ export class FirebasePerformanceWeb }: GetAttributesOptions): Promise { const trace = this.traces[traceName]; if (!trace) throw new Error(traceNotFoundError); - return { value: trace.getAttributes() }; + return { result: trace.getAttributes() }; } public async removeAttribute({ From 284d70becdbb761b28fc2f637c55e48d950b1bb8 Mon Sep 17 00:00:00 2001 From: Ehsan Barooni <66495214+ebarooni@users.noreply.github.com> Date: Sun, 8 Dec 2024 00:17:28 +0100 Subject: [PATCH 05/14] feat(performance): add missing methods (#505) Implement all methods on ios. --- .../ios/Plugin/FirebasePerformance.swift | 45 ++++++ .../ios/Plugin/FirebasePerformancePlugin.m | 7 + .../Plugin/FirebasePerformancePlugin.swift | 142 ++++++++++++++++++ 3 files changed, 194 insertions(+) diff --git a/packages/performance/ios/Plugin/FirebasePerformance.swift b/packages/performance/ios/Plugin/FirebasePerformance.swift index f34ad09b..188ae4bf 100644 --- a/packages/performance/ios/Plugin/FirebasePerformance.swift +++ b/packages/performance/ios/Plugin/FirebasePerformance.swift @@ -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 + } } diff --git a/packages/performance/ios/Plugin/FirebasePerformancePlugin.m b/packages/performance/ios/Plugin/FirebasePerformancePlugin.m index f5b9a4c9..4fdddf4e 100644 --- a/packages/performance/ios/Plugin/FirebasePerformancePlugin.m +++ b/packages/performance/ios/Plugin/FirebasePerformancePlugin.m @@ -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); ) diff --git a/packages/performance/ios/Plugin/FirebasePerformancePlugin.swift b/packages/performance/ios/Plugin/FirebasePerformancePlugin.swift index ff8c335d..66c3d65a 100644 --- a/packages/performance/ios/Plugin/FirebasePerformancePlugin.swift +++ b/packages/performance/ios/Plugin/FirebasePerformancePlugin.swift @@ -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() { @@ -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() + } } From 96856dccb3c9e5f144905b73dbbcb34ce5c22244 Mon Sep 17 00:00:00 2001 From: Ehsan Barooni <66495214+ebarooni@users.noreply.github.com> Date: Sun, 8 Dec 2024 12:25:49 +0100 Subject: [PATCH 06/14] feat(performance): add missing methods (#505) Add since attribute to definitions. --- packages/performance/src/definitions.ts | 33 +++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/packages/performance/src/definitions.ts b/packages/performance/src/definitions.ts index b043fb2f..33744152 100644 --- a/packages/performance/src/definitions.ts +++ b/packages/performance/src/definitions.ts @@ -66,7 +66,6 @@ export interface FirebasePerformancePlugin { * @since 6.3.0 */ getMetric(options: GetMetricOptions): Promise; - /** * Records a trace given its name and options. * @@ -149,6 +148,9 @@ export interface IsEnabledResult { enabled: boolean; } +/** + * @since 6.3.0 + */ export interface PutAttributeOptions { /** * Name of the trace to set its attribute. @@ -170,6 +172,9 @@ export interface PutAttributeOptions { value: string; } +/** + * @since 6.3.0 + */ export interface GetAttributeOptions { /** * Name of the trace to set its attribute. @@ -185,6 +190,9 @@ export interface GetAttributeOptions { attribute: string; } +/** + * @since 6.3.0 + */ export interface GetAttributeResult { /** * The value of the custom attribute. @@ -194,6 +202,9 @@ export interface GetAttributeResult { value: string | null; } +/** + * @since 6.3.0 + */ export interface GetAttributesOptions { /** * Name of the trace to get its attributes. @@ -203,17 +214,26 @@ export interface GetAttributesOptions { traceName: string; } +/** + * @since 6.3.0 + */ export interface GetAttributesResult { /** * A map of all custom attributes of a trace with their values. * * @since 6.3.0 */ - result: { [k: string]: string }; + result: { [key: string]: string }; } +/** + * @since 6.3.0 + */ export type RemoveAttributeOptions = GetAttributeOptions; +/** + * @since 6.3.0 + */ export interface PutMetricOptions { /** * Name of the trace to set its metric. @@ -236,6 +256,9 @@ export interface PutMetricOptions { num: number; } +/** + * @since 6.3.0 + */ export interface GetMetricOptions { /** * Name of the trace to get its metric. @@ -251,6 +274,9 @@ export interface GetMetricOptions { metricName: string; } +/** + * @since 6.3.0 + */ export interface GetMetricResult { /** * The value of the metric if exists. @@ -260,6 +286,9 @@ export interface GetMetricResult { value: number | null; } +/** + * @since 6.3.0 + */ export interface RecordOptions { /** * Name of the trace to record. From 1a10f1f21e1058530bf4fea2a2730d750a698dd9 Mon Sep 17 00:00:00 2001 From: Ehsan Barooni <66495214+ebarooni@users.noreply.github.com> Date: Tue, 10 Dec 2024 00:13:19 +0100 Subject: [PATCH 07/14] feat(performance): add missing methods (#505) Implement new methods on android. --- packages/performance/README.md | 6 +- .../performance/FirebasePerformance.java | 64 ++++++++ .../FirebasePerformancePlugin.java | 144 ++++++++++++++++++ .../classes/options/GetAttributeOptions.java | 38 +++++ .../classes/options/GetMetricOptions.java | 38 +++++ .../classes/options/PutAttributeOptions.java | 48 ++++++ .../classes/options/PutMetricOptions.java | 48 ++++++ .../classes/options/RecordOptions.java | 105 +++++++++++++ .../classes/results/GetAttributeResult.java | 19 +++ .../classes/results/GetAttributesResult.java | 24 +++ .../classes/results/GetMetricResult.java | 19 +++ packages/performance/src/definitions.ts | 2 + 12 files changed, 552 insertions(+), 3 deletions(-) create mode 100644 packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/GetAttributeOptions.java create mode 100644 packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/GetMetricOptions.java create mode 100644 packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/PutAttributeOptions.java create mode 100644 packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/PutMetricOptions.java create mode 100644 packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/RecordOptions.java create mode 100644 packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/results/GetAttributeResult.java create mode 100644 packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/results/GetAttributesResult.java create mode 100644 packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/results/GetMetricResult.java diff --git a/packages/performance/README.md b/packages/performance/README.md index 1e5f9e2b..7a56804b 100644 --- a/packages/performance/README.md +++ b/packages/performance/README.md @@ -359,9 +359,9 @@ Records a trace given its name and options. #### GetAttributesResult -| Prop | Type | Description | Since | -| ------------ | ------------------------------------- | ------------------------------------------------------------ | ----- | -| **`result`** | { [k: string]: string; } | A map of all custom attributes of a trace with their values. | 6.3.0 | +| Prop | Type | Description | Since | +| ------------ | --------------------------------------- | ------------------------------------------------------------ | ----- | +| **`result`** | { [key: string]: string; } | A map of all custom attributes of a trace with their values. | 6.3.0 | #### GetAttributesOptions diff --git a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformance.java b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformance.java index a32e5f41..b50aaa2b 100644 --- a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformance.java +++ b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformance.java @@ -1,11 +1,20 @@ package io.capawesome.capacitorjs.plugins.firebase.performance; +import androidx.annotation.Nullable; import com.google.firebase.perf.metrics.Trace; +import io.capawesome.capacitorjs.plugins.firebase.performance.classes.results.GetAttributeResult; +import io.capawesome.capacitorjs.plugins.firebase.performance.classes.results.GetAttributesResult; +import io.capawesome.capacitorjs.plugins.firebase.performance.classes.results.GetMetricResult; import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; public class FirebasePerformance { private HashMap traces = new HashMap(); + private ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); public void startTrace(String traceName) { Trace trace = this.getFirebasePerformanceInstance().newTrace(traceName); @@ -39,6 +48,61 @@ public Boolean isEnabled() { return this.getFirebasePerformanceInstance().isPerformanceCollectionEnabled(); } + public static void putAttribute(Trace trace, String attribute, String value) { + trace.putAttribute(attribute, value); + } + + public static GetAttributeResult getAttribute(Trace trace, String attribute) { + return new GetAttributeResult(trace.getAttribute(attribute)); + } + + public static GetAttributesResult getAttributes(Trace trace) { + return new GetAttributesResult(trace.getAttributes()); + } + + public static void removeAttribute(Trace trace, String attribute) { + trace.removeAttribute(attribute); + return; + } + + public static void putMetric(Trace trace, String metricName, long num) { + trace.putMetric(metricName, num); + } + + public static GetMetricResult getMetric(Trace trace, String metricName) { + return new GetMetricResult(trace.getLongMetric(metricName)); + } + + public void record( + Trace trace, + String traceName, + long startTime, + long duration, + @Nullable Map attributes, + @Nullable Map metrics + ) { + long currentTime = System.currentTimeMillis(); + long startDelay = Math.max(0, (startTime - currentTime)); + if (attributes != null) { + for (String key : attributes.keySet()) { + FirebasePerformance.putAttribute(trace, key, attributes.get(key)); + } + } + if (metrics != null) { + for (String key : metrics.keySet()) { + FirebasePerformance.putMetric(trace, key, metrics.get(key)); + } + } + this.scheduler.schedule( + () -> { + this.startTrace(traceName); + scheduler.schedule(() -> this.stopTrace(traceName), duration, TimeUnit.MILLISECONDS); + }, + startDelay, + TimeUnit.MILLISECONDS + ); + } + private com.google.firebase.perf.FirebasePerformance getFirebasePerformanceInstance() { return com.google.firebase.perf.FirebasePerformance.getInstance(); } diff --git a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformancePlugin.java b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformancePlugin.java index e51d144e..d40d402d 100644 --- a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformancePlugin.java +++ b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformancePlugin.java @@ -7,6 +7,14 @@ import com.getcapacitor.PluginMethod; import com.getcapacitor.annotation.CapacitorPlugin; import com.google.firebase.perf.metrics.Trace; +import io.capawesome.capacitorjs.plugins.firebase.performance.classes.options.GetAttributeOptions; +import io.capawesome.capacitorjs.plugins.firebase.performance.classes.options.GetMetricOptions; +import io.capawesome.capacitorjs.plugins.firebase.performance.classes.options.PutAttributeOptions; +import io.capawesome.capacitorjs.plugins.firebase.performance.classes.options.PutMetricOptions; +import io.capawesome.capacitorjs.plugins.firebase.performance.classes.options.RecordOptions; +import io.capawesome.capacitorjs.plugins.firebase.performance.classes.results.GetAttributeResult; +import io.capawesome.capacitorjs.plugins.firebase.performance.classes.results.GetAttributesResult; +import io.capawesome.capacitorjs.plugins.firebase.performance.classes.results.GetMetricResult; @CapacitorPlugin(name = "FirebasePerformance") public class FirebasePerformancePlugin extends Plugin { @@ -17,6 +25,12 @@ public class FirebasePerformancePlugin extends Plugin { public static final String ERROR_TRACE_NAME_ALREADY_ASSIGNED = "traceName already assigned."; public static final String ERROR_ENABLED_MISSING = "enabled must be provided."; public static final String ERROR_TRACE_NOT_FOUND = "No trace was found with the provided traceName."; + public static final String ERROR_ATTRIBUTE_MISSING = "attribute must be provided."; + public static final String ERROR_VALUE_MISSING = "value must be provided."; + public static final String ERROR_NUM_MISSING = "num must be provided."; + public static final String ERROR_START_TIME_MISSING = "startTime must be provided."; + public static final String ERROR_DURATION_MISSING = "duration must be provided."; + public static final String ERROR_INVALID_METRIC_VALUE = "provided metric value is not a number."; private FirebasePerformance implementation = new FirebasePerformance(); @PluginMethod @@ -116,4 +130,134 @@ public void isEnabled(PluginCall call) { call.reject(exception.getMessage()); } } + + @PluginMethod(returnType = PluginMethod.RETURN_NONE) + public void putAttribute(PluginCall call) { + try { + PutAttributeOptions options = new PutAttributeOptions(call); + Trace trace = implementation.getTraceByName(options.getTraceName()); + if (trace == null) { + call.reject(ERROR_TRACE_NOT_FOUND); + return; + } + FirebasePerformance.putAttribute(trace, options.getAttribute(), options.getValue()); + call.resolve(); + } catch (Exception exception) { + Logger.error(TAG, exception.getMessage(), exception); + call.reject(exception.getMessage()); + } + } + + @PluginMethod(returnType = PluginMethod.RETURN_PROMISE) + public void getAttribute(PluginCall call) { + try { + GetAttributeOptions options = new GetAttributeOptions(call); + Trace trace = implementation.getTraceByName(options.getTraceName()); + if (trace == null) { + call.reject(ERROR_TRACE_NOT_FOUND); + return; + } + GetAttributeResult result = FirebasePerformance.getAttribute(trace, options.getAttribute()); + call.resolve(result.toJSObject()); + } catch (Exception exception) { + Logger.error(TAG, exception.getMessage(), exception); + call.reject(exception.getMessage()); + } + } + + @PluginMethod(returnType = PluginMethod.RETURN_PROMISE) + public void getAttributes(PluginCall call) { + try { + String traceName = call.getString("traceName"); + if (traceName == null) { + call.reject(ERROR_TRACE_NAME_MISSING); + return; + } + Trace trace = implementation.getTraceByName(traceName); + if (trace == null) { + call.reject(ERROR_TRACE_NOT_FOUND); + return; + } + GetAttributesResult result = FirebasePerformance.getAttributes(trace); + call.resolve(result.toJSObject()); + } catch (Exception exception) { + Logger.error(TAG, exception.getMessage(), exception); + call.reject(exception.getMessage()); + } + } + + @PluginMethod(returnType = PluginMethod.RETURN_NONE) + public void removeAttribute(PluginCall call) { + try { + GetAttributeOptions options = new GetAttributeOptions(call); + Trace trace = implementation.getTraceByName(options.getTraceName()); + if (trace == null) { + call.reject(ERROR_TRACE_NOT_FOUND); + return; + } + FirebasePerformance.removeAttribute(trace, options.getAttribute()); + call.resolve(); + } catch (Exception exception) { + Logger.error(TAG, exception.getMessage(), exception); + call.reject(exception.getMessage()); + } + } + + @PluginMethod(returnType = PluginMethod.RETURN_NONE) + public void putMetric(PluginCall call) { + try { + PutMetricOptions options = new PutMetricOptions(call); + Trace trace = implementation.getTraceByName(options.getTraceName()); + if (trace == null) { + call.reject(ERROR_TRACE_NOT_FOUND); + return; + } + FirebasePerformance.putMetric(trace, options.getMetricName(), options.getNum()); + call.resolve(); + } catch (Exception exception) { + Logger.error(TAG, exception.getMessage(), exception); + call.reject(exception.getMessage()); + } + } + + @PluginMethod(returnType = PluginMethod.RETURN_PROMISE) + public void getMetric(PluginCall call) { + try { + GetMetricOptions options = new GetMetricOptions(call); + Trace trace = implementation.getTraceByName(options.getTraceName()); + if (trace == null) { + call.reject(ERROR_TRACE_NOT_FOUND); + return; + } + GetMetricResult result = FirebasePerformance.getMetric(trace, options.getMetricName()); + call.resolve(result.toJSObject()); + } catch (Exception exception) { + Logger.error(TAG, exception.getMessage(), exception); + call.reject(exception.getMessage()); + } + } + + @PluginMethod(returnType = PluginMethod.RETURN_NONE) + public void record(PluginCall call) { + try { + RecordOptions options = new RecordOptions(call); + Trace trace = implementation.getTraceByName(options.getTraceName()); + if (trace == null) { + call.reject(ERROR_TRACE_NOT_FOUND); + return; + } + implementation.record( + trace, + options.getTraceName(), + options.getStartTime(), + options.getDuration(), + options.getAttributes(), + options.getMetrics() + ); + call.resolve(); + } catch (Exception exception) { + Logger.error(TAG, exception.getMessage(), exception); + call.reject(exception.getMessage()); + } + } } diff --git a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/GetAttributeOptions.java b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/GetAttributeOptions.java new file mode 100644 index 00000000..faf64037 --- /dev/null +++ b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/GetAttributeOptions.java @@ -0,0 +1,38 @@ +package io.capawesome.capacitorjs.plugins.firebase.performance.classes.options; + +import com.getcapacitor.PluginCall; +import io.capawesome.capacitorjs.plugins.firebase.performance.FirebasePerformancePlugin; + +public class GetAttributeOptions { + + private final String traceName; + private final String attribute; + + public static class GetAttributeOptionsException extends Exception { + + public GetAttributeOptionsException(String message) { + super(message); + } + } + + public GetAttributeOptions(PluginCall call) throws GetAttributeOptionsException { + String traceName = call.getString("traceName"); + if (traceName == null) { + throw new GetAttributeOptionsException(FirebasePerformancePlugin.ERROR_TRACE_NAME_MISSING); + } + this.traceName = traceName; + String attribute = call.getString("attribute"); + if (attribute == null) { + throw new GetAttributeOptionsException(FirebasePerformancePlugin.ERROR_ATTRIBUTE_MISSING); + } + this.attribute = attribute; + } + + public String getTraceName() { + return traceName; + } + + public String getAttribute() { + return attribute; + } +} diff --git a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/GetMetricOptions.java b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/GetMetricOptions.java new file mode 100644 index 00000000..48e7f747 --- /dev/null +++ b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/GetMetricOptions.java @@ -0,0 +1,38 @@ +package io.capawesome.capacitorjs.plugins.firebase.performance.classes.options; + +import com.getcapacitor.PluginCall; +import io.capawesome.capacitorjs.plugins.firebase.performance.FirebasePerformancePlugin; + +public class GetMetricOptions { + + private final String traceName; + private final String metricName; + + public static class GetMetricOptionsException extends Exception { + + public GetMetricOptionsException(String message) { + super(message); + } + } + + public GetMetricOptions(PluginCall call) throws GetMetricOptionsException { + String traceName = call.getString("traceName"); + if (traceName == null) { + throw new GetMetricOptionsException(FirebasePerformancePlugin.ERROR_TRACE_NAME_MISSING); + } + this.traceName = traceName; + String metricName = call.getString("metricName"); + if (metricName == null) { + throw new GetMetricOptionsException(FirebasePerformancePlugin.ERROR_METRIC_NAME_MISSING); + } + this.metricName = metricName; + } + + public String getTraceName() { + return traceName; + } + + public String getMetricName() { + return metricName; + } +} diff --git a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/PutAttributeOptions.java b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/PutAttributeOptions.java new file mode 100644 index 00000000..dd9591af --- /dev/null +++ b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/PutAttributeOptions.java @@ -0,0 +1,48 @@ +package io.capawesome.capacitorjs.plugins.firebase.performance.classes.options; + +import com.getcapacitor.PluginCall; +import io.capawesome.capacitorjs.plugins.firebase.performance.FirebasePerformancePlugin; + +public class PutAttributeOptions { + + private final String traceName; + private final String attribute; + private final String value; + + public static class PutAttributeOptionsException extends Exception { + + public PutAttributeOptionsException(String message) { + super(message); + } + } + + public PutAttributeOptions(PluginCall call) throws PutAttributeOptionsException { + String traceName = call.getString("traceName"); + if (traceName == null) { + throw new PutAttributeOptionsException(FirebasePerformancePlugin.ERROR_TRACE_NAME_MISSING); + } + this.traceName = traceName; + String attribute = call.getString("attribute"); + if (attribute == null) { + throw new PutAttributeOptionsException(FirebasePerformancePlugin.ERROR_ATTRIBUTE_MISSING); + } + this.attribute = attribute; + String value = call.getString("value"); + if (value == null) { + throw new PutAttributeOptionsException(FirebasePerformancePlugin.ERROR_VALUE_MISSING); + } + this.value = value; + } + + public String getTraceName() { + return traceName; + } + + public String getAttribute() { + return attribute; + } + + public String getValue() { + return value; + } +} diff --git a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/PutMetricOptions.java b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/PutMetricOptions.java new file mode 100644 index 00000000..2917d6fc --- /dev/null +++ b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/PutMetricOptions.java @@ -0,0 +1,48 @@ +package io.capawesome.capacitorjs.plugins.firebase.performance.classes.options; + +import com.getcapacitor.PluginCall; +import io.capawesome.capacitorjs.plugins.firebase.performance.FirebasePerformancePlugin; + +public class PutMetricOptions { + + private final String traceName; + private final String metricName; + private final long num; + + public static class PutMetricOptionsException extends Exception { + + public PutMetricOptionsException(String message) { + super(message); + } + } + + public PutMetricOptions(PluginCall call) throws PutMetricOptionsException { + String traceName = call.getString("traceName"); + if (traceName == null) { + throw new PutMetricOptionsException(FirebasePerformancePlugin.ERROR_TRACE_NAME_MISSING); + } + this.traceName = traceName; + String metricName = call.getString("metricName"); + if (metricName == null) { + throw new PutMetricOptionsException(FirebasePerformancePlugin.ERROR_METRIC_NAME_MISSING); + } + this.metricName = metricName; + Double num = call.getDouble("num"); + if (num == null) { + throw new PutMetricOptionsException(FirebasePerformancePlugin.ERROR_NUM_MISSING); + } + this.num = (long) Math.floor(num); + } + + public String getTraceName() { + return traceName; + } + + public String getMetricName() { + return metricName; + } + + public long getNum() { + return num; + } +} diff --git a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/RecordOptions.java b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/RecordOptions.java new file mode 100644 index 00000000..2bd6beec --- /dev/null +++ b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/RecordOptions.java @@ -0,0 +1,105 @@ +package io.capawesome.capacitorjs.plugins.firebase.performance.classes.options; + +import androidx.annotation.Nullable; +import com.getcapacitor.JSObject; +import com.getcapacitor.PluginCall; +import io.capawesome.capacitorjs.plugins.firebase.performance.FirebasePerformancePlugin; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import org.json.JSONException; +import org.json.JSONObject; + +public class RecordOptions { + + private final String traceName; + private final long startTime; + private final long duration; + private final Map metrics; + private final Map attributes; + + public static class RecordOptionsException extends Exception { + + public RecordOptionsException(String message) { + super(message); + } + } + + public RecordOptions(PluginCall call) throws RecordOptionsException, JSONException { + String traceName = call.getString("traceName"); + if (traceName == null) { + throw new RecordOptionsException(FirebasePerformancePlugin.ERROR_TRACE_NAME_MISSING); + } + this.traceName = traceName; + Long startTime = call.getLong("startTime"); + if (startTime == null) { + throw new RecordOptionsException(FirebasePerformancePlugin.ERROR_START_TIME_MISSING); + } + this.startTime = startTime; + Long duration = call.getLong("duration"); + if (duration == null) { + throw new RecordOptionsException(FirebasePerformancePlugin.ERROR_DURATION_MISSING); + } + this.duration = duration; + JSObject metrics = call.getObject("metrics", new JSObject()); + if (metrics == null) { + this.metrics = null; + } else { + this.metrics = jsObjectToMetricsMap(metrics); + } + JSObject attributes = call.getObject("attributes", new JSObject()); + if (attributes == null) { + this.attributes = null; + } else { + this.attributes = jsObjectToAttributesMap(attributes); + } + } + + public String getTraceName() { + return traceName; + } + + public long getStartTime() { + return startTime; + } + + public long getDuration() { + return duration; + } + + @Nullable + public Map getAttributes() { + return attributes; + } + + @Nullable + public Map getMetrics() { + return metrics; + } + + private static Map jsObjectToAttributesMap(JSONObject object) throws JSONException { + Map map = new HashMap<>(); + Iterator keys = object.keys(); + while (keys.hasNext()) { + String key = keys.next(); + map.put(key, object.get(key).toString()); + } + return map; + } + + private static Map jsObjectToMetricsMap(JSONObject object) throws JSONException { + Map map = new HashMap<>(); + Iterator keys = object.keys(); + while (keys.hasNext()) { + String key = keys.next(); + if (object.get(key) instanceof Long) { + map.put(key, (Long) object.get(key)); + } else if (object.get(key) instanceof Double) { + map.put(key, (long) Math.floor((double) object.get(key))); + } else { + throw new JSONException(FirebasePerformancePlugin.ERROR_INVALID_METRIC_VALUE); + } + } + return map; + } +} diff --git a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/results/GetAttributeResult.java b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/results/GetAttributeResult.java new file mode 100644 index 00000000..5c4ab558 --- /dev/null +++ b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/results/GetAttributeResult.java @@ -0,0 +1,19 @@ +package io.capawesome.capacitorjs.plugins.firebase.performance.classes.results; + +import androidx.annotation.Nullable; +import com.getcapacitor.JSObject; + +public class GetAttributeResult { + + private final String value; + + public GetAttributeResult(@Nullable String value) { + this.value = value; + } + + public JSObject toJSObject() { + JSObject result = new JSObject(); + result.put("value", this.value); + return result; + } +} diff --git a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/results/GetAttributesResult.java b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/results/GetAttributesResult.java new file mode 100644 index 00000000..5f6cac68 --- /dev/null +++ b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/results/GetAttributesResult.java @@ -0,0 +1,24 @@ +package io.capawesome.capacitorjs.plugins.firebase.performance.classes.results; + +import androidx.annotation.NonNull; +import com.getcapacitor.JSObject; +import java.util.Map; + +public class GetAttributesResult { + + private final Map attributesMap; + + public GetAttributesResult(@NonNull Map value) { + this.attributesMap = value; + } + + public JSObject toJSObject() { + JSObject result = new JSObject(); + JSObject resultMap = new JSObject(); + for (String attribute : this.attributesMap.keySet()) { + resultMap.put(attribute, this.attributesMap.get(attribute)); + } + result.put("result", resultMap); + return result; + } +} diff --git a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/results/GetMetricResult.java b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/results/GetMetricResult.java new file mode 100644 index 00000000..5434c566 --- /dev/null +++ b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/results/GetMetricResult.java @@ -0,0 +1,19 @@ +package io.capawesome.capacitorjs.plugins.firebase.performance.classes.results; + +import androidx.annotation.NonNull; +import com.getcapacitor.JSObject; + +public class GetMetricResult { + + private final long num; + + public GetMetricResult(@NonNull long value) { + this.num = value; + } + + public JSObject toJSObject() { + JSObject result = new JSObject(); + result.put("value", this.num == 0 ? null : this.num); + return result; + } +} diff --git a/packages/performance/src/definitions.ts b/packages/performance/src/definitions.ts index 33744152..27e27b1c 100644 --- a/packages/performance/src/definitions.ts +++ b/packages/performance/src/definitions.ts @@ -162,12 +162,14 @@ export interface PutAttributeOptions { * Name of the attribute to set its value. * * @since 6.3.0 + * @example "experiment" */ attribute: string; /** * The value to set to the attribute. * * @since 6.3.0 + * @example "A" */ value: string; } From 4187829698ad5b34ace230e0268cce63c25472c7 Mon Sep 17 00:00:00 2001 From: Ehsan Barooni <66495214+ebarooni@users.noreply.github.com> Date: Tue, 10 Dec 2024 00:16:46 +0100 Subject: [PATCH 08/14] feat(performance): add missing methods (#505) Add changeset --- .changeset/chilly-seahorses-design.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/chilly-seahorses-design.md diff --git a/.changeset/chilly-seahorses-design.md b/.changeset/chilly-seahorses-design.md new file mode 100644 index 00000000..0a4c0f5f --- /dev/null +++ b/.changeset/chilly-seahorses-design.md @@ -0,0 +1,5 @@ +--- +'@capacitor-firebase/performance': minor +--- + +feat: implement `putAttribute`, `getAttribute`, `getAttributes`, `putMetric`, `getMetric` and `record` From 0bd68241e0ddcb965b64109b96ba15c688453049 Mon Sep 17 00:00:00 2001 From: Ehsan Barooni <66495214+ebarooni@users.noreply.github.com> Date: Tue, 10 Dec 2024 19:19:45 +0100 Subject: [PATCH 09/14] feat(performance): add missing methods (#505) Remove classes and update types. --- .changeset/chilly-seahorses-design.md | 2 +- packages/performance/README.md | 18 +- .../performance/FirebasePerformance.java | 21 +-- .../FirebasePerformancePlugin.java | 173 ++++++++++++++---- .../classes/options/GetAttributeOptions.java | 38 ---- .../classes/options/GetMetricOptions.java | 38 ---- .../classes/options/PutAttributeOptions.java | 48 ----- .../classes/options/PutMetricOptions.java | 48 ----- .../classes/options/RecordOptions.java | 105 ----------- .../classes/results/GetAttributeResult.java | 19 -- .../classes/results/GetAttributesResult.java | 24 --- .../classes/results/GetMetricResult.java | 19 -- .../Plugin/FirebasePerformancePlugin.swift | 2 +- packages/performance/src/definitions.ts | 10 +- packages/performance/src/web.ts | 33 ++-- 15 files changed, 185 insertions(+), 413 deletions(-) delete mode 100644 packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/GetAttributeOptions.java delete mode 100644 packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/GetMetricOptions.java delete mode 100644 packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/PutAttributeOptions.java delete mode 100644 packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/PutMetricOptions.java delete mode 100644 packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/RecordOptions.java delete mode 100644 packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/results/GetAttributeResult.java delete mode 100644 packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/results/GetAttributesResult.java delete mode 100644 packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/results/GetMetricResult.java diff --git a/.changeset/chilly-seahorses-design.md b/.changeset/chilly-seahorses-design.md index 0a4c0f5f..d0dcc3f3 100644 --- a/.changeset/chilly-seahorses-design.md +++ b/.changeset/chilly-seahorses-design.md @@ -2,4 +2,4 @@ '@capacitor-firebase/performance': minor --- -feat: implement `putAttribute`, `getAttribute`, `getAttributes`, `putMetric`, `getMetric` and `record` +feat: add `putAttribute(...)`, `getAttribute(...)`, `getAttributes(...)`, `putMetric(...)`, `getMetric(...)` and `record(...)` methods diff --git a/packages/performance/README.md b/packages/performance/README.md index 7a56804b..5f1c5002 100644 --- a/packages/performance/README.md +++ b/packages/performance/README.md @@ -382,9 +382,9 @@ Records a trace given its name and options. #### GetMetricResult -| Prop | Type | Description | Since | -| ----------- | --------------------------- | ---------------------------------- | ----- | -| **`value`** | number \| null | The value of the metric if exists. | 6.3.0 | +| Prop | Type | Description | Since | +| ----------- | ------------------- | ---------------------------------- | ----- | +| **`value`** | number | The value of the metric if exists. | 6.3.0 | #### GetMetricOptions @@ -397,12 +397,12 @@ Records a trace given its name and options. #### RecordOptions -| Prop | Type | Description | Since | -| --------------- | ----------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------- | ----- | -| **`traceName`** | string | Name of the trace to record. | 6.3.0 | -| **`startTime`** | number | Start time of the trace since epoch in milliseconds. | 6.3.0 | -| **`duration`** | number | The duration of the trace in milliseconds. | 6.3.0 | -| **`options`** | { metrics?: { [key: string]: number; }; attributes?: { [key: string]: string; }; } | An optional object that hold optional maps of custom metrics and attributes. | 6.3.0 | +| Prop | Type | Description | Since | +| --------------- | ----------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------- | ----- | +| **`traceName`** | string | Name of the trace to record. | 6.3.0 | +| **`startTime`** | number | Start time of the trace since epoch in milliseconds. | 6.3.0 | +| **`duration`** | number | The duration of the trace in milliseconds. | 6.3.0 | +| **`options`** | { metrics?: { [key: string]: number; }; attributes?: { [key: string]: string; }; } | An optional object that holds optional maps of custom metrics and attributes. | 6.3.0 | ### Type Aliases diff --git a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformance.java b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformance.java index b50aaa2b..f515c586 100644 --- a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformance.java +++ b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformance.java @@ -1,10 +1,6 @@ package io.capawesome.capacitorjs.plugins.firebase.performance; -import androidx.annotation.Nullable; import com.google.firebase.perf.metrics.Trace; -import io.capawesome.capacitorjs.plugins.firebase.performance.classes.results.GetAttributeResult; -import io.capawesome.capacitorjs.plugins.firebase.performance.classes.results.GetAttributesResult; -import io.capawesome.capacitorjs.plugins.firebase.performance.classes.results.GetMetricResult; import java.util.HashMap; import java.util.Map; import java.util.concurrent.Executors; @@ -52,25 +48,24 @@ public static void putAttribute(Trace trace, String attribute, String value) { trace.putAttribute(attribute, value); } - public static GetAttributeResult getAttribute(Trace trace, String attribute) { - return new GetAttributeResult(trace.getAttribute(attribute)); + public static String getAttribute(Trace trace, String attribute) { + return trace.getAttribute(attribute); } - public static GetAttributesResult getAttributes(Trace trace) { - return new GetAttributesResult(trace.getAttributes()); + public static Map getAttributes(Trace trace) { + return trace.getAttributes(); } public static void removeAttribute(Trace trace, String attribute) { trace.removeAttribute(attribute); - return; } public static void putMetric(Trace trace, String metricName, long num) { trace.putMetric(metricName, num); } - public static GetMetricResult getMetric(Trace trace, String metricName) { - return new GetMetricResult(trace.getLongMetric(metricName)); + public static long getMetric(Trace trace, String metricName) { + return trace.getLongMetric(metricName); } public void record( @@ -78,8 +73,8 @@ public void record( String traceName, long startTime, long duration, - @Nullable Map attributes, - @Nullable Map metrics + Map attributes, + Map metrics ) { long currentTime = System.currentTimeMillis(); long startDelay = Math.max(0, (startTime - currentTime)); diff --git a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformancePlugin.java b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformancePlugin.java index d40d402d..b3c9e8da 100644 --- a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformancePlugin.java +++ b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformancePlugin.java @@ -7,14 +7,11 @@ import com.getcapacitor.PluginMethod; import com.getcapacitor.annotation.CapacitorPlugin; import com.google.firebase.perf.metrics.Trace; -import io.capawesome.capacitorjs.plugins.firebase.performance.classes.options.GetAttributeOptions; -import io.capawesome.capacitorjs.plugins.firebase.performance.classes.options.GetMetricOptions; -import io.capawesome.capacitorjs.plugins.firebase.performance.classes.options.PutAttributeOptions; -import io.capawesome.capacitorjs.plugins.firebase.performance.classes.options.PutMetricOptions; -import io.capawesome.capacitorjs.plugins.firebase.performance.classes.options.RecordOptions; -import io.capawesome.capacitorjs.plugins.firebase.performance.classes.results.GetAttributeResult; -import io.capawesome.capacitorjs.plugins.firebase.performance.classes.results.GetAttributesResult; -import io.capawesome.capacitorjs.plugins.firebase.performance.classes.results.GetMetricResult; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import org.json.JSONException; +import org.json.JSONObject; @CapacitorPlugin(name = "FirebasePerformance") public class FirebasePerformancePlugin extends Plugin { @@ -134,13 +131,27 @@ public void isEnabled(PluginCall call) { @PluginMethod(returnType = PluginMethod.RETURN_NONE) public void putAttribute(PluginCall call) { try { - PutAttributeOptions options = new PutAttributeOptions(call); - Trace trace = implementation.getTraceByName(options.getTraceName()); + String traceName = call.getString("traceName"); + if (traceName == null) { + call.reject(FirebasePerformancePlugin.ERROR_TRACE_NAME_MISSING); + return; + } + String attribute = call.getString("attribute"); + if (attribute == null) { + call.reject(FirebasePerformancePlugin.ERROR_ATTRIBUTE_MISSING); + return; + } + String value = call.getString("value"); + if (value == null) { + call.reject(FirebasePerformancePlugin.ERROR_VALUE_MISSING); + return; + } + Trace trace = implementation.getTraceByName(traceName); if (trace == null) { call.reject(ERROR_TRACE_NOT_FOUND); return; } - FirebasePerformance.putAttribute(trace, options.getAttribute(), options.getValue()); + FirebasePerformance.putAttribute(trace, attribute, value); call.resolve(); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); @@ -151,14 +162,25 @@ public void putAttribute(PluginCall call) { @PluginMethod(returnType = PluginMethod.RETURN_PROMISE) public void getAttribute(PluginCall call) { try { - GetAttributeOptions options = new GetAttributeOptions(call); - Trace trace = implementation.getTraceByName(options.getTraceName()); + String traceName = call.getString("traceName"); + if (traceName == null) { + call.reject(FirebasePerformancePlugin.ERROR_TRACE_NAME_MISSING); + return; + } + String attribute = call.getString("attribute"); + if (attribute == null) { + call.reject(FirebasePerformancePlugin.ERROR_ATTRIBUTE_MISSING); + return; + } + Trace trace = implementation.getTraceByName(traceName); if (trace == null) { call.reject(ERROR_TRACE_NOT_FOUND); return; } - GetAttributeResult result = FirebasePerformance.getAttribute(trace, options.getAttribute()); - call.resolve(result.toJSObject()); + String value = FirebasePerformance.getAttribute(trace, attribute); + JSObject result = new JSObject(); + result.put("value", value); + call.resolve(result); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); call.reject(exception.getMessage()); @@ -178,8 +200,14 @@ public void getAttributes(PluginCall call) { call.reject(ERROR_TRACE_NOT_FOUND); return; } - GetAttributesResult result = FirebasePerformance.getAttributes(trace); - call.resolve(result.toJSObject()); + Map attributesMap = FirebasePerformance.getAttributes(trace); + JSObject result = new JSObject(); + JSObject resultMap = new JSObject(); + for (String attribute : attributesMap.keySet()) { + resultMap.put(attribute, attributesMap.get(attribute)); + } + result.put("result", resultMap); + call.resolve(result); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); call.reject(exception.getMessage()); @@ -189,13 +217,22 @@ public void getAttributes(PluginCall call) { @PluginMethod(returnType = PluginMethod.RETURN_NONE) public void removeAttribute(PluginCall call) { try { - GetAttributeOptions options = new GetAttributeOptions(call); - Trace trace = implementation.getTraceByName(options.getTraceName()); + String traceName = call.getString("traceName"); + if (traceName == null) { + call.reject(FirebasePerformancePlugin.ERROR_TRACE_NAME_MISSING); + return; + } + String attribute = call.getString("attribute"); + if (attribute == null) { + call.reject(FirebasePerformancePlugin.ERROR_ATTRIBUTE_MISSING); + return; + } + Trace trace = implementation.getTraceByName(traceName); if (trace == null) { call.reject(ERROR_TRACE_NOT_FOUND); return; } - FirebasePerformance.removeAttribute(trace, options.getAttribute()); + FirebasePerformance.removeAttribute(trace, attribute); call.resolve(); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); @@ -206,13 +243,27 @@ public void removeAttribute(PluginCall call) { @PluginMethod(returnType = PluginMethod.RETURN_NONE) public void putMetric(PluginCall call) { try { - PutMetricOptions options = new PutMetricOptions(call); - Trace trace = implementation.getTraceByName(options.getTraceName()); + String traceName = call.getString("traceName"); + if (traceName == null) { + call.reject(FirebasePerformancePlugin.ERROR_TRACE_NAME_MISSING); + return; + } + String metricName = call.getString("metricName"); + if (metricName == null) { + call.reject(FirebasePerformancePlugin.ERROR_METRIC_NAME_MISSING); + return; + } + Double num = call.getDouble("num"); + if (num == null) { + call.reject(FirebasePerformancePlugin.ERROR_NUM_MISSING); + return; + } + Trace trace = implementation.getTraceByName(traceName); if (trace == null) { call.reject(ERROR_TRACE_NOT_FOUND); return; } - FirebasePerformance.putMetric(trace, options.getMetricName(), options.getNum()); + FirebasePerformance.putMetric(trace, metricName, (long) Math.floor(num)); call.resolve(); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); @@ -223,14 +274,25 @@ public void putMetric(PluginCall call) { @PluginMethod(returnType = PluginMethod.RETURN_PROMISE) public void getMetric(PluginCall call) { try { - GetMetricOptions options = new GetMetricOptions(call); - Trace trace = implementation.getTraceByName(options.getTraceName()); + String traceName = call.getString("traceName"); + if (traceName == null) { + call.reject(FirebasePerformancePlugin.ERROR_TRACE_NAME_MISSING); + return; + } + String metricName = call.getString("metricName"); + if (metricName == null) { + call.reject(FirebasePerformancePlugin.ERROR_METRIC_NAME_MISSING); + return; + } + Trace trace = implementation.getTraceByName(traceName); if (trace == null) { call.reject(ERROR_TRACE_NOT_FOUND); return; } - GetMetricResult result = FirebasePerformance.getMetric(trace, options.getMetricName()); - call.resolve(result.toJSObject()); + long value = FirebasePerformance.getMetric(trace, metricName); + JSObject result = new JSObject(); + result.put("value", value); + call.resolve(result); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); call.reject(exception.getMessage()); @@ -240,24 +302,61 @@ public void getMetric(PluginCall call) { @PluginMethod(returnType = PluginMethod.RETURN_NONE) public void record(PluginCall call) { try { - RecordOptions options = new RecordOptions(call); - Trace trace = implementation.getTraceByName(options.getTraceName()); + String traceName = call.getString("traceName"); + if (traceName == null) { + call.reject(FirebasePerformancePlugin.ERROR_TRACE_NAME_MISSING); + return; + } + Long startTime = call.getLong("startTime"); + if (startTime == null) { + call.reject(FirebasePerformancePlugin.ERROR_START_TIME_MISSING); + return; + } + Long duration = call.getLong("duration"); + if (duration == null) { + call.reject(FirebasePerformancePlugin.ERROR_DURATION_MISSING); + return; + } + JSObject metrics = call.getObject("metrics", new JSObject()); + Map mappedMetrics = jsObjectToMetricsMap(metrics == null ? new JSObject() : metrics); + JSObject attributes = call.getObject("attributes", new JSObject()); + Map mappedAttributes = jsObjectToAttributesMap(attributes == null ? new JSObject() : attributes); + Trace trace = implementation.getTraceByName(traceName); if (trace == null) { call.reject(ERROR_TRACE_NOT_FOUND); return; } - implementation.record( - trace, - options.getTraceName(), - options.getStartTime(), - options.getDuration(), - options.getAttributes(), - options.getMetrics() - ); + implementation.record(trace, traceName, startTime, duration, mappedAttributes, mappedMetrics); call.resolve(); } catch (Exception exception) { Logger.error(TAG, exception.getMessage(), exception); call.reject(exception.getMessage()); } } + + private static Map jsObjectToAttributesMap(JSONObject object) throws JSONException { + Map map = new HashMap<>(); + Iterator keys = object.keys(); + while (keys.hasNext()) { + String key = keys.next(); + map.put(key, object.get(key).toString()); + } + return map; + } + + private static Map jsObjectToMetricsMap(JSONObject object) throws JSONException { + Map map = new HashMap<>(); + Iterator keys = object.keys(); + while (keys.hasNext()) { + String key = keys.next(); + if (object.get(key) instanceof Long) { + map.put(key, (Long) object.get(key)); + } else if (object.get(key) instanceof Double) { + map.put(key, (long) Math.floor((double) object.get(key))); + } else { + throw new JSONException(FirebasePerformancePlugin.ERROR_INVALID_METRIC_VALUE); + } + } + return map; + } } diff --git a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/GetAttributeOptions.java b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/GetAttributeOptions.java deleted file mode 100644 index faf64037..00000000 --- a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/GetAttributeOptions.java +++ /dev/null @@ -1,38 +0,0 @@ -package io.capawesome.capacitorjs.plugins.firebase.performance.classes.options; - -import com.getcapacitor.PluginCall; -import io.capawesome.capacitorjs.plugins.firebase.performance.FirebasePerformancePlugin; - -public class GetAttributeOptions { - - private final String traceName; - private final String attribute; - - public static class GetAttributeOptionsException extends Exception { - - public GetAttributeOptionsException(String message) { - super(message); - } - } - - public GetAttributeOptions(PluginCall call) throws GetAttributeOptionsException { - String traceName = call.getString("traceName"); - if (traceName == null) { - throw new GetAttributeOptionsException(FirebasePerformancePlugin.ERROR_TRACE_NAME_MISSING); - } - this.traceName = traceName; - String attribute = call.getString("attribute"); - if (attribute == null) { - throw new GetAttributeOptionsException(FirebasePerformancePlugin.ERROR_ATTRIBUTE_MISSING); - } - this.attribute = attribute; - } - - public String getTraceName() { - return traceName; - } - - public String getAttribute() { - return attribute; - } -} diff --git a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/GetMetricOptions.java b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/GetMetricOptions.java deleted file mode 100644 index 48e7f747..00000000 --- a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/GetMetricOptions.java +++ /dev/null @@ -1,38 +0,0 @@ -package io.capawesome.capacitorjs.plugins.firebase.performance.classes.options; - -import com.getcapacitor.PluginCall; -import io.capawesome.capacitorjs.plugins.firebase.performance.FirebasePerformancePlugin; - -public class GetMetricOptions { - - private final String traceName; - private final String metricName; - - public static class GetMetricOptionsException extends Exception { - - public GetMetricOptionsException(String message) { - super(message); - } - } - - public GetMetricOptions(PluginCall call) throws GetMetricOptionsException { - String traceName = call.getString("traceName"); - if (traceName == null) { - throw new GetMetricOptionsException(FirebasePerformancePlugin.ERROR_TRACE_NAME_MISSING); - } - this.traceName = traceName; - String metricName = call.getString("metricName"); - if (metricName == null) { - throw new GetMetricOptionsException(FirebasePerformancePlugin.ERROR_METRIC_NAME_MISSING); - } - this.metricName = metricName; - } - - public String getTraceName() { - return traceName; - } - - public String getMetricName() { - return metricName; - } -} diff --git a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/PutAttributeOptions.java b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/PutAttributeOptions.java deleted file mode 100644 index dd9591af..00000000 --- a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/PutAttributeOptions.java +++ /dev/null @@ -1,48 +0,0 @@ -package io.capawesome.capacitorjs.plugins.firebase.performance.classes.options; - -import com.getcapacitor.PluginCall; -import io.capawesome.capacitorjs.plugins.firebase.performance.FirebasePerformancePlugin; - -public class PutAttributeOptions { - - private final String traceName; - private final String attribute; - private final String value; - - public static class PutAttributeOptionsException extends Exception { - - public PutAttributeOptionsException(String message) { - super(message); - } - } - - public PutAttributeOptions(PluginCall call) throws PutAttributeOptionsException { - String traceName = call.getString("traceName"); - if (traceName == null) { - throw new PutAttributeOptionsException(FirebasePerformancePlugin.ERROR_TRACE_NAME_MISSING); - } - this.traceName = traceName; - String attribute = call.getString("attribute"); - if (attribute == null) { - throw new PutAttributeOptionsException(FirebasePerformancePlugin.ERROR_ATTRIBUTE_MISSING); - } - this.attribute = attribute; - String value = call.getString("value"); - if (value == null) { - throw new PutAttributeOptionsException(FirebasePerformancePlugin.ERROR_VALUE_MISSING); - } - this.value = value; - } - - public String getTraceName() { - return traceName; - } - - public String getAttribute() { - return attribute; - } - - public String getValue() { - return value; - } -} diff --git a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/PutMetricOptions.java b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/PutMetricOptions.java deleted file mode 100644 index 2917d6fc..00000000 --- a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/PutMetricOptions.java +++ /dev/null @@ -1,48 +0,0 @@ -package io.capawesome.capacitorjs.plugins.firebase.performance.classes.options; - -import com.getcapacitor.PluginCall; -import io.capawesome.capacitorjs.plugins.firebase.performance.FirebasePerformancePlugin; - -public class PutMetricOptions { - - private final String traceName; - private final String metricName; - private final long num; - - public static class PutMetricOptionsException extends Exception { - - public PutMetricOptionsException(String message) { - super(message); - } - } - - public PutMetricOptions(PluginCall call) throws PutMetricOptionsException { - String traceName = call.getString("traceName"); - if (traceName == null) { - throw new PutMetricOptionsException(FirebasePerformancePlugin.ERROR_TRACE_NAME_MISSING); - } - this.traceName = traceName; - String metricName = call.getString("metricName"); - if (metricName == null) { - throw new PutMetricOptionsException(FirebasePerformancePlugin.ERROR_METRIC_NAME_MISSING); - } - this.metricName = metricName; - Double num = call.getDouble("num"); - if (num == null) { - throw new PutMetricOptionsException(FirebasePerformancePlugin.ERROR_NUM_MISSING); - } - this.num = (long) Math.floor(num); - } - - public String getTraceName() { - return traceName; - } - - public String getMetricName() { - return metricName; - } - - public long getNum() { - return num; - } -} diff --git a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/RecordOptions.java b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/RecordOptions.java deleted file mode 100644 index 2bd6beec..00000000 --- a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/options/RecordOptions.java +++ /dev/null @@ -1,105 +0,0 @@ -package io.capawesome.capacitorjs.plugins.firebase.performance.classes.options; - -import androidx.annotation.Nullable; -import com.getcapacitor.JSObject; -import com.getcapacitor.PluginCall; -import io.capawesome.capacitorjs.plugins.firebase.performance.FirebasePerformancePlugin; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import org.json.JSONException; -import org.json.JSONObject; - -public class RecordOptions { - - private final String traceName; - private final long startTime; - private final long duration; - private final Map metrics; - private final Map attributes; - - public static class RecordOptionsException extends Exception { - - public RecordOptionsException(String message) { - super(message); - } - } - - public RecordOptions(PluginCall call) throws RecordOptionsException, JSONException { - String traceName = call.getString("traceName"); - if (traceName == null) { - throw new RecordOptionsException(FirebasePerformancePlugin.ERROR_TRACE_NAME_MISSING); - } - this.traceName = traceName; - Long startTime = call.getLong("startTime"); - if (startTime == null) { - throw new RecordOptionsException(FirebasePerformancePlugin.ERROR_START_TIME_MISSING); - } - this.startTime = startTime; - Long duration = call.getLong("duration"); - if (duration == null) { - throw new RecordOptionsException(FirebasePerformancePlugin.ERROR_DURATION_MISSING); - } - this.duration = duration; - JSObject metrics = call.getObject("metrics", new JSObject()); - if (metrics == null) { - this.metrics = null; - } else { - this.metrics = jsObjectToMetricsMap(metrics); - } - JSObject attributes = call.getObject("attributes", new JSObject()); - if (attributes == null) { - this.attributes = null; - } else { - this.attributes = jsObjectToAttributesMap(attributes); - } - } - - public String getTraceName() { - return traceName; - } - - public long getStartTime() { - return startTime; - } - - public long getDuration() { - return duration; - } - - @Nullable - public Map getAttributes() { - return attributes; - } - - @Nullable - public Map getMetrics() { - return metrics; - } - - private static Map jsObjectToAttributesMap(JSONObject object) throws JSONException { - Map map = new HashMap<>(); - Iterator keys = object.keys(); - while (keys.hasNext()) { - String key = keys.next(); - map.put(key, object.get(key).toString()); - } - return map; - } - - private static Map jsObjectToMetricsMap(JSONObject object) throws JSONException { - Map map = new HashMap<>(); - Iterator keys = object.keys(); - while (keys.hasNext()) { - String key = keys.next(); - if (object.get(key) instanceof Long) { - map.put(key, (Long) object.get(key)); - } else if (object.get(key) instanceof Double) { - map.put(key, (long) Math.floor((double) object.get(key))); - } else { - throw new JSONException(FirebasePerformancePlugin.ERROR_INVALID_METRIC_VALUE); - } - } - return map; - } -} diff --git a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/results/GetAttributeResult.java b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/results/GetAttributeResult.java deleted file mode 100644 index 5c4ab558..00000000 --- a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/results/GetAttributeResult.java +++ /dev/null @@ -1,19 +0,0 @@ -package io.capawesome.capacitorjs.plugins.firebase.performance.classes.results; - -import androidx.annotation.Nullable; -import com.getcapacitor.JSObject; - -public class GetAttributeResult { - - private final String value; - - public GetAttributeResult(@Nullable String value) { - this.value = value; - } - - public JSObject toJSObject() { - JSObject result = new JSObject(); - result.put("value", this.value); - return result; - } -} diff --git a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/results/GetAttributesResult.java b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/results/GetAttributesResult.java deleted file mode 100644 index 5f6cac68..00000000 --- a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/results/GetAttributesResult.java +++ /dev/null @@ -1,24 +0,0 @@ -package io.capawesome.capacitorjs.plugins.firebase.performance.classes.results; - -import androidx.annotation.NonNull; -import com.getcapacitor.JSObject; -import java.util.Map; - -public class GetAttributesResult { - - private final Map attributesMap; - - public GetAttributesResult(@NonNull Map value) { - this.attributesMap = value; - } - - public JSObject toJSObject() { - JSObject result = new JSObject(); - JSObject resultMap = new JSObject(); - for (String attribute : this.attributesMap.keySet()) { - resultMap.put(attribute, this.attributesMap.get(attribute)); - } - result.put("result", resultMap); - return result; - } -} diff --git a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/results/GetMetricResult.java b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/results/GetMetricResult.java deleted file mode 100644 index 5434c566..00000000 --- a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/classes/results/GetMetricResult.java +++ /dev/null @@ -1,19 +0,0 @@ -package io.capawesome.capacitorjs.plugins.firebase.performance.classes.results; - -import androidx.annotation.NonNull; -import com.getcapacitor.JSObject; - -public class GetMetricResult { - - private final long num; - - public GetMetricResult(@NonNull long value) { - this.num = value; - } - - public JSObject toJSObject() { - JSObject result = new JSObject(); - result.put("value", this.num == 0 ? null : this.num); - return result; - } -} diff --git a/packages/performance/ios/Plugin/FirebasePerformancePlugin.swift b/packages/performance/ios/Plugin/FirebasePerformancePlugin.swift index 66c3d65a..c8888dec 100644 --- a/packages/performance/ios/Plugin/FirebasePerformancePlugin.swift +++ b/packages/performance/ios/Plugin/FirebasePerformancePlugin.swift @@ -195,7 +195,7 @@ public class FirebasePerformancePlugin: CAPPlugin { return } let value = FirebasePerformance.getMetric(trace!, metricName) - call.resolve(["value": value == 0 ? NSNull() : value]) + call.resolve(["value": value]) } @objc func record(_ call: CAPPluginCall) { diff --git a/packages/performance/src/definitions.ts b/packages/performance/src/definitions.ts index 27e27b1c..2b705a7b 100644 --- a/packages/performance/src/definitions.ts +++ b/packages/performance/src/definitions.ts @@ -285,7 +285,7 @@ export interface GetMetricResult { * * @since 6.3.0 */ - value: number | null; + value: number; } /** @@ -311,12 +311,18 @@ export interface RecordOptions { */ duration: number; /** - * An optional object that hold optional maps of custom metrics and attributes. + * An optional object that holds optional maps of custom metrics and attributes. * * @since 6.3.0 */ options?: { + /** + * An optional map of metrics to be set on the trace. + */ metrics?: { [key: string]: number }; + /** + * An optional map of attributes to be set on the trace. + */ attributes?: { [key: string]: string }; }; } diff --git a/packages/performance/src/web.ts b/packages/performance/src/web.ts index b45dc1ab..8c482f66 100644 --- a/packages/performance/src/web.ts +++ b/packages/performance/src/web.ts @@ -72,7 +72,9 @@ export class FirebasePerformanceWeb value, }: PutAttributeOptions): Promise { const trace = this.traces[traceName]; - if (!trace) return; + if (!trace) { + return; + } trace.putAttribute(attribute, value); return; } @@ -82,7 +84,9 @@ export class FirebasePerformanceWeb attribute, }: GetAttributeOptions): Promise { const trace = this.traces[traceName]; - if (!trace) throw new Error(traceNotFoundError); + if (!trace) { + throw new Error(traceNotFoundError); + } return { value: trace.getAttribute(attribute) ?? null }; } @@ -90,7 +94,9 @@ export class FirebasePerformanceWeb traceName, }: GetAttributesOptions): Promise { const trace = this.traces[traceName]; - if (!trace) throw new Error(traceNotFoundError); + if (!trace) { + throw new Error(traceNotFoundError); + } return { result: trace.getAttributes() }; } @@ -99,9 +105,10 @@ export class FirebasePerformanceWeb attribute, }: RemoveAttributeOptions): Promise { const trace = this.traces[traceName]; - if (!trace) throw new Error(traceNotFoundError); + if (!trace) { + throw new Error(traceNotFoundError); + } trace.removeAttribute(attribute); - return; } public async putMetric({ @@ -110,9 +117,10 @@ export class FirebasePerformanceWeb num, }: PutMetricOptions): Promise { const trace = this.traces[traceName]; - if (!trace) throw new Error(traceNotFoundError); + if (!trace) { + throw new Error(traceNotFoundError); + } trace.putMetric(metricName, num); - return; } public async getMetric({ @@ -120,8 +128,10 @@ export class FirebasePerformanceWeb metricName, }: PutMetricOptions): Promise { const trace = this.traces[traceName]; - if (!trace) throw new Error(traceNotFoundError); - return { value: trace.getMetric(metricName) ?? null }; + if (!trace) { + throw new Error(traceNotFoundError); + } + return { value: trace.getMetric(metricName) }; } public async record({ @@ -131,8 +141,9 @@ export class FirebasePerformanceWeb options, }: RecordOptions): Promise { const trace = this.traces[traceName]; - if (!trace) throw new Error(traceNotFoundError); + if (!trace) { + throw new Error(traceNotFoundError); + } trace.record(startTime, duration, options); - return; } } From 77f4aaf0538e3e166e2de281dc7112ecc344dc0b Mon Sep 17 00:00:00 2001 From: Robin Genz Date: Wed, 11 Dec 2024 20:44:50 +0100 Subject: [PATCH 10/14] wip --- packages/performance/src/web.ts | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/packages/performance/src/web.ts b/packages/performance/src/web.ts index 8c482f66..769b5d74 100644 --- a/packages/performance/src/web.ts +++ b/packages/performance/src/web.ts @@ -1,23 +1,23 @@ import { WebPlugin } from '@capacitor/core'; import type { PerformanceTrace } from 'firebase/performance'; -import { getPerformance, trace as createTrace } from 'firebase/performance'; +import { trace as createTrace, getPerformance } from 'firebase/performance'; import type { FirebasePerformancePlugin, - IncrementMetricOptions, - IsEnabledResult, - SetEnabledOptions, - StartTraceOptions, - StopTraceOptions, - PutAttributeOptions, GetAttributeOptions, GetAttributeResult, GetAttributesOptions, GetAttributesResult, - RemoveAttributeOptions, - PutMetricOptions, GetMetricResult, + IncrementMetricOptions, + IsEnabledResult, + PutAttributeOptions, + PutMetricOptions, RecordOptions, + RemoveAttributeOptions, + SetEnabledOptions, + StartTraceOptions, + StopTraceOptions, } from './definitions'; const traceNotFoundError = 'No trace found for the given name.'; @@ -140,10 +140,8 @@ export class FirebasePerformanceWeb duration, options, }: RecordOptions): Promise { - const trace = this.traces[traceName]; - if (!trace) { - throw new Error(traceNotFoundError); - } + const perf = getPerformance(); + const trace = createTrace(perf, traceName); trace.record(startTime, duration, options); } } From 9aa213daed0edd428c44155b318ff40e70697342 Mon Sep 17 00:00:00 2001 From: Ehsan Barooni <66495214+ebarooni@users.noreply.github.com> Date: Wed, 11 Dec 2024 18:17:18 +0100 Subject: [PATCH 11/14] feat(performance): add missing methods (#505) Refactor helper methods into separate class. --- .../performance/FirebasePerformance.java | 6 ++- .../FirebasePerformanceHelper.java | 41 +++++++++++++++++ .../FirebasePerformancePlugin.java | 46 +++++-------------- .../ios/Plugin/FirebasePerformance.swift | 1 - 4 files changed, 57 insertions(+), 37 deletions(-) create mode 100644 packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformanceHelper.java diff --git a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformance.java b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformance.java index f515c586..305f27a4 100644 --- a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformance.java +++ b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformance.java @@ -1,5 +1,7 @@ package io.capawesome.capacitorjs.plugins.firebase.performance; +import androidx.annotation.Nullable; + import com.google.firebase.perf.metrics.Trace; import java.util.HashMap; import java.util.Map; @@ -73,8 +75,8 @@ public void record( String traceName, long startTime, long duration, - Map attributes, - Map metrics + @Nullable Map attributes, + @Nullable Map metrics ) { long currentTime = System.currentTimeMillis(); long startDelay = Math.max(0, (startTime - currentTime)); diff --git a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformanceHelper.java b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformanceHelper.java new file mode 100644 index 00000000..06af982d --- /dev/null +++ b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformanceHelper.java @@ -0,0 +1,41 @@ +package io.capawesome.capacitorjs.plugins.firebase.performance; + +import androidx.annotation.NonNull; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; + +public class FirebasePerformanceHelper { + + @NonNull + public static Map jsObjectToAttributesMap(JSONObject object) throws JSONException { + Map map = new HashMap<>(); + Iterator keys = object.keys(); + while (keys.hasNext()) { + String key = keys.next(); + map.put(key, object.get(key).toString()); + } + return map; + } + + @NonNull + public static Map jsObjectToMetricsMap(JSONObject object) throws JSONException { + Map map = new HashMap<>(); + Iterator keys = object.keys(); + while (keys.hasNext()) { + String key = keys.next(); + if (object.get(key) instanceof Long) { + map.put(key, (Long) object.get(key)); + } else if (object.get(key) instanceof Double) { + map.put(key, (long) Math.floor((double) object.get(key))); + } else { + throw new JSONException(FirebasePerformancePlugin.ERROR_INVALID_METRIC_VALUE); + } + } + return map; + } +} diff --git a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformancePlugin.java b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformancePlugin.java index b3c9e8da..0fe89145 100644 --- a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformancePlugin.java +++ b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformancePlugin.java @@ -7,11 +7,7 @@ import com.getcapacitor.PluginMethod; import com.getcapacitor.annotation.CapacitorPlugin; import com.google.firebase.perf.metrics.Trace; -import java.util.HashMap; -import java.util.Iterator; import java.util.Map; -import org.json.JSONException; -import org.json.JSONObject; @CapacitorPlugin(name = "FirebasePerformance") public class FirebasePerformancePlugin extends Plugin { @@ -317,10 +313,18 @@ public void record(PluginCall call) { call.reject(FirebasePerformancePlugin.ERROR_DURATION_MISSING); return; } - JSObject metrics = call.getObject("metrics", new JSObject()); - Map mappedMetrics = jsObjectToMetricsMap(metrics == null ? new JSObject() : metrics); - JSObject attributes = call.getObject("attributes", new JSObject()); - Map mappedAttributes = jsObjectToAttributesMap(attributes == null ? new JSObject() : attributes); + + JSObject options = call.getObject("options"); + Map mappedMetrics = null; + Map mappedAttributes = null; + + if (options != null) { + JSObject metrics = options.getJSObject("metrics"); + mappedMetrics = FirebasePerformanceHelper.jsObjectToMetricsMap(metrics == null ? new JSObject() : metrics); + JSObject attributes = options.getJSObject("attributes"); + mappedAttributes = FirebasePerformanceHelper.jsObjectToAttributesMap(attributes == null ? new JSObject() : attributes); + } + Trace trace = implementation.getTraceByName(traceName); if (trace == null) { call.reject(ERROR_TRACE_NOT_FOUND); @@ -333,30 +337,4 @@ public void record(PluginCall call) { call.reject(exception.getMessage()); } } - - private static Map jsObjectToAttributesMap(JSONObject object) throws JSONException { - Map map = new HashMap<>(); - Iterator keys = object.keys(); - while (keys.hasNext()) { - String key = keys.next(); - map.put(key, object.get(key).toString()); - } - return map; - } - - private static Map jsObjectToMetricsMap(JSONObject object) throws JSONException { - Map map = new HashMap<>(); - Iterator keys = object.keys(); - while (keys.hasNext()) { - String key = keys.next(); - if (object.get(key) instanceof Long) { - map.put(key, (Long) object.get(key)); - } else if (object.get(key) instanceof Double) { - map.put(key, (long) Math.floor((double) object.get(key))); - } else { - throw new JSONException(FirebasePerformancePlugin.ERROR_INVALID_METRIC_VALUE); - } - } - return map; - } } diff --git a/packages/performance/ios/Plugin/FirebasePerformance.swift b/packages/performance/ios/Plugin/FirebasePerformance.swift index 188ae4bf..c5484b57 100644 --- a/packages/performance/ios/Plugin/FirebasePerformance.swift +++ b/packages/performance/ios/Plugin/FirebasePerformance.swift @@ -86,6 +86,5 @@ import FirebasePerformance self.stopTrace(traceName) } } - return } } From a83e826e8c674fa3a3a219269aa13611eb05557b Mon Sep 17 00:00:00 2001 From: Ehsan Barooni <66495214+ebarooni@users.noreply.github.com> Date: Wed, 11 Dec 2024 21:03:44 +0100 Subject: [PATCH 12/14] feat(performance): add missing methods (#505) Remove record implementation from android. --- .../performance/FirebasePerformance.java | 35 ---------------- .../FirebasePerformanceHelper.java | 41 ------------------- .../FirebasePerformancePlugin.java | 40 +----------------- 3 files changed, 1 insertion(+), 115 deletions(-) delete mode 100644 packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformanceHelper.java diff --git a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformance.java b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformance.java index 305f27a4..490e3ecc 100644 --- a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformance.java +++ b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformance.java @@ -1,18 +1,13 @@ package io.capawesome.capacitorjs.plugins.firebase.performance; -import androidx.annotation.Nullable; import com.google.firebase.perf.metrics.Trace; import java.util.HashMap; import java.util.Map; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; public class FirebasePerformance { private HashMap traces = new HashMap(); - private ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); public void startTrace(String traceName) { Trace trace = this.getFirebasePerformanceInstance().newTrace(traceName); @@ -70,36 +65,6 @@ public static long getMetric(Trace trace, String metricName) { return trace.getLongMetric(metricName); } - public void record( - Trace trace, - String traceName, - long startTime, - long duration, - @Nullable Map attributes, - @Nullable Map metrics - ) { - long currentTime = System.currentTimeMillis(); - long startDelay = Math.max(0, (startTime - currentTime)); - if (attributes != null) { - for (String key : attributes.keySet()) { - FirebasePerformance.putAttribute(trace, key, attributes.get(key)); - } - } - if (metrics != null) { - for (String key : metrics.keySet()) { - FirebasePerformance.putMetric(trace, key, metrics.get(key)); - } - } - this.scheduler.schedule( - () -> { - this.startTrace(traceName); - scheduler.schedule(() -> this.stopTrace(traceName), duration, TimeUnit.MILLISECONDS); - }, - startDelay, - TimeUnit.MILLISECONDS - ); - } - private com.google.firebase.perf.FirebasePerformance getFirebasePerformanceInstance() { return com.google.firebase.perf.FirebasePerformance.getInstance(); } diff --git a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformanceHelper.java b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformanceHelper.java deleted file mode 100644 index 06af982d..00000000 --- a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformanceHelper.java +++ /dev/null @@ -1,41 +0,0 @@ -package io.capawesome.capacitorjs.plugins.firebase.performance; - -import androidx.annotation.NonNull; - -import org.json.JSONException; -import org.json.JSONObject; - -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; - -public class FirebasePerformanceHelper { - - @NonNull - public static Map jsObjectToAttributesMap(JSONObject object) throws JSONException { - Map map = new HashMap<>(); - Iterator keys = object.keys(); - while (keys.hasNext()) { - String key = keys.next(); - map.put(key, object.get(key).toString()); - } - return map; - } - - @NonNull - public static Map jsObjectToMetricsMap(JSONObject object) throws JSONException { - Map map = new HashMap<>(); - Iterator keys = object.keys(); - while (keys.hasNext()) { - String key = keys.next(); - if (object.get(key) instanceof Long) { - map.put(key, (Long) object.get(key)); - } else if (object.get(key) instanceof Double) { - map.put(key, (long) Math.floor((double) object.get(key))); - } else { - throw new JSONException(FirebasePerformancePlugin.ERROR_INVALID_METRIC_VALUE); - } - } - return map; - } -} diff --git a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformancePlugin.java b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformancePlugin.java index 0fe89145..451fb42a 100644 --- a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformancePlugin.java +++ b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformancePlugin.java @@ -297,44 +297,6 @@ public void getMetric(PluginCall call) { @PluginMethod(returnType = PluginMethod.RETURN_NONE) public void record(PluginCall call) { - try { - String traceName = call.getString("traceName"); - if (traceName == null) { - call.reject(FirebasePerformancePlugin.ERROR_TRACE_NAME_MISSING); - return; - } - Long startTime = call.getLong("startTime"); - if (startTime == null) { - call.reject(FirebasePerformancePlugin.ERROR_START_TIME_MISSING); - return; - } - Long duration = call.getLong("duration"); - if (duration == null) { - call.reject(FirebasePerformancePlugin.ERROR_DURATION_MISSING); - return; - } - - JSObject options = call.getObject("options"); - Map mappedMetrics = null; - Map mappedAttributes = null; - - if (options != null) { - JSObject metrics = options.getJSObject("metrics"); - mappedMetrics = FirebasePerformanceHelper.jsObjectToMetricsMap(metrics == null ? new JSObject() : metrics); - JSObject attributes = options.getJSObject("attributes"); - mappedAttributes = FirebasePerformanceHelper.jsObjectToAttributesMap(attributes == null ? new JSObject() : attributes); - } - - Trace trace = implementation.getTraceByName(traceName); - if (trace == null) { - call.reject(ERROR_TRACE_NOT_FOUND); - return; - } - implementation.record(trace, traceName, startTime, duration, mappedAttributes, mappedMetrics); - call.resolve(); - } catch (Exception exception) { - Logger.error(TAG, exception.getMessage(), exception); - call.reject(exception.getMessage()); - } + call.unimplemented("Not implemented on Android."); } } From b6ccc1f0976c1151d944d033c5dd8e76798da738 Mon Sep 17 00:00:00 2001 From: Ehsan Barooni <66495214+ebarooni@users.noreply.github.com> Date: Wed, 11 Dec 2024 21:09:04 +0100 Subject: [PATCH 13/14] feat(performance): add missing methods (#505) Remove record implementation from ios. --- .../ios/Plugin/FirebasePerformance.swift | 20 ---------------- .../Plugin/FirebasePerformancePlugin.swift | 23 +------------------ 2 files changed, 1 insertion(+), 42 deletions(-) diff --git a/packages/performance/ios/Plugin/FirebasePerformance.swift b/packages/performance/ios/Plugin/FirebasePerformance.swift index c5484b57..6e297289 100644 --- a/packages/performance/ios/Plugin/FirebasePerformance.swift +++ b/packages/performance/ios/Plugin/FirebasePerformance.swift @@ -67,24 +67,4 @@ import FirebasePerformance @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) - } - } - } } diff --git a/packages/performance/ios/Plugin/FirebasePerformancePlugin.swift b/packages/performance/ios/Plugin/FirebasePerformancePlugin.swift index c8888dec..111a9b96 100644 --- a/packages/performance/ios/Plugin/FirebasePerformancePlugin.swift +++ b/packages/performance/ios/Plugin/FirebasePerformancePlugin.swift @@ -199,27 +199,6 @@ public class FirebasePerformancePlugin: CAPPlugin { } @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() + call.unimplemented("Not implemented on iOS.") } } From 04e329800d5713ebb57b4fb2d000713e6e68ae1a Mon Sep 17 00:00:00 2001 From: Ehsan Barooni <66495214+ebarooni@users.noreply.github.com> Date: Wed, 11 Dec 2024 21:09:48 +0100 Subject: [PATCH 14/14] feat(performance): add missing methods (#505) Update method definitions.ts for record. --- packages/performance/README.md | 1 + .../plugins/firebase/performance/FirebasePerformance.java | 1 - packages/performance/src/definitions.ts | 1 + 3 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/performance/README.md b/packages/performance/README.md index 5f1c5002..8a026890 100644 --- a/packages/performance/README.md +++ b/packages/performance/README.md @@ -283,6 +283,7 @@ record(options: RecordOptions) => Promise ``` Records a trace given its name and options. +Only available on web. | Param | Type | | ------------- | ------------------------------------------------------- | diff --git a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformance.java b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformance.java index 490e3ecc..46c19870 100644 --- a/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformance.java +++ b/packages/performance/android/src/main/java/io/capawesome/capacitorjs/plugins/firebase/performance/FirebasePerformance.java @@ -1,6 +1,5 @@ package io.capawesome.capacitorjs.plugins.firebase.performance; - import com.google.firebase.perf.metrics.Trace; import java.util.HashMap; import java.util.Map; diff --git a/packages/performance/src/definitions.ts b/packages/performance/src/definitions.ts index 2b705a7b..8da6ab5e 100644 --- a/packages/performance/src/definitions.ts +++ b/packages/performance/src/definitions.ts @@ -68,6 +68,7 @@ export interface FirebasePerformancePlugin { getMetric(options: GetMetricOptions): Promise; /** * Records a trace given its name and options. + * Only available on web. * * @since 6.3.0 */