Skip to content

Commit

Permalink
fixed bug when getting last 7 days stats from db
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob3075 committed Dec 7, 2023
1 parent 9fdefe9 commit 704987f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface ApplicationDao {
WHERE date BETWEEN :startDate AND :endDate
""",
)
suspend fun getStatsForRange(startDate: LocalDate, endDate: LocalDate): Map<DayEntity, List<ProjectPerDay>>
suspend fun getStatsForRange(startDate: LocalDate, endDate: LocalDate): Map<DayEntity, ProjectPerDay>

@Query("SELECT * FROM ProjectPerDay WHERE name = :name")
suspend fun getStatsForProject(name: String): List<ProjectPerDay>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.jacob.wakatimeapp.core.common.data.local.entities.ProjectPerDay
import com.jacob.wakatimeapp.core.models.DetailedDailyStats
import com.jacob.wakatimeapp.core.models.project.DetailedProjectStatsForDay
import com.jacob.wakatimeapp.core.models.project.Project
import kotlin.collections.Map.Entry
import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.toImmutableList
import kotlinx.datetime.LocalDate
Expand All @@ -20,6 +21,7 @@ fun List<ProjectPerDay>.toModel(): ImmutableList<Project> {
)
}.toImmutableList()
}

fun DetailedProjectStatsForDay.toEntity(dayId: LocalDate) = ProjectPerDay(
projectPerDayId = 0,
day = dayId,
Expand All @@ -41,9 +43,13 @@ fun DetailedDailyStats.toEntity() = DayEntity(
machines = emptyList(),
)

fun Map<DayEntity, List<ProjectPerDay>>.toDayWithProjects() = map { (dayEntity, projectsPerDay) ->
fun Map<DayEntity, ProjectPerDay>.toDayWithProjects() = entries.groupBy {
it.key.date
}.values.map {
val dayEntity = (it.firstOrNull() ?: return@map null).key
val projects = it.map(Entry<DayEntity, ProjectPerDay>::value)
DayWithProjects(
day = dayEntity,
projectsForDay = projectsPerDay,
dayEntity,
projects,
)
}
}.filterNotNull()
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.jacob.wakatimeapp.home.domain.usecases

import arrow.core.raise.either
import arrow.core.Either
import com.jacob.wakatimeapp.core.common.data.local.WakaTimeAppDB
import com.jacob.wakatimeapp.core.common.data.local.entities.DayWithProjects
import com.jacob.wakatimeapp.core.common.utils.InstantProvider
import com.jacob.wakatimeapp.core.models.Error
import com.jacob.wakatimeapp.home.data.mappers.toLast7RangeDaysStats
import com.jacob.wakatimeapp.home.domain.models.Last7DaysStats
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.datetime.DateTimeUnit
Expand All @@ -15,13 +18,13 @@ internal class GetLast7DaysStatsUC @Inject constructor(
private val wakaTimeAppDB: WakaTimeAppDB,
) {

suspend operator fun invoke() = either {
suspend operator fun invoke(): Either<Error, Last7DaysStats> {
val today = instantProvider.date()
val lastWeek7Days = today.minus(DAYS_IN_WEEK, DateTimeUnit.DAY)
wakaTimeAppDB.getStatsForRange(lastWeek7Days, today).bind().toLast7RangeDaysStats()
val lastWeek7Days = today.minus(PREVIOUS_DAYS_IN_WEEK, DateTimeUnit.DAY)
return wakaTimeAppDB.getStatsForRange(lastWeek7Days, today).map(List<DayWithProjects>::toLast7RangeDaysStats)
}

companion object {
private const val DAYS_IN_WEEK = 7
private const val PREVIOUS_DAYS_IN_WEEK = 6
}
}

0 comments on commit 704987f

Please sign in to comment.