Skip to content

Commit

Permalink
26 list jobs (#29)
Browse files Browse the repository at this point in the history
* jobs table working

* builds -> jobs, with builds alias

* ensure jobs with same timestamp are added to job list, remove interface methods that will be implemented in a different PR

* upgrade tau
  • Loading branch information
tafseer-khan authored Aug 16, 2023
1 parent e5884d2 commit f7f0f56
Show file tree
Hide file tree
Showing 13 changed files with 396 additions and 8 deletions.
22 changes: 22 additions & 0 deletions cli/commands/resources/builds/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package builds

import (
"github.com/taubyte/tau-cli/cli/common"
"github.com/taubyte/tau-cli/cli/common/options"
"github.com/taubyte/tau-cli/env"
"github.com/taubyte/tau-cli/i18n"
"github.com/urfave/cli/v2"
)

func (link) Base() (*cli.Command, []common.Option) {
selected, err := env.GetSelectedProject()
if err != nil {
selected = "selected"
}

return common.Base(&cli.Command{
Name: "builds",
Aliases: []string{"jobs"},
ArgsUsage: i18n.ArgsUsageName,
}, options.NameFlagSelectedArg0(selected))
}
115 changes: 115 additions & 0 deletions cli/commands/resources/builds/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package builds

import (
"time"

"github.com/jedib0t/go-pretty/v6/table"
"github.com/taubyte/go-interfaces/services/patrick"
schemaCommon "github.com/taubyte/go-project-schema/common"
"github.com/taubyte/tau-cli/cli/common"
projectLib "github.com/taubyte/tau-cli/lib/project"
"github.com/taubyte/tau-cli/prompts"
authClient "github.com/taubyte/tau-cli/singletons/auth_client"
patrickClient "github.com/taubyte/tau-cli/singletons/patrick_client"
buildsTable "github.com/taubyte/tau-cli/table/builds"
"github.com/taubyte/tau-cli/validate"
"github.com/urfave/cli/v2"
)

func queryOrList() common.Command {
return common.Create(
&cli.Command{
Flags: []cli.Flag{
&cli.StringFlag{
Name: "time",
Aliases: []string{"t"},
Usage: "filters jobs by time range",
DefaultText: defaultTimeFilter,
},
&cli.BoolFlag{
Name: "defaulttime",
Aliases: []string{"dt"},
Usage: "use default time filter (10m)",
},
},
Action: query,
},
)
}

func (link) Query() common.Command {
return queryOrList()
}

func (link) List() common.Command {
return queryOrList()
}

func query(ctx *cli.Context) error {
prj, err := projectLib.SelectedProjectInterface()
if err != nil {
return err
}

patrickC, err := patrickClient.Load()
if err != nil {
return err
}

authC, err := authClient.Load()
if err != nil {
return err
}

jobIds, err := patrickC.Jobs(prj.Get().Id())
if err != nil {
// use i18n
return err
}

timeRange := defaultTimeFilter
if !ctx.Bool("defaulttime") {
timeRange = prompts.GetOrRequireAString(ctx, "time", "Job time range", validate.Time, defaultTimeFilter)
}

tRange, err := schemaCommon.StringToTime(timeRange)
if err != nil {
return err
}

tRangeNano := time.Duration(tRange) * time.Nanosecond
rangeEnd := time.Now().Unix() - int64(tRangeNano.Seconds())

// index string for unique jobs, int64 to order by time
jobMap := make(map[string]map[int64]*patrick.Job, len(jobIds))
for _, id := range jobIds {
job, err := patrickC.Job(id)
if err != nil {
// use i18n
return err
}

if job.Timestamp >= rangeEnd {
jobMap[id] = make(map[int64]*patrick.Job, 1)
jobMap[id][job.Timestamp] = job
}
}

// separate keys from original for loop to ensure unique values
keys := make([]int64, 0, len(jobIds))
for _, v := range jobMap {
for key, _ := range v {
keys = append(keys, key)
}
}

t, err := buildsTable.ListNoRender(authC, jobMap, keys)
if err != nil {
return err
}

t.SetStyle(table.StyleLight)
t.Render()

return nil
}
13 changes: 13 additions & 0 deletions cli/commands/resources/builds/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package builds

import (
"github.com/taubyte/tau-cli/cli/common"
)

type link struct {
common.UnimplementedBasic
}

func New() common.Basic {
return link{}
}
3 changes: 3 additions & 0 deletions cli/commands/resources/builds/vars.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package builds

const defaultTimeFilter = "10m"
2 changes: 2 additions & 0 deletions cli/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/taubyte/tau-cli/cli/commands/exit"
"github.com/taubyte/tau-cli/cli/commands/login"
"github.com/taubyte/tau-cli/cli/commands/resources/application"
"github.com/taubyte/tau-cli/cli/commands/resources/builds"
"github.com/taubyte/tau-cli/cli/commands/resources/database"
"github.com/taubyte/tau-cli/cli/commands/resources/domain"
"github.com/taubyte/tau-cli/cli/commands/resources/function"
Expand Down Expand Up @@ -72,6 +73,7 @@ func New() (*cli.App, error) {
smartops.New,
storage.New,
website.New,
builds.New,
)

app.Commands = append(app.Commands, []*cli.Command{
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/taubyte/go-simple-git v0.2.5
github.com/taubyte/go-specs v0.10.6
github.com/taubyte/http v0.10.4
github.com/taubyte/tau v1.0.7
github.com/taubyte/tau v1.0.8
github.com/taubyte/utils v0.1.6
)

Expand Down Expand Up @@ -119,6 +119,8 @@ require (
lukechampine.com/blake3 v1.2.1 // indirect
)

require github.com/taubyte/go-interfaces v0.2.12

require (
atomicgo.dev/schedule v0.0.2 // indirect
github.com/DataDog/zstd v1.5.2 // indirect
Expand Down Expand Up @@ -230,7 +232,6 @@ require (
github.com/raulk/go-watchdog v1.3.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/taubyte/domain-validation v1.0.0 // indirect
github.com/taubyte/go-interfaces v0.2.12 // indirect
github.com/taubyte/p2p v0.9.1 // indirect
github.com/whyrusleeping/cbor-gen v0.0.0-20230126041949-52956bd4c9aa // indirect
github.com/whyrusleeping/chunker v0.0.0-20181014151217-fe64bd25879f // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -889,8 +889,8 @@ github.com/taubyte/http v0.10.4 h1:Lvr43DYHamFlyr0ijNJrYDl0BAWeaMMQJdtnRQmKWNY=
github.com/taubyte/http v0.10.4/go.mod h1:hSJYC7/yqRkrsZYe1Q0c194GKkVD0KGWDzw0sC+SAiU=
github.com/taubyte/p2p v0.9.1 h1:W8whhWbYO5oCqOSUM+gNKCERtVGmTJHTZuTw+MJuswg=
github.com/taubyte/p2p v0.9.1/go.mod h1:0vWZjiX8yNT3VwZO2x+mgRdEDCbwzSPOyyfBU3UEiTk=
github.com/taubyte/tau v1.0.7 h1:QvPCv+Se10zRalF945nnQUO34OvP4S07L45ezOrU8Gs=
github.com/taubyte/tau v1.0.7/go.mod h1:whK+Kbau34XJISRErdAMzCiyR4RvFJalVundPRVY4Eg=
github.com/taubyte/tau v1.0.8 h1:gScrZei6Um8vVQOJ4alcfOKOFROpNdApe6zWC/AtpHA=
github.com/taubyte/tau v1.0.8/go.mod h1:whK+Kbau34XJISRErdAMzCiyR4RvFJalVundPRVY4Eg=
github.com/taubyte/utils v0.1.6 h1:bRSJZW/8E/wpT3wwyU3C97M9ddAJ52AfXFeP47JRGsY=
github.com/taubyte/utils v0.1.6/go.mod h1:b/tKY1xKIxLH5ixFRTsrdz+vGhmiOcR9SpdVYiJDpsI=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
Expand Down
8 changes: 8 additions & 0 deletions i18n/singletons/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ func LoadingAuthClientFailed(err error) error {
return fmt.Errorf(loadingAuthClientFailed, err)
}

func CreatingPatrickClientFailed(err error) error {
return fmt.Errorf(creatingAuthClientFailed, err)
}

func LoadingPatrickClientFailed(err error) error {
return fmt.Errorf(loadingAuthClientFailed, err)
}

func NoNetworkSelected() error {
return fmt.Errorf(noNetworkSelected)
}
7 changes: 6 additions & 1 deletion lib/project/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ func SelectedProjectConfig() (configProject config.Project, err error) {
return
}

return config.Projects().Get(selectedProject)
configProject, err = config.Projects().Get(selectedProject)
if err != nil {
i18n.Help().BeSureToCloneProject()
}

return
}

func ConfirmSelectedProject() error {
Expand Down
6 changes: 3 additions & 3 deletions singletons/auth_client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/taubyte/tau-cli/singletons/dreamland"
"github.com/taubyte/tau-cli/singletons/session"
"github.com/taubyte/tau-cli/states"
"github.com/taubyte/tau/clients/http"
client "github.com/taubyte/tau/clients/http/auth"
)

Expand Down Expand Up @@ -79,9 +80,8 @@ func loadClient() (config.Profile, *client.Client, error) {

client, err := client.New(
states.Context,
client.URL(url),
client.Auth(profile.Token),
client.Provider(profile.Provider),
http.URL(url),
http.Auth(profile.Token),
)
if err != nil {
return profile, nil, singletonsI18n.CreatingAuthClientFailed(err)
Expand Down
114 changes: 114 additions & 0 deletions singletons/patrick_client/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package patrickClient

import (
"context"
"fmt"
"strings"
"time"

"github.com/taubyte/tau-cli/common"
"github.com/taubyte/tau-cli/env"
"github.com/taubyte/tau-cli/i18n"
networkI18n "github.com/taubyte/tau-cli/i18n/network"
singletonsI18n "github.com/taubyte/tau-cli/i18n/singletons"
loginLib "github.com/taubyte/tau-cli/lib/login"
"github.com/taubyte/tau-cli/singletons/config"
"github.com/taubyte/tau-cli/singletons/dreamland"
"github.com/taubyte/tau-cli/singletons/session"
"github.com/taubyte/tau-cli/states"
"github.com/taubyte/tau/clients/http"
client "github.com/taubyte/tau/clients/http/patrick"
)

var _client *client.Client

func Clear() {
_client = nil
}

func getClientUrl() (url string, err error) {
profile, err := loginLib.GetSelectedProfile()
if err != nil {
return "", err
}

switch profile.NetworkType {
case common.DreamlandNetwork:
url = fmt.Sprintf("http://localhost:%d", getDreamlandPatrickPort())
case common.RemoteNetwork:
url = fmt.Sprintf("https://patrick.tau.%s", profile.Network)
default:
err = networkI18n.ErrorUnknownNetwork(profile.NetworkType)
}

return
}

func loadClient() (config.Profile, *client.Client, error) {
profileName, exist := session.Get().ProfileName()
if !exist {
// Check for a default if no profiles are selected
profileName, _, _ = loginLib.GetProfiles()
if len(profileName) == 0 {
i18n.Help().HaveYouLoggedIn()
return config.Profile{}, nil, singletonsI18n.ProfileDoesNotExist()
}
}

profile, err := config.Profiles().Get(profileName)
if err != nil {
return config.Profile{}, nil, err
}

selectedNetwork, _ := env.GetSelectedNetwork()
if selectedNetwork == "" {
i18n.Help().HaveYouSelectedANetwork()
return config.Profile{}, nil, singletonsI18n.NoNetworkSelected()
}

url, err := getClientUrl()
if err != nil {
return config.Profile{}, nil, err
}

client, err := client.New(
states.Context,
http.URL(url),
http.Auth(profile.Token),
)
if err != nil {
return profile, nil, singletonsI18n.CreatingPatrickClientFailed(err)
}

return profile, client, nil
}

func getDreamlandPatrickPort() int {
ctx, ctxC := context.WithTimeout(context.Background(), 30*time.Second)
defer ctxC()

dreamClient, err := dreamland.Client(ctx)
if err != nil {
return 0
}

selectedUniverse, _ := env.GetCustomNetworkUrl()
universe := dreamClient.Universe(selectedUniverse)
echart, err := universe.Status()
if err != nil {
return 0
}

for _, node := range echart.Nodes {
if strings.Contains(node.Name, "patrick") {
httpPort, ok := node.Value["http"]
if !ok {
return 0
}

return httpPort
}
}

return 0
}
19 changes: 19 additions & 0 deletions singletons/patrick_client/load.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package patrickClient

import (
singletonsI18n "github.com/taubyte/tau-cli/i18n/singletons"
patrickClient "github.com/taubyte/tau/clients/http/patrick"
)

func Load() (*patrickClient.Client, error) {
if _client == nil {
_, client, err := loadClient()
if err != nil {
return nil, singletonsI18n.LoadingAuthClientFailed(err)
}

_client = client
}

return _client, nil
}
Loading

0 comments on commit f7f0f56

Please sign in to comment.