2323import  com .google .firebase .perf .util .Clock ;
2424import  com .google .firebase .perf .util .Timer ;
2525import  com .google .firebase .perf .v1 .SessionVerbosity ;
26- import  com .google .firebase .sessions .api .SessionSubscriber ;
2726import  java .util .List ;
27+ import  java .util .UUID ;
2828import  java .util .concurrent .TimeUnit ;
2929
3030/** Details of a session including a unique Id and related information. */ 
3131public  class  PerfSession  implements  Parcelable  {
32- 
33-   private  final  String  sessionId ;
3432  private  final  Timer  creationTime ;
35-   @ Nullable  private  String  aqsSessionId ;
36- 
33+   private  final  String  sessionId ;
3734  private  boolean  isGaugeAndEventCollectionEnabled  = false ;
35+   public  final  boolean  isAqsReady ;
3836
3937  /* 
4038   * Creates a PerfSession object and decides what metrics to collect. 
4139   */ 
42-   public  static  PerfSession  createWithId (@ NonNull  String  sessionId ) {
43-     String  prunedSessionId  = sessionId .replace ("-" , "" );
44-     PerfSession  session  = new  PerfSession (prunedSessionId , new  Clock ());
45-     session .setGaugeAndEventCollectionEnabled (shouldCollectGaugesAndEvents ());
46- 
40+   public  static  PerfSession  createWithId (@ Nullable  String  aqsSessionId ) {
41+     String  sessionId  = UUID .randomUUID ().toString ().replace ("-" , "" );
42+     Boolean  isAqsReady  = false ;
43+     if  (aqsSessionId  != null ) {
44+       sessionId  = aqsSessionId ;
45+       isAqsReady  = true ;
46+     }
47+     PerfSession  session  = new  PerfSession (sessionId , new  Clock (), isAqsReady );
48+     session .setGaugeAndEventCollectionEnabled (shouldCollectGaugesAndEvents (sessionId ));
4749    return  session ;
4850  }
4951
5052  /** Creates a PerfSession with the provided {@code sessionId} and {@code clock}. */ 
5153  @ VisibleForTesting (otherwise  = VisibleForTesting .PACKAGE_PRIVATE )
52-   public  PerfSession (String  sessionId , Clock  clock ) {
54+   public  PerfSession (String  sessionId , Clock  clock ,  boolean   isAqsReady ) {
5355    this .sessionId  = sessionId ;
56+     this .isAqsReady  = isAqsReady ;
5457    creationTime  = clock .getTime ();
5558  }
5659
5760  private  PerfSession (@ NonNull  Parcel  in ) {
5861    super ();
5962    sessionId  = in .readString ();
6063    isGaugeAndEventCollectionEnabled  = in .readByte () != 0 ;
64+     isAqsReady  = in .readByte () != 0 ;
6165    creationTime  = in .readParcelable (Timer .class .getClassLoader ());
6266  }
6367
64-   /** Returns the sessionId of  the session. */ 
68+   /** Returns the sessionId for  the given  session. */ 
6569  public  String  sessionId () {
6670    return  sessionId ;
6771  }
6872
69-   /** Returns the AQS sessionId for the given session. */ 
70-   @ Nullable 
71-   public  String  aqsSessionId () {
72-     return  aqsSessionId ;
73-   }
74- 
75-   /** Sets the AQS sessionId for the given session. */ 
76-   public  void  setAQSId (SessionSubscriber .SessionDetails  aqs ) {
77-     if  (aqsSessionId  == null ) {
78-       aqsSessionId  = aqs .getSessionId ();
79-     }
80-   }
81- 
8273  /** 
8374   * Returns a timer object that has been seeded with the system time at which the session began. 
8475   */ 
@@ -105,18 +96,6 @@ public boolean isVerbose() {
10596    return  isGaugeAndEventCollectionEnabled ;
10697  }
10798
108-   /** Checks if the current {@link com.google.firebase.perf.v1.PerfSession} is verbose or not. */ 
109-   @ VisibleForTesting 
110-   static  boolean  isVerbose (@ NonNull  com .google .firebase .perf .v1 .PerfSession  perfSession ) {
111-     for  (SessionVerbosity  sessionVerbosity  : perfSession .getSessionVerbosityList ()) {
112-       if  (sessionVerbosity  == SessionVerbosity .GAUGES_AND_SYSTEM_EVENTS ) {
113-         return  true ;
114-       }
115-     }
116- 
117-     return  false ;
118-   }
119- 
12099  /** 
121100   * Checks if it has been more than {@link ConfigResolver#getSessionsMaxDurationMinutes()} time 
122101   * since the creation time of the current session. 
@@ -128,7 +107,6 @@ public boolean isSessionRunningTooLong() {
128107
129108  /** Creates and returns the proto object for PerfSession object. */ 
130109  public  com .google .firebase .perf .v1 .PerfSession  build () {
131-     // TODO(b/394127311): Switch to using AQS. 
132110    com .google .firebase .perf .v1 .PerfSession .Builder  sessionMetric  =
133111        com .google .firebase .perf .v1 .PerfSession .newBuilder ().setSessionId (sessionId );
134112
@@ -179,11 +157,10 @@ public static com.google.firebase.perf.v1.PerfSession[] buildAndSort(
179157  }
180158
181159  /** If true, Session Gauge collection is enabled. */ 
182-   public  static  boolean  shouldCollectGaugesAndEvents () {
160+   public  static  boolean  shouldCollectGaugesAndEvents (String   sessionId ) {
183161    ConfigResolver  configResolver  = ConfigResolver .getInstance ();
184- 
185162    return  configResolver .isPerformanceMonitoringEnabled ()
186-         && Math .random ()  < configResolver .getSessionsSamplingRate ();
163+         && ( Math .abs ( sessionId . hashCode () %  100 )  < configResolver .getSessionsSamplingRate () *  100 );
187164  }
188165
189166  /** 
@@ -207,6 +184,7 @@ public int describeContents() {
207184  public  void  writeToParcel (@ NonNull  Parcel  out , int  flags ) {
208185    out .writeString (sessionId );
209186    out .writeByte ((byte ) (isGaugeAndEventCollectionEnabled  ? 1  : 0 ));
187+     out .writeByte ((byte ) (isAqsReady  ? 1  : 0 ));
210188    out .writeParcelable (creationTime , 0 );
211189  }
212190
@@ -224,4 +202,16 @@ public PerfSession[] newArray(int size) {
224202          return  new  PerfSession [size ];
225203        }
226204      };
205+ 
206+   /** Checks if the current {@link com.google.firebase.perf.v1.PerfSession} is verbose or not. */ 
207+   @ VisibleForTesting 
208+   static  boolean  isVerbose (@ NonNull  com .google .firebase .perf .v1 .PerfSession  perfSession ) {
209+     for  (SessionVerbosity  sessionVerbosity  : perfSession .getSessionVerbosityList ()) {
210+       if  (sessionVerbosity  == SessionVerbosity .GAUGES_AND_SYSTEM_EVENTS ) {
211+         return  true ;
212+       }
213+     }
214+ 
215+     return  false ;
216+   }
227217}
0 commit comments