Skip to content

Commit

Permalink
Fix ratio count in repository
Browse files Browse the repository at this point in the history
Closes #155
  • Loading branch information
alexandr7035 committed Nov 8, 2021
1 parent 6c869d0 commit f9a4409
Showing 1 changed file with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import by.alexandr7035.gitstat.core.TimeHelper
import by.alexandr7035.gitstat.data.local.CacheDB
import by.alexandr7035.gitstat.data.local.model.*
import by.alexandr7035.gitstat.data.remote.mappers.*
import by.alexandr7035.gitstat.extensions.debug
import by.alexandr7035.gitstat.extensions.performRequestWithDataResult
import com.apollographql.apollo3.ApolloClient
import timber.log.Timber
Expand Down Expand Up @@ -47,7 +48,7 @@ class DataSyncRepository @Inject constructor(
val contributionDays = fetchContributionDays(accountCreationYear, currentYear)

val totalContributions = contributionDays.sumOf { it.count }
val contributionTypes = fetchContributionTypes(accountCreationYear, currentYear, totalContributions)
val contributionTypes = fetchContributionTypes(accountCreationYear, currentYear)
val contributionRates = fetchContributionRates(contributionDays)

// Write cache
Expand Down Expand Up @@ -115,19 +116,21 @@ class DataSyncRepository @Inject constructor(
}


// We need to get totalContributions here and pass them to mapper
// Some of contributions do not belong to 5 groups (commits, issues, PRs, code reviews, repositories)
// that Github API provides. We mark them as "Unknown" in mapper
// TODO try to find better solution later
private suspend fun fetchContributionTypes(profileCreationYear: Int, currentYear: Int, totalContributionsCount: Int): ArrayList<ContributionsRatioEntity> {
private suspend fun fetchContributionTypes(profileCreationYear: Int, currentYear: Int): ArrayList<ContributionsRatioEntity> {

val contributionsRatioCached = ArrayList<ContributionsRatioEntity>()

// Date range more than a year is not allowed in this api
// So we have to deal with multiple requests
for (year in profileCreationYear..currentYear) {

// Sync ratio of total contributions (commits, PRs, etc.)
// We need to get totalContributions here and pass them to mapper
// Some of contributions do not belong to 5 groups (commits, issues, PRs, code reviews, repositories)
// that Github API provides. We mark them as "Unknown" in mapper
val yearContributions = db.getContributionsDao().getContributionYearWithDays(year)
val totalContributionsCount = yearContributions.contributionDays.sumOf { it.count }

val ratioData = getContributionsRatioForDateRange(year)
contributionsRatioCached.add(ratioMapper.transform(ratioData, totalContributionsCount))
}
Expand Down

0 comments on commit f9a4409

Please sign in to comment.