fix pretty printing of active queries list to handle special characters and multiline queries

This commit is contained in:
Jaden Weiss 2020-06-04 13:40:34 -04:00
parent 023efcaba6
commit 1099a57945
No known key found for this signature in database
GPG key ID: 177F065773634B67

View file

@ -880,24 +880,24 @@ func (h *Handler) handleGetActiveQueries(w http.ResponseWriter, r *http.Request)
w.Header().Set("Content-Type", rtype)
switch rtype {
case "text/plain":
lines := make([]string, len(queries))
durations := make([]string, len(queries))
for i, q := range queries {
lines[i] = q.Age.String()
durations[i] = q.Age.String()
}
var maxlen int
for _, l := range lines {
for _, l := range durations {
if len(l) > maxlen {
maxlen = len(l)
}
}
spaces := strings.Repeat(" ", maxlen+2)
for i, l := range lines {
lines[i] += spaces[len(l):]
}
for i, q := range queries {
lines[i] += q.Query
_, err := fmt.Fprintf(w, "%*s%q\n", -(maxlen + 2), durations[i], q.Query)
if err != nil {
h.logger.Printf("sending GetActiveQueries response: %s", err)
return
}
}
if _, err := w.Write([]byte(strings.Join(lines, "\n") + "\n")); err != nil {
if _, err := w.Write([]byte{'\n'}); err != nil {
h.logger.Printf("sending GetActiveQueries response: %s", err)
}
case "application/json":