Skip to content

Commit

Permalink
views,usage: Move query specs to their own file
Browse files Browse the repository at this point in the history
  • Loading branch information
victorges committed Feb 28, 2024
1 parent 21cdd32 commit 5a94d8c
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 99 deletions.
25 changes: 0 additions & 25 deletions usage/bigquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,6 @@ import (
"google.golang.org/api/option"
)

type QueryFilter struct {
CreatorID string
UserID string
}

type QuerySpec struct {
TimeStep string
From, To *time.Time
Filter QueryFilter
BreakdownBy []string
}

var usageBreakdownFields = map[string]string{
"creatorId": "creator_id",
}

type FromToQuerySpec struct {
From, To *time.Time
}

var allowedTimeSteps = map[string]bool{
"hour": true,
"day": true,
}

type UsageSummaryRow struct {
UserID string `bigquery:"user_id"`
CreatorID bigquery.NullString `bigquery:"creator_id"`
Expand Down
19 changes: 0 additions & 19 deletions usage/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package usage
import (
"context"
"fmt"
"strings"

"cloud.google.com/go/bigquery"
livepeer "github.com/livepeer/go-api-client"
Expand Down Expand Up @@ -81,24 +80,6 @@ func toStringPtr(bqStr bigquery.NullString, asked bool) data.Nullable[string] {
return data.ToNullable(bqStr.StringVal, bqStr.Valid, asked)
}

func (q *QuerySpec) HasAnyBreakdown() bool {
return len(q.BreakdownBy) > 0 || q.TimeStep != ""
}

func (q *QuerySpec) hasBreakdownBy(e string) bool {
// callers always set `e` as a string literal so we can panic if it's not valid
if usageBreakdownFields[e] == "" {
panic(fmt.Sprintf("unknown breakdown field %q", e))
}

for _, a := range q.BreakdownBy {
if strings.EqualFold(a, e) {
return true
}
}
return false
}

func (c *Client) QuerySummaryWithBreakdown(ctx context.Context, spec QuerySpec) ([]Metric, error) {
summary, err := c.bigquery.QueryUsageSummaryWithBreakdown(ctx, spec)
if err != nil {
Expand Down
50 changes: 50 additions & 0 deletions usage/query_spec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package usage

import (
"fmt"
"strings"
"time"
)

type FromToQuerySpec struct {
From, To *time.Time
}

var allowedTimeSteps = map[string]bool{
"hour": true,
"day": true,
}

type QueryFilter struct {
CreatorID string
UserID string
}

type QuerySpec struct {
TimeStep string
From, To *time.Time
Filter QueryFilter
BreakdownBy []string
}

var usageBreakdownFields = map[string]string{
"creatorId": "creator_id",
}

func (q *QuerySpec) HasAnyBreakdown() bool {
return len(q.BreakdownBy) > 0 || q.TimeStep != ""
}

func (q *QuerySpec) hasBreakdownBy(e string) bool {
// callers always set `e` as a string literal so we can panic if it's not valid
if usageBreakdownFields[e] == "" {
panic(fmt.Sprintf("unknown breakdown field %q", e))
}

for _, a := range q.BreakdownBy {
if strings.EqualFold(a, e) {
return true
}
}
return false
}
40 changes: 0 additions & 40 deletions views/bigquery.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,6 @@ import (

const maxBigQueryResultRows = 10000

type QueryFilter struct {
PlaybackID string
CreatorID string
UserID string
}

type QuerySpec struct {
From, To *time.Time
TimeStep string
Filter QueryFilter
BreakdownBy []string
Detailed bool
}

var viewershipBreakdownFields = map[string]string{
"playbackId": "playback_id",
"dStorageUrl": "d_storage_url",
"deviceType": "device_type",
"device": "device",
"cpu": "cpu",
"os": "os",
"browser": "browser",
"browserEngine": "browser_engine",
"continent": "playback_continent_name",
"country": "playback_country_name",
"subdivision": "playback_subdivision_name",
"timezone": "playback_timezone",
"geohash": "playback_geo_hash",
"viewerId": "viewer_id",
"creatorId": "creator_id",
}

var allowedTimeSteps = map[string]bool{
"hour": true,
"day": true,
"week": true,
"month": true,
"year": true,
}

type ViewershipEventRow struct {
TimeInterval time.Time `bigquery:"time_interval"`

Expand Down
15 changes: 0 additions & 15 deletions views/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"strings"

"cloud.google.com/go/bigquery"
livepeer "github.com/livepeer/go-api-client"
Expand Down Expand Up @@ -212,17 +211,3 @@ func toFloat64Ptr(bqFloat bigquery.NullFloat64, asked bool) data.Nullable[float6
func toStringPtr(bqStr bigquery.NullString, asked bool) data.Nullable[string] {
return data.ToNullable(bqStr.StringVal, bqStr.Valid, asked)
}

func (q *QuerySpec) hasBreakdownBy(e string) bool {
// callers always set `e` as a string literal so we can panic if it's not valid
if viewershipBreakdownFields[e] == "" {
panic(fmt.Sprintf("unknown breakdown field %q", e))
}

for _, a := range q.BreakdownBy {
if strings.EqualFold(a, e) {
return true
}
}
return false
}
61 changes: 61 additions & 0 deletions views/query_spec.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package views

import (
"fmt"
"strings"
"time"
)

type QueryFilter struct {
PlaybackID string
CreatorID string
UserID string
}

type QuerySpec struct {
From, To *time.Time
TimeStep string
Filter QueryFilter
BreakdownBy []string
Detailed bool
}

var viewershipBreakdownFields = map[string]string{
"playbackId": "playback_id",
"dStorageUrl": "d_storage_url",
"deviceType": "device_type",
"device": "device",
"cpu": "cpu",
"os": "os",
"browser": "browser",
"browserEngine": "browser_engine",
"continent": "playback_continent_name",
"country": "playback_country_name",
"subdivision": "playback_subdivision_name",
"timezone": "playback_timezone",
"geohash": "playback_geo_hash",
"viewerId": "viewer_id",
"creatorId": "creator_id",
}

var allowedTimeSteps = map[string]bool{
"hour": true,
"day": true,
"week": true,
"month": true,
"year": true,
}

func (q *QuerySpec) hasBreakdownBy(e string) bool {

Check warning on line 49 in views/query_spec.go

View check run for this annotation

Codecov / codecov/patch

views/query_spec.go#L49

Added line #L49 was not covered by tests
// callers always set `e` as a string literal so we can panic if it's not valid
if viewershipBreakdownFields[e] == "" {
panic(fmt.Sprintf("unknown breakdown field %q", e))

Check warning on line 52 in views/query_spec.go

View check run for this annotation

Codecov / codecov/patch

views/query_spec.go#L51-L52

Added lines #L51 - L52 were not covered by tests
}

for _, a := range q.BreakdownBy {
if strings.EqualFold(a, e) {
return true

Check warning on line 57 in views/query_spec.go

View check run for this annotation

Codecov / codecov/patch

views/query_spec.go#L55-L57

Added lines #L55 - L57 were not covered by tests
}
}
return false

Check warning on line 60 in views/query_spec.go

View check run for this annotation

Codecov / codecov/patch

views/query_spec.go#L60

Added line #L60 was not covered by tests
}

0 comments on commit 5a94d8c

Please sign in to comment.