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

Add projectId to /data/queries #190

Merged
merged 3 commits into from
Jun 3, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions api/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ var (
}

userIdContextKey = &contextKeys{"userId"}
projectIdContextKey = &contextKeys{"projectId"}
isCallerAdminContextKey = &contextKeys{"isCallerAdmin"}
)

Expand Down Expand Up @@ -94,6 +95,11 @@ func authorization(authUrl string) middleware {
r = r.WithContext(ctx)
}

if projectID := authRes.Header.Get("X-Livepeer-Project-Id"); projectID != "" {
ctx := context.WithValue(r.Context(), projectIdContextKey, projectID)
r = r.WithContext(ctx)
}

if isCallerAdmin, err := strconv.ParseBool(authRes.Header.Get("X-Livepeer-Is-Caller-Admin")); err == nil {
ctx := context.WithValue(r.Context(), isCallerAdminContextKey, isCallerAdmin)
r = r.WithContext(ctx)
Expand Down Expand Up @@ -139,6 +145,13 @@ func callerUserId(r *http.Request) string {
return ""
}

func callerProjectId(r *http.Request) string {
if val, ok := r.Context().Value(projectIdContextKey).(string); ok {
return val
}
return ""
}

func isCallerAdmin(r *http.Request) bool {
if val, ok := r.Context().Value(isCallerAdminContextKey).(bool); ok {
return val
Expand Down
2 changes: 2 additions & 0 deletions api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ func (h *apiHandler) resolveViewershipQuerySpec(r *http.Request) (views.QuerySpe
if userId == "" {
return views.QuerySpec{}, http.StatusInternalServerError, []error{errors.New("request not authenticated")}
}
projectId := callerProjectId(r)

qs := r.URL.Query()
assetID, streamID := qs.Get("assetId"), qs.Get("streamId")
Expand All @@ -442,6 +443,7 @@ func (h *apiHandler) resolveViewershipQuerySpec(r *http.Request) (views.QuerySpe
TimeStep: qs.Get("timeStep"),
Filter: views.QueryFilter{
UserID: userId,
ProjectID: projectId,
PlaybackID: qs.Get("playbackId"),
CreatorID: qs.Get("creatorId"),
},
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/golang/glog v1.1.1
github.com/google/uuid v1.6.0
github.com/ipfs/go-cid v0.4.1
github.com/livepeer/go-api-client v0.4.6
github.com/livepeer/go-api-client v0.4.23
github.com/peterbourgon/ff v1.7.1
github.com/prometheus/client_golang v1.14.0
github.com/prometheus/common v0.42.0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0 h1:P6pPBnrTSX3DEVR4fDembhR
github.com/lann/ps v0.0.0-20150810152359-62de8c46ede0/go.mod h1:vmVJ0l/dxyfGW6FmdpVm2joNMFikkuWg0EoCKLGUMNw=
github.com/livepeer/go-api-client v0.4.6 h1:Eo9mq5k9gnu8fgclbT7ibQdTgBV7a/NDIBIuYXHMzV0=
github.com/livepeer/go-api-client v0.4.6/go.mod h1:Jdb+RI7JyzEZOHd1GUuKofwFDKMO/btTa80SdpUpYQw=
github.com/livepeer/go-api-client v0.4.23-0.20240523110406-c26cb9ff5975 h1:uJ1aVcuuWLP7efVffq0pqPRZiEqkh1r3iEDSsw+1+xw=
github.com/livepeer/go-api-client v0.4.23-0.20240523110406-c26cb9ff5975/go.mod h1:Jdb+RI7JyzEZOHd1GUuKofwFDKMO/btTa80SdpUpYQw=
github.com/livepeer/go-api-client v0.4.23 h1:buvWJGxLzwsmvkXaxdI2bOUOAiYZJtcTMfFWMqnOrco=
github.com/livepeer/go-api-client v0.4.23/go.mod h1:Jdb+RI7JyzEZOHd1GUuKofwFDKMO/btTa80SdpUpYQw=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
Expand Down
6 changes: 6 additions & 0 deletions views/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@
From("viewership_current_counts").
Where("user_id = ?", spec.Filter.UserID).
Limit(maxClickhouseResultRows + 1)
if spec.Filter.ProjectID != "" {
query = query.Where("project_id = ?", spec.Filter.ProjectID)

Check warning on line 100 in views/clickhouse.go

View check run for this annotation

Codecov / codecov/patch

views/clickhouse.go#L99-L100

Added lines #L99 - L100 were not covered by tests
}
return toSqlWithFiltersAndBreakdown(query, spec)
}

Expand All @@ -110,6 +113,9 @@
GroupBy("timestamp_ts").
OrderBy("timestamp_ts desc").
Limit(maxClickhouseResultRows + 1)
if spec.Filter.ProjectID != "" {
query = query.Where("project_id = ?", spec.Filter.ProjectID)

Check warning on line 117 in views/clickhouse.go

View check run for this annotation

Codecov / codecov/patch

views/clickhouse.go#L116-L117

Added lines #L116 - L117 were not covered by tests
}
if spec.From != nil {
// timestamp_ts is DateTime, but it's automatically converted to seconds
query = query.Where("timestamp_ts >= ?", spec.From.UnixMilli()/1000)
Expand Down
6 changes: 6 additions & 0 deletions views/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@
if res.Filter.UserID != asset.UserID {
return QuerySpec{}, fmt.Errorf("error getting asset: verify that asset exists and you are using proper credentials")
}
if res.Filter.ProjectID != "" && asset.ProjectID != "" && res.Filter.ProjectID != asset.ProjectID {
return QuerySpec{}, fmt.Errorf("error getting asset: verify that asset exists and you are using proper credentials related to the project")

Check warning on line 181 in views/client.go

View check run for this annotation

Codecov / codecov/patch

views/client.go#L180-L181

Added lines #L180 - L181 were not covered by tests
}
}
} else if streamID != "" {
var stream *livepeer.Stream
Expand All @@ -186,6 +189,9 @@
if res.Filter.UserID != stream.UserID {
return QuerySpec{}, fmt.Errorf("error getting stream: verify that stream exists and you are using proper credentials")
}
if res.Filter.ProjectID != "" && stream.ProjectID != "" && res.Filter.ProjectID != stream.UserID {
return QuerySpec{}, fmt.Errorf("error getting stream: verify that stream exists and you are using proper credentials related to the project")

Check warning on line 193 in views/client.go

View check run for this annotation

Codecov / codecov/patch

views/client.go#L192-L193

Added lines #L192 - L193 were not covered by tests
}
}
}

Expand Down
1 change: 1 addition & 0 deletions views/query_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type QueryFilter struct {
PlaybackID string
CreatorID string
UserID string
ProjectID string
}

type QuerySpec struct {
Expand Down
Loading