Skip to content
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

TRT-1615: Create api to use the new variant registry #1616

Merged
merged 1 commit into from May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 62 additions & 0 deletions pkg/api/component_report.go
Expand Up @@ -115,6 +115,15 @@ func GetComponentTestVariantsFromBigQuery(client *bqcachedclient.Client, gcsBuck
return getDataFromCacheOrGenerate[apitype.ComponentReportTestVariants](client.Cache, cache.RequestOptions{}, GetPrefixedCacheKey("TestVariants~", generator), generator.GenerateVariants, apitype.ComponentReportTestVariants{})
}

func GetJobVariantsFromBigQuery(client *bqcachedclient.Client, gcsBucket string) (apitype.JobVariants, []error) {
generator := componentReportGenerator{
client: client,
gcsBucket: gcsBucket,
}

return getDataFromCacheOrGenerate[apitype.JobVariants](client.Cache, cache.RequestOptions{}, GetPrefixedCacheKey("TestAllVariants~", generator), generator.GenerateJobVariants, apitype.JobVariants{})
}

func GetComponentReportFromBigQuery(client *bqcachedclient.Client, prowURL, gcsBucket string,
baseRelease, sampleRelease apitype.ComponentReportRequestReleaseOptions,
testIDOption apitype.ComponentReportRequestTestIdentificationOptions,
Expand Down Expand Up @@ -215,6 +224,59 @@ func (c *componentReportGenerator) GenerateVariants() (apitype.ComponentReportTe
}, errs
}

func (c *componentReportGenerator) GenerateJobVariants() (apitype.JobVariants, []error) {
errs := []error{}
variants := apitype.JobVariants{Variants: map[string][]string{}}
queryString := fmt.Sprintf(`SELECT variant_name, ARRAY_AGG(DISTINCT variant_value ORDER BY variant_value) AS variant_values
FROM
%s.job_variants
WHERE
variant_value!=""
GROUP BY
variant_name`, c.client.Dataset)
query := c.client.BQ.Query(queryString)
it, err := query.Read(context.TODO())
if err != nil {
log.WithError(err).Errorf("error querying variants from bigquery for %s", queryString)
return variants, []error{err}
}

floatVariants := sets.NewString("FromRelease", "FromReleaseMajor", "FromReleaseMinor", "Release", "ReleaseMajor", "ReleaseMinor")
for {
row := apitype.JobVariant{}
err := it.Next(&row)
if err == iterator.Done {
break
}
if err != nil {
wrappedErr := errors.Wrapf(err, "error fetching variant row")
log.WithError(err).Error("error fetching variants from bigquery")
errs = append(errs, wrappedErr)
return variants, errs
}

// Sort all releases in proper orders
if floatVariants.Has(row.VariantName) {
sort.Slice(row.VariantValues, func(i, j int) bool {
iStrings := strings.Split(row.VariantValues[i], ".")
jStrings := strings.Split(row.VariantValues[j], ".")
for idx, iString := range iStrings {
if iValue, err := strconv.ParseInt(iString, 10, 32); err == nil {
if jValue, err := strconv.ParseInt(jStrings[idx], 10, 32); err == nil {
if iValue != jValue {
return iValue < jValue
}
}
}
}
return false
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Impressive sorting.

}
variants.Variants[row.VariantName] = row.VariantValues
}
return variants, nil
}

func (c *componentReportGenerator) GenerateReport() (apitype.ComponentReport, []error) {
before := time.Now()
componentReportTestStatus, errs := c.GenerateComponentReportTestStatus()
Expand Down
11 changes: 11 additions & 0 deletions pkg/apis/api/types.go
Expand Up @@ -1026,6 +1026,17 @@ type ComponentReportTestVariants struct {
Variant []string `json:"variant,omitempty"`
}

// JobVariant defines a variant and the possible values
type JobVariant struct {
VariantName string `bigquery:"variant_name"`
VariantValues []string `bigquery:"variant_values"`
}

// JobVariants contains all variants supported in the system.
type JobVariants struct {
Variants map[string][]string `json:"variants,omitempty"`
}

var FailureRiskLevelNone = RiskLevel{Name: "None", Level: 0}
var FailureRiskLevelLow = RiskLevel{Name: "Low", Level: 1}
var FailureRiskLevelUnknown = RiskLevel{Name: "Unknown", Level: 25}
Expand Down
29 changes: 29 additions & 0 deletions pkg/sippyserver/server.go
Expand Up @@ -627,6 +627,29 @@ func (s *Server) jsonComponentTestVariantsFromBigQuery(w http.ResponseWriter, re
api.RespondWithJSON(http.StatusOK, w, outputs)
}

func (s *Server) jsonJobVariantsFromBigQuery(w http.ResponseWriter, req *http.Request) {
if s.bigQueryClient == nil {
api.RespondWithJSON(http.StatusBadRequest, w, map[string]interface{}{
"code": http.StatusBadRequest,
"message": "job variants API is only available when google-service-account-credential-file is configured",
})
return
}
outputs, errs := api.GetJobVariantsFromBigQuery(s.bigQueryClient, s.gcsBucket)
if len(errs) > 0 {
log.Warningf("%d errors were encountered while querying job variants from big query:", len(errs))
for _, err := range errs {
log.Error(err.Error())
}
api.RespondWithJSON(http.StatusInternalServerError, w, map[string]interface{}{
"code": http.StatusInternalServerError,
"message": fmt.Sprintf("error querying job variants from big query: %v", errs),
})
return
}
api.RespondWithJSON(http.StatusOK, w, outputs)
}

func (s *Server) jsonComponentReportFromBigQuery(w http.ResponseWriter, req *http.Request) {
baseRelease, sampleRelease, testIDOption, variantOption, excludeOption, advancedOption, cacheOption, err := s.parseComponentReportRequest(req)
if err != nil {
Expand Down Expand Up @@ -1451,6 +1474,12 @@ func (s *Server) Serve() {
Capabilities: []string{LocalDBCapability},
HandlerFunc: s.jsonJobBugsFromDB,
},
{
EndpointPath: "/api/job_variants",
Description: "Reports all job variants defined in BigQuery",
Capabilities: []string{ComponentReadinessCapability},
HandlerFunc: s.jsonJobVariantsFromBigQuery,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like all other apis under /api/jobs/ are specific to a job. This new one is not. Maybe not worth worrying about, but we could do /api/job_variants/ or /api/variants to designate that we're operating on a different resource/?

We could also end up colliding with our wish to have an API to fetch the variants for a specific job. Probably should try to find another path to move it to.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/api/variants is already taken. :) Let me do /api/job_variants/ for now. We need to clean up on those old variants API later.

{
EndpointPath: "/api/pull_requests",
Description: "Reports on pull requests",
Expand Down