Skip to content

Commit

Permalink
fix(tag): support datatype and type properties (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
mure authored Oct 24, 2023
1 parent 85101c9 commit 0004773
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 11 deletions.
43 changes: 37 additions & 6 deletions src/datasources/tag/TagDataSource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ describe('queries', () => {

test('current value for all data types', async () => {
backendSrv.fetch
.mockReturnValueOnce(createQueryTagsResponse({ datatype: 'INT', path: 'tag1' }, { value: { value: '3' } }))
.mockReturnValueOnce(createQueryTagsResponse({ datatype: 'DOUBLE', path: 'tag2' }, { value: { value: '3.3' } }))
.mockReturnValueOnce(createQueryTagsResponse({ datatype: 'STRING', path: 'tag3' }, { value: { value: 'foo' } }))
.mockReturnValueOnce(createQueryTagsResponse({ datatype: 'BOOLEAN', path: 'tag4' }, { value: { value: 'True' } }))
.mockReturnValueOnce(createQueryTagsResponse({ type: 'INT', path: 'tag1' }, { value: { value: '3' } }))
.mockReturnValueOnce(createQueryTagsResponse({ type: 'DOUBLE', path: 'tag2' }, { value: { value: '3.3' } }))
.mockReturnValueOnce(createQueryTagsResponse({ type: 'STRING', path: 'tag3' }, { value: { value: 'foo' } }))
.mockReturnValueOnce(createQueryTagsResponse({ type: 'BOOLEAN', path: 'tag4' }, { value: { value: 'True' } }))
.mockReturnValueOnce(
createQueryTagsResponse({ datatype: 'U_INT64', path: 'tag5' }, { value: { value: '2147483648' } })
createQueryTagsResponse({ type: 'U_INT64', path: 'tag5' }, { value: { value: '2147483648' } })
);

const result = await ds.query(
Expand Down Expand Up @@ -247,6 +247,37 @@ describe('queries', () => {
expect(templateSrv.replace).toHaveBeenCalledTimes(2);
expect(templateSrv.replace.mock.calls[1][0]).toBe(workspaceVariable);
});

test('supports legacy tag service property "workspace_id"', async () => {
backendSrv.fetch
.mockReturnValueOnce(
createFetchResponse({
tagsWithValues: [{ tag: { datatype: 'DOUBLE', path: 'my.tag', workspace_id: '1' } }],
})
)
.mockReturnValueOnce(createTagHistoryResponse('my.tag', 'DOUBLE', []));

await ds.query(buildQuery({ path: 'my.tag', type: TagQueryType.History }));

expect(backendSrv.fetch.mock.lastCall?.[0].data).toHaveProperty('workspace', '1');
});

test('supports legacy tag service property "datatype"', async () => {
backendSrv.fetch.mockReturnValueOnce(
createFetchResponse({
tagsWithValues: [
{
current: { value: { value: '3.14' }, timestamp: '2023-10-04T00:00:00.000000Z' },
tag: { datatype: 'DOUBLE', path: 'my.tag', workspace_id: '1' },
},
],
})
);

const result = await ds.query(buildQuery({ path: 'my.tag' }));

expect(result.data).toMatchSnapshot();
});
});

function createQueryTagsResponse(
Expand All @@ -259,7 +290,7 @@ function createQueryTagsResponse(
{ tag, current },
{
current: { value: { value: '3.14' }, timestamp: '2023-10-04T00:00:00.000000Z' },
tag: { datatype: 'DOUBLE', path: 'my.tag', properties: {}, workspace: '1' },
tag: { type: 'DOUBLE', path: 'my.tag', properties: {}, workspace: '1' },
}
),
],
Expand Down
2 changes: 1 addition & 1 deletion src/datasources/tag/TagDataSource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export class TagDataSource extends DataSourceBase<TagQuery> {

if (query.type === TagQueryType.Current) {
result.fields = [
{ name, values: [this.convertTagValue(tag.datatype, current?.value.value)] },
{ name, values: [this.convertTagValue(tag.type ?? tag.datatype, current?.value.value)] },
{ name: 'updated', values: [current?.timestamp], type: FieldType.time, config: { unit: 'dateTimeFromNow' } },
];
} else {
Expand Down
26 changes: 26 additions & 0 deletions src/datasources/tag/__snapshots__/TagDataSource.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,32 @@ exports[`queries string tag history 1`] = `
]
`;

exports[`queries supports legacy tag service property "datatype" 1`] = `
[
{
"fields": [
{
"name": "my.tag",
"values": [
3.14,
],
},
{
"config": {
"unit": "dateTimeFromNow",
},
"name": "updated",
"type": "time",
"values": [
"2023-10-04T00:00:00.000000Z",
],
},
],
"refId": "A",
},
]
`;

exports[`queries tag current value 1`] = `
[
{
Expand Down
25 changes: 21 additions & 4 deletions src/datasources/tag/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,37 @@ export interface TagQuery extends DataQuery {
properties: boolean;
}

export interface TagWithValue {
interface TagWithValueBase {
current: {
value: { value: string };
timestamp: string;
} | null;
tag: {
datatype: string;
path: string;
properties: Record<string, string> | null;
workspace: string;
workspace_id: string;
};
}

// Legacy tag properties from SystemLink Cloud
interface TagWithValueV1 {
tag: {
collect_aggregates: boolean;
datatype: string;
last_updated: number;
workspace_id: string;
}
}

// Tag properties renamed in SystemLink Server and Enterprise
interface TagWithValueV2 {
tag: {
type: string;
workspace: string;
}
}

export type TagWithValue = TagWithValueBase & TagWithValueV1 & TagWithValueV2;

export interface TagsWithValues {
tagsWithValues: TagWithValue[];
}
Expand Down

0 comments on commit 0004773

Please sign in to comment.