-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
check_template.go
130 lines (115 loc) · 3.65 KB
/
check_template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright 2020 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 importer
import (
"strings"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/pingcap/tidb/lightning/pkg/precheck"
)
// Template is the interface for lightning check.
type Template interface {
// Collect mainly collect performance related checks' results and critical level checks' results.
// If the performance is not as expect or one of critical check not passed. it will stop import task.
Collect(t precheck.CheckType, passed bool, msg string)
// Success represents the whole check has passed or not.
Success() bool
// FailedCount represents (the warn check failed count, the critical check failed count)
FailedCount(t precheck.CheckType) int
// Output print all checks results.
Output() string
// FailedMsg represents the error msg for the failed check.
FailedMsg() string
}
// SimpleTemplate is a simple template for lightning check.
type SimpleTemplate struct {
count int
// export them for test
warnFailedCount int
criticalFailedCount int
normalMsgs []string // only used in unit test now
criticalMsgs []string
t table.Writer
}
// NewSimpleTemplate returns a simple template.
func NewSimpleTemplate() Template {
t := table.NewWriter()
t.AppendHeader(table.Row{"#", "Check Item", "Type", "Passed"})
t.SetColumnConfigs([]table.ColumnConfig{
{Name: "#", WidthMax: 6},
{Name: "Check Item", WidthMax: 130},
{Name: "Type", WidthMax: 20},
{Name: "Passed", WidthMax: 6},
})
return &SimpleTemplate{
t: t,
}
}
// FailedMsg returns the error msg for the failed check.
func (c *SimpleTemplate) FailedMsg() string {
return strings.Join(c.criticalMsgs, ";\n")
}
// Collect mainly collect performance related checks' results and critical level checks' results.
func (c *SimpleTemplate) Collect(t precheck.CheckType, passed bool, msg string) {
c.count++
if !passed {
switch t {
case precheck.Critical:
c.criticalFailedCount++
case precheck.Warn:
c.warnFailedCount++
}
}
if !passed && t == precheck.Critical {
c.criticalMsgs = append(c.criticalMsgs, msg)
} else {
c.normalMsgs = append(c.normalMsgs, msg)
}
c.t.AppendRow(table.Row{c.count, msg, t, passed})
c.t.AppendSeparator()
}
// Success represents the whole check has passed or not.
func (c *SimpleTemplate) Success() bool {
return c.criticalFailedCount == 0
}
// FailedCount represents (the warn check failed count, the critical check failed count)
func (c *SimpleTemplate) FailedCount(t precheck.CheckType) int {
if t == precheck.Warn {
return c.warnFailedCount
}
if t == precheck.Critical {
return c.criticalFailedCount
}
return 0
}
// Output print all checks results.
func (c *SimpleTemplate) Output() string {
c.t.SetAllowedRowLength(170)
c.t.SetRowPainter(func(row table.Row) text.Colors {
if passed, ok := row[3].(bool); ok {
if !passed {
if typ, ok := row[2].(precheck.CheckType); ok {
if typ == precheck.Warn {
return text.Colors{text.FgYellow}
}
if typ == precheck.Critical {
return text.Colors{text.FgRed}
}
}
}
}
return nil
})
return c.t.Render() + "\n"
}