Skip to content

Commit

Permalink
WTA #71: Updated GetProjectStatsUC to get data in parallel.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob3075 committed Dec 27, 2022
1 parent fd76109 commit a7c3899
Showing 1 changed file with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,21 @@ import com.jacob.wakatimeapp.details.domain.models.DetailedProjectStatsUiData
import com.jacob.wakatimeapp.details.domain.models.ProjectStats
import javax.inject.Inject
import javax.inject.Singleton
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.datetime.DatePeriod
import kotlinx.datetime.plus

@Singleton
internal class GetProjectStatsUC @Inject constructor(
dispatcher: CoroutineContext = Dispatchers.IO,
private val projectStatsNetworkData: ProjectDetailsNetworkData,
private val instantProvider: InstantProvider,
) {
private val ioScope = CoroutineScope(dispatcher)

suspend operator fun invoke(projectName: String) = either {
val totalProjectTime = projectStatsNetworkData.getTotalTimeForProject(projectName).bind()
Expand All @@ -28,14 +35,17 @@ internal class GetProjectStatsUC @Inject constructor(
.takeWhile { it < now }
.plusElement(now)
.zipWithNext()
.toList()
.map { (start, end) ->
projectStatsNetworkData.getStatsForProject(
projectName = projectName,
startDate = start.toString(),
endDate = end.toString(),
)
ioScope.async {
projectStatsNetworkData.getStatsForProject(
projectName = projectName,
startDate = start.toString(),
endDate = end.toString(),
)
}
}
.toList()
.awaitAll()
.map { it.bind() }
.fold(ProjectStats.ZERO, ProjectStats::plus)

Expand Down

0 comments on commit a7c3899

Please sign in to comment.