Skip to content

Commit

Permalink
fix: added ID field to analytics parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
0xcadams committed Aug 7, 2024
1 parent 26ce39a commit 203bdfd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
11 changes: 7 additions & 4 deletions handlers/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"crypto/sha256"
"encoding/json"
"fmt"
"github.com/livepeer/catalyst-api/metrics"
"github.com/ua-parser/uap-go/uaparser"
"io"
"net"
"net/http"
"strconv"
"time"

"github.com/livepeer/catalyst-api/metrics"
"github.com/ua-parser/uap-go/uaparser"

"github.com/golang/glog"
"github.com/julienschmidt/httprouter"
cerrors "github.com/livepeer/catalyst-api/errors"
Expand Down Expand Up @@ -45,8 +46,9 @@ type AnalyticsLog struct {

type AnalyticsLogEvent struct {
// Shared fields by all events
Type string `json:"type"`
Timestamp int64 `json:"timestamp"`
Id *string `json:"id"`
Type string `json:"type"`
Timestamp int64 `json:"timestamp"`

// Heartbeat event
Errors *int `json:"errors"`
Expand Down Expand Up @@ -250,6 +252,7 @@ func (c *AnalyticsHandlersCollection) toAnalyticsData(log *AnalyticsLog, geo Ana
EventType: e.Type,
EventTimestamp: e.Timestamp,
EventData: analytics.LogDataEvent{
Id: e.Id,
Errors: e.Errors,
AutoplayStatus: e.AutoplayStatus,
StalledCount: e.StalledCount,
Expand Down
4 changes: 3 additions & 1 deletion handlers/analytics/log_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"context"
"crypto/tls"
"encoding/json"
"time"

"github.com/golang/glog"
"github.com/livepeer/catalyst-api/metrics"
"github.com/segmentio/kafka-go"
"github.com/segmentio/kafka-go/sasl/plain"
"time"
)

const (
Expand All @@ -28,6 +29,7 @@ type LogProcessor struct {

type LogDataEvent struct {
// Heartbeat event
Id *string `json:"id,omitempty"`
Errors *int `json:"errors,omitempty"`
AutoplayStatus *string `json:"autoplay_status,omitempty"`
StalledCount *int `json:"stalled_count,omitempty"`
Expand Down
13 changes: 9 additions & 4 deletions handlers/analytics_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package handlers

import (
"github.com/julienschmidt/httprouter"
"github.com/livepeer/catalyst-api/handlers/analytics"
"github.com/stretchr/testify/require"
"github.com/ua-parser/uap-go/uaparser"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/julienschmidt/httprouter"
"github.com/livepeer/catalyst-api/handlers/analytics"
"github.com/stretchr/testify/require"
"github.com/ua-parser/uap-go/uaparser"
)

const userID = "user-id"
Expand Down Expand Up @@ -55,6 +56,7 @@ func TestHandleLog(t *testing.T) {
"uid": "abcdef",
"events": [
{
"id": "dcba4321",
"type": "heartbeat",
"timestamp": 1234567895,
"errors": 0,
Expand Down Expand Up @@ -83,6 +85,7 @@ func TestHandleLog(t *testing.T) {
"some_field": "some value"
},
{
"id": "abcde12345",
"type": "error",
"timestamp": 1234567895,
"error_message": "error message",
Expand Down Expand Up @@ -115,6 +118,7 @@ func TestHandleLog(t *testing.T) {
EventType: "heartbeat",
EventTimestamp: 1234567895,
EventData: analytics.LogDataEvent{
Id: strPtr("dcba4321"),
Errors: intPtr(0),
AutoplayStatus: strPtr("autoplay"),
StalledCount: intPtr(5),
Expand Down Expand Up @@ -157,6 +161,7 @@ func TestHandleLog(t *testing.T) {
EventType: "error",
EventTimestamp: 1234567895,
EventData: analytics.LogDataEvent{
Id: strPtr("abcde12345"),
ErrorMessage: strPtr("error message"),
Category: strPtr("offline"),
},
Expand Down

0 comments on commit 203bdfd

Please sign in to comment.