Skip to content

Commit

Permalink
RSDK-5477 - clearer tabular data objects (#185)
Browse files Browse the repository at this point in the history
Co-authored-by: Cheuk <[email protected]>
  • Loading branch information
purplenicole730 and cheukt authored Oct 31, 2023
1 parent d856ede commit 4479cce
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/app/data-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ describe('DataClient tests', () => {
const promise = await subject().tabularDataByFilter();
expect(promise.length).toEqual(2);
const [data1, data2] = promise;
expect(data1).toMatchObject(tabData1.toObject());
expect(data2).toMatchObject(tabData2.toObject());
expect(data1).toMatchObject({ key: 'value1' });
expect(data2).toMatchObject({ key: 'value2' });
});

test('get filtered tabular data', async () => {
Expand Down
32 changes: 25 additions & 7 deletions src/app/data-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export type FilterOptions = Partial<pb.Filter.AsObject> & {
};

type TabularData = {
data?: googleStructPb.Struct.AsObject;
metadataIndex: number;
data?: { [key: string]: googleStructPb.JavaScriptValue };
metadata?: pb.CaptureMetadata.AsObject;
timeRequested?: Date;
timeReceived?: Date;
};
Expand All @@ -27,6 +27,14 @@ export class DataClient {
this.service = new DataServiceClient(serviceHost, grpcOptions);
}

/**
* Filter and download tabular data. The returned metadata might be empty if
* the metadata index of the data is out of the bounds of the returned
* metadata list.
*
* @param filter - Optional `pb.Filter` specifying tabular data to retrieve.
* No `filter` implies all tabular data.
*/
async tabularDataByFilter(filter?: pb.Filter) {
const { service } = this;

Expand All @@ -52,12 +60,22 @@ export class DataClient {
if (!dataList || dataList.length === 0) {
break;
}
const mdListLength = response.getMetadataList().length;

dataArray.push(
...dataList.map((data) => ({
...data.toObject(),
timeRequested: data.getTimeRequested()?.toDate(),
timeReceived: data.getTimeReceived()?.toDate(),
}))
...dataList.map((data) => {
const mdIndex = data.getMetadataIndex();
const metadata =
mdListLength !== 0 && mdIndex >= mdListLength
? new pb.CaptureMetadata().toObject()
: response.getMetadataList()[mdIndex]?.toObject();
return {
data: data.getData()?.toJavaScript(),
metadata,
timeRequested: data.getTimeRequested()?.toDate(),
timeReceived: data.getTimeReceived()?.toDate(),
};
})
);
last = response.getLast();
}
Expand Down

0 comments on commit 4479cce

Please sign in to comment.