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

DATA-3397 Add GetLatestTabularData endpoint to Go SDK #4587

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
30 changes: 30 additions & 0 deletions app/data_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ type DatabaseConnReturn struct {
HasDatabaseUser bool
}

// LatestTabularDataReturn represents the response returned by GetLatestTabularData. It contains the most recently captured data
// payload, the time it was captured, and the time it was synced.
type LatestTabularDataReturn struct {
TimeCaptured time.Time
TimeSynced time.Time
Payload *structpb.Struct
}

// DataSyncClient structs

// SensorMetadata contains the time the sensor data was requested and was received.
Expand Down Expand Up @@ -563,6 +571,28 @@ func (d *DataClient) TabularDataByMQL(ctx context.Context, organizationID string
return result, nil
}

// GetLatestTabularData gets the most recent tabular data captured from the specified data source, as well as the time that it was captured
// and synced. If no data was synced to the data source within the last year, LatestTabularDataReturn will be empty.
func (d *DataClient) GetLatestTabularData(ctx context.Context, partID, resourceName, resourceSubtype, methodName string) (
LatestTabularDataReturn, error,
) {
resp, err := d.dataClient.GetLatestTabularData(ctx, &pb.GetLatestTabularDataRequest{
PartId: partID,
ResourceName: resourceName,
ResourceSubtype: resourceSubtype,
MethodName: methodName,
})
if err != nil {
return LatestTabularDataReturn{}, err
}

return LatestTabularDataReturn{
TimeCaptured: resp.TimeCaptured.AsTime(),
TimeSynced: resp.TimeSynced.AsTime(),
Payload: resp.Payload,
}, nil
}

// BinaryDataByFilter queries binary data and metadata based on given filters.
func (d *DataClient) BinaryDataByFilter(
ctx context.Context,
Expand Down
32 changes: 30 additions & 2 deletions app/data_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,11 @@ func TestDataClient(t *testing.T) {
grpcClient := createDataGrpcClient()
client := DataClient{dataClient: grpcClient}

timeNow := time.Now()

captureInterval := CaptureInterval{
Start: time.Now(),
End: time.Now(),
Start: timeNow,
End: timeNow,
}
tagsFilter := TagsFilter{
Type: TagsFilterTypeUnspecified,
Expand Down Expand Up @@ -317,6 +319,32 @@ func TestDataClient(t *testing.T) {
test.That(t, response, test.ShouldResemble, rawData)
})

t.Run("GetLatestTabularData", func(t *testing.T) {
dataStruct, _ := utils.StructToStructPb(data)
latestTabularData := LatestTabularDataReturn{
TimeCaptured: timeNow,
TimeSynced: timeNow,
Payload: dataStruct,
}

grpcClient.GetLatestTabularDataFunc = func(ctx context.Context, in *pb.GetLatestTabularDataRequest,
opts ...grpc.CallOption,
) (*pb.GetLatestTabularDataResponse, error) {
test.That(t, in.PartId, test.ShouldEqual, partID)
test.That(t, in.ResourceName, test.ShouldEqual, componentName)
test.That(t, in.ResourceSubtype, test.ShouldEqual, componentType)
test.That(t, in.MethodName, test.ShouldResemble, method)
return &pb.GetLatestTabularDataResponse{
TimeCaptured: timestamppb.New(timeNow),
TimeSynced: timestamppb.New(timeNow),
Payload: dataStruct,
}, nil
}

resp, _ := client.GetLatestTabularData(context.Background(), partID, componentName, componentType, method)
test.That(t, resp, test.ShouldResemble, latestTabularData)
})

t.Run("BinaryDataByFilter", func(t *testing.T) {
includeBinary := true
grpcClient.BinaryDataByFilterFunc = func(ctx context.Context, in *pb.BinaryDataByFilterRequest,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ require (
go.uber.org/atomic v1.11.0
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.27.0
go.viam.com/api v0.1.366
go.viam.com/api v0.1.367
go.viam.com/test v1.2.4
go.viam.com/utils v0.1.116
goji.io v2.0.2+incompatible
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1516,8 +1516,8 @@ go.uber.org/zap v1.18.1/go.mod h1:xg/QME4nWcxGxrpdeYfq7UvYrLh66cuVKdrbD1XF/NI=
go.uber.org/zap v1.23.0/go.mod h1:D+nX8jyLsMHMYrln8A0rJjFt/T/9/bGgIhAqxv5URuY=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
go.viam.com/api v0.1.366 h1:lUen0W04hwdFL95GoQkYaweZO5ySG40BnUl7HHVZE3o=
go.viam.com/api v0.1.366/go.mod h1:g5eipXHNm0rQmW7DWya6avKcmzoypLmxnMlAaIsE5Ls=
go.viam.com/api v0.1.367 h1:nd2CLnNAwCV54h7EcNThe2CVdH5OCsmqWvFqSA/r5OY=
go.viam.com/api v0.1.367/go.mod h1:g5eipXHNm0rQmW7DWya6avKcmzoypLmxnMlAaIsE5Ls=
go.viam.com/test v1.2.4 h1:JYgZhsuGAQ8sL9jWkziAXN9VJJiKbjoi9BsO33TW3ug=
go.viam.com/test v1.2.4/go.mod h1:zI2xzosHdqXAJ/kFqcN+OIF78kQuTV2nIhGZ8EzvaJI=
go.viam.com/utils v0.1.116 h1:hoCj3SsV8LZAOEP75TjMeX57axhravS8rNUYmhpTWtM=
Expand Down
12 changes: 12 additions & 0 deletions testutils/inject/data_service_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type DataServiceClient struct {
opts ...grpc.CallOption) (*datapb.TabularDataBySQLResponse, error)
TabularDataByMQLFunc func(ctx context.Context, in *datapb.TabularDataByMQLRequest,
opts ...grpc.CallOption) (*datapb.TabularDataByMQLResponse, error)
GetLatestTabularDataFunc func(ctx context.Context, in *datapb.GetLatestTabularDataRequest,
opts ...grpc.CallOption) (*datapb.GetLatestTabularDataResponse, error)
BinaryDataByFilterFunc func(ctx context.Context, in *datapb.BinaryDataByFilterRequest,
opts ...grpc.CallOption) (*datapb.BinaryDataByFilterResponse, error)
BinaryDataByIDsFunc func(ctx context.Context, in *datapb.BinaryDataByIDsRequest,
Expand Down Expand Up @@ -84,6 +86,16 @@ func (client *DataServiceClient) TabularDataByMQL(ctx context.Context, in *datap
return client.TabularDataByMQLFunc(ctx, in, opts...)
}

// GetLatestTabularData calls the injected GetLatestTabularData or the real version.
func (client *DataServiceClient) GetLatestTabularData(ctx context.Context, in *datapb.GetLatestTabularDataRequest,
opts ...grpc.CallOption,
) (*datapb.GetLatestTabularDataResponse, error) {
if client.GetLatestTabularDataFunc == nil {
return client.DataServiceClient.GetLatestTabularData(ctx, in, opts...)
}
return client.GetLatestTabularDataFunc(ctx, in, opts...)
}

// BinaryDataByFilter calls the injected BinaryDataByFilter or the real version.
func (client *DataServiceClient) BinaryDataByFilter(ctx context.Context, in *datapb.BinaryDataByFilterRequest,
opts ...grpc.CallOption,
Expand Down
Loading