Skip to content
This repository was archived by the owner on Jul 6, 2021. It is now read-only.

Commit 8e71ded

Browse files
committed
Merge branch 'dmius-360-improvements' into 'master'
Small improvements of checkup See merge request postgres-ai-team/postgres-checkup!291
2 parents 476a746 + cd34abb commit 8e71ded

File tree

8 files changed

+268
-276
lines changed

8 files changed

+268
-276
lines changed

pghrep/src/reportutils.go

+134-135
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,50 @@
11
package main
22

33
import (
4-
"strings"
5-
"strconv"
6-
"./pyraconv"
7-
"./fmtutils"
8-
"fmt"
9-
"./dateparse"
10-
"time"
4+
"fmt"
5+
"strconv"
6+
"strings"
7+
"time"
8+
9+
"./dateparse"
10+
"./fmtutils"
11+
"./pyraconv"
1112
)
1213

1314
func Split(s interface{}, d interface{}) []string {
14-
str1 := pyraconv.ToString(s)
15-
str2 := pyraconv.ToString(d)
16-
arr := strings.Split(str1, str2)
17-
return arr
15+
str1 := pyraconv.ToString(s)
16+
str2 := pyraconv.ToString(d)
17+
arr := strings.Split(str1, str2)
18+
return arr
1819
}
1920

2021
func Trim(s string, d string) string {
21-
return strings.Trim(s, d)
22+
return strings.Trim(s, d)
2223
}
2324

2425
func Replace(str interface{}, src interface{}, dst interface{}) string {
25-
str1 := pyraconv.ToString(str)
26-
src1 := pyraconv.ToString(src)
27-
dst1 := pyraconv.ToString(dst)
28-
return strings.Replace(str1, src1, dst1, -1)
26+
str1 := pyraconv.ToString(str)
27+
src1 := pyraconv.ToString(src)
28+
dst1 := pyraconv.ToString(dst)
29+
return strings.Replace(str1, src1, dst1, -1)
2930
}
3031

31-
3232
func Nobr(s interface{}) string {
33-
str := pyraconv.ToString(s)
34-
str = strings.Join(strings.Split(str, "\n"), " ")
35-
str = strings.Join(strings.Split(str, " "), " ")
36-
return str
33+
str := pyraconv.ToString(s)
34+
str = strings.Join(strings.Split(str, "\n"), " ")
35+
str = strings.Join(strings.Split(str, " "), " ")
36+
return str
3737
}
3838

3939
func Br(s interface{}) string {
40-
str := pyraconv.ToString(s)
41-
return strings.Join(strings.Split(str, ","), ", ")
40+
str := pyraconv.ToString(s)
41+
return strings.Join(strings.Split(str, ","), ", ")
4242
}
4343

4444
/* Divide text to rows by given row length
45-
*/
45+
*/
4646
func WordWrap(s string, limit int) string {
47-
srcStr := s //strings.Join(strings.Split(s, "\n"), " ")
47+
srcStr := s //strings.Join(strings.Split(s, "\n"), " ")
4848
str := ""
4949
pos := 0
5050
for _, c := range srcStr {
@@ -61,171 +61,170 @@ func WordWrap(s string, limit int) string {
6161

6262
/* Escape Markdown symbols in a SQL query,
6363
* convert to a single line
64-
*/
64+
*/
6565
func EscapeQuery(s interface{}) string {
66-
str := pyraconv.ToString(s)
67-
68-
str = strings.Join(strings.Split(str, "\n \n"), "<br/>")
69-
str = strings.Join(strings.Split(str, " "), "&nbsp;")
70-
str = strings.Join(strings.Split(str, "*"), "\\*")
71-
str = strings.Join(strings.Split(str, "_"), "\\_")
72-
str = strings.Join(strings.Split(str, "-"), "\\-")
73-
str = strings.Join(strings.Split(str, "~"), "\\~")
74-
str = strings.Join(strings.Split(str, "`"), "\\`")
75-
// str = strings.Join(strings.Split(str, "\""), "&#34;")
76-
str = strings.Join(strings.Split(str, "|"), "&#448;")
77-
str = strings.Join(strings.Split(str, "\n\n"), "<br/>")
78-
str = strings.Join(strings.Split(str, "\n\r"), "<br/>")
79-
str = strings.Join(strings.Split(str, "\n"), "<br/>")
80-
str = strings.Join(strings.Split(str, "<br/><br/>"), "<br/>")
81-
return str
66+
str := pyraconv.ToString(s)
67+
str = strings.Join(strings.Split(str, "http"), "&#104;ttp")
68+
str = strings.Join(strings.Split(str, "\n \n"), "<br/>")
69+
str = strings.Join(strings.Split(str, " "), "&nbsp;")
70+
str = strings.Join(strings.Split(str, "*"), "\\*")
71+
str = strings.Join(strings.Split(str, "_"), "\\_")
72+
str = strings.Join(strings.Split(str, "-"), "\\-")
73+
str = strings.Join(strings.Split(str, "~"), "\\~")
74+
str = strings.Join(strings.Split(str, "`"), "\\`")
75+
str = strings.Join(strings.Split(str, "|"), "&#448;")
76+
str = strings.Join(strings.Split(str, "\n\n"), "<br/>")
77+
str = strings.Join(strings.Split(str, "\n\r"), "<br/>")
78+
str = strings.Join(strings.Split(str, "\n"), "<br/>")
79+
str = strings.Join(strings.Split(str, "<br/><br/>"), "<br/>")
80+
return str
8281
}
8382

8483
/* Add \t before every row in text to preview block as code block
8584
* s String - string for preprocces
8685
* skipFirst bool - flag to skip first row
87-
*/
86+
*/
8887
func Code(s string, skipFirst bool) string {
89-
codeLines := strings.Split(s, "\n")
90-
for i, line := range codeLines {
91-
if i > 0 {
92-
codeLines[i] = "\t" + line
93-
}
94-
}
95-
if skipFirst {
96-
return strings.Join(codeLines, "\n")
97-
} else {
98-
return "\t" + strings.Join(codeLines, "\n")
99-
}
88+
codeLines := strings.Split(s, "\n")
89+
for i, line := range codeLines {
90+
if i > 0 {
91+
codeLines[i] = "\t" + line
92+
}
93+
}
94+
if skipFirst {
95+
return strings.Join(codeLines, "\n")
96+
} else {
97+
return "\t" + strings.Join(codeLines, "\n")
98+
}
10099
}
101100

102101
func UnitValue(value interface{}, unit interface{}) string {
103-
val := pyraconv.ToString(value)
104-
un := pyraconv.ToString(unit)
105-
if len(un) <= 0 {
106-
return ""
107-
}
108-
intval, err := strconv.ParseInt(val, 10, 64)
109-
if err != nil {
110-
return "" // val + "(" + un + ")"
111-
}
112-
if intval < 0 {
113-
return "" // val
114-
}
115-
unitFactor := fmtutils.GetUnit(un)
116-
if unitFactor != -1 {
117-
intval = intval * unitFactor
118-
return fmtutils.ByteFormat(float64(intval), 2)
119-
}
120-
return "" //val + "(" + un + ")"
102+
val := pyraconv.ToString(value)
103+
un := pyraconv.ToString(unit)
104+
if len(un) <= 0 {
105+
return ""
106+
}
107+
intval, err := strconv.ParseInt(val, 10, 64)
108+
if err != nil {
109+
return "" // val + "(" + un + ")"
110+
}
111+
if intval < 0 {
112+
return "" // val
113+
}
114+
unitFactor := fmtutils.GetUnit(un)
115+
if unitFactor != -1 {
116+
intval = intval * unitFactor
117+
return fmtutils.ByteFormat(float64(intval), 2)
118+
}
119+
return "" //val + "(" + un + ")"
121120
}
122121

123-
124122
func RawIntUnitValue(value interface{}, unit interface{}) int {
125-
val := pyraconv.ToString(value)
126-
un := pyraconv.ToString(unit)
127-
intval, err := strconv.ParseInt(val, 10, 64)
128-
if err != nil {
129-
return -1
130-
}
131-
if intval < 0 {
132-
return -1
133-
}
134-
if len(un) <= 0 {
135-
return int(intval)
136-
}
137-
unitFactor := fmtutils.GetUnit(un)
138-
if unitFactor != -1 {
139-
intval = intval * unitFactor
140-
return int(intval)
141-
}
142-
return -1
123+
val := pyraconv.ToString(value)
124+
un := pyraconv.ToString(unit)
125+
intval, err := strconv.ParseInt(val, 10, 64)
126+
if err != nil {
127+
return -1
128+
}
129+
if intval < 0 {
130+
return -1
131+
}
132+
if len(un) <= 0 {
133+
return int(intval)
134+
}
135+
unitFactor := fmtutils.GetUnit(un)
136+
if unitFactor != -1 {
137+
intval = intval * unitFactor
138+
return int(intval)
139+
}
140+
return -1
143141
}
144142

145143
func LimitStr(value interface{}, limit int) string {
146-
val := pyraconv.ToString(value)
147-
if len(val) > limit {
148-
return val[0:limit-1] + "..."
149-
}
150-
return val
144+
val := pyraconv.ToString(value)
145+
if len(val) > limit {
146+
return val[0:limit-1] + "..."
147+
}
148+
return val
151149
}
152150

153151
func Round(value interface{}, places interface{}) string {
154-
val := pyraconv.ToFloat64(value)
155-
pl := pyraconv.ToInt64(places)
156-
if value != nil && places != nil {
157-
return fmt.Sprintf("%v", fmtutils.RoundUp(val, int(pl)))
158-
}
159-
return fmt.Sprintf("%v", val)
152+
val := pyraconv.ToFloat64(value)
153+
pl := pyraconv.ToInt64(places)
154+
if value != nil && places != nil {
155+
return fmt.Sprintf("%v", fmtutils.RoundUp(val, int(pl)))
156+
}
157+
return fmt.Sprintf("%v", val)
160158
}
161159

162160
func Add(a int, b int) int {
163-
return a + b
161+
return a + b
164162
}
165163

166164
func Mul(a int, b int) float64 {
167-
return float64(a * b)
165+
return float64(a * b)
168166
}
169167

170168
func Div(a int, b int) int {
171-
return a / b
169+
return a / b
172170
}
173171

174172
func Sub(a int, b int) int {
175-
return a - b
173+
return a - b
176174
}
177175

178176
func MsFormat(value interface{}) string {
179-
val := pyraconv.ToInt64(value)
180-
if val != 0 {
181-
tm, _ := time.ParseDuration(strconv.FormatInt(val, 10) + "ms")
182-
return tm.String()
183-
} else {
184-
return strconv.FormatInt(val, 10) + "ms"
185-
}
177+
val := pyraconv.ToString(value)
178+
floatVal := pyraconv.ToFloat64(value)
179+
tm, terr := time.ParseDuration(val + "&nbsp;ms")
180+
if tm > time.Second && terr == nil {
181+
return tm.String()
182+
} else {
183+
return strconv.FormatFloat(floatVal, 'f', 3, 64) + "&nbsp;ms"
184+
}
186185
}
187186

188187
func NumFormat(value interface{}, places interface{}) string {
189-
val := pyraconv.ToFloat64(value)
190-
pl := pyraconv.ToInt64(places)
191-
if pl == -1 {
192-
return strconv.FormatFloat(val, 'f', int(pl), 64)
193-
} else {
194-
return fmtutils.NumFormat(val, int(pl))
195-
}
188+
val := pyraconv.ToFloat64(value)
189+
pl := pyraconv.ToInt64(places)
190+
if pl == -1 {
191+
return strconv.FormatFloat(val, 'f', int(pl), 64)
192+
} else {
193+
return fmtutils.NumFormat(val, int(pl))
194+
}
196195
}
197196

198197
func DtFormat(value interface{}) string {
199-
val := pyraconv.ToString(value)
198+
val := pyraconv.ToString(value)
200199
t, err := dateparse.ParseAny(val)
201200
if err != nil {
202201
} else {
203202
return t.String()
204-
}
203+
}
205204
return val
206205
}
207206

208207
func RawIntFormat(value interface{}) string {
209-
val := pyraconv.ToInt64(value)
210-
return fmtutils.RawIntFormat(val)
208+
val := pyraconv.ToInt64(value)
209+
return fmtutils.RawIntFormat(val)
211210
}
212211

213212
func RawFloatFormat(value interface{}, places interface{}) string {
214-
val := pyraconv.ToFloat64(value)
215-
pl := pyraconv.ToInt64(places)
216-
return fmtutils.RawFloatFormat(val, int(pl))
213+
val := pyraconv.ToFloat64(value)
214+
pl := pyraconv.ToInt64(places)
215+
return fmtutils.RawFloatFormat(val, int(pl))
217216
}
218217

219218
func Int(value interface{}) int {
220-
if value != nil {
221-
return int(pyraconv.ToInt64(value))
222-
}
223-
return 0
219+
if value != nil {
220+
return int(pyraconv.ToInt64(value))
221+
}
222+
return 0
224223
}
225224

226225
func ByteFormat(value interface{}, places interface{}) string {
227-
val := pyraconv.ToFloat64(value)
228-
pl := pyraconv.ToInt64(places)
229-
result := fmtutils.ByteFormat(val, int(pl));
230-
return Nobr(result);
226+
val := pyraconv.ToFloat64(value)
227+
pl := pyraconv.ToInt64(places)
228+
result := fmtutils.ByteFormat(val, int(pl))
229+
return Nobr(result)
231230
}

0 commit comments

Comments
 (0)