Skip to content

Commit

Permalink
fix(stmt & slowquery): append ignored virtual fields to table_columns…
Browse files Browse the repository at this point in the history
… api
  • Loading branch information
shhdgit committed Jun 22, 2021
1 parent 7fdbb69 commit f8afa37
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 14 deletions.
12 changes: 12 additions & 0 deletions pkg/apiserver/slowquery/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
package slowquery

import (
"github.com/thoas/go-funk"

"github.com/pingcap/tidb-dashboard/pkg/apiserver/utils"
)

Expand Down Expand Up @@ -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)
}
3 changes: 2 additions & 1 deletion pkg/apiserver/slowquery/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"time"

"github.com/joomcode/errorx"
"github.com/thoas/go-funk"

"github.com/gin-gonic/gin"
"go.uber.org/fx"
Expand Down Expand Up @@ -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()...)))
}
12 changes: 12 additions & 0 deletions pkg/apiserver/statement/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
}
3 changes: 2 additions & 1 deletion pkg/apiserver/statement/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"time"

"github.com/joomcode/errorx"
"github.com/thoas/go-funk"

"github.com/gin-gonic/gin"

Expand Down Expand Up @@ -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)...)))
}
15 changes: 3 additions & 12 deletions pkg/apiserver/statement/statement_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"strings"

"github.com/thoas/go-funk"

"github.com/pingcap/tidb-dashboard/pkg/apiserver/utils"
)

var (
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
32 changes: 32 additions & 0 deletions pkg/apiserver/utils/subset.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit f8afa37

Please sign in to comment.