Skip to content

Commit

Permalink
Read Toggl format (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
agile-jordi authored Jun 2, 2023
2 parents 4e5b2f2 + 7a6b2a5 commit b11cfe6
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ While developing:
- `./gradlew koverHtmlReport` to generate and open the test coverage report

To test your code:

- `docker-compose up`
- `./gradlew test`
1 change: 1 addition & 0 deletions buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ object Dependencies {
val suspendApp = "$arrowKt:suspendapp:0.4.1-alpha.5"
val suspendAppKtor = "$arrowKt:suspendapp-ktor:0.4.1-alpha.5"

val kotlincsv = "com.github.doyaaaaaken:kotlin-csv-jvm:1.9.1"
val kolinx = "org.jetbrains.kotlinx"
val kotlinxSerializationVersion = "1.5.0"
val kotlinXSerializationJson = "$kolinx:kotlinx-serialization-json:$kotlinxSerializationVersion"
Expand Down
6 changes: 6 additions & 0 deletions components/toggl/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Dependencies.kotlincsv

dependencies {
implementation(kotlincsv)
api(project(":domain"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.agilogy.timetracking.toggl

import com.agilogy.timetracking.domain.ProjectName
import com.github.doyaaaaaken.kotlincsv.dsl.csvReader
import java.io.InputStream
import java.time.Duration
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.ZoneId
import java.time.temporal.ChronoUnit

fun readTogglCsv(inputStream: InputStream, zoneId: ZoneId): List<Triple<ProjectName, ClosedRange<Instant>, ZoneId>> =
csvReader().readAllWithHeader(inputStream).map { row ->
val projectName = ProjectName(row.getValue("Project"))
val startDate = LocalDate.parse(row.getValue("Start date"))
val startTime = LocalTime.parse(row.getValue("Start time"))
val parsedDuration = LocalTime.parse(row.getValue("Duration"))

val startInstant = LocalDateTime.of(startDate, startTime).atZone(zoneId).toInstant()

val duration = Duration.of(parsedDuration.toSecondOfDay().toLong(), ChronoUnit.SECONDS)!!
val range = startInstant..startInstant.plus(duration)
Triple(projectName, range, zoneId)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.agilogy.timetracking.toggl

import com.agilogy.timetracking.domain.ProjectName
import io.kotest.core.spec.style.FunSpec
import org.junit.jupiter.api.Assertions.assertEquals
import java.time.Instant
import java.time.ZoneId

class TogglTest : FunSpec() {
init {
test("TODO") {
this::class.java.getResourceAsStream("/togglTimeEntries.csv").use { inputStream ->
val zoneId = ZoneId.of("Europe/Madrid")
val result: List<Triple<ProjectName, ClosedRange<Instant>, ZoneId>> = readTogglCsv(
inputStream!!,
zoneId,
)
val expected: List<Triple<ProjectName, ClosedRange<Instant>, ZoneId>> = listOf(
Triple(
ProjectName("ProjectA"),
Instant.parse("2023-05-12T06:30:00Z")..Instant.parse("2023-05-12T14:30:00Z"),
zoneId,
),
Triple(
ProjectName("ProjectB"),
Instant.parse("2023-05-15T06:30:00Z")..Instant.parse("2023-05-15T14:30:00Z"),
zoneId,
),
Triple(
ProjectName("ProjectA"),
Instant.parse("2023-05-15T14:30:00Z")..Instant.parse("2023-05-15T16:30:00Z"),
zoneId,
),
Triple(
ProjectName("ProjectB"),
Instant.parse("2023-05-16T06:30:00Z")..Instant.parse("2023-05-16T14:30:00Z"),
zoneId,
),
Triple(
ProjectName("ProjectB"),
Instant.parse("2023-05-17T06:30:00Z")..Instant.parse("2023-05-17T14:30:00Z"),
zoneId,
),
)
assertEquals(expected, result)
}
}
}
}
6 changes: 6 additions & 0 deletions components/toggl/src/test/resources/togglTimeEntries.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
User,Email,Client,Project,Task,Description,Billable,Start date,Start time,End date,End time,Duration,Tags,Amount ()
JohnDoe,[email protected],,ProjectA,,"",No,2023-05-12,08:30:00,2023-05-12,16:30:00,08:00:00,,
JohnDoe,[email protected],,ProjectB,,"",No,2023-05-15,08:30:00,2023-05-15,16:30:00,08:00:00,,
JohnDoe,[email protected],,ProjectA,,"",No,2023-05-15,16:30:00,2023-05-15,18:30:00,02:00:00,,
JohnDoe,[email protected],,ProjectB,,"",No,2023-05-16,08:30:00,2023-05-16,16:30:00,08:00:00,,
JohnDoe,[email protected],,ProjectB,,"",No,2023-05-17,08:30:00,2023-05-17,16:30:00,08:00:00,,

0 comments on commit b11cfe6

Please sign in to comment.