Skip to content

Commit

Permalink
Remove Stop Calc; Tests in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
janschill committed Aug 23, 2024
1 parent 3db8629 commit 88ab3ec
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 105 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,24 @@ jobs:
- name: Build Docker image
run: docker build -t trackme-builder .

- name: Run unit tests inside Docker container
id: test
run: |
docker create --name test-container trackme-builder
docker start test-container
docker exec test-container go test -v ./...
docker stop test-container
docker rm test-container
- name: Extract binary from Docker container
if: steps.test.outcome == 'success'
run: |
docker create --name extract trackme-builder
docker cp extract:/app/trackme ./trackme
docker rm extract
- name: Copy assets and templates
if: steps.test.outcome == 'success'
uses: appleboy/[email protected]
with:
host: ${{ secrets.SERVER_IP }}
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ require (
github.com/getsentry/sentry-go v0.28.1
github.com/joho/godotenv v1.5.1
github.com/mattn/go-sqlite3 v1.14.22
github.com/stretchr/testify v1.9.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/stretchr/testify v1.9.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
25 changes: 0 additions & 25 deletions internal/utils/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,31 +201,6 @@ func CalculateElevationGainAndLoss(events []repository.Event) (elevationGain, el
return elevationGain, elevationLoss
}

// TODO:
func CalculateStops(events []repository.Event) (numberOfStops, totalStopTimeInSeconds int) {
if len(events) < 2 {
return 0, 0
}

inStop := false
for i := 1; i < len(events); i++ {
distance := haversine(events[i-1].Latitude, events[i-1].Longitude, events[i].Latitude, events[i].Longitude)
timeDiff := time.Unix(events[i].TimeStamp, 0).Sub(time.Unix(events[i-1].TimeStamp, 0)).Seconds()
speed := (distance / timeDiff) * 3.6 // Convert m/s to km/h

if speed < 1.0 { // Assuming speed < 1 km/h is considered a stop
if !inStop {
numberOfStops++
inStop = true
}
totalStopTimeInSeconds += int(timeDiff)
} else {
inStop = false
}
}
return numberOfStops, totalStopTimeInSeconds
}

func CalculateMaxSpeed(events []repository.Event) (maxSpeed float64) {
maxSpeed = math.Inf(-1)
for i := 1; i < len(events); i++ {
Expand Down
79 changes: 0 additions & 79 deletions internal/utils/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,82 +192,3 @@ func TestCalculateElevationGainAndLoss(t *testing.T) {
})
}
}

func TestCalculateStops(t *testing.T) {
tests := []struct {
name string
events []repository.Event
expectedNumberOfStops int
expectedTotalStopTimeInSeconds int
}{
{
name: "multiple events with stops",
events: []repository.Event{
{Latitude: 40.7128, Longitude: -74.0060, TimeStamp: 1609459200},
{Latitude: 40.7128, Longitude: -74.0060, TimeStamp: 1609459260},
{Latitude: 40.7138, Longitude: -74.0065, TimeStamp: 1609459320},
{Latitude: 40.7138, Longitude: -74.0065, TimeStamp: 1609459380},
},
expectedNumberOfStops: 2,
expectedTotalStopTimeInSeconds: 120,
},
{
name: "no stops",
events: []repository.Event{
{Latitude: 40.7128, Longitude: -74.0060, TimeStamp: 1609459200},
{Latitude: 40.7138, Longitude: -74.0065, TimeStamp: 1609459260},
{Latitude: 40.7148, Longitude: -74.0070, TimeStamp: 1609459320},
},
expectedNumberOfStops: 0,
expectedTotalStopTimeInSeconds: 0,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
numberOfStops, totalStopTimeInSeconds := CalculateStops(tt.events)
if numberOfStops != tt.expectedNumberOfStops {
t.Errorf("CalculateStops() numberOfStops = %d; expected %d", numberOfStops, tt.expectedNumberOfStops)
}
if totalStopTimeInSeconds != tt.expectedTotalStopTimeInSeconds {
t.Errorf("CalculateStops() totalStopTimeInSeconds = %d; expected %d", totalStopTimeInSeconds, tt.expectedTotalStopTimeInSeconds)
}
})
}
}

func TestCalculateMaxSpeed(t *testing.T) {
tests := []struct {
name string
events []repository.Event
expectedMaxSpeed float64
}{
{
name: "multiple events with varying speeds",
events: []repository.Event{
{Latitude: 40.7128, Longitude: -74.0060, TimeStamp: 1609459200},
{Latitude: 40.7138, Longitude: -74.0065, TimeStamp: 1609459260},
{Latitude: 40.7148, Longitude: -74.0070, TimeStamp: 1609459320},
},
expectedMaxSpeed: 10.0,
},
{
name: "constant speed",
events: []repository.Event{
{Latitude: 40.7128, Longitude: -74.0060, TimeStamp: 1609459200},
{Latitude: 40.7138, Longitude: -74.0065, TimeStamp: 1609459260},
{Latitude: 40.7148, Longitude: -74.0070, TimeStamp: 1609459320},
},
expectedMaxSpeed: 7.5,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
maxSpeed := CalculateMaxSpeed(tt.events)
if maxSpeed != tt.expectedMaxSpeed {
t.Errorf("CalculateSpeedMetrics() maxSpeed = %f; expected %f", maxSpeed, tt.expectedMaxSpeed)
}
})
}
}

0 comments on commit 88ab3ec

Please sign in to comment.