From f4b148da628447c7c29cb32b4e00292662f9b2fe Mon Sep 17 00:00:00 2001 From: Suhaha Date: Wed, 23 Jun 2021 09:21:22 +0800 Subject: [PATCH] fix(stmt & slowquery): ignored virtual fields (#939) --- pkg/apiserver/slowquery/model.go | 12 +++++++++ pkg/apiserver/slowquery/service.go | 3 ++- pkg/apiserver/statement/models.go | 12 +++++++++ pkg/apiserver/statement/service.go | 3 ++- pkg/apiserver/statement/statement_gen.go | 15 +++-------- pkg/apiserver/utils/subset.go | 32 ++++++++++++++++++++++++ 6 files changed, 63 insertions(+), 14 deletions(-) create mode 100644 pkg/apiserver/utils/subset.go diff --git a/pkg/apiserver/slowquery/model.go b/pkg/apiserver/slowquery/model.go index c64e976a4f..645707f8f7 100644 --- a/pkg/apiserver/slowquery/model.go +++ b/pkg/apiserver/slowquery/model.go @@ -14,6 +14,8 @@ package slowquery import ( + "github.com/thoas/go-funk" + "github.com/pingcap/tidb-dashboard/pkg/apiserver/utils" ) @@ -119,3 +121,13 @@ func getFieldsAndTags() (slowQueryFields []Field) { return } + +func getVirtualFields() []string { + fields := getFieldsAndTags() + vFields := funk.Filter(fields, func(f Field) bool { + return f.Projection != "" + }).([]Field) + return funk.Map(vFields, func(f Field) string { + return f.ColumnName + }).([]string) +} diff --git a/pkg/apiserver/slowquery/service.go b/pkg/apiserver/slowquery/service.go index 2e749229f4..ebeb1bedff 100644 --- a/pkg/apiserver/slowquery/service.go +++ b/pkg/apiserver/slowquery/service.go @@ -20,6 +20,7 @@ import ( "time" "github.com/joomcode/errorx" + "github.com/thoas/go-funk" "github.com/gin-gonic/gin" "go.uber.org/fx" @@ -190,5 +191,5 @@ func (s *Service) queryTableColumns(c *gin.Context) { _ = c.Error(err) return } - c.JSON(http.StatusOK, cs) + c.JSON(http.StatusOK, funk.UniqString(append(cs, getVirtualFields()...))) } diff --git a/pkg/apiserver/statement/models.go b/pkg/apiserver/statement/models.go index 821cf65c3e..bcf1d6aa8a 100644 --- a/pkg/apiserver/statement/models.go +++ b/pkg/apiserver/statement/models.go @@ -19,6 +19,8 @@ import ( "gorm.io/gorm" "gorm.io/gorm/schema" + "github.com/thoas/go-funk" + "github.com/pingcap/tidb-dashboard/pkg/apiserver/utils" ) @@ -164,3 +166,13 @@ func getFieldsAndTags() (stmtFields []Field) { return } + +func getVirtualFields(tableFields []string) []string { + fields := getFieldsAndTags() + vFields := funk.Filter(fields, func(f Field) bool { + return len(f.Related) != 0 && utils.IsSubsets(tableFields, f.Related) + }).([]Field) + return funk.Map(vFields, func(f Field) string { + return f.JSONName + }).([]string) +} diff --git a/pkg/apiserver/statement/service.go b/pkg/apiserver/statement/service.go index 9dacfc9938..a7ce2c0a7e 100644 --- a/pkg/apiserver/statement/service.go +++ b/pkg/apiserver/statement/service.go @@ -20,6 +20,7 @@ import ( "time" "github.com/joomcode/errorx" + "github.com/thoas/go-funk" "github.com/gin-gonic/gin" @@ -334,5 +335,5 @@ func (s *Service) queryTableColumns(c *gin.Context) { _ = c.Error(err) return } - c.JSON(http.StatusOK, cs) + c.JSON(http.StatusOK, funk.UniqString(append(cs, getVirtualFields(cs)...))) } diff --git a/pkg/apiserver/statement/statement_gen.go b/pkg/apiserver/statement/statement_gen.go index 427318cfdb..0ce3f4400d 100644 --- a/pkg/apiserver/statement/statement_gen.go +++ b/pkg/apiserver/statement/statement_gen.go @@ -18,6 +18,8 @@ import ( "strings" "github.com/thoas/go-funk" + + "github.com/pingcap/tidb-dashboard/pkg/apiserver/utils" ) var ( @@ -45,7 +47,7 @@ func (s *Service) genSelectStmt(tableColumns []string, reqJSONColumns []string) representedColumns = []string{f.JSONName} } - return isSubsets(tableColumns, representedColumns) + return utils.IsSubsets(tableColumns, representedColumns) }).([]Field) if len(fields) == 0 { @@ -60,14 +62,3 @@ func (s *Service) genSelectStmt(tableColumns []string, reqJSONColumns []string) }).([]string) return strings.Join(stmt, ", "), nil } - -func isSubsets(a []string, b []string) bool { - lowercaseA := funk.Map(a, func(x string) string { - return strings.ToLower(x) - }).([]string) - lowercaseB := funk.Map(b, func(x string) string { - return strings.ToLower(x) - }).([]string) - - return len(funk.Join(lowercaseA, lowercaseB, funk.InnerJoin).([]string)) == len(lowercaseB) -} diff --git a/pkg/apiserver/utils/subset.go b/pkg/apiserver/utils/subset.go new file mode 100644 index 0000000000..1312755af2 --- /dev/null +++ b/pkg/apiserver/utils/subset.go @@ -0,0 +1,32 @@ +// Copyright 2021 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package utils + +import ( + "strings" + + "github.com/thoas/go-funk" +) + +func IsSubsets(a []string, b []string) bool { + lowercaseA := funk.Map(a, func(x string) string { + return strings.ToLower(x) + }).([]string) + lowercaseB := funk.Map(b, func(x string) string { + return strings.ToLower(x) + }).([]string) + + return len(funk.Join(lowercaseA, lowercaseB, funk.InnerJoin).([]string)) == len(lowercaseB) +}