Skip to content

Commit

Permalink
SI-2724-flag-missing-data (#33)
Browse files Browse the repository at this point in the history
* indicate when we think we're missing trip data

* flag missing data
  • Loading branch information
Allyson-English authored Jun 20, 2024
1 parent 1b1c9e3 commit 60b6594
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 47 deletions.
40 changes: 21 additions & 19 deletions docs/docs.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Package docs Code generated by swaggo/swag. DO NOT EDIT
// Package docs GENERATED BY SWAG; DO NOT EDIT
// This file was generated by swaggo/swag
package docs

import "github.com/swaggo/swag"
Expand Down Expand Up @@ -45,15 +46,15 @@ const docTemplate = `{
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/github_com_DIMO-Network_trips-api_internal_api_types.VehicleTrips"
"$ref": "#/definitions/types.VehicleTrips"
}
}
}
}
}
},
"definitions": {
"github_com_DIMO-Network_trips-api_internal_api_types.Location": {
"types.Location": {
"type": "object",
"properties": {
"latitude": {
Expand All @@ -64,47 +65,50 @@ const docTemplate = `{
}
}
},
"github_com_DIMO-Network_trips-api_internal_api_types.TripDetails": {
"types.TripDetails": {
"type": "object",
"properties": {
"droppedData": {
"type": "boolean"
},
"end": {
"$ref": "#/definitions/github_com_DIMO-Network_trips-api_internal_api_types.TripEnd"
"$ref": "#/definitions/types.TripEnd"
},
"id": {
"type": "string",
"example": "2Y83IHPItgk0uHD7hybGnA776Bo"
},
"start": {
"$ref": "#/definitions/github_com_DIMO-Network_trips-api_internal_api_types.TripStart"
"$ref": "#/definitions/types.TripStart"
}
}
},
"github_com_DIMO-Network_trips-api_internal_api_types.TripEnd": {
"types.TripEnd": {
"type": "object",
"properties": {
"location": {
"$ref": "#/definitions/github_com_DIMO-Network_trips-api_internal_api_types.Location"
"$ref": "#/definitions/types.Location"
},
"time": {
"type": "string"
}
}
},
"github_com_DIMO-Network_trips-api_internal_api_types.TripStart": {
"types.TripStart": {
"type": "object",
"properties": {
"estimatedLocation": {
"$ref": "#/definitions/github_com_DIMO-Network_trips-api_internal_api_types.Location"
"$ref": "#/definitions/types.Location"
},
"location": {
"$ref": "#/definitions/github_com_DIMO-Network_trips-api_internal_api_types.Location"
"$ref": "#/definitions/types.Location"
},
"time": {
"type": "string"
}
}
},
"github_com_DIMO-Network_trips-api_internal_api_types.VehicleTrips": {
"types.VehicleTrips": {
"type": "object",
"properties": {
"currentPage": {
Expand All @@ -118,7 +122,7 @@ const docTemplate = `{
"trips": {
"type": "array",
"items": {
"$ref": "#/definitions/github_com_DIMO-Network_trips-api_internal_api_types.TripDetails"
"$ref": "#/definitions/types.TripDetails"
}
}
}
Expand All @@ -135,16 +139,14 @@ const docTemplate = `{

// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = &swag.Spec{
Version: "1.0",
Version: "",
Host: "",
BasePath: "/v1",
BasePath: "",
Schemes: []string{},
Title: "DIMO Segment API",
Description: "segments",
Title: "",
Description: "",
InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate,
LeftDelim: "{{",
RightDelim: "}}",
}

func init() {
Expand Down
30 changes: 14 additions & 16 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
basePath: /v1
definitions:
github_com_DIMO-Network_trips-api_internal_api_types.Location:
types.Location:
properties:
latitude:
type: number
longitude:
type: number
type: object
github_com_DIMO-Network_trips-api_internal_api_types.TripDetails:
types.TripDetails:
properties:
droppedData:
type: boolean
end:
$ref: '#/definitions/github_com_DIMO-Network_trips-api_internal_api_types.TripEnd'
$ref: '#/definitions/types.TripEnd'
id:
example: 2Y83IHPItgk0uHD7hybGnA776Bo
type: string
start:
$ref: '#/definitions/github_com_DIMO-Network_trips-api_internal_api_types.TripStart'
$ref: '#/definitions/types.TripStart'
type: object
github_com_DIMO-Network_trips-api_internal_api_types.TripEnd:
types.TripEnd:
properties:
location:
$ref: '#/definitions/github_com_DIMO-Network_trips-api_internal_api_types.Location'
$ref: '#/definitions/types.Location'
time:
type: string
type: object
github_com_DIMO-Network_trips-api_internal_api_types.TripStart:
types.TripStart:
properties:
estimatedLocation:
$ref: '#/definitions/github_com_DIMO-Network_trips-api_internal_api_types.Location'
$ref: '#/definitions/types.Location'
location:
$ref: '#/definitions/github_com_DIMO-Network_trips-api_internal_api_types.Location'
$ref: '#/definitions/types.Location'
time:
type: string
type: object
github_com_DIMO-Network_trips-api_internal_api_types.VehicleTrips:
types.VehicleTrips:
properties:
currentPage:
example: 1
Expand All @@ -43,14 +44,11 @@ definitions:
type: integer
trips:
items:
$ref: '#/definitions/github_com_DIMO-Network_trips-api_internal_api_types.TripDetails'
$ref: '#/definitions/types.TripDetails'
type: array
type: object
info:
contact: {}
description: segments
title: DIMO Segment API
version: "1.0"
paths:
/vehicle/{tokenId}/trips:
get:
Expand All @@ -71,7 +69,7 @@ paths:
"200":
description: OK
schema:
$ref: '#/definitions/github_com_DIMO-Network_trips-api_internal_api_types.VehicleTrips'
$ref: '#/definitions/types.VehicleTrips'
security:
- BearerAuth: []
securityDefinitions:
Expand Down
27 changes: 16 additions & 11 deletions internal/services/consumer/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,23 @@ func (c *Consumer) BeginSegment(ctx context.Context, event shared.CloudEvent[Seg
StartPosition: nullLocationToDB(event.Data.Start.Location),
}

if segment.StartPosition.Valid && len(veh.R.VehicleTokenTrips) > 0 {
if lastLoc := veh.R.VehicleTokenTrips[0].EndPosition; lastLoc.Valid && geo.InterpolateTripStart(lastLoc.Point, segment.StartPosition.Point) {
segment.StartPositionEstimate = lastLoc
segment.DroppedData = true
} else {
// if new trip start is greater than allowable distance, we still want to indicate there was dropped data
segment.DroppedData = true
if segment.StartPosition.Valid {
if len(veh.R.VehicleTokenTrips) > 0 {
if lastLoc := veh.R.VehicleTokenTrips[0].EndPosition; lastLoc.Valid {
if lastLoc.Point != segment.StartPosition.Point {
// if new trip does not start where last trip ended, indicate dropped data
segment.DroppedData = true
}
// only estimate start position if last trip ended within threshold distance
if geo.InterpolateTripStart(lastLoc.Point, segment.StartPosition.Point) {
segment.StartPositionEstimate = lastLoc
}
}
}
} else {
// anytime we get invalid location in trip start event
// indicate dropped data
segment.DroppedData = true
}

return segment.Insert(ctx, c.pg.DB.DBS().Writer, boil.Infer())
Expand Down Expand Up @@ -145,10 +154,6 @@ func (c *Consumer) CompleteSegment(ctx context.Context, event shared.CloudEvent[
estLoc := nullLocationToDB(event.Data.Start.Location)
if lastLoc := veh.R.VehicleTokenTrips[0].EndPosition; lastLoc.Valid && geo.InterpolateTripStart(lastLoc.Point, estLoc.Point) {
segment.StartPositionEstimate = lastLoc
segment.DroppedData = true
} else {
// if new trip start is greater than allowable distance, we still want to indicate there was dropped data
segment.DroppedData = true
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion internal/services/consumer/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ func Test_TripWithGeos(t *testing.T) {
trp, _ := models.Trips(models.TripWhere.ID.EQ(segment1.Data.ID)).One(ctx, pdb.DBS().Reader)
assert.Equal(t, trp.StartPosition.X, segment1.Data.Start.Location.Longitude)
assert.Equal(t, trp.StartPosition.Y, segment1.Data.Start.Location.Latitude)
assert.Equal(t, trp.StartPosition.Y, segment1.Data.Start.Location.Latitude)
assert.Equal(t, trp.StartTime, segment1.Data.Start.Time)
assert.Equal(t, false, trp.DroppedData)

segment1.Data.Completed = true
if err := consumer.ProcessSegmentEvent(ctx, segment1); err != nil {
Expand All @@ -145,6 +147,7 @@ func Test_TripWithGeos(t *testing.T) {
assert.Equal(t, trp.EndPosition.X, segment1.Data.End.Location.Longitude)
assert.Equal(t, trp.EndPosition.Y, segment1.Data.End.Location.Latitude)
assert.Equal(t, trp.EndTime.Time, segment1.Data.End.Time)
assert.Equal(t, false, trp.DroppedData)

}

Expand Down Expand Up @@ -185,12 +188,14 @@ func Test_NewTripEstimateStart(t *testing.T) {
estTrp, _ := models.Trips(models.TripWhere.ID.EQ(segment2.Data.ID)).One(ctx, pdb.DBS().Reader)
assert.Equal(t, estTrp.StartPositionEstimate.X, segment1.Data.End.Location.Longitude)
assert.Equal(t, estTrp.StartPositionEstimate.Y, segment1.Data.End.Location.Latitude)
assert.Equal(t, estTrp.DroppedData, true)

}

// New user trip (has a prev trip)
// Current trip includes start geo
// Do not interpolate start estimate bc new start loc is too far from prev trip end
// Still indicate that we have dropped data
func Test_NewTripDontEstimateStart(t *testing.T) {
ctx := context.Background()
pdb := test.StartContainerDatabase(ctx, t, migrationsDirRelPath)
Expand Down Expand Up @@ -226,6 +231,7 @@ func Test_NewTripDontEstimateStart(t *testing.T) {
assert.False(t, newTripNoEst.StartPositionEstimate.Valid)
assert.Equal(t, newTripNoEst.StartPosition.X, segment3.Data.Start.Location.Longitude)
assert.Equal(t, newTripNoEst.StartPosition.Y, segment3.Data.Start.Location.Latitude)
assert.Equal(t, true, newTripNoEst.DroppedData)
}

// New user trip
Expand Down Expand Up @@ -267,7 +273,7 @@ func Test_StartLocationNotIncludedInFirstEvent(t *testing.T) {
assert.Equal(t, trp.EndPosition.X, segment1.Data.End.Location.Longitude)
assert.Equal(t, trp.EndPosition.Y, segment1.Data.End.Location.Latitude)
assert.True(t, trp.EndTime.Time.Equal(segment1.Data.End.Time))

assert.True(t, trp.DroppedData)
}

// New user trip (has prev trip)
Expand Down Expand Up @@ -310,6 +316,7 @@ func Test_EstimateStartOnCompletion(t *testing.T) {
estTrp, _ := models.Trips(models.TripWhere.ID.EQ(segment2.Data.ID)).One(ctx, pdb.DBS().Reader)
assert.False(t, estTrp.StartPosition.Valid)
assert.Equal(t, estTrp.StartTime, segment2.Data.Start.Time)
assert.True(t, estTrp.DroppedData)

completed.Data.Completed = true
if err := consumer.ProcessSegmentEvent(ctx, completed); err != nil {
Expand Down

0 comments on commit 60b6594

Please sign in to comment.