Skip to content

Commit

Permalink
WTA #82: updated how time is calculated and updated some models and m…
Browse files Browse the repository at this point in the history
…appers
  • Loading branch information
Jacob3075 committed Nov 1, 2023
1 parent 74c2c7a commit 4d56b9e
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ data class DependencyDTO(
val percent: Double,
val seconds: Int,
val text: String,
@SerialName("total_seconds") val totalSeconds: Double,
@SerialName("total_seconds") val totalSeconds: Long,
)

@Serializable
Expand All @@ -26,7 +26,7 @@ data class EditorDTO(
val percent: Double,
val seconds: Int,
val text: String,
@SerialName("total_seconds") val totalSeconds: Double,
@SerialName("total_seconds") val totalSeconds: Long,
)

@Serializable
Expand All @@ -39,7 +39,7 @@ data class LanguageDTO(
val percent: Double,
val seconds: Int,
val text: String,
@SerialName("total_seconds") val totalSeconds: Double,
@SerialName("total_seconds") val totalSeconds: Long,
)

@Serializable
Expand All @@ -53,7 +53,7 @@ data class MachineDTO(
val seconds: Int,
val text: String,
@SerialName("machine_name_id") val machineNameId: String,
@SerialName("total_seconds") val totalSeconds: Double,
@SerialName("total_seconds") val totalSeconds: Long,
)

@Serializable
Expand All @@ -66,7 +66,7 @@ data class OperatingSystemDTO(
val percent: Double,
val seconds: Int,
val text: String,
@SerialName("total_seconds") val totalSeconds: Double,
@SerialName("total_seconds") val totalSeconds: Long,
)

@Serializable
Expand All @@ -80,7 +80,7 @@ data class EntityDTO(
val seconds: Int,
val text: String,
val type: String,
@SerialName("total_seconds") val totalSeconds: Double,
@SerialName("total_seconds") val totalSeconds: Long,
)

@Serializable
Expand All @@ -93,5 +93,5 @@ data class CategoryDTO(
val percent: Double,
val seconds: Int,
val text: String,
@SerialName("total_seconds") val totalSeconds: Double,
@SerialName("total_seconds") val totalSeconds: Long,
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import kotlinx.serialization.Serializable
data class CumulativeTotalDTO(
val decimal: String,
val digital: String,
val seconds: Double,
val seconds: Long,
val text: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ data class ExtractedDataDTO(
val percent: Double,
val seconds: Int,
val text: String,
@SerialName("total_seconds") val totalSeconds: Double,
@SerialName("total_seconds") val totalSeconds: Long,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ data class GrandTotalDTO(
val hours: Int,
val minutes: Int,
val text: String,
@SerialName("total_seconds") val totalSeconds: Double,
@SerialName("total_seconds") val totalSeconds: Long,
)
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
package com.jacob.wakatimeapp.core.common.data.mappers // ktlint-disable filename
package com.jacob.wakatimeapp.core.common.data.mappers

import com.jacob.wakatimeapp.core.common.data.dtos.EditorDTO
import com.jacob.wakatimeapp.core.common.data.dtos.LanguageDTO
import com.jacob.wakatimeapp.core.common.data.dtos.OperatingSystemDTO
import com.jacob.wakatimeapp.core.common.data.dtos.ProjectDTO
import com.jacob.wakatimeapp.core.models.Time
import com.jacob.wakatimeapp.core.models.secondarystats.Editor
import com.jacob.wakatimeapp.core.models.secondarystats.Editors
import com.jacob.wakatimeapp.core.models.secondarystats.Language
import com.jacob.wakatimeapp.core.models.secondarystats.Languages
import com.jacob.wakatimeapp.core.models.secondarystats.OperatingSystem
import com.jacob.wakatimeapp.core.models.secondarystats.OperatingSystems
import kotlinx.collections.immutable.toImmutableList

fun List<LanguageDTO>.toModel() = map(LanguageDTO::toModel).let(::Languages)

fun List<EditorDTO>.toModel() = map(EditorDTO::toModel).let(::Editors)

fun List<OperatingSystemDTO>.toModel() = map(OperatingSystemDTO::toModel).let(::OperatingSystems)

fun List<ProjectDTO>.toModel() = filterNot(ProjectDTO::isUnknownProject)
.map(ProjectDTO::toModel)
.toImmutableList()

fun LanguageDTO.toModel() = Language(
name = name,
time = Time.fromDecimal(decimal.toFloat()),
time = Time.fromTotalSeconds(totalSeconds),
)

fun EditorDTO.toModel() = Editor(
name = name,
time = Time.fromDecimal(decimal.toFloat()),
time = Time.fromTotalSeconds(totalSeconds),
)

fun OperatingSystemDTO.toModel() = OperatingSystem(
name = name,
time = Time.fromDecimal(decimal.toFloat()),
time = Time.fromTotalSeconds(totalSeconds),
)

fun List<EditorDTO>.fromDto() = map(EditorDTO::toModel).let(::Editors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fun DayDTO.toEntity() = DayEntity(
machines = this.machines.map { machine ->
Machine(
name = machine.name,
time = Time.fromDecimal(machine.totalSeconds.toFloat()),
time = Time.fromTotalSeconds(machine.totalSeconds),
)
},
)
Expand All @@ -27,13 +27,13 @@ fun ProjectDTO.toEntity(dayId: Long): ProjectPerDay {
val branches = branches.map { branch ->
com.jacob.wakatimeapp.core.models.Branch(
name = branch.name,
time = Time.fromDecimal(branch.totalSeconds.toFloat()),
time = Time.fromTotalSeconds(branch.totalSeconds),
)
}
val machines = machines.map { machine ->
Machine(
name = machine.name,
time = Time.fromDecimal(machine.totalSeconds.toFloat()),
time = Time.fromTotalSeconds(machine.totalSeconds),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ data class DailyStats(
val mostUsedEditor: String,
val mostUsedOs: String,
val date: LocalDate,
)
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.jacob.wakatimeapp.core.models

import com.jacob.wakatimeapp.core.models.secondarystats.Editors
import com.jacob.wakatimeapp.core.models.secondarystats.Languages
import com.jacob.wakatimeapp.core.models.secondarystats.OperatingSystems
import kotlinx.collections.immutable.ImmutableList
import kotlinx.datetime.LocalDate

data class DetailedDailyStats(
val date: LocalDate,
val projects: ImmutableList<Project>,
val languages: Languages,
val editors: Editors,
val operatingSystems: OperatingSystems,
val timeSpent: Time,
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,36 @@ data class Time(
val hours: Int,
val minutes: Int,
val decimal: Float,
val totalSeconds: Int = calculateTotalSeconds(hours, minutes),
val totalSeconds: Long = calculateTotalSeconds(hours.toLong(), minutes.toLong()),
) {
fun toMinutes(): Int = (hours * MINUTES_IN_HOURS) + minutes

fun formattedPrint() = "${hours}H, ${minutes}M"

fun longFormattedPrint() = "$hours Hours, $minutes Minutes"
operator fun plus(other: Time) = fromDecimal(decimal + other.decimal)

operator fun plus(other: Time) = fromTotalSeconds(totalSeconds + other.totalSeconds)

companion object {
val ZERO = Time(0, 0, 0f)

fun fromTotalSeconds(totalSeconds: Long): Time {
val hours = totalSeconds / (SECONDS_IN_MINUTES * MINUTES_IN_HOURS)
val minutes = (totalSeconds % (SECONDS_IN_MINUTES * MINUTES_IN_HOURS)) / MINUTES_IN_HOURS
val decimal = hours + (minutes.toFloat() / MINUTES_IN_HOURS)
return Time(
decimal = decimal,
hours = hours.toInt(),
minutes = minutes.toInt(),
totalSeconds = totalSeconds,
)
}

fun fromDecimal(decimal: Float): Time {
val hours = decimal.toInt()
val minutesDecimal = (decimal - hours) * MINUTES_IN_HOURS
val minutes = minutesDecimal.toInt()
val totalSeconds = calculateTotalSeconds(hours, minutes)
val totalSeconds = calculateTotalSeconds(hours.toLong(), minutes.toLong())
return Time(
decimal = decimal,
hours = hours,
Expand All @@ -38,9 +51,10 @@ data class Time(
return Time(hours, minutes, decimal.toFloat())
}

private fun calculateTotalSeconds(hours: Int, minutes: Int) =
(hours * MINUTES_IN_HOURS * MINUTES_IN_HOURS) + (minutes * MINUTES_IN_HOURS)
private fun calculateTotalSeconds(hours: Long, minutes: Long): Long =
(hours * MINUTES_IN_HOURS * SECONDS_IN_MINUTES) + (minutes * SECONDS_IN_MINUTES)

private const val MINUTES_IN_HOURS = 60
private const val SECONDS_IN_MINUTES = 60
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.jacob.wakatimeapp.core.common.data.dtos.CumulativeTotalDTO
import com.jacob.wakatimeapp.core.common.data.dtos.DependencyDTO
import com.jacob.wakatimeapp.core.common.data.dtos.EditorDTO
import com.jacob.wakatimeapp.core.common.data.dtos.EntityDTO
import com.jacob.wakatimeapp.core.common.data.dtos.ExtractedDataDTO.DayDTO.ProjectDTO.BranchDTO
import com.jacob.wakatimeapp.core.common.data.dtos.GrandTotalDTO
import com.jacob.wakatimeapp.core.common.data.dtos.LanguageDTO
import com.jacob.wakatimeapp.core.common.data.dtos.MachineDTO
Expand Down Expand Up @@ -35,18 +36,5 @@ data class DetailedProjectStatsDTO(
val range: RangeDTO,
@SerialName("operating_systems") val operatingSystems: List<OperatingSystemDTO>,
@SerialName("grand_total") val grandTotal: GrandTotalDTO,
) {
@Serializable
data class BranchDTO(
val decimal: String,
val digital: String,
val hours: Int,
val minutes: Int,
val name: String,
val percent: Double,
val seconds: Int,
val text: String,
@SerialName("total_seconds") val totalSeconds: Double,
)
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import kotlinx.datetime.toLocalDate

fun DetailedProjectStatsDTO.toModel(name: String): ProjectStats {
val dailyStats = data.associate {
it.range.date.toLocalDate() to Time.fromDecimal(it.grandTotal.decimal.toFloat())
it.range.date.toLocalDate() to Time.fromTotalSeconds(it.grandTotal.totalSeconds)
}

val editors = data.flatMap(Data::editors)
Expand All @@ -31,21 +31,21 @@ fun DetailedProjectStatsDTO.toModel(name: String): ProjectStats {
.map { branch ->
Branch(
name = branch.name,
time = Time.fromDecimal(branch.totalSeconds.toFloat()),
time = Time.fromTotalSeconds(branch.totalSeconds),
)
}

val machines = data.flatMap(Data::machines)
.map { machine ->
Machine(
name = machine.name,
time = Time.fromDecimal(machine.totalSeconds.toFloat()),
time = Time.fromTotalSeconds(machine.totalSeconds),
)
}

return ProjectStats(
name = name,
totalTime = Time.fromDecimal(cumulativeTotal.decimal.toFloat()),
totalTime = Time.fromTotalSeconds(cumulativeTotal.seconds),
dailyProjectStats = dailyStats,
range = Range(startDate = start, endDate = end),
languages = languages,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ data class Streak(
operator fun plus(other: Streak) = when {
this == ZERO -> other
other == ZERO -> this
this in other -> Streak(other.start, other.end)
other in this -> Streak(start, end)
this in other -> other
other in this -> this
other.start in padded() -> Streak(start, other.end)
start in other.padded() -> Streak(other.start, end)
else -> {
Expand Down

0 comments on commit 4d56b9e

Please sign in to comment.