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

[TT-9476]: Refactor graph pumps to use the new GraphQLStats #739

Merged
merged 11 commits into from
Nov 8, 2023
54 changes: 34 additions & 20 deletions analytics/aggregate_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package analytics

import (
"encoding/base64"
"fmt"
"testing"
"time"
Expand Down Expand Up @@ -90,8 +89,6 @@ func TestTrimTag(t *testing.T) {
}

func TestAggregateGraphData(t *testing.T) {
query := `{"query":"query{\n characters(filter: {\n \n }){\n info{\n count\n }\n }\n}"}`
rawResponse := `{"data":{"characters":{"info":{"count":758}}}}`
sampleRecord := AnalyticsRecord{
TimeStamp: time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC),
Method: "POST",
Expand All @@ -100,8 +97,6 @@ func TestAggregateGraphData(t *testing.T) {
RawPath: "/",
APIName: "test-api",
APIID: "test-api",
ApiSchema: base64.StdEncoding.EncodeToString([]byte(sampleSchema)),
Tags: []string{PredefinedTagGraphAnalytics},
ResponseCode: 200,
Day: 1,
Month: 1,
Expand All @@ -111,8 +106,16 @@ func TestAggregateGraphData(t *testing.T) {
APIKey: "test-key",
TrackPath: true,
OauthID: "test-id",
RawRequest: base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf(requestTemplate, len(query), query))),
RawResponse: base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf(responseTemplate, len(rawResponse), rawResponse))),
GraphQLStats: GraphQLStats{
IsGraphQL: true,
Types: map[string][]string{
"Characters": {"info"},
"Info": {"count"},
},
RootFields: []string{"characters"},
OperationType: OperationQuery,
HasErrors: false,
},
}

compareFields := func(r *require.Assertions, expected, actual map[string]*Counter) {
Expand Down Expand Up @@ -164,7 +167,7 @@ func TestAggregateGraphData(t *testing.T) {
for i := range records {
record := sampleRecord
if i == 1 {
record.Tags = []string{}
record.GraphQLStats.IsGraphQL = false
}
records[i] = record
}
Expand Down Expand Up @@ -193,8 +196,12 @@ func TestAggregateGraphData(t *testing.T) {
for i := range records {
record := sampleRecord
if i == 1 {
response := graphErrorResponse
record.RawResponse = base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf(responseTemplate, len(response), response)))
record.GraphQLStats.HasErrors = true
record.GraphQLStats.Errors = []GraphError{
{
Message: "Name for character with ID 1002 could not be fetched.",
},
}
}
records[i] = record
}
Expand Down Expand Up @@ -274,24 +281,27 @@ func TestAggregateGraphData_Dimension(t *testing.T) {
RawPath: "/",
APIName: "test-api",
APIID: "test-api",
ApiSchema: base64.StdEncoding.EncodeToString([]byte(sampleSchema)),
Tags: []string{PredefinedTagGraphAnalytics},
ResponseCode: 200,
Day: 1,
Month: 1,
Year: 2022,
Hour: 0,
OrgID: "test-org",
GraphQLStats: GraphQLStats{
IsGraphQL: true,
Types: map[string][]string{
"Characters": {"info"},
"Info": {"count"},
},
RootFields: []string{"characters"},
OperationType: OperationQuery,
HasErrors: false,
},
}

records := make([]interface{}, 3)
for i := range records {
record := sampleRecord
query := `{"query":"query{\n characters(filter: {\n \n }){\n info{\n count\n }\n }\n}"}`
response := `{"data":{"characters":{"info":{"count":758}}}}`
record.RawRequest = base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf(requestTemplate, len(query), query)))
record.RawResponse = base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf(responseTemplate, len(response), response)))
records[i] = record
records[i] = sampleRecord
}

responsesCheck := map[string][]string{
Expand Down Expand Up @@ -374,14 +384,18 @@ func TestAggregateData_SkipGraphRecords(t *testing.T) {
},
{
OrgID: "777-graph",
Tags: []string{"tag_1", "tag_2", PredefinedTagGraphAnalytics},
GraphQLStats: GraphQLStats{
IsGraphQL: true,
},
},
{
OrgID: "987",
},
{
OrgID: "555-graph",
Tags: []string{PredefinedTagGraphAnalytics},
GraphQLStats: GraphQLStats{
IsGraphQL: true,
},
},
},
2,
Expand Down
14 changes: 2 additions & 12 deletions analytics/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type AnalyticsRecord struct {
ExpireAt time.Time `bson:"expireAt" json:"expireAt"`
ApiSchema string `json:"api_schema" bson:"-" gorm:"-:all"` //nolint

GraphQLStats GraphQLStats `json:"-" bson:"-" gorm:"-:all"`
GraphQLStats GraphQLStats `json:"graphql_stats" bson:"-" gorm:"-:all"`
CollectionName string `json:"-" bson:"-" gorm:"-:all"`
}

Expand Down Expand Up @@ -374,17 +374,7 @@ func GeoIPLookup(ipStr string, GeoIPDB *maxminddb.Reader) (*GeoData, error) {
}

func (a *AnalyticsRecord) IsGraphRecord() bool {
if len(a.Tags) == 0 {
return false
}

for _, tag := range a.Tags {
if tag == PredefinedTagGraphAnalytics {
return true
}
}

return false
return a.GraphQLStats.IsGraphQL
}

func (a *AnalyticsRecord) RemoveIgnoredFields(ignoreFields []string) {
Expand Down
6 changes: 4 additions & 2 deletions analytics/analytics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ func TestAnalyticsRecord_IsGraphRecord(t *testing.T) {
assert.False(t, record.IsGraphRecord())
})

t.Run("should return true when tags contain the graph analytics tag", func(t *testing.T) {
t.Run("should return true with graph stats", func(t *testing.T) {
record := AnalyticsRecord{
Tags: []string{"tag_1", "tag_2", PredefinedTagGraphAnalytics, "tag_4", "tag_5"},
GraphQLStats: GraphQLStats{
IsGraphQL: true,
},
}
assert.True(t, record.IsGraphRecord())
})
Expand Down
Loading
Loading