-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(analyzer): flow based implementation in kotlin
- Loading branch information
Showing
5 changed files
with
108 additions
and
56 deletions.
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
51 changes: 51 additions & 0 deletions
51
...er-direct-kt/src/main/kotlin/io/github/tassiLuca/analyzer/lib/GitHubAnalyzerByChannels.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,51 @@ | ||
package io.github.tassiLuca.analyzer.lib | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.coroutineScope | ||
import kotlinx.coroutines.launch | ||
|
||
internal class GitHubAnalyzerByChannels(private val provider: GitHubRepositoryProvider) : Analyzer { | ||
|
||
override suspend fun analyze( | ||
organizationName: String, | ||
updateResults: suspend (RepositoryReport) -> Unit, | ||
): Result<Set<RepositoryReport>> = coroutineScope { | ||
runCatching { | ||
val repositories = provider.repositoriesOf(organizationName).getOrThrow() | ||
val resultsChannel = analyzeAll(organizationName, repositories) | ||
collectResults(resultsChannel, repositories.size, updateResults) | ||
} | ||
} | ||
|
||
private fun CoroutineScope.analyzeAll( | ||
organizationName: String, | ||
repositories: List<Repository>, | ||
): Channel<RepositoryReport> { | ||
val channel = Channel<RepositoryReport>() | ||
repositories.map { | ||
launch { | ||
val contributors = async { provider.contributorsOf(organizationName, it.name).getOrThrow() } | ||
val release = async { provider.lastReleaseOf(organizationName, it.name).getOrThrow() } | ||
channel.send(RepositoryReport(it.name, it.issues, it.stars, contributors.await(), release.await())) | ||
} | ||
} | ||
return channel | ||
} | ||
|
||
private suspend fun collectResults( | ||
resultsChannel: Channel<RepositoryReport>, | ||
expectedResults: Int, | ||
updateResults: suspend (RepositoryReport) -> Unit, | ||
): Set<RepositoryReport> { | ||
var allReports = emptySet<RepositoryReport>() | ||
repeat(expectedResults) { | ||
val report = resultsChannel.receive() | ||
allReports = allReports + report | ||
updateResults(report) | ||
} | ||
resultsChannel.close() | ||
return allReports | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
analyzer-direct-kt/src/main/kotlin/io/github/tassiLuca/analyzer/lib/GitHubAnalyzerByFlows.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,46 @@ | ||
package io.github.tassiLuca.analyzer.lib | ||
|
||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.coroutineScope | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.channelFlow | ||
import kotlinx.coroutines.flow.flatMapConcat | ||
import kotlinx.coroutines.flow.flowOn | ||
import kotlinx.coroutines.flow.toList | ||
import kotlinx.coroutines.launch | ||
|
||
internal class GitHubAnalyzerByFlows(private val provider: GitHubRepositoryProvider) : Analyzer { | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
@SuppressWarnings("InjectDispatcher") | ||
override suspend fun analyze( | ||
organizationName: String, | ||
updateResults: suspend (RepositoryReport) -> Unit, | ||
): Result<Set<RepositoryReport>> = coroutineScope { | ||
runCatching { | ||
val reports = provider.flowingRepositoriesOf(organizationName) | ||
.flatMapConcat { analyzeAll(it) } | ||
.flowOn(Dispatchers.Default) | ||
var allReports = emptySet<RepositoryReport>() | ||
reports.collect { | ||
updateResults(it) | ||
allReports = allReports + it | ||
} | ||
allReports | ||
} | ||
} | ||
|
||
@SuppressWarnings("InjectDispatcher") | ||
private fun analyzeAll(repositories: List<Repository>): Flow<RepositoryReport> = channelFlow { | ||
repositories.forEach { repository -> | ||
launch { | ||
val release = async { provider.lastReleaseOf(repository.organization, repository.name).getOrThrow() } | ||
provider.flowingContributorsOf(repository.organization, repository.name).toList().forEach { | ||
send(RepositoryReport(repository.name, repository.issues, repository.stars, it, release.await())) | ||
} | ||
} | ||
} | ||
}.flowOn(Dispatchers.Default) | ||
} |
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