Skip to content

Commit

Permalink
DATA-3398: Add GetLatestTabularData endpoint to Flutter SDK (#309)
Browse files Browse the repository at this point in the history
* add endpoint

* lint

* bug fix

* moved test

* moving test

* working?
  • Loading branch information
vpandiarajan20 authored Dec 11, 2024
1 parent 281fa3a commit 6cc4426
Show file tree
Hide file tree
Showing 3 changed files with 839 additions and 56 deletions.
29 changes: 29 additions & 0 deletions lib/src/app/data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,35 @@ class DataClient {
final response = await _datasetClient.listDatasetsByIDs(request);
return response.datasets;
}

/// Gets the most recent tabular data captured from the specified data source,
/// as long as it was synced within the last year.
///
/// For more information, see [Data Client API](https://docs.viam.com/appendix/apis/data-client/).
Future<({DateTime timeCaptured, DateTime timeSynced, Map<String, dynamic> payload})?> getLatestTabularData(
String partId,
String resourceName,
String resourceSubtype,
String methodName,
) async {
final request = GetLatestTabularDataRequest()
..partId = partId
..resourceName = resourceName
..resourceSubtype = resourceSubtype
..methodName = methodName;

final response = await _dataClient.getLatestTabularData(request);

if (!response.hasPayload() || !response.hasTimeCaptured() || !response.hasTimeSynced()) {
return null;
}

return (
timeCaptured: response.timeCaptured.toDateTime(),
timeSynced: response.timeSynced.toDateTime(),
payload: response.payload.toMap(),
);
}
}

/// {@category Viam SDK}
Expand Down
26 changes: 26 additions & 0 deletions test/unit_test/app/data_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'package:viam_sdk/src/gen/app/data/v1/data.pb.dart';
import 'package:viam_sdk/src/gen/app/data/v1/data.pbgrpc.dart';
import 'package:viam_sdk/src/gen/google/protobuf/timestamp.pb.dart';
import 'package:viam_sdk/src/media/image.dart';
import 'package:viam_sdk/src/utils.dart';

import '../mocks/mock_response_future.dart';
import '../mocks/service_clients_mocks.mocks.dart';
Expand Down Expand Up @@ -294,6 +295,31 @@ void main() {
.removeBinaryDataFromDatasetByIds([BinaryID(fileId: 'fileId', organizationId: 'orgId', locationId: 'locId')], 'dataset');
verify(serviceClient.removeBinaryDataFromDatasetByIDs(any)).called(1);
});
test('getLatestTabularData', () async {
final timeCaptured = DateTime.utc(2023, 1, 1);
final timeSynced = DateTime.utc(2023, 1, 2);
final Map<String, dynamic> payload = {'key': 'value'};

when(serviceClient.getLatestTabularData(any)).thenAnswer((_) => MockResponseFuture.value(GetLatestTabularDataResponse(
timeCaptured: Timestamp.fromDateTime(timeCaptured),
timeSynced: Timestamp.fromDateTime(timeSynced),
payload: payload.toStruct())));

final response = await dataClient.getLatestTabularData('part-id', 'resource-name', 'resource-subtype', 'method-name');

expect(response?.timeCaptured, equals(timeCaptured));
expect(response?.timeSynced, equals(timeSynced));
expect(response?.payload, equals({'key': 'value'}));

verify(serviceClient.getLatestTabularData(any)).called(1);

// Test null response
when(serviceClient.getLatestTabularData(any)).thenAnswer((_) => MockResponseFuture.value(GetLatestTabularDataResponse()));

final nullResponse = await dataClient.getLatestTabularData('part-id', 'resource-name', 'resource-subtype', 'method-name');

expect(nullResponse, isNull);
});
});

group('DataSync Tests', () {
Expand Down
Loading

0 comments on commit 6cc4426

Please sign in to comment.