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 endpoint /views/internal/server/now #194

Merged
merged 1 commit into from
Jun 20, 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
27 changes: 27 additions & 0 deletions api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ func (h *apiHandler) viewershipHandler() chi.Router {
With(h.cache(false)).
MethodFunc("GET", fmt.Sprintf(`/{%s}/total`, assetIDParam), h.getTotalViews)

// realtime viewership server side (internal-only)
h.withMetrics(router, "query_realtime_server_viewership").
MethodFunc("GET", "/internal/server/now", h.queryRealtimeServerViewership())
thomshutt marked this conversation as resolved.
Show resolved Hide resolved

// total views public API
h.withMetrics(router, "query_total_viewership").
With(h.cache(false)).
Expand Down Expand Up @@ -558,6 +562,29 @@ func (h *apiHandler) getTotalViews(rw http.ResponseWriter, r *http.Request) {
respondJson(rw, http.StatusOK, totalViews)
}

func (h *apiHandler) queryRealtimeServerViewership() http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) {
userId := r.URL.Query().Get("userId")
leszko marked this conversation as resolved.
Show resolved Hide resolved
if userId == "" {
respondError(rw, http.StatusBadRequest, errors.New("userId is required"))
return
}

if !isCallerAdmin(r) {
respondError(rw, http.StatusForbidden, errors.New("only admins can query server-side viewership"))
return
}

metrics, err := h.views.QueryRealtimeServerViews(r.Context(), userId)
if err != nil {
respondError(rw, http.StatusInternalServerError, err)
return
}

respondJson(rw, http.StatusOK, metrics)
}
}

func (h *apiHandler) getStreamHealth(rw http.ResponseWriter, r *http.Request) {
respondJson(rw, http.StatusOK, getStreamStatus(r))
}
Expand Down
10 changes: 10 additions & 0 deletions views/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@
}}, nil
}

func (c *Client) QueryRealtimeServerViews(ctx context.Context, userId string) ([]Metric, error) {
viewCount, err := c.prom.QueryRealtimeViews(ctx, userId)
if err != nil {
return nil, fmt.Errorf("error querying start views: %w", err)

Check warning on line 113 in views/client.go

View check run for this annotation

Codecov / codecov/patch

views/client.go#L110-L113

Added lines #L110 - L113 were not covered by tests
}
return []Metric{{
ViewCount: viewCount,
}}, nil

Check warning on line 117 in views/client.go

View check run for this annotation

Codecov / codecov/patch

views/client.go#L115-L117

Added lines #L115 - L117 were not covered by tests
}

func (c *Client) QuerySummary(ctx context.Context, playbackID string) (*Metric, error) {
summary, err := c.bigquery.QueryViewsSummary(ctx, playbackID)
if err != nil {
Expand Down
31 changes: 20 additions & 11 deletions views/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,26 @@

func (c *Prometheus) QueryStartViews(ctx context.Context, asset *livepeer.Asset) (int64, error) {
query := startViewsQuery(asset.PlaybackID, asset.PlaybackRecordingID)
return c.queryInt64(ctx, query)

Check warning on line 36 in views/prometheus.go

View check run for this annotation

Codecov / codecov/patch

views/prometheus.go#L36

Added line #L36 was not covered by tests
}

func startViewsQuery(playbackID, playbackRecordingID string) string {
queryID := playbackID
if playbackRecordingID != "" {
queryID = fmt.Sprintf("(%s|%s)", playbackID, playbackRecordingID)

Check warning on line 42 in views/prometheus.go

View check run for this annotation

Codecov / codecov/patch

views/prometheus.go#L39-L42

Added lines #L39 - L42 were not covered by tests
}
return fmt.Sprintf(
`sum(increase(mist_playux_count{strm=~"video(rec)?\\+%s"} [1y]))`,
queryID,
)

Check warning on line 47 in views/prometheus.go

View check run for this annotation

Codecov / codecov/patch

views/prometheus.go#L44-L47

Added lines #L44 - L47 were not covered by tests
}

func (c *Prometheus) QueryRealtimeViews(ctx context.Context, userId string) (int64, error) {
query := fmt.Sprintf(`sum(mist_sessions{sessType="viewers", user_id="%s"})`, userId)
return c.queryInt64(ctx, query)

Check warning on line 52 in views/prometheus.go

View check run for this annotation

Codecov / codecov/patch

views/prometheus.go#L50-L52

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

func (c *Prometheus) queryInt64(ctx context.Context, query string) (int64, error) {

Check warning on line 55 in views/prometheus.go

View check run for this annotation

Codecov / codecov/patch

views/prometheus.go#L55

Added line #L55 was not covered by tests
value, warn, err := c.api.Query(ctx, query, time.Time{})
if len(warn) > 0 {
glog.Warningf("Prometheus query warnings: %q", warn)
Expand All @@ -51,14 +71,3 @@
}
return int64(vec[0].Value), nil
}

func startViewsQuery(playbackID, playbackRecordingID string) string {
queryID := playbackID
if playbackRecordingID != "" {
queryID = fmt.Sprintf("(%s|%s)", playbackID, playbackRecordingID)
}
return fmt.Sprintf(
`sum(increase(mist_playux_count{strm=~"video(rec)?\\+%s"} [1y]))`,
queryID,
)
}
Loading