Skip to content

Commit

Permalink
fix: app crash on update endpoint fix
Browse files Browse the repository at this point in the history
ALS-1884
  • Loading branch information
shah272728 committed Nov 12, 2024
1 parent 8e3e5ae commit a7a2790
Showing 1 changed file with 87 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,104 +91,104 @@ class AnalyticsUtils(
) {
if (BuildConfig.ANALYTICS_APP_ID == "null") return
CoroutineScope(Dispatchers.IO).launch {
if (!mLocationProvider.isUnAuthCredentialsValid(true)) {
runBlocking { initAnalytics() }
}
val events: List<EventInput> =
if (event == EventTypeEnum.SESSION_STOP.eventType) {
listOf(
EventInput(
eventType = event,
attributes = emptyMap(),
EventSession(
session.id,
session.startTimestamp,
Instant.now().toString(),
try {
if (!mLocationProvider.isUnAuthCredentialsValid(true)) {
runBlocking { initAnalytics() }
}
val events: List<EventInput> =
if (event == EventTypeEnum.SESSION_STOP.eventType) {
listOf(
EventInput(
eventType = event,
attributes = emptyMap(),
EventSession(
session.id,
session.startTimestamp,
Instant.now().toString(),
),
),
),
)
} else {
listOf(EventInput(eventType = event, attributes = emptyMap()))
}
val mUserId: String?
val mAuthStatus =
mPreferenceManager.getValue(
KEY_CLOUD_FORMATION_STATUS,
AuthEnum.DEFAULT.name,
)
} else {
listOf(EventInput(eventType = event, attributes = emptyMap()))
}
val mUserId: String?
val mAuthStatus =
mPreferenceManager.getValue(
KEY_CLOUD_FORMATION_STATUS,
AuthEnum.DEFAULT.name,
)
var connectedStatus = AnalyticsAttribute.USER_AWS_ACCOUNT_CONNECTION_STATUS_NOT_CONNECTED
var authStatus = AnalyticsAttribute.USER_AWS_ACCOUNT_CONNECTION_STATUS_UNAUTHENTICATED
when (mAuthStatus) {
AuthEnum.SIGNED_IN.name -> {
mUserId = mLocationProvider.getIdentityId()
connectedStatus =
AnalyticsAttribute.USER_AWS_ACCOUNT_CONNECTION_STATUS_CONNECTED
authStatus = AnalyticsAttribute.USER_AWS_ACCOUNT_CONNECTION_STATUS_AUTHENTICATED
}
var connectedStatus = AnalyticsAttribute.USER_AWS_ACCOUNT_CONNECTION_STATUS_NOT_CONNECTED
var authStatus = AnalyticsAttribute.USER_AWS_ACCOUNT_CONNECTION_STATUS_UNAUTHENTICATED
when (mAuthStatus) {
AuthEnum.SIGNED_IN.name -> {
mUserId = mLocationProvider.getIdentityId()
connectedStatus =
AnalyticsAttribute.USER_AWS_ACCOUNT_CONNECTION_STATUS_CONNECTED
authStatus = AnalyticsAttribute.USER_AWS_ACCOUNT_CONNECTION_STATUS_AUTHENTICATED
}

AuthEnum.AWS_CONNECTED.name -> {
mUserId = "AnonymousUser:$endpointId"
connectedStatus =
AnalyticsAttribute.USER_AWS_ACCOUNT_CONNECTION_STATUS_CONNECTED
authStatus =
AnalyticsAttribute.USER_AWS_ACCOUNT_CONNECTION_STATUS_UNAUTHENTICATED
}
AuthEnum.AWS_CONNECTED.name -> {
mUserId = "AnonymousUser:$endpointId"
connectedStatus =
AnalyticsAttribute.USER_AWS_ACCOUNT_CONNECTION_STATUS_CONNECTED
authStatus =
AnalyticsAttribute.USER_AWS_ACCOUNT_CONNECTION_STATUS_UNAUTHENTICATED
}

else -> {
mUserId = "AnonymousUser:$endpointId"
else -> {
mUserId = "AnonymousUser:$endpointId"
}
}
}

if (mUserId != null && mUserId != userId) {
userId = mUserId
runBlocking { createOrUpdateEndpoint() }
}
val sessionStopEvent =
events.find { it.eventType == EventTypeEnum.SESSION_STOP.eventType }
if (session.creationStatus == AnalyticsSessionStatus.NOT_CREATED) {
startSession()
}

val finalEvents =
if (sessionStopEvent != null) {
events +
EventInput(
EventTypeEnum.SESSION_END.eventType,
sessionStopEvent.attributes,
)
} else {
events
if (mUserId != null && mUserId != userId) {
userId = mUserId
runBlocking { createOrUpdateEndpoint() }
}
val attributes = mutableMapOf<String, String>()
properties.forEach { (propertyName, propertyValue) ->
attributes[propertyName] = propertyValue
}
attributes[AnalyticsAttribute.USER_AWS_ACCOUNT_CONNECTION_STATUS] = connectedStatus
attributes[AnalyticsAttribute.USER_AUTHENTICATION_STATUS] = authStatus
finalEvents.forEach {
it.attributes = attributes
}
val eventMap: Map<String, Event> =
finalEvents.associate {
UUID.randomUUID().toString() to it.toEvent()
val sessionStopEvent =
events.find { it.eventType == EventTypeEnum.SESSION_STOP.eventType }
if (session.creationStatus == AnalyticsSessionStatus.NOT_CREATED) {
startSession()
}
val batchItem = mutableMapOf<String, EventsBatch>()

batchItem[endpointId] =
EventsBatch {
endpoint = PublicEndpoint {}
this.events = eventMap
val finalEvents =
if (sessionStopEvent != null) {
events +
EventInput(
EventTypeEnum.SESSION_END.eventType,
sessionStopEvent.attributes,
)
} else {
events
}
val attributes = mutableMapOf<String, String>()
properties.forEach { (propertyName, propertyValue) ->
attributes[propertyName] = propertyValue
}

val putEventsRequest =
PutEventsRequest {
applicationId = BuildConfig.ANALYTICS_APP_ID
eventsRequest =
EventsRequest {
this.batchItem = batchItem
}
attributes[AnalyticsAttribute.USER_AWS_ACCOUNT_CONNECTION_STATUS] = connectedStatus
attributes[AnalyticsAttribute.USER_AUTHENTICATION_STATUS] = authStatus
finalEvents.forEach {
it.attributes = attributes
}
try {
val eventMap: Map<String, Event> =
finalEvents.associate {
UUID.randomUUID().toString() to it.toEvent()
}
val batchItem = mutableMapOf<String, EventsBatch>()

batchItem[endpointId] =
EventsBatch {
endpoint = PublicEndpoint {}
this.events = eventMap
}

val putEventsRequest =
PutEventsRequest {
applicationId = BuildConfig.ANALYTICS_APP_ID
eventsRequest =
EventsRequest {
this.batchItem = batchItem
}
}
pinpointClient?.putEvents(putEventsRequest)
if (sessionStopEvent != null){
session = SessionData()
Expand Down

0 comments on commit a7a2790

Please sign in to comment.