diff --git a/result_set.go b/result_set.go index d9f2db21..a3eae91b 100644 --- a/result_set.go +++ b/result_set.go @@ -12,6 +12,7 @@ import ( "bytes" "encoding/json" "fmt" + "regexp" "sort" "strings" "time" @@ -1270,24 +1271,36 @@ func (res ResultSet) MakePlanByRow() [][]interface{} { } if planNodeDesc.IsSetProfiles() { - var strArr []string + var profileArr []string for i, profile := range planNodeDesc.GetProfiles() { - otherStats := profile.GetOtherStats() - if otherStats != nil { - strArr = append(strArr, "{") - } - s := fmt.Sprintf("ver: %d, rows: %d, execTime: %dus, totalTime: %dus", - i, profile.GetRows(), profile.GetExecDurationInUs(), profile.GetTotalDurationInUs()) - strArr = append(strArr, s) - - for k, v := range otherStats { - strArr = append(strArr, fmt.Sprintf("%s: %s", k, v)) - } - if otherStats != nil { - strArr = append(strArr, "}") + var statArr []string + statArr = append(statArr, fmt.Sprintf("\"version\":%d", i)) + statArr = append(statArr, fmt.Sprintf("\"rows\":%d", profile.GetRows())) + statArr = append(statArr, fmt.Sprintf("\"execTime\":\"%d(us)\"", profile.GetExecDurationInUs())) + statArr = append(statArr, fmt.Sprintf("\"totalTime\":\"%d(us)\"", profile.GetTotalDurationInUs())) + for k, v := range profile.GetOtherStats() { + s := string(v) + if matched, err := regexp.Match(`^[^{(\[]\w+`, v); err == nil && matched { + if !strings.HasPrefix(s, "\"") { + s = fmt.Sprintf("\"%s", s) + } + if !strings.HasSuffix(s, "\"") { + s = fmt.Sprintf("%s\"", s) + } + } + statArr = append(statArr, fmt.Sprintf("\"%s\": %s", k, s)) } + sort.Strings(statArr) + statStr := fmt.Sprintf("{%s}", strings.Join(statArr, ",\n")) + profileArr = append(profileArr, statStr) + } + allProfiles := strings.Join(profileArr, ",\n") + if len(profileArr) > 1 { + allProfiles = fmt.Sprintf("[%s]", allProfiles) } - row = append(row, strings.Join(strArr, "\n")) + var buffer bytes.Buffer + json.Indent(&buffer, []byte(allProfiles), "", " ") + row = append(row, string(buffer.Bytes())) } else { row = append(row, "") }