Skip to content

Commit

Permalink
add endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
vpandiarajan20 committed Dec 2, 2024
1 parent 1ebdd10 commit cd6d1e8
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/src/app/data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,36 @@ 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, DateTime, Map<String, dynamic>)?> 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 (
response.timeCaptured.toDateTime(),
response.timeSynced.toDateTime(),
response.payload.toStruct().toMap(),
);
}

}

/// {@category Viam SDK}
Expand Down
41 changes: 41 additions & 0 deletions test/unit_test/app/data_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,47 @@ void main() {
});
});
});

test('getLatestTabularData', () async {
final timeCaptured = DateTime(2023, 1, 1);
final timeSynced = DateTime(2023, 1, 2);
final payload = Struct()..fields.addAll({
'key': Value()..stringValue = 'value'
});

when(dataServiceClient.getLatestTabularData(any)).thenAnswer((_) => MockResponseFuture.value(
GetLatestTabularDataResponse()
..timeCaptured = Timestamp.fromDateTime(timeCaptured)
..timeSynced = Timestamp.fromDateTime(timeSynced)
..payload = payload
));

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

expect(response?.$1, equals(timeCaptured));
expect(response?.$2, equals(timeSynced));
expect(response?.$3, equals({'key': 'value'}));

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

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

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

expect(nullResponse, isNull);
});

group('Filter Utils Tests', () {
test('setDateTimeCaptureInterval', () {
Expand Down

0 comments on commit cd6d1e8

Please sign in to comment.