Skip to content

Commit

Permalink
feat(performance): add missing methods (#505)
Browse files Browse the repository at this point in the history
Refactor helper methods into separate class.
  • Loading branch information
ebarooni committed Dec 11, 2024
1 parent 77f4aaf commit 9aa213d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -73,8 +75,8 @@ public void record(
String traceName,
long startTime,
long duration,
Map<String, String> attributes,
Map<String, Long> metrics
@Nullable Map<String, String> attributes,
@Nullable Map<String, Long> metrics
) {
long currentTime = System.currentTimeMillis();
long startDelay = Math.max(0, (startTime - currentTime));
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> jsObjectToAttributesMap(JSONObject object) throws JSONException {
Map<String, String> map = new HashMap<>();
Iterator<String> keys = object.keys();
while (keys.hasNext()) {
String key = keys.next();
map.put(key, object.get(key).toString());
}
return map;
}

@NonNull
public static Map<String, Long> jsObjectToMetricsMap(JSONObject object) throws JSONException {
Map<String, Long> map = new HashMap<>();
Iterator<String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -317,10 +313,18 @@ public void record(PluginCall call) {
call.reject(FirebasePerformancePlugin.ERROR_DURATION_MISSING);
return;
}
JSObject metrics = call.getObject("metrics", new JSObject());
Map<String, Long> mappedMetrics = jsObjectToMetricsMap(metrics == null ? new JSObject() : metrics);
JSObject attributes = call.getObject("attributes", new JSObject());
Map<String, String> mappedAttributes = jsObjectToAttributesMap(attributes == null ? new JSObject() : attributes);

JSObject options = call.getObject("options");
Map<String, Long> mappedMetrics = null;
Map<String, String> 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);
Expand All @@ -333,30 +337,4 @@ public void record(PluginCall call) {
call.reject(exception.getMessage());
}
}

private static Map<String, String> jsObjectToAttributesMap(JSONObject object) throws JSONException {
Map<String, String> map = new HashMap<>();
Iterator<String> keys = object.keys();
while (keys.hasNext()) {
String key = keys.next();
map.put(key, object.get(key).toString());
}
return map;
}

private static Map<String, Long> jsObjectToMetricsMap(JSONObject object) throws JSONException {
Map<String, Long> map = new HashMap<>();
Iterator<String> 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;
}
}
1 change: 0 additions & 1 deletion packages/performance/ios/Plugin/FirebasePerformance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,5 @@ import FirebasePerformance
self.stopTrace(traceName)
}
}
return
}
}

0 comments on commit 9aa213d

Please sign in to comment.