Skip to content

Commit

Permalink
added proto to go serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
kofoworola committed Oct 24, 2023
1 parent ebddce4 commit 82de2f5
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 60 deletions.
21 changes: 20 additions & 1 deletion analytics/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ type AnalyticsRecord struct {
ExpireAt time.Time `bson:"expireAt" json:"expireAt"`
ApiSchema string `json:"api_schema" bson:"-" gorm:"-:all"` //nolint

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

func (a *AnalyticsRecord) TableName() string {
Expand All @@ -92,6 +93,24 @@ func (a *AnalyticsRecord) SetObjectID(id model.ObjectID) {
a.id = id
}

type GraphQLOperations int

const (
Operation_Unknown GraphQLOperations = iota
Operation_Query
Operation_Mutation
Operation_Subscription
)

type GraphQLStats struct {
Types map[string][]string
OperationType GraphQLOperations
Variables string
RootFields []string
HasErrors bool
Errors []GraphError
}

type GraphError struct {
Message string `json:"message"`
Path []interface{} `json:"path"`
Expand Down
20 changes: 11 additions & 9 deletions analytics/analytics.proto
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,20 @@ message NetworkStats {
}

enum GraphQLOperations{
OPERATION_QUERY = 0;
OPERATION_MUTATION = 1;
OPERATION_SUBSCRIPTION = 2;
OPERATION_UNKNOWN = 0;
OPERATION_QUERY = 1;
OPERATION_MUTATION = 2;
OPERATION_SUBSCRIPTION = 3;
}

message GraphQLStats{
map<string, RepeatedFields> Types = 1;
GraphQLOperations OperationType = 2;
string Variables = 3;
repeated string RootFields = 4;
bool HasError = 5;
repeated string GraphErrors = 6;
bool IsGraphQL = 1;
map<string, RepeatedFields> Types = 2;
GraphQLOperations OperationType = 3;
string Variables = 4;
repeated string RootFields = 5;
bool HasError = 6;
repeated string GraphErrors = 7;
}

message RepeatedFields{
Expand Down
113 changes: 64 additions & 49 deletions analytics/proto/analytics.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 32 additions & 1 deletion serializer/protobuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ func (pb *ProtobufSerializer) TransformSingleRecordToProto(rec analytics.Analyti
}

func (pb *ProtobufSerializer) TransformSingleProtoToAnalyticsRecord(rec analyticsproto.AnalyticsRecord, record *analytics.AnalyticsRecord) error {

tmpRecord := analytics.AnalyticsRecord{
Method: rec.Method,
Host: rec.Host,
Expand Down Expand Up @@ -146,6 +145,38 @@ func (pb *ProtobufSerializer) TransformSingleProtoToAnalyticsRecord(rec analytic
ApiSchema: rec.ApiSchema,
}
tmpRecord.TimeStampFromProto(rec)

// process anc convert graphql stats
operationType := analytics.Operation_Unknown
switch rec.GraphQLStats.OperationType {
case analyticsproto.GraphQLOperations_OPERATION_QUERY:
operationType = analytics.Operation_Query
case analyticsproto.GraphQLOperations_OPERATION_MUTATION:
operationType = analytics.Operation_Mutation
case analyticsproto.GraphQLOperations_OPERATION_SUBSCRIPTION:
operationType = analytics.Operation_Subscription
default:
operationType = analytics.Operation_Unknown
}

types := make(map[string][]string)
for key, val := range rec.GraphQLStats.Types {
types[key] = val.Fields
}
errors := make([]analytics.GraphError, len(rec.GraphQLStats.GraphErrors))
for i, val := range rec.GraphQLStats.GraphErrors {
errors[i].Message = val
}

tmpRecord.GraphQLStats = analytics.GraphQLStats{
OperationType: operationType,
HasErrors: rec.GraphQLStats.HasError,
RootFields: rec.GraphQLStats.RootFields,
Types: types,
Variables: rec.GraphQLStats.Variables,
Errors: errors,
}

*record = tmpRecord
return nil
}

0 comments on commit 82de2f5

Please sign in to comment.