Skip to content

Commit c9ee5f4

Browse files
DATA-3396: Add GetLatestTabularData endpoint to TypeScript SDK (#419)
1 parent 7b779cc commit c9ee5f4

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

src/app/data-client.spec.ts

+63
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ import {
4747
TagsByFilterRequest,
4848
TagsByFilterResponse,
4949
TagsFilter,
50+
GetLatestTabularDataRequest,
51+
GetLatestTabularDataResponse,
5052
} from '../gen/app/data/v1/data_pb';
5153
import { DatasetService } from '../gen/app/dataset/v1/dataset_connect';
5254
import {
@@ -830,6 +832,67 @@ describe('DataClient tests', () => {
830832
expect(testFilter).toEqual(expectedFilter);
831833
});
832834
});
835+
836+
describe('getLatestTabularData tests', () => {
837+
const timeCaptured = new Date(2024, 1, 1);
838+
const timeSynced = new Date(2024, 1, 2);
839+
const payload = { key: 'value' };
840+
841+
beforeEach(() => {
842+
mockTransport = createRouterTransport(({ service }) => {
843+
service(DataService, {
844+
getLatestTabularData: (req) => {
845+
capReq = req;
846+
return new GetLatestTabularDataResponse({
847+
timeCaptured: Timestamp.fromDate(timeCaptured),
848+
timeSynced: Timestamp.fromDate(timeSynced),
849+
payload: Struct.fromJson(payload),
850+
});
851+
},
852+
});
853+
});
854+
});
855+
856+
let capReq: GetLatestTabularDataRequest;
857+
858+
it('get latest tabular data', async () => {
859+
const expectedRequest = new GetLatestTabularDataRequest({
860+
partId: 'testPartId',
861+
resourceName: 'testResource',
862+
resourceSubtype: 'testSubtype',
863+
methodName: 'testMethod',
864+
});
865+
866+
const result = await subject().getLatestTabularData(
867+
'testPartId',
868+
'testResource',
869+
'testSubtype',
870+
'testMethod'
871+
);
872+
873+
expect(capReq).toStrictEqual(expectedRequest);
874+
expect(result).toEqual([timeCaptured, timeSynced, payload]);
875+
});
876+
877+
it('returns null when no data available', async () => {
878+
mockTransport = createRouterTransport(({ service }) => {
879+
service(DataService, {
880+
getLatestTabularData: () => {
881+
return new GetLatestTabularDataResponse({});
882+
},
883+
});
884+
});
885+
886+
const result = await subject().getLatestTabularData(
887+
'testPartId',
888+
'testResource',
889+
'testSubtype',
890+
'testMethod'
891+
);
892+
893+
expect(result).toBeNull();
894+
});
895+
});
833896
});
834897

835898
describe('DatasetClient tests', () => {

src/app/data-client.ts

+38
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,44 @@ export class DataClient {
687687

688688
return filter;
689689
}
690+
691+
/**
692+
* Gets the most recent tabular data captured from the specified data source,
693+
* as long as it was synced within the last year.
694+
*
695+
* @param partId The ID of the part that owns the data
696+
* @param resourceName The name of the requested resource that captured the
697+
* data
698+
* @param resourceSubtype The subtype of the requested resource that captured
699+
* the data
700+
* @param methodName The data capture method name
701+
* @returns A tuple containing [timeCaptured, timeSynced, payload] or null if
702+
* no data has been synced for the specified resource OR the most recently
703+
* captured data was over a year ago
704+
*/
705+
async getLatestTabularData(
706+
partId: string,
707+
resourceName: string,
708+
resourceSubtype: string,
709+
methodName: string
710+
): Promise<[Date, Date, Record<string, JsonValue>] | null> {
711+
const resp = await this.dataClient.getLatestTabularData({
712+
partId,
713+
resourceName,
714+
resourceSubtype,
715+
methodName,
716+
});
717+
718+
if (!resp.payload || !resp.timeCaptured || !resp.timeSynced) {
719+
return null;
720+
}
721+
722+
return [
723+
resp.timeCaptured.toDate(),
724+
resp.timeSynced.toDate(),
725+
resp.payload.toJson() as Record<string, JsonValue>,
726+
];
727+
}
690728
}
691729

692730
export { type BinaryID, type Order } from '../gen/app/data/v1/data_pb';

0 commit comments

Comments
 (0)