golang.org/x/build@v0.0.0-20240506185731-218518f32b70/perfdata/app/query.go (about) 1 // Copyright 2017 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package app 6 7 import ( 8 "encoding/json" 9 "net/http" 10 "strconv" 11 12 "golang.org/x/perf/storage/benchfmt" 13 ) 14 15 func (a *App) search(w http.ResponseWriter, r *http.Request) { 16 ctx := requestContext(r) 17 18 if err := r.ParseForm(); err != nil { 19 http.Error(w, err.Error(), 500) 20 return 21 } 22 23 q := r.Form.Get("q") 24 if q == "" { 25 http.Error(w, "missing q parameter", 400) 26 return 27 } 28 29 query := a.DB.Query(q) 30 defer query.Close() 31 32 infof(ctx, "query: %s", query.Debug()) 33 34 w.Header().Set("Content-Type", "text/plain; charset=utf-8") 35 bw := benchfmt.NewPrinter(w) 36 for query.Next() { 37 if err := bw.Print(query.Result()); err != nil { 38 http.Error(w, err.Error(), 500) 39 return 40 } 41 } 42 if err := query.Err(); err != nil { 43 errorf(ctx, "query returned error: %v", err) 44 http.Error(w, err.Error(), 500) 45 return 46 } 47 } 48 49 // uploads serves a list of upload IDs on /uploads. 50 // If the query parameter q is provided, only uploads containing matching records are returned. 51 // The format of the result is "<count> <uploadid>\n" where count is the number of matching records. 52 // The lines are sorted in order from most to least recent. 53 // If the query parameter limit is provided, only the most recent limit upload IDs are returned. 54 // If limit is not provided, the most recent 1000 upload IDs are returned. 55 func (a *App) uploads(w http.ResponseWriter, r *http.Request) { 56 ctx := requestContext(r) 57 58 if err := r.ParseForm(); err != nil { 59 http.Error(w, err.Error(), 500) 60 return 61 } 62 63 q := r.Form.Get("q") 64 65 limit := 1000 66 limitStr := r.Form.Get("limit") 67 if limitStr != "" { 68 var err error 69 limit, err = strconv.Atoi(limitStr) 70 if err != nil { 71 http.Error(w, "invalid limit parameter", 400) 72 return 73 } 74 } 75 76 res := a.DB.ListUploads(q, r.Form["extra_label"], limit) 77 defer res.Close() 78 79 infof(ctx, "query: %s", res.Debug()) 80 81 w.Header().Set("Content-Type", "application/json") 82 e := json.NewEncoder(w) 83 for res.Next() { 84 ui := res.Info() 85 if err := e.Encode(&ui); err != nil { 86 errorf(ctx, "failed to encode JSON: %v", err) 87 http.Error(w, err.Error(), 500) 88 return 89 } 90 } 91 if err := res.Err(); err != nil { 92 http.Error(w, err.Error(), 500) 93 return 94 } 95 }