Skip to content

Commit

Permalink
restructure ClickHouseStats
Browse files Browse the repository at this point in the history
Signed-off-by: Yun-Tang Hsu <[email protected]>
  • Loading branch information
yuntanghsu committed Nov 29, 2022
1 parent c7492db commit cded8f6
Show file tree
Hide file tree
Showing 9 changed files with 382 additions and 175 deletions.
39 changes: 37 additions & 2 deletions pkg/apis/stats/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

package v1alpha1

import metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// +genclient
// +genclient:nonNamespaced
Expand All @@ -24,5 +26,38 @@ type ClickHouseStats struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Stat [][]string `json:"stat,omitempty"`
DiskInfos []DiskInfo `json:"diskInfos,omitempty"`
TableInfos []TableInfo `json:"tableInfos,omitempty"`
InsertRates []InsertRate `json:"insertRates,omitempty"`
StackTraces []StackTrace `json:"stackTraces,omitempty"`
}

type DiskInfo struct {
Shard string `json:"shard,omitempty"`
Database string `json:"name,omitempty"`
Path string `json:"path,omitempty"`
FreeSpace string `json:"freeSpace,omitempty"`
TotalSpace string `json:"totalSpace,omitempty"`
UsedPercentage string `usedPercentage:"name,omitempty"`
}

type TableInfo struct {
Shard string `json:"shard,omitempty"`
Database string `json:"database,omitempty"`
TableName string `json:"tableName,omitempty"`
TotalRows string `json:"totalRows,omitempty"`
TotalBytes string `json:"totalBytes,omitempty"`
TotalCols string `json:"totalCols,omitempty"`
}

type InsertRate struct {
Shard string `json:"shard,omitempty"`
RowsPerSec string `json:"rowsPerSec,omitempty"`
BytesPerSec string `json:"bytesPerSec,omitempty"`
}

type StackTrace struct {
Shard string `json:"shard,omitempty"`
TraceFunctions string `json:"traceFunctions,omitempty"`
Count string `json:"count,omitempty"`
}
84 changes: 84 additions & 0 deletions pkg/apis/stats/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 19 additions & 7 deletions pkg/apiserver/registry/stats/clickhouse/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,36 @@ func (r *REST) New() runtime.Object {
}

func (r *REST) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
var stats [][]string
var status v1alpha1.ClickHouseStats
var err error
switch name {
case "diskInfo":
stats, err = r.clickHouseStatusQuerier.GetDiskInfo(defaultNameSpace)
err = r.clickHouseStatusQuerier.GetDiskInfo(defaultNameSpace, &status)
if status.DiskInfos == nil {
return nil, fmt.Errorf("no diskInfo data is returned by database")
}
case "tableInfo":
stats, err = r.clickHouseStatusQuerier.GetTableInfo(defaultNameSpace)
err = r.clickHouseStatusQuerier.GetTableInfo(defaultNameSpace, &status)
if status.TableInfos == nil {
return nil, fmt.Errorf("no tableInfo data is returned by database")
}
case "insertRate":
stats, err = r.clickHouseStatusQuerier.GetInsertRate(defaultNameSpace)
case "stackTraces":
stats, err = r.clickHouseStatusQuerier.GetStackTraces(defaultNameSpace)
err = r.clickHouseStatusQuerier.GetInsertRate(defaultNameSpace, &status)
if status.InsertRates == nil {
return nil, fmt.Errorf("no insertRate data is returned by database")
}
case "stackTrace":
err = r.clickHouseStatusQuerier.GetStackTrace(defaultNameSpace, &status)
if status.StackTraces == nil {
return nil, fmt.Errorf("no stackTrace data is returned by database")
}
default:
return nil, fmt.Errorf("cannot recognize the statua name: %s", name)
}
if err != nil {
return nil, fmt.Errorf("error when sending query to ClickHouse: %s", err)
}
return &v1alpha1.ClickHouseStats{Stat: stats}, nil
return &status, nil
}

func (r *REST) NamespaceScoped() bool {
Expand Down
80 changes: 54 additions & 26 deletions pkg/apiserver/registry/stats/clickhouse/rest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,47 @@ func TestREST_Get(t *testing.T) {
name string
queryName string
expectErr error
expectResult [][]string
expectResult *stats.ClickHouseStats
}{
{
name: "Get diskInfo",
queryName: "diskInfo",
expectErr: nil,
expectResult: [][]string{{"diskInfo_test"}},
name: "Get diskInfo",
queryName: "diskInfo",
expectErr: nil,
expectResult: &stats.ClickHouseStats{
DiskInfos: []stats.DiskInfo{{
Shard: "Shard_test",
}},
},
},
{
name: "Get tableInfo",
queryName: "tableInfo",
expectErr: nil,
expectResult: [][]string{{"tableInfo_test"}},
name: "Get tableInfo",
queryName: "tableInfo",
expectErr: nil,
expectResult: &stats.ClickHouseStats{
TableInfos: []stats.TableInfo{{
Shard: "Shard_test",
}},
},
},
{
name: "Get insertRate",
queryName: "insertRate",
expectErr: nil,
expectResult: [][]string{{"insertRate_test"}},
name: "Get insertRate",
queryName: "insertRate",
expectErr: nil,
expectResult: &stats.ClickHouseStats{
InsertRates: []stats.InsertRate{{
Shard: "Shard_test",
}},
},
},
{
name: "Get stackTraces",
queryName: "stackTraces",
expectErr: nil,
expectResult: [][]string{{"stackTraces_test"}},
name: "Get stackTraces",
queryName: "stackTrace",
expectErr: nil,
expectResult: &stats.ClickHouseStats{
StackTraces: []stats.StackTrace{{
Shard: "Shard_test",
}},
},
},
{
name: "not found",
Expand All @@ -73,7 +89,7 @@ func TestREST_Get(t *testing.T) {
assert.NoError(t, err)
status, ok := result.(*stats.ClickHouseStats)
assert.True(t, ok)
assert.ElementsMatch(t, tt.expectResult, status.Stat)
assert.EqualValues(t, tt.expectResult, status)

} else {
assert.Error(t, err)
Expand All @@ -83,15 +99,27 @@ func TestREST_Get(t *testing.T) {
}
}

func (c *fakeQuerier) GetDiskInfo(namespace string) ([][]string, error) {
return [][]string{{"diskInfo_test"}}, nil
func (c *fakeQuerier) GetDiskInfo(namespace string, status *stats.ClickHouseStats) error {
status.DiskInfos = []stats.DiskInfo{{
Shard: "Shard_test",
}}
return nil
}
func (c *fakeQuerier) GetTableInfo(namespace string) ([][]string, error) {
return [][]string{{"tableInfo_test"}}, nil
func (c *fakeQuerier) GetTableInfo(namespace string, status *stats.ClickHouseStats) error {
status.TableInfos = []stats.TableInfo{{
Shard: "Shard_test",
}}
return nil
}
func (c *fakeQuerier) GetInsertRate(namespace string) ([][]string, error) {
return [][]string{{"insertRate_test"}}, nil
func (c *fakeQuerier) GetInsertRate(namespace string, status *stats.ClickHouseStats) error {
status.InsertRates = []stats.InsertRate{{
Shard: "Shard_test",
}}
return nil
}
func (c *fakeQuerier) GetStackTraces(namespace string) ([][]string, error) {
return [][]string{{"stackTraces_test"}}, nil
func (c *fakeQuerier) GetStackTrace(namespace string, status *stats.ClickHouseStats) error {
status.StackTraces = []stats.StackTrace{{
Shard: "Shard_test",
}}
return nil
}
Loading

0 comments on commit cded8f6

Please sign in to comment.