-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathlocation_history_store.go
More file actions
71 lines (62 loc) · 1.97 KB
/
Copy pathlocation_history_store.go
File metadata and controls
71 lines (62 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package main
import (
"context"
"fmt"
"time"
"github.com/OneBusAway/vehicle-positions/db"
)
// LocationPoint represents a single persisted location point for history queries.
type LocationPoint struct {
Latitude float64
Longitude float64
Bearing *float64
Speed *float64
Accuracy *float64
Timestamp int64
TripID string
ReceivedAt time.Time
}
// LocationHistoryLister retrieves historical location points for a vehicle.
type LocationHistoryLister interface {
GetLocationHistory(ctx context.Context, vehicleID string, from, to int64, limit int) ([]LocationPoint, error)
}
// VehicleChecker checks whether a vehicle exists.
type VehicleChecker interface {
VehicleExists(ctx context.Context, vehicleID string) (bool, error)
}
// GetLocationHistory returns location points for a vehicle within the given
// timestamp range, ordered by most recent first.
func (s *Store) GetLocationHistory(ctx context.Context, vehicleID string, from, to int64, limit int) ([]LocationPoint, error) {
rows, err := s.queries.GetLocationHistory(ctx, db.GetLocationHistoryParams{
VehicleID: vehicleID,
Timestamp: from,
Timestamp_2: to,
Limit: int32(limit),
})
if err != nil {
return nil, fmt.Errorf("query location history: %w", err)
}
points := make([]LocationPoint, 0, len(rows))
for _, row := range rows {
p := LocationPoint{
Latitude: row.Latitude,
Longitude: row.Longitude,
Timestamp: row.Timestamp,
TripID: row.TripID,
ReceivedAt: row.ReceivedAt.Time,
}
p.Bearing = nullableFloat(row.Bearing)
p.Speed = nullableFloat(row.Speed)
p.Accuracy = nullableFloat(row.Accuracy)
points = append(points, p)
}
return points, nil
}
// VehicleExists returns true if a vehicle with the given ID exists.
func (s *Store) VehicleExists(ctx context.Context, vehicleID string) (bool, error) {
exists, err := s.queries.VehicleExists(ctx, vehicleID)
if err != nil {
return false, fmt.Errorf("check vehicle exists: %w", err)
}
return exists, nil
}