Skip to content

Commit

Permalink
feat(performance): add missing methods (#505)
Browse files Browse the repository at this point in the history
Implement new methods on android.
  • Loading branch information
ebarooni committed Dec 9, 2024
1 parent 96856dc commit 1a10f1f
Show file tree
Hide file tree
Showing 12 changed files with 552 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/performance/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,9 @@ Records a trace given its name and options.

#### GetAttributesResult

| Prop | Type | Description | Since |
| ------------ | ------------------------------------- | ------------------------------------------------------------ | ----- |
| **`result`** | <code>{ [k: string]: string; }</code> | A map of all custom attributes of a trace with their values. | 6.3.0 |
| Prop | Type | Description | Since |
| ------------ | --------------------------------------- | ------------------------------------------------------------ | ----- |
| **`result`** | <code>{ [key: string]: string; }</code> | A map of all custom attributes of a trace with their values. | 6.3.0 |


#### GetAttributesOptions
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Trace> traces = new HashMap<String, Trace>();
private ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

public void startTrace(String traceName) {
Trace trace = this.getFirebasePerformanceInstance().newTrace(traceName);
Expand Down Expand Up @@ -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<String, String> attributes,
@Nullable Map<String, Long> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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());
}
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading

0 comments on commit 1a10f1f

Please sign in to comment.