Skip to content
This repository has been archived by the owner on Nov 6, 2019. It is now read-only.

Commit

Permalink
Migrate /test-runs and / to use /api/runs (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukebjerring authored Nov 29, 2017
1 parent 09c5ddd commit ec535d6
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 50 deletions.
8 changes: 7 additions & 1 deletion components/runs.html
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion components/wpt-runs.html
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
}

_computeDateTooltip (date) {
return date.toDateString()
return date && date.toDateString()
}

async connectedCallback () {
Expand Down
13 changes: 10 additions & 3 deletions params.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion templates/test-runs.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<div id="content">
{{ template "_header.html" }}
<div>
<wpt-runs test-runs="{{ .TestRuns }}"></wpt-runs>
<wpt-runs test-run-resources="{{ .TestRunSources }}"></wpt-runs>
</div>
</div>
{{ template "_ga.html" }}
Expand Down
14 changes: 2 additions & 12 deletions test_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
41 changes: 9 additions & 32 deletions test_run_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,48 +34,28 @@ 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 {
http.Error(w, err.Error(), http.StatusInternalServerError)
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) }

0 comments on commit ec535d6

Please sign in to comment.