summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/module/telemetry/PerformanceEvent.java
blob: 12d1fe46cffe3295e92bcf0ee7a7ce67d23f948c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package com.mapbox.mapboxsdk.module.telemetry;

import com.google.gson.Gson;

import com.google.gson.JsonObject;

import com.google.gson.reflect.TypeToken;
import com.mapbox.android.telemetry.Event;

import android.os.Bundle;
import android.os.Parcel;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * Generic Performance Event that can be used for performance measurements.
 * Customer measurements can be added to the bundle.
 *
 * Bundle is expected to have following properties:
 * "attributes", "counters", and "metadata" with String values.
 *
 * Attributes: a string representing an array of name/string value pair objects.
 * Counters: a string representing an array of name/number value pair objects.
 * Metadata is a string representation of a JsonObject with string values.
 *
 *  Here is an example of a Performance event bundle data:
 *
 * "attributes": [{ "name": "style_id", "value": "mapbox://styles/mapbox/streets-v10"}]
 *
 * "counters": [{"name": "fps_average", "value": 90.7655486547093},
 *              {"name": "fps_deviation", "value": 29.301809631465574}]
 * “metadata”: {
 *       “version”: “9”,
 *       “screenSize”: “1080x1794”,
 *       “country”: “US”,
 *       “device”: “Pixel 2”,
 *       “abi”: “arm64-v8a”,
 *       “brand”: “google”,
 *       “ram”: “3834167296”,
 *       “os”: “android”,
 *       “gpu”: “Qualcomm, Adreno (TM) 540, OpenGL ES 3.2 V@313.0 (GIT@7bf2852, Ie32bfa6f6f)“,
 *       “manufacturer”: “Google”
 *   }
 */
public class PerformanceEvent extends Event {

  private static final String PERFORMANCE_TRACE = "mobile.performance_trace";

  private final String event;

  private final String created;

  private final String sessionId;

  private final List<Attribute<String>> attributes;

  private final List<Attribute<Double>> counters;

  private final JsonObject metadata;


  private static final SimpleDateFormat DATE_FORMAT =
          new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);

  PerformanceEvent(String sessionId, Bundle bundle) {

    this.event = PERFORMANCE_TRACE;
    this.created = DATE_FORMAT.format(new Date());
    this.sessionId = sessionId;
    this.attributes = initList(bundle.getString("attributes"),
      new TypeToken<ArrayList<Attribute<String>>>() {});
    this.counters = initList(bundle.getString("counters"),
      new TypeToken<ArrayList<Attribute<Double>>>() {});
    this.metadata = initMetaData(bundle.getString("metadata"));
  }

  private PerformanceEvent(Parcel in) {
    this.event = in.readString();
    this.created = in.readString();
    this.sessionId = in.readString();

    this.attributes = initList(in.readString(), new TypeToken<ArrayList<Attribute<String>>>() {});
    this.counters = initList(in.readString(), new TypeToken<ArrayList<Attribute<Double>>>() {});
    this.metadata = initMetaData(in.readString());
  }

  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel parcel, int i) {
    parcel.writeString(event);
    parcel.writeString(created);
    parcel.writeString(sessionId);

    Gson gson = new Gson();

    parcel.writeString(gson.toJson(attributes));
    parcel.writeString(gson.toJson(counters));

    if (metadata != null) {
      parcel.writeString(metadata.toString());
    }
  }

  private <T> ArrayList<Attribute<T>> initList(String fromString, TypeToken typeToken) {
    if (fromString == null || fromString.isEmpty()) {
      return new ArrayList<>();
    }
    return new Gson().fromJson(fromString, typeToken.getType());
  }

  private JsonObject initMetaData(String fromString) {
    if (fromString == null) {
      return new JsonObject();
    } else {
      return new Gson().fromJson(fromString, JsonObject.class);
    }
  }

  public static final Creator<PerformanceEvent> CREATOR = new Creator<PerformanceEvent>() {
    @Override
    public PerformanceEvent createFromParcel(Parcel in) {
      return new PerformanceEvent(in);
    }

    @Override
    public PerformanceEvent[] newArray(int size) {
      return new PerformanceEvent[size];
    }
  };


  private class Attribute<T> {
    private final String name;
    private final T value;

    Attribute(String name, T value) {
      this.name = name;
      this.value = value;
    }
  }
}