| // Copyright 2022 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package main |
| |
| import ( |
| "context" |
| |
| "cloud.google.com/go/bigquery" |
| "github.com/maruel/subcommands" |
| "go.chromium.org/luci/auth" |
| "go.chromium.org/luci/auth/client/authcli" |
| "golang.org/x/exp/constraints" |
| "google.golang.org/api/iterator" |
| ) |
| |
| type commonFlags struct { |
| subcommands.CommandRunBase |
| authFlags authcli.Flags |
| project string |
| |
| parsedAuthOpts auth.Options |
| } |
| |
| func (c *commonFlags) Init(authOpts auth.Options) { |
| c.authFlags = authcli.Flags{} |
| c.authFlags.Register(&c.Flags, authOpts) |
| } |
| |
| func (c *commonFlags) Parse() error { |
| var err error |
| c.parsedAuthOpts, err = c.authFlags.Options() |
| if err != nil { |
| return err |
| } |
| return nil |
| } |
| |
| func runQuery[T any](ctx context.Context, client *bigquery.Client, query string, params map[string]any) ([]T, error) { |
| q := client.Query(query) |
| for k, v := range params { |
| q.Parameters = append(q.Parameters, bigquery.QueryParameter{Name: k, Value: v}) |
| } |
| iter, err := q.Read(ctx) |
| if err != nil { |
| return nil, err |
| } |
| |
| var rows []T |
| for { |
| var row T |
| err := iter.Next(&row) |
| if err == iterator.Done { |
| break |
| } else if err != nil { |
| return nil, err |
| } |
| rows = append(rows, row) |
| } |
| |
| return rows, nil |
| } |
| |
| func average[T constraints.Float | constraints.Integer](nums []T) float64 { |
| if len(nums) == 0 { |
| panic("average of empty slice") |
| } |
| var sum T |
| for _, num := range nums { |
| sum += num |
| } |
| return float64(sum) / float64(len(nums)) |
| } |