Skip to content

Commit

Permalink
Merge pull request #97 from anyproto/go-1453-add-alpha-users-flag-in-…
Browse files Browse the repository at this point in the history
…middleware

GO-1453  Add alpha user flag in anysync
  • Loading branch information
fat-fellow authored Jun 15, 2023
2 parents 9699b61 + 67114ef commit 8cb480c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
16 changes: 13 additions & 3 deletions core/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,12 @@ func (mw *Middleware) AccountCreate(cctx context.Context, req *pb.RpcAccountCrea
mw.EventSender,
}

if mw.app, err = anytype.StartNewApp(context.WithValue(context.Background(), metrics.CtxKeyRequest, "account_create"), mw.clientVersion, comps...); err != nil {
mw.requireClientWithVersion()
if mw.app, err = anytype.StartNewApp(
context.WithValue(context.Background(), metrics.CtxKeyRequest, "account_create"),
mw.clientWithVersion,
comps...,
); err != nil {
return response(newAcc, pb.RpcAccountCreateResponseError_ACCOUNT_CREATED_BUT_FAILED_TO_START_NODE, err)
}

Expand Down Expand Up @@ -303,7 +308,12 @@ func (mw *Middleware) AccountSelect(cctx context.Context, req *pb.RpcAccountSele
// if we have created the repo, we need to highlight that we are recovering the account
request = request + "_recover"
}
if mw.app, err = anytype.StartNewApp(context.WithValue(context.Background(), metrics.CtxKeyRequest, request), mw.clientVersion, comps...); err != nil {
mw.requireClientWithVersion()
if mw.app, err = anytype.StartNewApp(
context.WithValue(context.Background(), metrics.CtxKeyRequest, request),
mw.clientWithVersion,
comps...,
); err != nil {
if errors.Is(err, spacesyncproto.ErrSpaceMissing) {
return response(nil, pb.RpcAccountSelectResponseError_FAILED_TO_FIND_ACCOUNT_INFO, err)
}
Expand Down Expand Up @@ -655,7 +665,7 @@ func (mw *Middleware) startApp(cfg *config.Config, derivationResult crypto.Deriv

ctxWithValue := context.WithValue(context.Background(), metrics.CtxKeyRequest, "account_create")
var err error
if mw.app, err = anytype.StartNewApp(ctxWithValue, mw.clientVersion, comps...); err != nil {
if mw.app, err = anytype.StartNewApp(ctxWithValue, mw.clientWithVersion, comps...); err != nil {
return err
}
return nil
Expand Down
16 changes: 11 additions & 5 deletions core/anytype/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package anytype
import (
"context"
"os"
"regexp"
"time"

"github.com/anyproto/any-sync/app"
Expand Down Expand Up @@ -87,9 +88,9 @@ func BootstrapWallet(rootPath string, derivationResult crypto.DerivationResult)
return wallet.NewWithAccountRepo(rootPath, derivationResult)
}

func StartNewApp(ctx context.Context, clientVersion string, components ...app.Component) (a *app.App, err error) {
func StartNewApp(ctx context.Context, clientWithVersion string, components ...app.Component) (a *app.App, err error) {
a = new(app.App)
a.SetVersionName(appVersion(a, clientVersion))
a.SetVersionName(appVersion(a, clientWithVersion))
Bootstrap(a, components...)
metrics.SharedClient.SetAppVersion(a.Version())
metrics.SharedClient.Run()
Expand All @@ -102,10 +103,11 @@ func StartNewApp(ctx context.Context, clientVersion string, components ...app.Co
return
}

func appVersion(a *app.App, clientVersion string) string {
middleVersion := vcs.GetVCSInfo().Version()
func appVersion(a *app.App, clientWithVersion string) string {
clientWithVersion = regexp.MustCompile(`(@|\/)+`).ReplaceAllString(clientWithVersion, "_")
middleVersion := MiddlewareVersion()
anySyncVersion := a.AnySyncVersion()
return "client:" + clientVersion + "/middle:" + middleVersion + "/any-sync:" + anySyncVersion
return clientWithVersion + "/middle:" + middleVersion + "/any-sync:" + anySyncVersion
}

func Bootstrap(a *app.App, components ...app.Component) {
Expand Down Expand Up @@ -213,3 +215,7 @@ func Bootstrap(a *app.App, components ...app.Component) {
Register(graphRenderer)
return
}

func MiddlewareVersion() string {
return vcs.GetVCSInfo().Version()
}
12 changes: 9 additions & 3 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ type Middleware struct {
accountSearchCancel context.CancelFunc
EventSender event.Sender

sessions session.Service
clientVersion string
app *app.App
sessions session.Service
clientWithVersion string
app *app.App

m sync.RWMutex
}
Expand Down Expand Up @@ -180,3 +180,9 @@ func (mw *Middleware) OnPanic(v interface{}) {
os.Stderr.Write(stack)
log.With("stack", stack).Errorf("panic recovered: %v", v)
}

func (mw *Middleware) requireClientWithVersion() {
if mw.clientWithVersion == "" {
panic(errors.New("client platform with the version must be set using the MetricsSetParameters method"))
}
}
7 changes: 6 additions & 1 deletion core/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package core

import (
"context"
"errors"

"github.com/anyproto/anytype-heart/metrics"
"github.com/anyproto/anytype-heart/pb"
Expand All @@ -16,7 +17,11 @@ func (mw *Middleware) MetricsSetParameters(cctx context.Context, req *pb.RpcMetr

return m
}
mw.clientVersion = req.Platform + "-" + req.Version
if req.Version == "" {
return response(pb.RpcMetricsSetParametersResponseError_BAD_INPUT,
errors.New("version is empty. Version must be in format: 1.0.0-optional-commit-hash-for-dev-builds"))
}
mw.clientWithVersion = req.Platform + ":" + req.Version
metrics.SharedClient.SetPlatform(req.Platform)

return response(pb.RpcMetricsSetParametersResponseError_NULL, nil)
Expand Down

0 comments on commit 8cb480c

Please sign in to comment.