Permalink
Browse files

Database interface cleanup (#118)

* Stop leaking mgo into updater

* Remove dead prediction code

* Generate database IDs inside of database interface

* Fix linter errors
  • Loading branch information...
kochman authored and jlyon1 committed Dec 7, 2017
1 parent 870d184 commit 5dc051c3b1591930b47670dbcd641cabdc24447c
Showing with 32 additions and 87 deletions.
  1. +0 −69 api/predict.go
  2. +14 −15 api/routes.go
  3. +8 −0 database/database.go
  4. +8 −0 database/mongodb.go
  5. +2 −3 updater/updater.go
View

This file was deleted.

Oops, something went wrong.
View
@@ -15,7 +15,6 @@ import (
"github.com/gorilla/mux"
"github.com/wtg/shuttletracker/model"
"gopkg.in/mgo.v2/bson"
)
// RoutesHandler finds all of the routes in the database
@@ -87,7 +86,6 @@ func (api *API) RoutesCreateHandler(w http.ResponseWriter, r *http.Request) {
currentTime := time.Now()
// Create a new route
route := model.Route{
ID: bson.NewObjectId().Hex(),
Name: routeData["name"],
Description: routeData["description"],
StartTime: routeData["startTime"],
@@ -161,31 +159,32 @@ func (api *API) StopsCreateHandler(w http.ResponseWriter, r *http.Request) {
// Create a new stop object using request fields
stop := model.Stop{}
err := json.NewDecoder(r.Body).Decode(&stop)
stop.ID = bson.NewObjectId().Hex()
route, err1 := api.db.GetRoute(stop.RouteID)
// Error handling
if err1 != nil {
http.Error(w, err1.Error(), http.StatusInternalServerError)
if err != nil {
log.WithError(err).Error("Unable to decode stop.")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
route, err := api.db.GetRoute(stop.RouteID)
if err != nil {
log.WithError(err).Error("Unable to get route.")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// We have to know the order of the stop and store a velocity vector into duration for the prediction
route.StopsID = append(route.StopsID, stop.ID) // THIS REQUIRES the front end to have correct order << to be improved
fmt.Println(route.StopsID)
// Store new stop under stops collection
err = api.db.CreateStop(&stop)
// Error handling
if err != nil {
fmt.Println(err.Error())
log.WithError(err).Error("Unable to create stop.")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// We have to know the order of the stop and store a velocity vector into duration for the prediction
route.StopsID = append(route.StopsID, stop.ID) // THIS REQUIRES the front end to have correct order << to be improved
err = api.db.ModifyRoute(&route)
if err != nil {
fmt.Println(err.Error())
log.WithError(err).Error("Unable to modify route.")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
WriteJSON(w, stop)
}
View
@@ -1,6 +1,7 @@
package database
import (
"errors"
"time"
"github.com/wtg/shuttletracker/model"
@@ -40,3 +41,10 @@ type Database interface {
// Users
GetUsers() ([]model.User, error)
}
var (
// ErrVehicleNotFound indicates that a Vehicle is not in the database.
ErrVehicleNotFound = errors.New("Vehicle not found")
// ErrUpdateNotFound indicates that an Update is not in the database.
ErrUpdateNotFound = errors.New("Update not found")
)
View
@@ -77,6 +77,7 @@ func NewMongoDBConfig(v *viper.Viper) *MongoDBConfig {
// CreateRoute creates a Route.
func (m *MongoDB) CreateRoute(route *model.Route) error {
route.ID = bson.NewObjectId().Hex()
return m.routes.Insert(&route)
}
@@ -106,6 +107,7 @@ func (m *MongoDB) ModifyRoute(route *model.Route) error {
// CreateStop creates a Stop.
func (m *MongoDB) CreateStop(stop *model.Stop) error {
stop.ID = bson.NewObjectId().Hex()
return m.stops.Insert(&stop)
}
@@ -146,6 +148,9 @@ func (m *MongoDB) DeleteUpdatesBefore(before time.Time) (int, error) {
func (m *MongoDB) GetLastUpdateForVehicle(vehicleID string) (model.VehicleUpdate, error) {
var update model.VehicleUpdate
err := m.updates.Find(bson.M{"vehicleID": vehicleID}).Sort("-created").One(&update)
if err == mgo.ErrNotFound {
return update, ErrUpdateNotFound
}
return update, err
}
@@ -177,6 +182,9 @@ func (m *MongoDB) DeleteVehicle(vehicleID string) error {
func (m *MongoDB) GetVehicle(vehicleID string) (model.Vehicle, error) {
var vehicle model.Vehicle
err := m.vehicles.Find(bson.M{"vehicleID": vehicleID}).One(&vehicle)
if err == mgo.ErrNotFound {
return vehicle, ErrVehicleNotFound
}
return vehicle, err
}
View
@@ -11,7 +11,6 @@ import (
"time"
"github.com/spf13/viper"
mgo "gopkg.in/mgo.v2"
"github.com/wtg/shuttletracker/database"
"github.com/wtg/shuttletracker/log"
@@ -129,7 +128,7 @@ func (u *Updater) update() {
vehicleID := strings.Replace(result["id"], "Vehicle ID:", "", -1)
vehicle, err := u.db.GetVehicle(vehicleID)
if err == mgo.ErrNotFound {
if err == database.ErrVehicleNotFound {
log.Warnf("Unknown vehicle ID \"%s\" returned by iTrak. Make sure all vehicles have been added.", vehicleID)
return
} else if err != nil {
@@ -139,7 +138,7 @@ func (u *Updater) update() {
// determine if this is a new update from itrak by comparing timestamps
lastUpdate, err := u.db.GetLastUpdateForVehicle(vehicle.VehicleID)
if err != nil && err != mgo.ErrNotFound {
if err != nil && err != database.ErrUpdateNotFound {
log.WithError(err).Error("Unable to retrieve last update.")
return
}

0 comments on commit 5dc051c

Please sign in to comment.