Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature lookup #131

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions cmd/humanlog/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func environmentCmd(
auth.Interceptors(ll, tokenSource)...,
)
orgClient := organizationv1connect.NewOrganizationServiceClient(httpClient, apiURL, clOpts)
iter := ListEnvironments(ctx, orgID, orgClient)
iter := ListEnvironments(ctx, orgClient)
a, ok, err := iterapi.Find(iter, func(el *organizationv1.ListEnvironmentResponse_ListItem) bool {
return el.Environment.Id == environmentID
})
Expand All @@ -126,7 +126,7 @@ func environmentCmd(
apiURL := getAPIUrl(cctx)
httpClient := getHTTPClient(cctx, apiURL)

orgID, err := ensureOrgSelected(ctx, ll, cctx, state, tokenSource, apiURL, httpClient)
_, err := ensureOrgSelected(ctx, ll, cctx, state, tokenSource, apiURL, httpClient)
if err != nil {
return err
}
Expand All @@ -136,9 +136,7 @@ func environmentCmd(
)
orgClient := organizationv1connect.NewOrganizationServiceClient(httpClient, apiURL, clOpts)

res, err := orgClient.CreateEnvironment(ctx, connect.NewRequest(&organizationv1.CreateEnvironmentRequest{
OrganizationId: orgID,
}))
res, err := orgClient.CreateEnvironment(ctx, connect.NewRequest(&organizationv1.CreateEnvironmentRequest{}))
if err != nil {
return err
}
Expand All @@ -165,7 +163,7 @@ func environmentCmd(
return cli.ShowSubcommandHelp(cctx)
}

orgID, err := ensureOrgSelected(ctx, ll, cctx, state, tokenSource, apiURL, httpClient)
_, err := ensureOrgSelected(ctx, ll, cctx, state, tokenSource, apiURL, httpClient)
if err != nil {
return err
}
Expand All @@ -175,7 +173,7 @@ func environmentCmd(
)
orgClient := organizationv1connect.NewOrganizationServiceClient(httpClient, apiURL, clOpts)

el, ok, err := iterapi.Find(ListEnvironments(ctx, orgID, orgClient), func(el *organizationv1.ListEnvironmentResponse_ListItem) bool {
el, ok, err := iterapi.Find(ListEnvironments(ctx, orgClient), func(el *organizationv1.ListEnvironmentResponse_ListItem) bool {
return el.Environment.Name == environmentName
})
if err != nil {
Expand Down Expand Up @@ -204,12 +202,12 @@ func environmentCmd(
)
orgClient := organizationv1connect.NewOrganizationServiceClient(httpClient, apiURL, clOpts)

orgID, err := ensureOrgSelected(ctx, ll, cctx, state, tokenSource, apiURL, httpClient)
_, err := ensureOrgSelected(ctx, ll, cctx, state, tokenSource, apiURL, httpClient)
if err != nil {
return err
}

iter := ListEnvironments(ctx, orgID, orgClient)
iter := ListEnvironments(ctx, orgClient)

for iter.Next() {
li := iter.Current()
Expand Down
16 changes: 7 additions & 9 deletions cmd/humanlog/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ func ensureEnvironmentSelected(

var options []huh.Option[*typesv1.Environment]
client := organizationv1connect.NewOrganizationServiceClient(httpClient, apiURL, clOpts)
iter := ListEnvironments(ctx, orgID, client)
iter := ListEnvironments(ctx, client)
for iter.Next() {
item := iter.Current().Environment
options = append(options, huh.NewOption(item.Name, item))
Expand Down Expand Up @@ -296,12 +296,11 @@ func ListOrganizations(ctx context.Context, client userv1connect.UserServiceClie
})
}

func ListOrgUser(ctx context.Context, orgID int64, client organizationv1connect.OrganizationServiceClient) *iterapi.Iter[*organizationv1.ListUserResponse_ListItem] {
func ListOrgUser(ctx context.Context, client organizationv1connect.OrganizationServiceClient) *iterapi.Iter[*organizationv1.ListUserResponse_ListItem] {
return iterapi.New(ctx, 100, func(ctx context.Context, cursor *typesv1.Cursor, limit int32) ([]*organizationv1.ListUserResponse_ListItem, *typesv1.Cursor, error) {
list, err := client.ListUser(ctx, connect.NewRequest(&organizationv1.ListUserRequest{
Cursor: cursor,
Limit: limit,
OrganizationId: orgID,
Cursor: cursor,
Limit: limit,
}))
if err != nil {
return nil, nil, err
Expand All @@ -310,12 +309,11 @@ func ListOrgUser(ctx context.Context, orgID int64, client organizationv1connect.
})
}

func ListEnvironments(ctx context.Context, orgID int64, client organizationv1connect.OrganizationServiceClient) *iterapi.Iter[*organizationv1.ListEnvironmentResponse_ListItem] {
func ListEnvironments(ctx context.Context, client organizationv1connect.OrganizationServiceClient) *iterapi.Iter[*organizationv1.ListEnvironmentResponse_ListItem] {
return iterapi.New(ctx, 100, func(ctx context.Context, cursor *typesv1.Cursor, limit int32) ([]*organizationv1.ListEnvironmentResponse_ListItem, *typesv1.Cursor, error) {
list, err := client.ListEnvironment(ctx, connect.NewRequest(&organizationv1.ListEnvironmentRequest{
Cursor: cursor,
Limit: limit,
OrganizationId: orgID,
Cursor: cursor,
Limit: limit,
}))
if err != nil {
return nil, nil, err
Expand Down
8 changes: 8 additions & 0 deletions cmd/humanlog/localhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
"sync"
"time"

"connectrpc.com/connect"
connectcors "connectrpc.com/cors"

"github.com/humanlogio/api/go/svc/feature/v1/featurev1connect"
"github.com/humanlogio/api/go/svc/ingest/v1/ingestv1connect"
"github.com/humanlogio/api/go/svc/localhost/v1/localhostv1connect"
"github.com/humanlogio/api/go/svc/query/v1/queryv1connect"
Expand Down Expand Up @@ -43,6 +45,9 @@ func startLocalhostServer(
machineID uint64,
port int,
localhostHttpClient *http.Client,
apiHttpClient *http.Client,
apiURL string,
apiClientOpts []connect.ClientOption,
ownVersion *typesv1.Version,
) (localsink sink.Sink, done func(context.Context) error, err error) {

Expand Down Expand Up @@ -74,11 +79,14 @@ func startLocalhostServer(
}, nil
}

features := featurev1connect.NewFeatureServiceClient(apiHttpClient, apiURL, apiClientOpts...)

storage, err := localstorage.Open(
ctx,
cfg.ExperimentalFeatures.ServeLocalhost.Engine,
ll.WithGroup("storage"),
cfg.ExperimentalFeatures.ServeLocalhost.Cfg,
features,
)
if err != nil {
return nil, nil, fmt.Errorf("opening localstorage %q: %v", cfg.ExperimentalFeatures.ServeLocalhost.Engine, err)
Expand Down
11 changes: 10 additions & 1 deletion cmd/humanlog/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,17 @@ func newApp() *cli.App {
return fmt.Errorf("this feature requires a valid machine ID, which requires an environment. failed to login: %v", err)
}
}
clOpts := []connect.ClientOption{connect.WithInterceptors(
auth.Interceptors(ll, getTokenSource(cctx))...,
)}

machineID = uint64(*state.MachineID)
localhostSink, done, err := startLocalhostServer(ctx, ll, cfg, state, machineID, localhostCfg.Port, getLocalhostHTTPClient(cctx), version)
localhostSink, done, err := startLocalhostServer(
ctx, ll, cfg, state, machineID, localhostCfg.Port,
getLocalhostHTTPClient(cctx),
getHTTPClient(cctx, getAPIUrl(cctx)), getAPIUrl(cctx), clOpts,
version,
)
if err != nil {
loginfo("starting experimental localhost service: %v", err)
} else {
Expand Down
8 changes: 1 addition & 7 deletions cmd/humanlog/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,21 +274,15 @@ func organizationCmd(
Action: func(cctx *cli.Context) error {
ctx := getCtx(cctx)
ll := getLogger(cctx)
state := getState(cctx)
tokenSource := getTokenSource(cctx)
apiURL := getAPIUrl(cctx)
httpClient := getHTTPClient(cctx, apiURL)

orgID, err := ensureOrgSelected(ctx, ll, cctx, state, tokenSource, apiURL, httpClient)
if err != nil {
return err
}

clOpts := connect.WithInterceptors(
auth.Interceptors(ll, tokenSource)...,
)
organizationClient := organizationv1connect.NewOrganizationServiceClient(httpClient, apiURL, clOpts)
iter := ListOrgUser(ctx, orgID, organizationClient)
iter := ListOrgUser(ctx, organizationClient)
for iter.Next() {
u := iter.Current().User
printFact("id", u.Id)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
github.com/gen2brain/beeep v0.0.0-20240516210008-9c006672e7f4
github.com/go-logfmt/logfmt v0.5.1
github.com/google/uuid v1.6.0
github.com/humanlogio/api/go v0.0.0-20241113101248-3e03dc2ef4e0
github.com/humanlogio/api/go v0.0.0-20241128170213-590d167300cd
github.com/humanlogio/humanlog-pro v0.0.0-20241029032705-0ce172ecfc87
github.com/kr/logfmt v0.0.0-20210122060352-19f9bcb100e6
github.com/lrstanley/bubblezone v0.0.0-20240914071701-b48c55a5e78e
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c h1:6rhixN/i8
github.com/gsterjov/go-libsecret v0.0.0-20161001094733-a6f4afe4910c/go.mod h1:NMPJylDgVpX0MLRlPy15sqSwOFv/U1GZ2m21JhFfek0=
github.com/humanlogio/api/go v0.0.0-20241113101248-3e03dc2ef4e0 h1:NFmmOmTIlhNCWQX7TLKQtr6xkdOvesoygcqWruCBx2w=
github.com/humanlogio/api/go v0.0.0-20241113101248-3e03dc2ef4e0/go.mod h1:+hU/MU1g6QvtbeknKOlUI1yEStVqkPJ8jmYIj63OV5I=
github.com/humanlogio/api/go v0.0.0-20241128170213-590d167300cd h1:449C6cnB4W6DblDMPfCfA4xyEkiYMpngGf7TEX9O8ro=
github.com/humanlogio/api/go v0.0.0-20241128170213-590d167300cd/go.mod h1:+hU/MU1g6QvtbeknKOlUI1yEStVqkPJ8jmYIj63OV5I=
github.com/humanlogio/humanlog-pro v0.0.0-20241029032705-0ce172ecfc87 h1:z6x3cbdSyvnlmiuQ0v3Qi2iuJoRh1dyKydHIFy4J0is=
github.com/humanlogio/humanlog-pro v0.0.0-20241029032705-0ce172ecfc87/go.mod h1:1aHRO1O8oH4Mht2hOAo+kiLalHNu1SpFDcvuyauZuos=
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
Expand Down
3 changes: 2 additions & 1 deletion internal/memstorage/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sync"
"time"

"github.com/humanlogio/api/go/svc/feature/v1/featurev1connect"
typesv1 "github.com/humanlogio/api/go/types/v1"
"github.com/humanlogio/humanlog/pkg/localstorage"
"github.com/humanlogio/humanlog/pkg/sink"
Expand All @@ -23,7 +24,7 @@ var (
)

func init() {
localstorage.RegisterStorage("basic", func(ctx context.Context, ll *slog.Logger, cfg map[string]interface{}) (localstorage.Storage, error) {
localstorage.RegisterStorage("basic", func(ctx context.Context, ll *slog.Logger, cfg map[string]interface{}, features featurev1connect.FeatureServiceClient) (localstorage.Storage, error) {
return NewMemStorage(ll), nil
})
}
Expand Down
12 changes: 9 additions & 3 deletions pkg/localstorage/queryable.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ import (
"log/slog"
"time"

"github.com/humanlogio/api/go/svc/feature/v1/featurev1connect"
typesv1 "github.com/humanlogio/api/go/types/v1"
"github.com/humanlogio/humanlog/pkg/sink"
)

type StorageBuilder func(ctx context.Context, ll *slog.Logger, cfg map[string]interface{}) (Storage, error)
type StorageBuilder func(
ctx context.Context,
ll *slog.Logger,
cfg map[string]interface{},
features featurev1connect.FeatureServiceClient,
) (Storage, error)

var registry = make(map[string]StorageBuilder)

Expand All @@ -22,12 +28,12 @@ func RegisterStorage(name string, builder StorageBuilder) {
registry[name] = builder
}

func Open(ctx context.Context, name string, ll *slog.Logger, cfg map[string]interface{}) (Storage, error) {
func Open(ctx context.Context, name string, ll *slog.Logger, cfg map[string]interface{}, features featurev1connect.FeatureServiceClient) (Storage, error) {
builder, ok := registry[name]
if !ok {
return nil, fmt.Errorf("no storage engine with name %q", name)
}
return builder(ctx, ll, cfg)
return builder(ctx, ll, cfg, features)
}

type Storage interface {
Expand Down
5 changes: 2 additions & 3 deletions pkg/tui/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,8 @@ func listEnvironmentsCmd(
return func() tea.Msg {
log.Printf("environment: listEnvironments")
res, err := organizationClient.ListEnvironment(ctx, connect.NewRequest(&organizationv1.ListEnvironmentRequest{
Cursor: nil,
Limit: 10,
OrganizationId: *state.CurrentOrgID,
Cursor: nil,
Limit: 10,
}))
if err != nil {
cerr := new(connect.Error)
Expand Down
Loading