4040import java .util .List ;
4141import java .util .Locale ;
4242import java .util .Objects ;
43+ import java .util .UUID ;
4344
4445import io .sentry .Sentry ;
46+ import io .sentry .SentryLevel ;
4547
4648public class SynchronizeService extends JobIntentService {
4749 private static final int JOB_ID = 6541259 ; //Random unique Job id
4850 private static final String LOG_TAG = "SYNCSERVICE" ;
51+ private static final String TRACE_TAG = "SYNC_TRACE" ;
4952
5053 private static final String ACTION_UPLOAD_CLAIMS = "SynchronizeService.ACTION_UPLOAD_CLAIMS" ;
5154 private static final String ACTION_EXPORT_CLAIMS = "SynchronizeService.ACTION_EXPORT_CLAIMS" ;
@@ -112,13 +115,37 @@ protected void onHandleWork(@NonNull Intent intent) {
112115 }
113116
114117 private void handleUploadClaims () {
118+ String syncSessionId = UUID .randomUUID ().toString ();
119+ long uploadStartTs = System .currentTimeMillis ();
120+ String uploadThread = Thread .currentThread ().getName () + ":" + Thread .currentThread ().getId ();
115121 if (!global .isNetworkAvailable ()) {
122+ String message = String .format (
123+ Locale .US ,
124+ "SYNC_TRACE session=%s event=UPLOAD_ABORT_NO_NETWORK ts=%d thread=%s" ,
125+ syncSessionId , uploadStartTs , uploadThread
126+ );
127+ Log .i (TRACE_TAG , message );
128+ Sentry .captureMessage (message , SentryLevel .INFO );
116129 broadcastError (getResources ().getString (R .string .CheckInternet ), ACTION_UPLOAD_CLAIMS );
117130 return ;
118131 }
119132
120133 JSONArray claimsArray = sqlHandler .getAllPendingClaims ();
134+ String message = String .format (
135+ Locale .US ,
136+ "SYNC_TRACE session=%s event=UPLOAD_START ts=%d thread=%s pendingClaims=%d" ,
137+ syncSessionId , uploadStartTs , uploadThread , claimsArray .length ()
138+ );
139+ Log .i (TRACE_TAG , message );
140+ Sentry .captureMessage (message , SentryLevel .INFO );
121141 if (claimsArray .length () < 1 ) {
142+ message = String .format (
143+ Locale .US ,
144+ "SYNC_TRACE session=%s event=UPLOAD_ABORT_NO_CLAIM ts=%d thread=%s" ,
145+ syncSessionId , System .currentTimeMillis (), uploadThread
146+ );
147+ Log .i (TRACE_TAG , message );
148+ Sentry .captureMessage (message , SentryLevel .INFO );
122149 broadcastError (getResources ().getString (R .string .NoClaim ), ACTION_UPLOAD_CLAIMS );
123150 return ;
124151 }
@@ -165,13 +192,48 @@ private void handleUploadClaims() {
165192 } else {
166193 //upload claim
167194 try {
195+ String claimUUID = sqlHandler .getClaimUUIDForCode (claim .getClaimNumber ());
196+ long createStartTs = System .currentTimeMillis ();
197+ String createMessage = String .format (
198+ Locale .US ,
199+ "SYNC_TRACE session=%s event=CREATECLAIM_EXECUTE_START ts=%d thread=%s claimCode=%s claimUUID=%s insureeId=%d programId=%d" ,
200+ syncSessionId , createStartTs , uploadThread , claim .getClaimNumber (), claimUUID , insureeId , programId
201+ );
202+ Log .i (TRACE_TAG , createMessage );
203+ Sentry .captureMessage (createMessage , SentryLevel .INFO );
204+
168205 Integer status = new CreateClaim ().execute (claim , Integer .parseInt (adminId ),Integer .parseInt (hfId ),insureeId ,programId , diagnosisId , programCode );
206+ String createEndMessage = String .format (
207+ Locale .US ,
208+ "SYNC_TRACE session=%s event=CREATECLAIM_EXECUTE_END ts=%d thread=%s claimCode=%s claimUUID=%s status=%s durationMs=%d" ,
209+ syncSessionId , System .currentTimeMillis (), uploadThread , claim .getClaimNumber (), claimUUID ,
210+ status == STATUS_ERROR ? "ERROR" : "SUCCESS" ,
211+ System .currentTimeMillis () - createStartTs
212+ );
213+ Log .i (TRACE_TAG , createEndMessage );
214+ Sentry .captureMessage (createEndMessage , SentryLevel .INFO );
215+ String createFinalMessage = String .format (
216+ Locale .US ,
217+ "SYNC_TRACE session=%s event=CREATECLAIM_EXECUTE_END ts=%d thread=%s claimCode=%s claimUUID=%s status=%s durationMs=%d" ,
218+ syncSessionId , System .currentTimeMillis (), uploadThread , claim .getClaimNumber (), claimUUID ,
219+ status == STATUS_ERROR ? "ERROR" : "SUCCESS" ,
220+ System .currentTimeMillis () - createStartTs
221+ );
222+ Log .i (TRACE_TAG , createFinalMessage );
223+ Sentry .captureMessage (createFinalMessage , SentryLevel .INFO );
169224 if (status == STATUS_ERROR ){
170225 result = new PostNewClaims .Result (claim .getClaimNumber (), PostNewClaims .Result .Status .ERROR , getResources ().getString (R .string .FailedToCreateClaim ));
171226 }else {
172227 result = new PostNewClaims .Result (claim .getClaimNumber (), PostNewClaims .Result .Status .SUCCESS , null );
173228 }
174229 } catch (HttpException e ){
230+ String errorMessage = String .format (
231+ Locale .US ,
232+ "SYNC_TRACE session=%s event=CREATECLAIM_EXECUTE_ERROR ts=%d thread=%s claimCode=%s errorClass=%s" ,
233+ syncSessionId , System .currentTimeMillis (), uploadThread , claim .getClaimNumber (), e .getClass ().getSimpleName ()
234+ );
235+ Log .i (TRACE_TAG , errorMessage );
236+ Sentry .captureMessage (errorMessage , SentryLevel .ERROR );
175237 result = new PostNewClaims .Result (claim .getClaimNumber (), PostNewClaims .Result .Status .ERROR , getResources ().getString (R .string .SomethingWentWrongServer ));
176238 }
177239 results .add (result );
@@ -184,13 +246,38 @@ private void handleUploadClaims() {
184246 } else {
185247 //upload claim
186248 try {
249+ String claimUUID = sqlHandler .getClaimUUIDForCode (claim .getClaimNumber ());
250+ long createStartTs = System .currentTimeMillis ();
251+ String createMessage = String .format (
252+ Locale .US ,
253+ "SYNC_TRACE session=%s event=CREATECLAIM_EXECUTE_START ts=%d thread=%s claimCode=%s claimUUID=%s insureeId=%d programId=%d" ,
254+ syncSessionId , createStartTs , uploadThread , claim .getClaimNumber (), claimUUID , insureeId , programId
255+ );
256+ Log .i (TRACE_TAG , createMessage );
257+ Sentry .captureMessage (createMessage , SentryLevel .INFO );
187258 Integer status = new CreateClaim ().execute (claim , Integer .parseInt (adminId ),Integer .parseInt (hfId ),insureeId ,programId , diagnosisId , programCode );
259+ String endMessage = String .format (
260+ Locale .US ,
261+ "SYNC_TRACE session=%s event=CREATECLAIM_EXECUTE_END ts=%d thread=%s claimCode=%s claimUUID=%s status=%s durationMs=%d" ,
262+ syncSessionId , System .currentTimeMillis (), uploadThread , claim .getClaimNumber (), claimUUID ,
263+ status == STATUS_ERROR ? "ERROR" : "SUCCESS" ,
264+ System .currentTimeMillis () - createStartTs
265+ );
266+ Log .i (TRACE_TAG , endMessage );
267+ Sentry .captureMessage (endMessage , SentryLevel .INFO );
188268 if (status == STATUS_ERROR ){
189269 result = new PostNewClaims .Result (claim .getClaimNumber (), PostNewClaims .Result .Status .ERROR , getResources ().getString (R .string .FailedToCreateClaim ));
190270 }else {
191271 result = new PostNewClaims .Result (claim .getClaimNumber (), PostNewClaims .Result .Status .SUCCESS , null );
192272 }
193273 } catch (HttpException e ){
274+ String errorMessage = String .format (
275+ Locale .US ,
276+ "SYNC_TRACE session=%s event=CREATECLAIM_EXECUTE_ERROR ts=%d thread=%s claimCode=%s errorClass=%s" ,
277+ syncSessionId , System .currentTimeMillis (), uploadThread , claim .getClaimNumber (), e .getClass ().getSimpleName ()
278+ );
279+ Log .i (TRACE_TAG , errorMessage );
280+ Sentry .captureMessage (errorMessage , SentryLevel .ERROR );
194281 result = new PostNewClaims .Result (claim .getClaimNumber (), PostNewClaims .Result .Status .ERROR , getResources ().getString (R .string .SomethingWentWrongServer ));
195282 }
196283 results .add (result );
@@ -222,10 +309,24 @@ private void handleUploadClaims() {
222309 }
223310 }
224311 JSONArray claimStatus = processClaimResponse (results );
312+ String uploadEndMessage = String .format (
313+ Locale .US ,
314+ "SYNC_TRACE session=%s event=UPLOAD_END ts=%d thread=%s durationMs=%d messagesCount=%d" ,
315+ syncSessionId , System .currentTimeMillis (), uploadThread , System .currentTimeMillis () - uploadStartTs , claimStatus .length ()
316+ );
317+ Log .i (TRACE_TAG , uploadEndMessage );
318+ Sentry .captureMessage (uploadEndMessage , SentryLevel .INFO );
225319 broadcastSyncSuccess (claimStatus );
226320 } catch (Exception e ) {
227321 e .printStackTrace ();
228322 Sentry .captureException (e );
323+ String uploadErrorMessage = String .format (
324+ Locale .US ,
325+ "SYNC_TRACE session=%s event=UPLOAD_ERROR ts=%d thread=%s durationMs=%d errorClass=%s" ,
326+ syncSessionId , System .currentTimeMillis (), uploadThread , System .currentTimeMillis () - uploadStartTs , e .getClass ().getSimpleName ()
327+ );
328+ Log .i (TRACE_TAG , uploadErrorMessage );
329+ Sentry .captureMessage (uploadErrorMessage , SentryLevel .ERROR );
229330 broadcastError (getResources ().getString (R .string .ErrorOccurred ) + ": " + e .getMessage (), ACTION_UPLOAD_CLAIMS );
230331 }
231332 }
@@ -531,4 +632,4 @@ private void broadcastClaimCount(int entered, int accepted, int rejected) {
531632 sendBroadcast (resultIntent );
532633 Log .i (LOG_TAG , String .format (Locale .US , "%s finished with %s, result: p: %d,a: %d,r: %d" , ACTION_CLAIM_COUNT , ACTION_CLAIM_COUNT_RESULT , entered , accepted , rejected ));
533634 }
534- }
635+ }
0 commit comments