From 00047738960b535efa2ca1280f7079f59090a720 Mon Sep 17 00:00:00 2001 From: Carson Date: Tue, 24 Oct 2023 12:39:03 -0500 Subject: [PATCH] fix(tag): support `datatype` and `type` properties (#48) --- src/datasources/tag/TagDataSource.test.ts | 43 ++++++++++++++++--- src/datasources/tag/TagDataSource.ts | 2 +- .../__snapshots__/TagDataSource.test.ts.snap | 26 +++++++++++ src/datasources/tag/types.ts | 25 +++++++++-- 4 files changed, 85 insertions(+), 11 deletions(-) diff --git a/src/datasources/tag/TagDataSource.test.ts b/src/datasources/tag/TagDataSource.test.ts index 22201ce..45860be 100644 --- a/src/datasources/tag/TagDataSource.test.ts +++ b/src/datasources/tag/TagDataSource.test.ts @@ -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( @@ -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( @@ -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' }, } ), ], diff --git a/src/datasources/tag/TagDataSource.ts b/src/datasources/tag/TagDataSource.ts index 31cbc80..0a58ed4 100644 --- a/src/datasources/tag/TagDataSource.ts +++ b/src/datasources/tag/TagDataSource.ts @@ -42,7 +42,7 @@ export class TagDataSource extends DataSourceBase { 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 { diff --git a/src/datasources/tag/__snapshots__/TagDataSource.test.ts.snap b/src/datasources/tag/__snapshots__/TagDataSource.test.ts.snap index 2ca8bc5..2340ab8 100644 --- a/src/datasources/tag/__snapshots__/TagDataSource.test.ts.snap +++ b/src/datasources/tag/__snapshots__/TagDataSource.test.ts.snap @@ -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`] = ` [ { diff --git a/src/datasources/tag/types.ts b/src/datasources/tag/types.ts index 06d2cee..8ddab11 100644 --- a/src/datasources/tag/types.ts +++ b/src/datasources/tag/types.ts @@ -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 | 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[]; }