Skip to content

Commit

Permalink
Merge pull request #12 from datasparq-ai/feature/mission-params
Browse files Browse the repository at this point in the history
Mission Params
  • Loading branch information
matt-sparq authored Jan 26, 2024
2 parents 8830ee2 + a8d809c commit 8678bf1
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 17 deletions.
5 changes: 4 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func (a *API) DeleteKey(key string) error {
// - set start time
// - store in database
// - return created ID
func (a *API) CreateMissionFromPlan(key string, planNameOrPlan string, missionId string) (string, error) {
func (a *API) CreateMissionFromPlan(key string, planNameOrPlan string, missionId string, missionParameters map[string]interface{}) (string, error) {

var planBytes []byte

Expand Down Expand Up @@ -343,6 +343,9 @@ func (a *API) CreateMissionFromPlan(key string, planNameOrPlan string, missionId
m.Id = missionId
m.Start = time.Now()

// TODO: set mission parameters
m.Params = missionParameters

// TODO: this could only be a database connection error - these should be retried at least 3 times
err = a.db.Set(key, m.Id, string(m.Bytes()))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestAPI_CompletedMissions(t *testing.T) {
t.Fatalf("New plan has active missions.")
}

missionId, err := api.CreateMissionFromPlan(key, "test-plan", "")
missionId, err := api.CreateMissionFromPlan(key, "test-plan", "", map[string]interface{}{"foo": "bar", "d": map[string]interface{}{"fp": 3}})
if err != nil {
t.Fatalf("Failed to start a mission")
}
Expand Down
2 changes: 1 addition & 1 deletion api/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (a *API) HealthCheck() {

log.Infof("Health check: total memory usage is %v bytes", m.Alloc)
if int64(m.Alloc) > memoryLimitBytes {
log.Warnf("Houston is using more memory that the safe limit; %v out of %v bytes used.", m.Alloc, memoryLimitBytes)
log.Warnf("Houston is using more memory than the safe limit; %v out of %v bytes used.", m.Alloc, memoryLimitBytes)
}

}
Expand Down
2 changes: 1 addition & 1 deletion api/router-missions.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (a *API) PostMission(w http.ResponseWriter, r *http.Request) {

key := r.Header.Get("x-access-key") // key has been checked by checkKey middleware

newMissionId, err := a.CreateMissionFromPlan(key, mission.Plan, mission.Id)
newMissionId, err := a.CreateMissionFromPlan(key, mission.Plan, mission.Id, mission.Params)
if err != nil {
handleError(err, w)
return
Expand Down
8 changes: 4 additions & 4 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func New(key string, baseUrl string) Client {
httpClient := &http.Client{}
_, err := httpClient.Do(req)
if err != nil {
fmt.Println("A base URL (e.g. 'http://localhost:8000/api/v1') was not provided, and no Houston API was not found locally. Provide the base URL with the 'HOUSTON_BASE_URL' environment variable.")
baseUrl = "https://callhouston.io/api/v1"
fmt.Println("A base URL (e.g. 'http://localhost:8000/api/v1') was not provided, and no Houston API was not found locally. Provide the base URL with the 'HOUSTON_BASE_URL' environment variable. You can start a server locally by running: houston api")
os.Exit(1)
}
} else if !(strings.HasPrefix(baseUrl, "http://") || strings.HasPrefix(baseUrl, "https://")) {
fmt.Printf("Base URL '%s' isn't a valid URL; must start with either 'http://' or 'https://'\n", baseUrl)
Expand Down Expand Up @@ -117,12 +117,12 @@ func loadPlan(plan string) (string, error) {

// CreateMission will create a new mission from a plan or plan name. If a plan name is provided then it must correspond
// to a plan saved with SavePlan.
func (client *Client) CreateMission(plan string, id string) (model.MissionCreatedResponse, error) {
func (client *Client) CreateMission(plan string, id string, params map[string]interface{}) (model.MissionCreatedResponse, error) {
plan, err := loadPlan(plan) // if file path was provided then load file, else do nothing
if err != nil {
panic(err)
}
res, err := client.postMissions(model.MissionCreateRequest{Plan: plan, Id: id})
res, err := client.postMissions(model.MissionCreateRequest{Plan: plan, Id: id, Params: params})
return res, err
}

Expand Down
4 changes: 2 additions & 2 deletions client/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package client
import "fmt"

// Start starts a new mission from the plan provided
func Start(plan string, id string, stages []string, exclude []string, skip []string) error {
func Start(plan string, id string, stages []string, exclude []string, skip []string, params map[string]interface{}) error {
client := New("", "")
mission, err := client.CreateMission(plan, id)
mission, err := client.CreateMission(plan, id, params)
if err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions client/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ func HandleCommandLineError(err error) {
" See the docs for a guide on creating keys: https://github.com/datasparq-ai/houston/blob/main/docs/keys.md" + end)
case *model.PlanNotFoundError:
fmt.Println(errorText + err.Error() + end)
case *json.SyntaxError:
fmt.Println(
errorText + "Couldn't parse JSON string. " + err.Error() + end)
default:
fmt.Printf("Unhandled %T\n", err)
panic(err)
}

Expand Down
4 changes: 2 additions & 2 deletions demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func demo(createCmd *cobra.Command) {
fmt.Printf("\u001B[37mcreating a new mission%[2]v\n", s, e)
fmt.Printf(">>> %[1]vhouston start%[2]v \u001B[1m-p apollo -i apollo-11%[2]v\n", s, e)

_, err = a.CreateMissionFromPlan("demo", "apollo", "apollo-11")
_, err = a.CreateMissionFromPlan("demo", "apollo", "apollo-11", map[string]interface{}{"foo": "bar", "biz": map[string]interface{}{"foo": "bar", "biz": 123}})
if err != nil {
panic(err)
}
Expand All @@ -107,7 +107,7 @@ func demo(createCmd *cobra.Command) {
time.Sleep(time.Duration(rand.Float32()*timeBetweenMissions) * time.Millisecond)
//fmt.Printf(">>> %[1]vhouston start%[2]v \u001B[1m-p apollo -i apollo-12%[2]v\n", s, e)
missionId := fmt.Sprintf("apollo-%v", 11+missionCount)
_, err = a.CreateMissionFromPlan("demo", "apollo", missionId)
_, err = a.CreateMissionFromPlan("demo", "apollo", missionId, nil)
if err != nil {
panic(err)
}
Expand Down
15 changes: 13 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"fmt"
"github.com/datasparq-ai/houston/api"
"github.com/datasparq-ai/houston/client"
Expand Down Expand Up @@ -33,7 +34,7 @@ func main() {
Use: "version",
Short: "Print the version number",
Run: func(c *cobra.Command, args []string) {
fmt.Println("v0.6.1")
fmt.Println("v0.7.0")
},
}
return
Expand Down Expand Up @@ -116,14 +117,23 @@ func main() {
var stages = ""
var exclude = ""
var skip = ""
var params = ""
createCmd = &cobra.Command{
Use: "start",
Short: "Create a new mission and trigger the first stage(s)",
Run: func(c *cobra.Command, args []string) {
var parsedParams map[string]interface{}
if params != "" {
err := json.Unmarshal([]byte(params), &parsedParams)
if err != nil {
client.HandleCommandLineError(err)
}
}
err := client.Start(plan, missionId,
strings.Split(strings.Replace(stages, " ", "", -1), ","),
strings.Split(strings.Replace(exclude, " ", "", -1), ","),
strings.Split(strings.Replace(skip, " ", "", -1), ","))
strings.Split(strings.Replace(skip, " ", "", -1), ","),
parsedParams)
if err != nil {
client.HandleCommandLineError(err)
}
Expand All @@ -135,6 +145,7 @@ func main() {
createCmd.Flags().StringVarP(&stages, "stages", "s", "", "Comma separated list of stage names to be used as the starting point for the mission. \nIf not provided, all stages with no upstream stages will be triggered")
createCmd.Flags().StringVarP(&exclude, "exclude", "i", "", "Comma separated list of stage names to be excluded in the new mission")
createCmd.Flags().StringVarP(&skip, "skip", "k", "", "Comma separated list of stage names to be skipped in the new mission")
createCmd.Flags().StringVar(&params, "params", "", "Mission parameters as a JSON string")
return
}())

Expand Down
6 changes: 3 additions & 3 deletions mission/mission.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ type Response struct {

type Mission struct {
Id string `json:"i" name:"id"`
Name string `json:"n" name:"name"` // the plan name, note: not needed in a mission
Services []string `json:"a" name:"services"` // note: not needed in a mission but kept for convenience
Name string `json:"n" name:"name"` // the plan name, note: not needed in a mission
Services []string `json:"a" name:"services"`
Stages []*Stage `json:"s" name:"stages"`
Params map[string]interface{} `json:"p" name:"params"` // note: not needed in a mission but kept for convenience
Params map[string]interface{} `json:"p" name:"params"`
Start time.Time `json:"t" name:"start"`
End time.Time `json:"e" name:"end"`
isComplete bool
Expand Down

0 comments on commit 8678bf1

Please sign in to comment.