From f84b1115ca459dd8af3c2110556534df1dee27ba Mon Sep 17 00:00:00 2001 From: Luke Bjerring Date: Mon, 27 Nov 2017 10:53:18 -0500 Subject: [PATCH] Migrate /test-runs and / to use /api/runs --- components/runs.html | 8 +++++++- components/wpt-runs.html | 2 +- params.go | 13 ++++++++++--- templates/test-runs.html | 2 +- test_handler.go | 14 ++------------ test_run_handler.go | 41 +++++++++------------------------------- 6 files changed, 30 insertions(+), 50 deletions(-) diff --git a/components/runs.html b/components/runs.html index ebfac327d..dd87f1df6 100644 --- a/components/runs.html +++ b/components/runs.html @@ -51,7 +51,13 @@ return response.json() }) ) - this.testRuns = fetches.filter(e => e) // Filter unresolved fetches. + // Filter unresolved fetches and flatten any array-fetches into the array. + const nonEmpty = fetches.filter(e => e) + const flattened = nonEmpty + .reduce((sum, item) => { + return Array.isArray(item) ? sum.concat(item) : [...sum, item] + }, []) + this.testRuns = flattened } } } diff --git a/components/wpt-runs.html b/components/wpt-runs.html index 5e6bab9c7..4fdd7cabc 100644 --- a/components/wpt-runs.html +++ b/components/wpt-runs.html @@ -136,7 +136,7 @@ } _computeDateTooltip(date) { - return date.toDateString() + return date && date.toDateString() } async connectedCallback () { diff --git a/params.go b/params.go index 276864720..0da581f44 100644 --- a/params.go +++ b/params.go @@ -96,12 +96,19 @@ func ParseBrowsersParam(r *http.Request) (browsers []string, err error) { return browsers, nil } -// ParseMaxCountParam parses the 'max-count' parameter as an integer. +// ParseMaxCountParam parses the 'max-count' parameter as an integer, or returns 1 if no param +// is present, or on error. func ParseMaxCountParam(r *http.Request) (count int, err error) { - count = MaxCountDefaultValue + return ParseMaxCountParamWithDefault(r, MaxCountDefaultValue) +} + +// ParseMaxCountParamWithDefault parses the 'max-count' parameter as an integer, or returns the +// default when no param is present, or on error. +func ParseMaxCountParamWithDefault(r *http.Request, defaultValue int) (count int, err error) { + count = defaultValue if maxCountParam := r.URL.Query().Get("max-count"); maxCountParam != "" { if count, err = strconv.Atoi(maxCountParam); err != nil { - return MaxCountDefaultValue, err + return defaultValue, err } if count < MaxCountMinValue { count = MaxCountMinValue diff --git a/templates/test-runs.html b/templates/test-runs.html index 75cc8c38b..b2e88100b 100644 --- a/templates/test-runs.html +++ b/templates/test-runs.html @@ -10,7 +10,7 @@
{{ template "_header.html" }}
- +
{{ template "_ga.html" }} diff --git a/test_handler.go b/test_handler.go index 5d83bd4ab..74f78c83a 100644 --- a/test_handler.go +++ b/test_handler.go @@ -34,18 +34,8 @@ func testHandler(w http.ResponseWriter, r *http.Request) { return } - var testRunSources []string - var browserNames []string - browserNames, err = GetBrowserNames() - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - - const sourceURL = `/api/run?browser=%s&sha=%s` - for _, browserName := range browserNames { - testRunSources = append(testRunSources, fmt.Sprintf(sourceURL, browserName, runSHA)) - } + const sourceURL = `/api/runs?sha=%s` + testRunSources := []string{fmt.Sprintf(sourceURL, runSHA)} testRunSourcesBytes, err := json.Marshal(testRunSources) if err != nil { diff --git a/test_run_handler.go b/test_run_handler.go index 262b03f95..c4a3dbfac 100644 --- a/test_run_handler.go +++ b/test_run_handler.go @@ -16,11 +16,8 @@ package wptdashboard import ( "encoding/json" + "fmt" "net/http" - "sort" - - "google.golang.org/appengine" - "google.golang.org/appengine/datastore" ) // testRunsHandler handles GET/POST requests to /test-runs @@ -37,38 +34,24 @@ func testRunsHandler(w http.ResponseWriter, r *http.Request) { } func handleTestRunGet(w http.ResponseWriter, r *http.Request) { - ctx := appengine.NewContext(r) - var err error - var browserNames []string - if browserNames, err = GetBrowserNames(); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + maxCount, err := ParseMaxCountParamWithDefault(r, 100) + if err != nil { + http.Error(w, "Invalid max-count param: "+err.Error(), http.StatusBadRequest) return } - - baseQuery := datastore.NewQuery("TestRun").Order("-CreatedAt").Limit(100) - - var runs []TestRun - for _, browserName := range browserNames { - var queryResults []TestRun - query := baseQuery.Filter("BrowserName =", browserName) - if _, err := query.GetAll(ctx, &queryResults); err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) - return - } - runs = append(runs, queryResults...) - } - sort.Sort(byCreatedAtDesc(runs)) + sourceURL := fmt.Sprintf(`/api/runs?max-count=%d`, maxCount) // Serialize the data + pipe through the test-runs.html template. - testRunsBytes, err := json.Marshal(runs) + testRunSourcesBytes, err := json.Marshal([]string{sourceURL}) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } + data := struct { - TestRuns string + TestRunSources string }{ - string(testRunsBytes), + string(testRunSourcesBytes), } if err := templates.ExecuteTemplate(w, "test-runs.html", data); err != nil { @@ -76,9 +59,3 @@ func handleTestRunGet(w http.ResponseWriter, r *http.Request) { return } } - -type byCreatedAtDesc []TestRun - -func (a byCreatedAtDesc) Len() int { return len(a) } -func (a byCreatedAtDesc) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a byCreatedAtDesc) Less(i, j int) bool { return a[i].CreatedAt.Before(a[j].CreatedAt) }