Skip to content

Commit

Permalink
Merge pull request #84 from trickest/update/api
Browse files Browse the repository at this point in the history
Update API endpoints data structure
mhmdiaa authored Apr 25, 2023
2 parents 7335b48 + b98d7b7 commit ccea0cc
Showing 12 changed files with 124 additions and 131 deletions.
11 changes: 6 additions & 5 deletions cmd/execute/config.go
Original file line number Diff line number Diff line change
@@ -3,14 +3,15 @@ package execute
import (
"errors"
"fmt"
"gopkg.in/yaml.v3"
"io/ioutil"
"os"
"path"
"strconv"
"strings"
"trickest-cli/cmd/output"
"trickest-cli/types"

"gopkg.in/yaml.v3"
)

func readConfig(fileName string, wfVersion *types.WorkflowVersionDetailed, tool *types.Tool) (
@@ -510,13 +511,13 @@ func readConfigOutputs(config *map[string]interface{}) map[string]output.NodeInf
return downloadNodes
}

func readConfigMachines(config *map[string]interface{}, isTool bool, maximumMachines *types.Bees) *types.Bees {
func readConfigMachines(config *map[string]interface{}, isTool bool, maximumMachines *types.Machines) *types.Machines {
if !isTool && maximumMachines == nil {
fmt.Println("No maximum machines specified!")
os.Exit(0)
}

execMachines := &types.Bees{}
execMachines := &types.Machines{}
if machines, exists := (*config)["machines"]; exists && machines != nil {
machinesList, ok := machines.(map[string]interface{})
if !ok {
@@ -610,9 +611,9 @@ func readConfigMachines(config *map[string]interface{}, isTool bool, maximumMach
if isTool {
oneMachine := 1
if maxMachines {
return &types.Bees{Large: &oneMachine}
return &types.Machines{Large: &oneMachine}
} else {
return &types.Bees{Small: &oneMachine}
return &types.Machines{Small: &oneMachine}
}
} else {
if maxMachines {
13 changes: 7 additions & 6 deletions cmd/execute/execute.go
Original file line number Diff line number Diff line change
@@ -3,7 +3,6 @@ package execute
import (
"errors"
"fmt"
"github.com/google/uuid"
"io/ioutil"
"math"
"os"
@@ -17,6 +16,8 @@ import (
"trickest-cli/types"
"trickest-cli/util"

"github.com/google/uuid"

"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)
@@ -26,8 +27,8 @@ var (
configFile string
watch bool
showParams bool
executionMachines types.Bees
hive *types.Hive
executionMachines types.Machines
fleet *types.Fleet
nodesToDownload = make(map[string]output.NodeInfo, 0)
allNodes map[string]*types.TreeNode
roots []*types.TreeNode
@@ -63,8 +64,8 @@ var ExecuteCmd = &cobra.Command{
}
}

hive = util.GetHiveInfo()
if hive == nil {
fleet = util.GetFleetInfo()
if fleet == nil {
return
}
var version *types.WorkflowVersionDetailed
@@ -755,7 +756,7 @@ func readWorkflowYAMLandCreateVersion(fileName string, workflowName string, obje
}

func createToolWorkflow(wfName string, space *types.SpaceDetailed, project *types.Project, deleteProjectOnError bool,
tool *types.Tool, primitiveNodes map[string]*types.PrimitiveNode, machine types.Bees) *types.WorkflowVersionDetailed {
tool *types.Tool, primitiveNodes map[string]*types.PrimitiveNode, machine types.Machines) *types.WorkflowVersionDetailed {
if tool == nil {
fmt.Println("No tool specified, couldn't create a workflow!")
os.Exit(0)
54 changes: 27 additions & 27 deletions cmd/execute/helpers.go
Original file line number Diff line number Diff line change
@@ -97,11 +97,11 @@ func getScripts(pageSize int, search string, name string) []types.Script {
return scripts.Results
}

func createRun(versionID uuid.UUID, watch bool, machines *types.Bees, outputNodes []string, outputsDir string) {
func createRun(versionID uuid.UUID, watch bool, machines *types.Machines, outputNodes []string, outputsDir string) {
run := types.CreateRun{
VersionID: versionID,
HiveInfo: hive.ID,
Bees: executionMachines,
Vault: fleet.Vault,
Machines: executionMachines,
}

data, err := json.Marshal(run)
@@ -110,7 +110,7 @@ func createRun(versionID uuid.UUID, watch bool, machines *types.Bees, outputNode
os.Exit(0)
}

resp := request.Trickest.Post().Body(data).DoF("run/")
resp := request.Trickest.Post().Body(data).DoF("execution/")
if resp == nil {
fmt.Println("Error: Couldn't create run!")
os.Exit(0)
@@ -136,10 +136,10 @@ func createRun(versionID uuid.UUID, watch bool, machines *types.Bees, outputNode
if watch {
WatchRun(createRunResp.ID, outputsDir, nodesToDownload, nil, false, &executionMachines, showParams)
} else {
availableBees := GetAvailableMachines()
availableMachines := GetAvailableMachines()
fmt.Println("Run successfully created! ID: " + createRunResp.ID.String())
fmt.Print("Machines:\n" + FormatMachines(*machines, false))
fmt.Print("\nAvailable:\n" + FormatMachines(availableBees, false))
fmt.Print("\nAvailable:\n" + FormatMachines(availableMachines, false))
}
}

@@ -355,7 +355,7 @@ func processInvalidInputStructure() {
os.Exit(0)
}

func processMaxMachinesOverflow(maximumMachines types.Bees) {
func processMaxMachinesOverflow(maximumMachines types.Machines) {
fmt.Println("Invalid number or machines!")
fmt.Println("The maximum number of machines you can allocate for this workflow: ")
fmt.Println(FormatMachines(maximumMachines, false))
@@ -381,28 +381,28 @@ func processInvalidInputType(newPNode, existingPNode types.PrimitiveNode) {
os.Exit(0)
}

func GetAvailableMachines() types.Bees {
hiveInfo := util.GetHiveInfo()
availableBees := types.Bees{}
for _, bee := range hiveInfo.Bees {
if bee.Name == "small" {
available := bee.Total - bee.Running
availableBees.Small = &available
func GetAvailableMachines() types.Machines {
hiveInfo := util.GetFleetInfo()
availableMachines := types.Machines{}
for _, machine := range hiveInfo.Machines {
if machine.Name == "small" {
available := machine.Total - machine.Running
availableMachines.Small = &available
}
if bee.Name == "medium" {
available := bee.Total - bee.Running
availableBees.Medium = &available
if machine.Name == "medium" {
available := machine.Total - machine.Running
availableMachines.Medium = &available
}
if bee.Name == "large" {
available := bee.Total - bee.Running
availableBees.Large = &available
if machine.Name == "large" {
available := machine.Total - machine.Running
availableMachines.Large = &available
}
}
return availableBees
return availableMachines
}

func GetRunByID(id uuid.UUID) *types.Run {
resp := request.Trickest.Get().DoF("run/%s/", id)
resp := request.Trickest.Get().DoF("execution/%s/", id)
if resp == nil {
fmt.Println("Error: Couldn't get run!")
os.Exit(0)
@@ -427,7 +427,7 @@ func GetSubJobs(runID uuid.UUID) []types.SubJob {
fmt.Println("Couldn't list sub-jobs, no run ID parameter specified!")
os.Exit(0)
}
urlReq := "subjob/?run=" + runID.String()
urlReq := "subjob/?execution=" + runID.String()
urlReq = urlReq + "&page_size=" + strconv.Itoa(math.MaxInt)

resp := request.Trickest.Get().DoF(urlReq)
@@ -451,7 +451,7 @@ func GetSubJobs(runID uuid.UUID) []types.SubJob {
}

func stopRun(runID uuid.UUID) {
resp := request.Trickest.Post().DoF("run/%s/stop/", runID)
resp := request.Trickest.Post().DoF("execution/%s/stop/", runID)
if resp == nil {
fmt.Println("Error: Couldn't stop run!")
os.Exit(0)
@@ -462,7 +462,7 @@ func stopRun(runID uuid.UUID) {
}
}

func setMachinesToMinimum(machines *types.Bees) {
func setMachinesToMinimum(machines *types.Machines) {
if machines.Small != nil {
*machines.Small = 1
}
@@ -474,7 +474,7 @@ func setMachinesToMinimum(machines *types.Bees) {
}
}

func FormatMachines(machines types.Bees, inline bool) string {
func FormatMachines(machines types.Machines, inline bool) string {
var small, medium, large string
if machines.Small != nil {
small = "small: " + strconv.Itoa(*machines.Small)
@@ -580,7 +580,7 @@ func uploadFilesIfNeeded(primitiveNodes map[string]*types.PrimitiveNode) {
}
}

func maxMachinesTypeCompatible(machines, maxMachines types.Bees) bool {
func maxMachinesTypeCompatible(machines, maxMachines types.Machines) bool {
if (machines.Small != nil && maxMachines.Small == nil) ||
(machines.Medium != nil && maxMachines.Medium == nil) ||
(machines.Large != nil && maxMachines.Large == nil) {
38 changes: 19 additions & 19 deletions cmd/execute/layout.go
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ func adjustChildrenHeight(root *types.TreeNode, nodesPerHeight *map[int][]*types
child.Height = root.Height - 1
found := false
for _, node := range (*nodesPerHeight)[child.Height] {
if node.NodeName == child.NodeName {
if node.Name == child.Name {
found = true
break
}
@@ -104,8 +104,8 @@ func generateNodesCoordinates(version *types.WorkflowVersionDetailed) {
for height, nodes := range nodesPerHeight {
maxInputs := 0
for _, node := range nodes {
if version.Data.Nodes[node.NodeName] != nil && len(version.Data.Nodes[node.NodeName].Inputs) > maxInputs {
maxInputs = len(version.Data.Nodes[node.NodeName].Inputs)
if version.Data.Nodes[node.Name] != nil && len(version.Data.Nodes[node.Name].Inputs) > maxInputs {
maxInputs = len(version.Data.Nodes[node.Name].Inputs)
}
}
maxInputsPerHeight[height] = maxInputs
@@ -116,7 +116,7 @@ func generateNodesCoordinates(version *types.WorkflowVersionDetailed) {
for height := 0; height < len(nodesPerHeight); height++ {
nodes := nodesPerHeight[height]
sort.SliceStable(nodes, func(i, j int) bool {
return nodes[i].NodeName < nodes[j].NodeName
return nodes[i].Name < nodes[j].Name
})
total := (len(nodes) - 1) * distance
start := -total / 2
@@ -126,31 +126,31 @@ func generateNodesCoordinates(version *types.WorkflowVersionDetailed) {
previousHeightNodeSizeIndent = float64(distance * (maxInputsPerHeight[height-1] / 10))
}
for i, node := range nodes {
if version.Data.Nodes[node.NodeName] != nil {
version.Data.Nodes[node.NodeName].Meta.Coordinates.X = X
if version.Data.Nodes[node.Name] != nil {
version.Data.Nodes[node.Name].Meta.Coordinates.X = X
if i == 0 && height > 0 {
version.Data.Nodes[node.NodeName].Meta.Coordinates.X += nodeSizeIndent
version.Data.Nodes[node.Name].Meta.Coordinates.X += nodeSizeIndent
}
version.Data.Nodes[node.NodeName].Meta.Coordinates.X += previousHeightNodeSizeIndent
version.Data.Nodes[node.NodeName].Meta.Coordinates.Y = 1.2 * float64(start)
version.Data.Nodes[node.Name].Meta.Coordinates.X += previousHeightNodeSizeIndent
version.Data.Nodes[node.Name].Meta.Coordinates.Y = 1.2 * float64(start)
start += distance
if i+1 < len(nodes) && version.Data.Nodes[nodes[i+1].NodeName] != nil &&
len(version.Data.Nodes[nodes[i+1].NodeName].Inputs) == maxInputsPerHeight[height] {
if i+1 < len(nodes) && version.Data.Nodes[nodes[i+1].Name] != nil &&
len(version.Data.Nodes[nodes[i+1].Name].Inputs) == maxInputsPerHeight[height] {
start += int(nodeSizeIndent)
}
if len(version.Data.Nodes[node.NodeName].Inputs) == maxInputsPerHeight[height] {
if len(version.Data.Nodes[node.Name].Inputs) == maxInputsPerHeight[height] {
start += int(nodeSizeIndent)
}
} else if version.Data.PrimitiveNodes[node.NodeName] != nil {
version.Data.PrimitiveNodes[node.NodeName].Coordinates.X = X
} else if version.Data.PrimitiveNodes[node.Name] != nil {
version.Data.PrimitiveNodes[node.Name].Coordinates.X = X
if i == 0 && height > 0 {
version.Data.PrimitiveNodes[node.NodeName].Coordinates.X += nodeSizeIndent
version.Data.PrimitiveNodes[node.Name].Coordinates.X += nodeSizeIndent
}
version.Data.PrimitiveNodes[node.NodeName].Coordinates.X += previousHeightNodeSizeIndent
version.Data.PrimitiveNodes[node.NodeName].Coordinates.Y = 1.2 * float64(start)
version.Data.PrimitiveNodes[node.Name].Coordinates.X += previousHeightNodeSizeIndent
version.Data.PrimitiveNodes[node.Name].Coordinates.Y = 1.2 * float64(start)
start += distance
if i+1 < len(nodes) && version.Data.Nodes[nodes[i+1].NodeName] != nil &&
len(version.Data.Nodes[nodes[i+1].NodeName].Inputs) == maxInputsPerHeight[height] {
if i+1 < len(nodes) && version.Data.Nodes[nodes[i+1].Name] != nil &&
len(version.Data.Nodes[nodes[i+1].Name].Inputs) == maxInputsPerHeight[height] {
start += int(nodeSizeIndent)
}
}
28 changes: 15 additions & 13 deletions cmd/execute/watch.go
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ import (
"github.com/xlab/treeprint"
)

func WatchRun(runID uuid.UUID, downloadPath string, nodesToDownload map[string]output.NodeInfo, filesToDownload []string, timestampOnly bool, machines *types.Bees, showParameters bool) {
func WatchRun(runID uuid.UUID, downloadPath string, nodesToDownload map[string]output.NodeInfo, filesToDownload []string, timestampOnly bool, machines *types.Machines, showParameters bool) {
const fmtStr = "%-12s %v\n"
writer := uilive.New()
writer.Start()
@@ -70,9 +70,9 @@ func WatchRun(runID uuid.UUID, downloadPath string, nodesToDownload map[string]o
out := ""
out += fmt.Sprintf(fmtStr, "Name:", run.WorkflowName)
out += fmt.Sprintf(fmtStr, "Status:", strings.ToLower(run.Status))
availableBees := GetAvailableMachines()
availableMachines := GetAvailableMachines()
out += fmt.Sprintf(fmtStr, "Machines:", FormatMachines(*machines, true)+
" (currently available: "+FormatMachines(availableBees, true)+")")
" (currently available: "+FormatMachines(availableMachines, true)+")")
out += fmt.Sprintf(fmtStr, "Created:", run.CreatedDate.In(time.Local).Format(time.RFC1123)+
" ("+util.FormatDuration(time.Since(run.CreatedDate))+" ago)")
if run.Status != "PENDING" {
@@ -94,12 +94,14 @@ func WatchRun(runID uuid.UUID, downloadPath string, nodesToDownload map[string]o

subJobs := GetSubJobs(runID)
for _, sj := range subJobs {
allNodes[sj.NodeName].Status = strings.ToLower(sj.Status)
allNodes[sj.NodeName].OutputStatus = strings.ReplaceAll(strings.ToLower(sj.OutputsStatus), "_", " ")
allNodes[sj.Name].Status = strings.ToLower(sj.Status)
allNodes[sj.Name].OutputStatus = strings.ReplaceAll(strings.ToLower(sj.OutputsStatus), "_", " ")
if sj.Finished {
allNodes[sj.NodeName].Duration = sj.FinishedDate.Sub(sj.StartedDate).Round(time.Second)
allNodes[sj.Name].Duration = sj.FinishedDate.Sub(sj.StartedDate).Round(time.Second)
} else if sj.StartedDate.IsZero() {
allNodes[sj.Name].Duration = *new(time.Duration)
} else {
allNodes[sj.NodeName].Duration = time.Since(sj.StartedDate).Round(time.Second)
allNodes[sj.Name].Duration = time.Since(sj.StartedDate).Round(time.Second)
}
}

@@ -185,7 +187,7 @@ func printTree(node *types.TreeNode, branch *treeprint.Tree, allNodes *map[strin
prefixSymbol = "\u274c " //❌
}

printValue := prefixSymbol + node.Label + " (" + node.NodeName + ")"
printValue := prefixSymbol + node.Label + " (" + node.Name + ")"
if branch == nil {
tree := treeprint.NewWithRoot(printValue)
branch = &tree
@@ -231,12 +233,12 @@ func printTree(node *types.TreeNode, branch *treeprint.Tree, allNodes *map[strin
}

for _, child := range node.Children {
if !(*allNodes)[node.NodeName].Printed {
if !(*allNodes)[node.Name].Printed {
printTree(child, branch, allNodes, showParameters)
}
}

(*allNodes)[node.NodeName].Printed = true
(*allNodes)[node.Name].Printed = true

return (*branch).String()
}
@@ -247,7 +249,7 @@ func CreateTrees(wfVersion *types.WorkflowVersionDetailed, includePrimitiveNodes

for _, node := range wfVersion.Data.Nodes {
allNodes[node.Name] = &types.TreeNode{
NodeName: node.Name,
Name: node.Name,
Label: node.Meta.Label,
Inputs: &node.Inputs,
Status: "pending",
@@ -260,8 +262,8 @@ func CreateTrees(wfVersion *types.WorkflowVersionDetailed, includePrimitiveNodes
if includePrimitiveNodes {
for _, node := range wfVersion.Data.PrimitiveNodes {
allNodes[node.Name] = &types.TreeNode{
NodeName: node.Name,
Label: node.Label,
Name: node.Name,
Label: node.Label,
}
}
}
2 changes: 1 addition & 1 deletion cmd/get/get.go
Original file line number Diff line number Diff line change
@@ -64,7 +64,7 @@ var GetCmd = &cobra.Command{
if runs[0].Status == "COMPLETED" && runs[0].CompletedDate.IsZero() {
runs[0].Status = "RUNNING"
}
execute.WatchRun(runs[0].ID, "", map[string]output.NodeInfo{}, []string{}, !watch, &runs[0].Bees, showNodeParams)
execute.WatchRun(runs[0].ID, "", map[string]output.NodeInfo{}, []string{}, !watch, &runs[0].Machines, showNodeParams)
return
} else {
const fmtStr = "%-15s %v\n"
Loading

0 comments on commit ccea0cc

Please sign in to comment.