Skip to content

Commit

Permalink
DATA-3396: Add GetLatestTabularData endpoint to TypeScript SDK (#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
vpandiarajan20 authored Dec 6, 2024
1 parent 7b779cc commit c9ee5f4
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/app/data-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ import {
TagsByFilterRequest,
TagsByFilterResponse,
TagsFilter,
GetLatestTabularDataRequest,
GetLatestTabularDataResponse,
} from '../gen/app/data/v1/data_pb';
import { DatasetService } from '../gen/app/dataset/v1/dataset_connect';
import {
Expand Down Expand Up @@ -830,6 +832,67 @@ describe('DataClient tests', () => {
expect(testFilter).toEqual(expectedFilter);
});
});

describe('getLatestTabularData tests', () => {
const timeCaptured = new Date(2024, 1, 1);
const timeSynced = new Date(2024, 1, 2);
const payload = { key: 'value' };

beforeEach(() => {
mockTransport = createRouterTransport(({ service }) => {
service(DataService, {
getLatestTabularData: (req) => {
capReq = req;
return new GetLatestTabularDataResponse({
timeCaptured: Timestamp.fromDate(timeCaptured),
timeSynced: Timestamp.fromDate(timeSynced),
payload: Struct.fromJson(payload),
});
},
});
});
});

let capReq: GetLatestTabularDataRequest;

it('get latest tabular data', async () => {
const expectedRequest = new GetLatestTabularDataRequest({
partId: 'testPartId',
resourceName: 'testResource',
resourceSubtype: 'testSubtype',
methodName: 'testMethod',
});

const result = await subject().getLatestTabularData(
'testPartId',
'testResource',
'testSubtype',
'testMethod'
);

expect(capReq).toStrictEqual(expectedRequest);
expect(result).toEqual([timeCaptured, timeSynced, payload]);
});

it('returns null when no data available', async () => {
mockTransport = createRouterTransport(({ service }) => {
service(DataService, {
getLatestTabularData: () => {
return new GetLatestTabularDataResponse({});
},
});
});

const result = await subject().getLatestTabularData(
'testPartId',
'testResource',
'testSubtype',
'testMethod'
);

expect(result).toBeNull();
});
});
});

describe('DatasetClient tests', () => {
Expand Down
38 changes: 38 additions & 0 deletions src/app/data-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,44 @@ export class DataClient {

return filter;
}

/**
* Gets the most recent tabular data captured from the specified data source,
* as long as it was synced within the last year.
*
* @param partId The ID of the part that owns the data
* @param resourceName The name of the requested resource that captured the
* data
* @param resourceSubtype The subtype of the requested resource that captured
* the data
* @param methodName The data capture method name
* @returns A tuple containing [timeCaptured, timeSynced, payload] or null if
* no data has been synced for the specified resource OR the most recently
* captured data was over a year ago
*/
async getLatestTabularData(
partId: string,
resourceName: string,
resourceSubtype: string,
methodName: string
): Promise<[Date, Date, Record<string, JsonValue>] | null> {
const resp = await this.dataClient.getLatestTabularData({
partId,
resourceName,
resourceSubtype,
methodName,
});

if (!resp.payload || !resp.timeCaptured || !resp.timeSynced) {
return null;
}

return [
resp.timeCaptured.toDate(),
resp.timeSynced.toDate(),
resp.payload.toJson() as Record<string, JsonValue>,
];
}
}

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

0 comments on commit c9ee5f4

Please sign in to comment.