Skip to content

Commit

Permalink
fix query metric for multi org (#1253)
Browse files Browse the repository at this point in the history
* fix query metric for multi org

* fix query org

* format

* fix metric query
  • Loading branch information
recallsong authored Aug 2, 2021
1 parent c59ad33 commit 0092191
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 37 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ require (
github.com/gogap/stack v0.0.0-20150131034635-fef68dddd4f8 // indirect
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3
github.com/golang-jwt/jwt v3.2.1+incompatible
github.com/golang/protobuf v1.5.2
github.com/google/uuid v1.2.0
github.com/googlecloudplatform/flink-operator v0.0.0-00010101000000-000000000000
github.com/gorilla/mux v1.8.0
Expand Down Expand Up @@ -125,6 +126,7 @@ require (
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
golang.org/x/text v0.3.6
google.golang.org/genproto v0.0.0-20210729151513-df9385d47c1b
google.golang.org/grpc v1.39.0
google.golang.org/protobuf v1.27.1
gopkg.in/Knetic/govaluate.v3 v3.0.0
gopkg.in/flosch/pongo2.v3 v3.0.0-20141028000813-5e81b817a0c4
Expand Down
21 changes: 20 additions & 1 deletion modules/monitor/dashboard/org-apis/permission.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,15 @@ func (p *provider) checkOrgMetrics(ctx httpserver.Context) (string, error) {
if info == nil {
return "", fmt.Errorf("not found org")
}
clusters, err := p.listClustersByOrg(orgID)
if err != nil {
return "", fmt.Errorf("fail to list cluster by org(%d)", orgID)
}
q := ctx.Request().URL.Query()
q.Add("filter_org_name", info.Name)
q.Add("or_in_org_name", info.Name)
for _, cluster := range clusters {
q.Add("or_in_cluster_name", cluster)
}
ctx.Request().URL.RawQuery = q.Encode()
return idStr, nil
}
Expand All @@ -160,6 +167,18 @@ func (p *provider) checkOrgIDByClusters(orgID uint64, clusterNames []string) err
return nil
}

func (p *provider) listClustersByOrg(orgID uint64) ([]string, error) {
resp, err := p.bundle.ListClusters("", orgID)
if err != nil {
return nil, err
}
var list []string
for _, item := range resp {
list = append(list, item.Name)
}
return list, nil
}

func (p *provider) checkOrgByClusters(ctx httpserver.Context, clusters []*resourceCluster) error {
idStr := api.OrgID(ctx.Request())
orgID, err := strconv.ParseUint(idStr, 10, 64)
Expand Down
24 changes: 10 additions & 14 deletions modules/monitor/dashboard/org-apis/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,6 @@ import (
"github.com/erda-project/erda/pkg/http/httpclient"
)

type define struct{}

func (d *define) Services() []string { return []string{"org-apis"} }
func (d *define) Dependencies() []string { return []string{"http-server", "metrics-query", "i18n"} }
func (d *define) Summary() string { return "org apis" }
func (d *define) Description() string { return d.Summary() }
func (d *define) Config() interface{} { return &config{} }
func (d *define) Creator() servicehub.Creator {
return func() servicehub.Provider {
return &provider{}
}
}

type config struct {
OfflineTimeout time.Duration `file:"offline_timeout"`
OfflineSleep time.Duration `file:"offline_sleep"`
Expand All @@ -61,6 +48,7 @@ func (p *provider) Init(ctx servicehub.Context) error {
p.bundle = bundle.New(
bundle.WithHTTPClient(hc),
bundle.WithCoreServices(),
bundle.WithClusterManager(),
)
p.cmdb = cmdb.New(cmdb.WithHTTPClient(hc))
p.metricq = ctx.Service("metrics-query").(metricq.Queryer)
Expand All @@ -70,5 +58,13 @@ func (p *provider) Init(ctx servicehub.Context) error {
}

func init() {
servicehub.RegisterProvider("org-apis", &define{})
servicehub.Register("org-apis", &servicehub.Spec{
Services: []string{"org-apis"},
Dependencies: []string{"http-server", "metrics-query", "i18n"},
Description: "org apis",
ConfigFunc: func() interface{} { return &config{} },
Creator: func() servicehub.Provider {
return &provider{}
},
})
}
17 changes: 6 additions & 11 deletions modules/monitor/dashboard/project-apis/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@ import (
"github.com/erda-project/erda/modules/core/monitor/metric/query/metricq"
)

type define struct{}

func (d *define) Services() []string { return []string{"project-apis"} }
func (d *define) Dependencies() []string { return []string{"http-server", "metrics-query"} }
func (d *define) Summary() string { return "project apis" }
func (d *define) Description() string { return d.Summary() }
func (d *define) Creator() servicehub.Creator {
return func() servicehub.Provider { return &provider{} }
}

type provider struct {
L logs.Logger
metricq metricq.Queryer
Expand All @@ -43,5 +33,10 @@ func (p *provider) Init(ctx servicehub.Context) error {
}

func init() {
servicehub.RegisterProvider("project-apis", &define{})
servicehub.Register("project-apis", &servicehub.Spec{
Services: []string{"project-apis"},
Dependencies: []string{"http-server", "metrics-query"},
Description: "project apis",
Creator: func() servicehub.Provider { return &provider{} },
})
}
17 changes: 6 additions & 11 deletions modules/monitor/dashboard/runtime-apis/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@ import (
"github.com/erda-project/erda/modules/core/monitor/metric/query/metricq"
)

type define struct{}

func (d *define) Services() []string { return []string{"runtime-apis"} }
func (d *define) Dependencies() []string { return []string{"http-server", "metrics-query"} }
func (d *define) Summary() string { return "runtime apis" }
func (d *define) Description() string { return d.Summary() }
func (d *define) Creator() servicehub.Creator {
return func() servicehub.Provider { return &provider{} }
}

type provider struct {
L logs.Logger
metricq metricq.Queryer
Expand All @@ -43,5 +33,10 @@ func (p *provider) Init(ctx servicehub.Context) error {
}

func init() {
servicehub.RegisterProvider("runtime-apis", &define{})
servicehub.Register("runtime-apis", &servicehub.Spec{
Services: []string{"runtime-apis"},
Dependencies: []string{"http-server", "metrics-query"},
Description: "runtime apis",
Creator: func() servicehub.Provider { return &provider{} },
})
}

0 comments on commit 0092191

Please sign in to comment.