Skip to content

Combine desktop and mobile for "Missing one" chart #1461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 47 additions & 4 deletions backend/pkg/httpserver/list_missing_one_implementation_counts.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ import (
"github.com/GoogleChrome/webstatus.dev/lib/gen/openapi/backend"
)

var ErrNoMatchingMobileBrowser = errors.New("browser does not have a matching mobile browser")

// GetDesktopsMobileProduct returns the mobile version of the given desktop browser.
func GetDesktopsMobileProduct(browser backend.BrowserPathParam) (backend.BrowserPathParam, error) {
switch browser {
case backend.Chrome:
return backend.ChromeAndroid, nil
case backend.Firefox:
return backend.FirefoxAndroid, nil
case backend.Safari:
return backend.SafariIos, nil
case backend.Edge, backend.ChromeAndroid, backend.FirefoxAndroid, backend.SafariIos:
return backend.BrowserPathParam(""), ErrNoMatchingMobileBrowser
}

return backend.BrowserPathParam(""), ErrNoMatchingMobileBrowser
}

// ListMissingOneImplementationCounts implements backend.StrictServerInterface.
// nolint: ireturn // Signature generated from openapi
func (s *Server) ListMissingOneImplementationCounts(
Expand All @@ -34,13 +52,38 @@ func (s *Server) ListMissingOneImplementationCounts(
if found {
return cachedResponse, nil
}
otherBrowsers := make([]string, len(request.Params.Browser))
for i := 0; i < len(request.Params.Browser); i++ {
otherBrowsers[i] = string(request.Params.Browser[i])

var targetBrowsers = []string{}
targetBrowsers = append(targetBrowsers, string(request.Browser))
if request.Params.IncludeBaselineMobileBrowsers != nil {
targetMobileBrowser, err := GetDesktopsMobileProduct(request.Browser)
// nolint: nilerr // Error is used for message, but 400 should be returned rather than 500.
if err != nil {
return backend.ListMissingOneImplementationCounts400JSONResponse{
Code: 400,
Message: err.Error(),
}, nil
}
targetBrowsers = append(targetBrowsers, string(targetMobileBrowser))
}

otherBrowsers := [][]string{}
for i := range len(request.Params.Browser) {
otherBrowser := []string{}
otherBrowser = append(otherBrowser, string(request.Params.Browser[i]))
// Add the mobile version of the browser if include_baseline_mobile_browsers is set.
if request.Params.IncludeBaselineMobileBrowsers != nil {
matchingMobileBrowser, err := GetDesktopsMobileProduct(request.Params.Browser[i])
if err == nil {
otherBrowser = append(otherBrowser, string(matchingMobileBrowser))
}
}
otherBrowsers = append(otherBrowsers, otherBrowser)
}

page, err := s.wptMetricsStorer.ListMissingOneImplCounts(
ctx,
string(request.Browser),
targetBrowsers,
otherBrowsers,
request.Params.StartAt.Time,
request.Params.EndAt.Time,
Expand Down
108 changes: 72 additions & 36 deletions backend/pkg/httpserver/list_missing_one_implementation_counts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,18 @@ func TestListMissingOneImplementationCounts(t *testing.T) {
{
name: "Success Case - no optional params - use defaults",
mockConfig: &MockListMissingOneImplCountsConfig{
expectedTargetBrowser: "chrome",
expectedOtherBrowsers: []string{"edge", "firefox", "safari"},
expectedStartAt: time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC),
expectedEndAt: time.Date(2000, time.January, 10, 0, 0, 0, 0, time.UTC),
expectedPageSize: 100,
expectedPageToken: nil,
pageToken: nil,
err: nil,
expectedTargetBrowsers: []string{"chrome"},
expectedOtherBrowsers: [][]string{
{"edge"},
{"firefox"},
{"safari"},
},
expectedStartAt: time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC),
expectedEndAt: time.Date(2000, time.January, 10, 0, 0, 0, 0, time.UTC),
expectedPageSize: 100,
expectedPageToken: nil,
pageToken: nil,
err: nil,
page: &backend.BrowserReleaseFeatureMetricsPage{
Metadata: &backend.PageMetadata{
NextPageToken: nil,
Expand Down Expand Up @@ -143,13 +147,16 @@ func TestListMissingOneImplementationCounts(t *testing.T) {
{
name: "Success Case - include optional params",
mockConfig: &MockListMissingOneImplCountsConfig{
expectedTargetBrowser: "chrome",
expectedOtherBrowsers: []string{"edge", "firefox", "safari"},
expectedStartAt: time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC),
expectedEndAt: time.Date(2000, time.January, 10, 0, 0, 0, 0, time.UTC),
expectedPageSize: 50,
expectedPageToken: inputPageToken,
err: nil,
expectedTargetBrowsers: []string{"chrome", "chrome_android"},
expectedOtherBrowsers: [][]string{
{"firefox", "firefox_android"},
{"safari", "safari_ios"},
},
expectedStartAt: time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC),
expectedEndAt: time.Date(2000, time.January, 10, 0, 0, 0, 0, time.UTC),
expectedPageSize: 50,
expectedPageToken: inputPageToken,
err: nil,
page: &backend.BrowserReleaseFeatureMetricsPage{
Metadata: &backend.PageMetadata{
NextPageToken: nextPageToken,
Expand All @@ -171,7 +178,7 @@ func TestListMissingOneImplementationCounts(t *testing.T) {
{
Key: `ListMissingOneImplementationCounts-{"browser":"chrome","Params":{"startAt":"2000-01-01",` +
`"endAt":"2000-01-10","page_token":"input-token",` +
`"page_size":50,"browser":["edge","firefox","safari"]}}`,
`"page_size":50,"include_baseline_mobile_browsers":true,"browser":["firefox","safari"]}}`,
Value: nil,
Err: cachetypes.ErrCachedDataNotFound,
},
Expand All @@ -180,7 +187,7 @@ func TestListMissingOneImplementationCounts(t *testing.T) {
{
Key: `ListMissingOneImplementationCounts-{"browser":"chrome","Params":{"startAt":"2000-01-01",` +
`"endAt":"2000-01-10","page_token":"input-token",` +
`"page_size":50,"browser":["edge","firefox","safari"]}}`,
`"page_size":50,"include_baseline_mobile_browsers":true,"browser":["firefox","safari"]}}`,
Value: []byte(
`{"data":[{"count":10,"timestamp":"2000-01-10T00:00:00Z"},` +
`{"count":9,"timestamp":"2000-01-09T00:00:00Z"}],` +
Expand Down Expand Up @@ -208,7 +215,7 @@ func TestListMissingOneImplementationCounts(t *testing.T) {
}`),
request: httptest.NewRequest(http.MethodGet,
"/v1/stats/features/browsers/chrome/missing_one_implementation_counts?"+
"browser=edge&browser=firefox&browser=safari&"+
"browser=firefox&browser=safari&include_baseline_mobile_browsers=true&"+
"startAt=2000-01-01&endAt=2000-01-10&page_size=50&page_token="+*inputPageToken, nil),
},
{
Expand Down Expand Up @@ -253,15 +260,19 @@ func TestListMissingOneImplementationCounts(t *testing.T) {
{
name: "500 case",
mockConfig: &MockListMissingOneImplCountsConfig{
expectedTargetBrowser: "chrome",
expectedOtherBrowsers: []string{"edge", "firefox", "safari"},
expectedStartAt: time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC),
expectedEndAt: time.Date(2000, time.January, 10, 0, 0, 0, 0, time.UTC),
expectedPageSize: 100,
expectedPageToken: nil,
page: nil,
pageToken: nil,
err: errTest,
expectedTargetBrowsers: []string{"chrome"},
expectedOtherBrowsers: [][]string{
{"edge"},
{"firefox"},
{"safari"},
},
expectedStartAt: time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC),
expectedEndAt: time.Date(2000, time.January, 10, 0, 0, 0, 0, time.UTC),
expectedPageSize: 100,
expectedPageToken: nil,
page: nil,
pageToken: nil,
err: errTest,
},
expectedGetCalls: []*ExpectedGetCall{
{
Expand All @@ -283,15 +294,19 @@ func TestListMissingOneImplementationCounts(t *testing.T) {
{
name: "400 case - invalid page token",
mockConfig: &MockListMissingOneImplCountsConfig{
expectedTargetBrowser: "chrome",
expectedOtherBrowsers: []string{"edge", "firefox", "safari"},
expectedStartAt: time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC),
expectedEndAt: time.Date(2000, time.January, 10, 0, 0, 0, 0, time.UTC),
expectedPageSize: 100,
expectedPageToken: badPageToken,
page: nil,
pageToken: nil,
err: backendtypes.ErrInvalidPageToken,
expectedTargetBrowsers: []string{"chrome"},
expectedOtherBrowsers: [][]string{
{"edge"},
{"firefox"},
{"safari"},
},
expectedStartAt: time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC),
expectedEndAt: time.Date(2000, time.January, 10, 0, 0, 0, 0, time.UTC),
expectedPageSize: 100,
expectedPageToken: badPageToken,
page: nil,
pageToken: nil,
err: backendtypes.ErrInvalidPageToken,
},
expectedGetCalls: []*ExpectedGetCall{
{
Expand All @@ -309,6 +324,27 @@ func TestListMissingOneImplementationCounts(t *testing.T) {
"browser=edge&browser=firefox&browser=safari&"+
"startAt=2000-01-01&endAt=2000-01-10&page_token"+*badPageToken, nil),
},
{
name: "400 case - no matching mobile browser",
mockConfig: nil,
expectedGetCalls: []*ExpectedGetCall{
{
Key: `ListMissingOneImplementationCounts-{"browser":"edge","Params":{"startAt":"2000-01-01",` +
`"endAt":"2000-01-10","include_baseline_mobile_browsers":true,` +
`"browser":["chrome","firefox","safari"]}}`,
Value: nil,
Err: cachetypes.ErrCachedDataNotFound,
},
},
expectedCacheCalls: nil,
expectedCallCount: 0,
expectedResponse: testJSONResponse(400,
`{"code":400,"message":"browser does not have a matching mobile browser"}`),
request: httptest.NewRequest(http.MethodGet,
"/v1/stats/features/browsers/edge/missing_one_implementation_counts?"+
"browser=chrome&browser=firefox&browser=safari&"+
"startAt=2000-01-01&endAt=2000-01-10&include_baseline_mobile_browsers=true", nil),
},
}

for _, tc := range testCases {
Expand Down
32 changes: 28 additions & 4 deletions backend/pkg/httpserver/list_missing_one_implementation_features.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,37 @@ func (s *Server) ListMissingOneImplementationFeatures(
ctx context.Context,
request backend.ListMissingOneImplementationFeaturesRequestObject) (
backend.ListMissingOneImplementationFeaturesResponseObject, error) {
otherBrowsers := make([]string, len(request.Params.Browser))
for i := 0; i < len(request.Params.Browser); i++ {
otherBrowsers[i] = string(request.Params.Browser[i])

var targetBrowsers = []string{}
targetBrowsers = append(targetBrowsers, string(request.Browser))
if request.Params.IncludeBaselineMobileBrowsers != nil {
targetMobileBrowser, err := GetDesktopsMobileProduct(request.Browser)
if err != nil {
return backend.ListMissingOneImplementationFeatures400JSONResponse{
Code: 400,
Message: "browser does not have a matching mobile browser",
}, err
}
targetBrowsers = append(targetBrowsers, string(targetMobileBrowser))
}

otherBrowsers := [][]string{}
for i := range len(request.Params.Browser) {
otherBrowser := []string{}
otherBrowser = append(otherBrowser, string(request.Params.Browser[i]))
// Add the mobile version of the browser if include_baseline_mobile_browsers is set.
if request.Params.IncludeBaselineMobileBrowsers != nil {
matchingMobileBrowser, err := GetDesktopsMobileProduct(request.Params.Browser[i])
if err == nil {
otherBrowser = append(otherBrowser, string(matchingMobileBrowser))
}
}
otherBrowsers = append(otherBrowsers, otherBrowser)
}

page, err := s.wptMetricsStorer.ListMissingOneImplementationFeatures(
ctx,
string(request.Browser),
targetBrowsers,
otherBrowsers,
request.Date.Time,
getPageSizeOrDefault(request.Params.PageSize),
Expand Down
Loading
Loading