~mil/mobroute

8aecd9c6d081572553c5952ade5f6308b93597d0 — Miles Alan 2 months ago e2e0eb3
Add better docs and validation for mobsql subcommand
4 files changed, 36 insertions(+), 8 deletions(-)

M api/apioneshot/oneshotapi.go
M api/apioneshot/types.go
M cli/const.go
M cli/mobroutecli.go
M api/apioneshot/oneshotapi.go => api/apioneshot/oneshotapi.go +5 -2
@@ 7,13 7,14 @@ import (
	"git.sr.ht/~mil/mobroute/api/apirtinit"
	"git.sr.ht/~mil/mobroute/api/apirtroute"
	"git.sr.ht/~mil/mobsql"
	"github.com/go-playground/validator/v10"
)

// OneshotRoute performs routing in a one-shot / single function fashion.
// All parameters for the entire request are passed in the OneShotRouteRequest.
func OneshotRoute(r *OneshotRouteRequest) (*apirtroute.RouteResponse, error) {
	if r.RouteParams == nil {
		return nil, errors.New("Must define RouteParams in OneshotRouteRequest")
		return nil, errors.New("Required field RouteParams missing")
	} else if mobrouteRuntime, err := apirtinit.RTInitialize(r.MobrouteRuntimeConfig); err != nil {
		return nil, err
	} else {


@@ 25,7 26,9 @@ func OneshotRoute(r *OneshotRouteRequest) (*apirtroute.RouteResponse, error) {
// API commands. This is essentially just a thin layer to run underlying
// mobsql API fns).
func OneshotMobsql(r *OneshotMobsqlRequest) (any, error) {
	if mobrouteRuntime, err := apirtinit.RTInitialize(r.MobrouteRuntimeConfig); err != nil {
	if err := validator.New(validator.WithRequiredStructEnabled()).Struct(r); err != nil {
		return nil, fmt.Errorf("Validation failed for oneshot mobsql request: %v", err)
	} else if mobrouteRuntime, err := apirtinit.RTInitialize(r.MobrouteRuntimeConfig); err != nil {
		return nil, err
	} else if r.MobsqlParams == nil {
		return nil, errors.New("Mobsql parameters not passed in OneshotMobsqlRequest struct")

M api/apioneshot/types.go => api/apioneshot/types.go +4 -4
@@ 8,7 8,7 @@ import (
// OneShotRouteRequest groups both the MobrouteRuntimeConfig and RouteParams
type OneshotRouteRequest struct {
	MobrouteRuntimeConfig *apirtinit.MobrouteRuntimeConfig `json:"mobroute_runtime_config"`
	RouteParams           *apirtroute.RouteParams          `json:"route_params"`
	RouteParams           *apirtroute.RouteParams          `json:"route_params" validate:"required"`
}

// OneshotMobsqlRequest groups both the MobrouteRuntimeConfig and MobsqlParams


@@ 16,7 16,7 @@ type OneshotRouteRequest struct {
type OneshotMobsqlRequest struct {
	MobrouteRuntimeConfig *apirtinit.MobrouteRuntimeConfig `json:"mobroute_runtime_config"`
	MobsqlParams          *struct {
		Operation string `json:"op"`
		FeedIDs   []int  `json:"feed_ids"`
	} `json:"mobsql_params"`
		Operation string `json:"op" validate:"required,oneof=status load compute purge_gtfs purge_computed"`
		FeedIDs   []int  `json:"feed_ids" validate:"required,gt=0"`
	} `json:"mobsql_params" validate:"required"`
}

M cli/const.go => cli/const.go +15 -1
@@ 6,6 6,15 @@ var (
		"Krakow Routing Example":   `mobroute route -rp '{"route_params":{"feed_ids":[1270],"from":[50.012338,19.88192],"to":[50.08785,20.02387],"transfer_categories":["f","i"],"output_formats":["legs","diagnostics","mapurl","request"]}}'`,
		"NYC Routing Example":      `mobroute route -rp '{"route_params":{"feed_ids":[516],"from":[40.70940,-74.00537],"to":[40.72879,-73.95215],"transfer_categories":["f","i"],"output_formats":["legs","diagnostics","mapurl","request"]}}'`,
	}

	subcmdMobsqlExamples = map[string]string{
		"Determine GTFS Feed Status":                    `mobroute mobsql -mp '{"mobsql_params": {"op": "status", "feed_ids": [767]}}'`,
		"Load a GTFS Feed":                              `mobroute mobsql -mp '{"mobsql_params": {"op": "load", "feed_ids": [767]}}'`,
		"Compute Routing-Optimized GTFS Derived Tables": `mobroute mobsql -mp '{"mobsql_params": {"op": " compute", "feed_ids": [767]}}'`,
		"Purge GTFS Feed Data":                          `mobroute mobsql -mp '{"mobsql_params": {"op": " purge_gtfs", "feed_ids": [767]}}'`,
		"Purge Computed Tables":                         `mobroute mobsql -mp '{"mobsql_params": {"op": " purge_computed", "feed_ids": [767]}}'`,
		"Purge All Tables":                              `mobroute mobsql -mp '{"mobsql_params": {"op": " purge_all", "feed_ids": [767]}}'`,
	}
)

const (


@@ 29,5 38,10 @@ Optional route parameters:
  - walkspeed_km_hr: Ratio of kilometers per hour the user walk (default: 4.5)
`

	subcmdMobsqlDoc = `JSON Parameters for oneshot mobsql subcommand, should be in format of: {}.`
	subcmdMobsqlDoc = `JSON Parameters for oneshot mobsql subcommand, should be in format of: {"mobsql_params": {"feed_ids": [767], "op": "status"}}.

Required mobsql parameters:
  - feed_ids: Array of integer feed IDs (MDBID) to use for mobsql requests
  - op: Operation to perform, one of - ["status", "load", "compute", "purge_gtfs", "purge_computed", "purge_all"]
`
)

M cli/mobroutecli.go => cli/mobroutecli.go +12 -1
@@ 23,6 23,8 @@ func main() {

	// Debug Same usage for all subcommands
	for _, f := range []*flag.FlagSet{flagRouteSet, flagMobsqlSet} {
		var b bool
		f.BoolVar(&b, "h", false, "Show this usage message")
		f.StringVar(&flagBaseDebugCategories, "d", "i", "Verbose debug categories: (i) Information (w) Warn (d) Debug")
		f.StringVar(&flagBaseDebugProfile, "dprof", "", "Write debug pprof CPU profile to file")
	}


@@ 46,7 48,7 @@ func main() {
func cmdRoute(flagRouteSet *flag.FlagSet, flagRouteParams *string) {
	var routeParams mobroute.OneshotRouteRequest
	flagRouteSet.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
		fmt.Fprintf(os.Stderr, "Usage of %s route:\n", os.Args[0])
		flagRouteSet.PrintDefaults()
		fmt.Fprintf(os.Stderr, "\nExamples:\n")
		for exName, exCmd := range subcmdRouteExamples {


@@ 70,6 72,15 @@ func cmdRoute(flagRouteSet *flag.FlagSet, flagRouteParams *string) {

func cmdMobsql(flagMobsqlSet *flag.FlagSet, flagMobsqlParams *string) {
	var mobsqlParams mobroute.OneshotMobsqlRequest
	flagMobsqlSet.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage of %s mobsql:\n", os.Args[0])
		flagMobsqlSet.PrintDefaults()
		fmt.Fprintf(os.Stderr, "\nExamples:\n")
		for exName, exCmd := range subcmdMobsqlExamples {
			fmt.Fprintf(os.Stderr, "  %s:\n    %s\n", exName, exCmd)
		}
		fmt.Fprintf(os.Stderr, "\n")
	}
	flagMobsqlSet.Parse(os.Args[2:])
	if *flagMobsqlParams == "" {
		flagMobsqlSet.Usage()