forked from pingcap/tidb-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.go
151 lines (127 loc) · 3.59 KB
/
report.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2018 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"sync"
)
const (
// Pass means all data and struct of tables are equal
Pass = "pass"
// Fail means not all data or struct of tables are equal
Fail = "fail"
)
// TableResult saves the check result for every table.
type TableResult struct {
Schema string
Table string
StructEqual bool
DataEqual bool
}
// Report saves the check results.
type Report struct {
sync.RWMutex
// Result is pass or fail
Result string
PassNum int32
FailedNum int32
TableResults map[string]map[string]*TableResult
}
// NewReport returns a new Report.
func NewReport() *Report {
return &Report{
TableResults: make(map[string]map[string]*TableResult),
Result: Pass,
}
}
// String returns a string of this Report.
func (r *Report) String() (report string) {
r.RLock()
defer r.RUnlock()
/*
output example:
check result: fail!
1 tables' check passed, 2 tables' check failed.
table: test1
table's struct equal
table's data not equal
table: test2
table's struct equal
table's data not equal
table: test3
table's struct equal
table's data equal
*/
report = fmt.Sprintf("\ncheck result: %s!\n", r.Result)
report += fmt.Sprintf("%d tables' check passed, %d tables' check failed.\n", r.PassNum, r.FailedNum)
var failTableRsult, passTableResult string
for schema, tableMap := range r.TableResults {
for table, result := range tableMap {
var structResult, dataResult string
if !result.StructEqual {
structResult = "table's struct not equal"
} else {
structResult = "table's struct equal"
}
if !result.DataEqual {
dataResult = "table's data not equal"
} else {
dataResult = "table's data equal"
}
if !result.StructEqual || !result.DataEqual {
failTableRsult = fmt.Sprintf("%stable: %s.%s\n%s\n%s\n\n", failTableRsult, schema, table, structResult, dataResult)
} else {
passTableResult = fmt.Sprintf("%stable: %s.%s\n%s\n%s\n\n", passTableResult, schema, table, structResult, dataResult)
}
}
}
// first print the check failed table's information
report += fmt.Sprintf("\n%s%s", failTableRsult, passTableResult)
return
}
// SetTableStructCheckResult sets the struct check result for table.
func (r *Report) SetTableStructCheckResult(schema, table string, equal bool) {
r.Lock()
defer r.Unlock()
if _, ok := r.TableResults[schema]; !ok {
r.TableResults[schema] = make(map[string]*TableResult)
}
if tableResult, ok := r.TableResults[schema][table]; ok {
tableResult.StructEqual = equal
} else {
r.TableResults[schema][table] = &TableResult{
StructEqual: equal,
}
}
if !equal {
r.Result = Fail
}
}
// SetTableDataCheckResult sets the data check result for table.
func (r *Report) SetTableDataCheckResult(schema, table string, equal bool) {
r.Lock()
defer r.Unlock()
if _, ok := r.TableResults[schema]; !ok {
r.TableResults[schema] = make(map[string]*TableResult)
}
if tableResult, ok := r.TableResults[schema][table]; ok {
tableResult.DataEqual = equal
} else {
r.TableResults[schema][table] = &TableResult{
DataEqual: equal,
}
}
if !equal {
r.Result = Fail
}
}