public static JSONObject translateFromEvent(AnalyticsEvent source) { if (null == source) { log.warn("The Event provided was null"); return new JSONObject(); } JSONObject json = source.toJSONObject(); if (json.has("class")) { json.remove("class"); } if (json.has("hashCode")) { json.remove("hashCode"); } return json; }
public static AnalyticsEvent createFromEvent( PinpointContext context, String sessionId, long timestamp, AnalyticsEvent copyEvent) { return new AnalyticsEvent( copyEvent.getEventId(), copyEvent.getEventType(), copyEvent.getAllAttributes(), copyEvent.getAllMetrics(), context.getSDKInfo(), sessionId, copyEvent.getSession().getSessionStart(), copyEvent.getSession().getSessionStop(), copyEvent.getSession().getSessionDuration(), timestamp, context.getUniqueId(), context.getSystem().getAppDetails(), context.getSystem().getDeviceDetails()); }
public static AnalyticsEvent translateToEvent(JSONObject source) throws JSONException { Map<String, String> attributes = new HashMap<String, String>(); Map<String, Double> metrics = new HashMap<String, Double>(); AndroidAppDetails appDetails = new AndroidAppDetails( source.optString("app_package_name"), source.optString("app_version_code"), source.optString("app_version_name"), source.optString("app_title"), source.optString(ClientContext.APP_ID_KEY)); SDKInfo sdkInfo = new SDKInfo(source.optString("sdk_version"), source.optString("sdk_name")); AndroidDeviceDetails deviceDetails = new AndroidDeviceDetails(source.optString("carrier")); String eventId = source.getString("event_id"); String eventType = source.getString("event_type"); Long timestamp = source.getLong("timestamp"); String uniqueId = source.getString("unique_id"); String sessionId = ""; Long sessionStart = null; Long sessionStop = null; Long sessionDuration = null; JSONObject sessionJSON = source.getJSONObject("session"); if (sessionJSON != null) { sessionId = sessionJSON.getString("id"); sessionStart = sessionJSON.getLong("startTimestamp"); sessionStop = sessionJSON.optLong("stopTimestamp"); sessionDuration = sessionJSON.optLong("duration"); } JSONObject attributesJSON = source.optJSONObject("attributes"); if (attributesJSON != null) { Iterator<String> keysIterator = attributesJSON.keys(); String key; while (keysIterator.hasNext()) { key = keysIterator.next(); attributes.put(key, attributesJSON.optString(key)); } } JSONObject metricsJSON = source.optJSONObject("metrics"); if (metricsJSON != null) { Iterator<String> keysIterator = metricsJSON.keys(); String key; while (keysIterator.hasNext()) { key = keysIterator.next(); try { metrics.put(key, metricsJSON.getDouble(key)); } catch (JSONException e) { log.error("Failed to convert metric back to double from JSON value", e); } } } return AnalyticsEvent.newInstance( eventId, eventType, attributes, metrics, sdkInfo, sessionId, sessionStart, sessionStop, sessionDuration, timestamp, uniqueId, appDetails, deviceDetails); }