-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
e5884d2
commit f7f0f56
Showing
13 changed files
with
396 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package builds | ||
|
||
const defaultTimeFilter = "10m" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.