-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: New user properties for Analytics [WPB-16121] #3312
Open
borichellow
wants to merge
4
commits into
develop
Choose a base branch
from
feat/new_user_properties_for_analytics
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
7afeb41
feat: New user properties for Analytics
borichellow fa3ef82
Merge develop into feat/new_user_properties_for_analytics
borichellow 389894b
Added doc to the UseCases
borichellow 96cfab5
Merge branch 'develop' into feat/new_user_properties_for_analytics
borichellow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...monMain/kotlin/com/wire/kalium/logic/feature/analytics/GetAnalyticsContactsDataUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2025 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.kalium.logic.feature.analytics | ||
|
||
import com.wire.kalium.common.functional.flatMapLeft | ||
import com.wire.kalium.common.functional.getOrElse | ||
import com.wire.kalium.common.functional.getOrNull | ||
import com.wire.kalium.logic.configuration.UserConfigRepository | ||
import com.wire.kalium.logic.data.id.SelfTeamIdProvider | ||
import com.wire.kalium.logic.data.id.TeamId | ||
import com.wire.kalium.logic.data.sync.SlowSyncRepository | ||
import com.wire.kalium.logic.data.sync.SlowSyncStatus | ||
import com.wire.kalium.logic.data.user.UserRepository | ||
import kotlinx.coroutines.flow.first | ||
|
||
/** | ||
* Use case that combine contacts data necessary for analytics [AnalyticsContactsData]. | ||
* It always get a Cached data and, except case when there is no cache, in that case useCase selects all the data from DB. | ||
*/ | ||
interface GetAnalyticsContactsDataUseCase { | ||
suspend operator fun invoke(): AnalyticsContactsData | ||
} | ||
|
||
class GetAnalyticsContactsDataUseCaseImpl internal constructor( | ||
private val selfTeamIdProvider: SelfTeamIdProvider, | ||
private val slowSyncRepository: SlowSyncRepository, | ||
private val userRepository: UserRepository, | ||
private val userConfigRepository: UserConfigRepository, | ||
) : GetAnalyticsContactsDataUseCase { | ||
|
||
override suspend fun invoke(): AnalyticsContactsData { | ||
slowSyncRepository.slowSyncStatus.first { it is SlowSyncStatus.Complete } | ||
|
||
val teamId = selfTeamIdProvider().getOrNull() | ||
return getAnalyticsContactsData(teamId) | ||
} | ||
|
||
private suspend fun getAnalyticsContactsData(teamId: TeamId?): AnalyticsContactsData = | ||
if (teamId == null) { | ||
val contactsSize = userRepository.getContactsAmountCached() | ||
.flatMapLeft { userRepository.countContactsAmount() } | ||
.getOrNull() | ||
|
||
AnalyticsContactsData( | ||
teamId = null, | ||
teamSize = null, | ||
isEnterprise = null, | ||
contactsSize = contactsSize, | ||
isTeamMember = false | ||
) | ||
} else { | ||
val teamSize = userRepository.getTeamMembersAmountCached() | ||
.flatMapLeft { userRepository.countTeamMembersAmount() } | ||
.getOrNull() ?: 0 | ||
val isEnterprise = userConfigRepository.isConferenceCallingEnabled().getOrElse { false } | ||
|
||
if (teamSize > SMALL_TEAM_MAX) { | ||
AnalyticsContactsData( | ||
teamId = teamId.value, | ||
teamSize = teamSize, | ||
contactsSize = null, | ||
isEnterprise = isEnterprise, | ||
isTeamMember = true | ||
) | ||
} else { | ||
// Smaller teams are not tracked due to legal precautions and the potential for user identification. | ||
AnalyticsContactsData( | ||
teamId = null, | ||
teamSize = null, | ||
contactsSize = null, | ||
isEnterprise = isEnterprise, | ||
isTeamMember = true | ||
) | ||
} | ||
} | ||
|
||
companion object { | ||
private const val SMALL_TEAM_MAX = 5 | ||
} | ||
|
||
} | ||
|
||
/** | ||
* If val is null mean it shouldn't be provided to the analytics. | ||
* More details in task https://wearezeta.atlassian.net/browse/WPB-16121 | ||
*/ | ||
data class AnalyticsContactsData( | ||
val teamId: String?, | ||
val contactsSize: Int?, | ||
val teamSize: Int?, | ||
val isEnterprise: Boolean?, | ||
val isTeamMember: Boolean | ||
) |
67 changes: 67 additions & 0 deletions
67
...nMain/kotlin/com/wire/kalium/logic/feature/analytics/UpdateContactsAmountsCacheUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2025 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.kalium.logic.feature.analytics | ||
|
||
import com.wire.kalium.common.functional.getOrNull | ||
import com.wire.kalium.logic.data.id.SelfTeamIdProvider | ||
import com.wire.kalium.logic.data.sync.SlowSyncRepository | ||
import com.wire.kalium.logic.data.sync.SlowSyncStatus | ||
import com.wire.kalium.logic.data.user.UserRepository | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.datetime.Clock | ||
import kotlin.time.Duration.Companion.days | ||
|
||
/** | ||
* Use case that checks if users ContactsAmount and TeamSize cache are too old and updates it. | ||
* Currently max live period is [UpdateContactsAmountsCacheUseCaseImpl.CACHE_PERIOD] 7 days | ||
*/ | ||
interface UpdateContactsAmountsCacheUseCase { | ||
suspend operator fun invoke() | ||
} | ||
|
||
class UpdateContactsAmountsCacheUseCaseImpl internal constructor( | ||
private val selfTeamIdProvider: SelfTeamIdProvider, | ||
private val slowSyncRepository: SlowSyncRepository, | ||
private val userRepository: UserRepository, | ||
) : UpdateContactsAmountsCacheUseCase { | ||
|
||
override suspend fun invoke() { | ||
slowSyncRepository.slowSyncStatus.first { it is SlowSyncStatus.Complete } | ||
|
||
val nowDate = Clock.System.now() | ||
val updateTime = userRepository.getLastContactsDateUpdateDate().getOrNull() | ||
|
||
if (updateTime != null && nowDate.minus(updateTime) < CACHE_PERIOD) return | ||
|
||
val teamId = selfTeamIdProvider().getOrNull() | ||
|
||
with(userRepository) { | ||
val contactsAmount = countContactsAmount().getOrNull() ?: 0 | ||
val teamAmount = teamId?.let { countTeamMembersAmount().getOrNull() } ?: 0 | ||
|
||
setContactsAmountCached(contactsAmount) | ||
setTeamMembersAmountCached(teamAmount) | ||
setContactsAmountCachingDate(nowDate) | ||
} | ||
} | ||
|
||
companion object { | ||
private val CACHE_PERIOD = 7.days | ||
} | ||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can those be moved out of the UserRepository to their own, maybe a AnalyticsRepo ?