From fd5f66083c91b9759ae7772f99b80c9342b93290 Mon Sep 17 00:00:00 2001 From: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com> Date: Thu, 23 Nov 2023 10:43:48 +0100 Subject: [PATCH] Loki: Move `convertToWebSocketUrl` from Explore to Loki (#78542) * Move convertToWebSocketUrl to Loki * Add tests * Fix test --- public/app/core/utils/explore.ts | 11 ----- .../app/plugins/datasource/loki/datasource.ts | 3 +- .../plugins/datasource/loki/streaming.test.ts | 45 +++++++++++++++++++ .../app/plugins/datasource/loki/streaming.ts | 11 ++++- 4 files changed, 56 insertions(+), 14 deletions(-) create mode 100644 public/app/plugins/datasource/loki/streaming.test.ts diff --git a/public/app/core/utils/explore.ts b/public/app/core/utils/explore.ts index 9a77f8ca438..afb606427ba 100644 --- a/public/app/core/utils/explore.ts +++ b/public/app/core/utils/explore.ts @@ -28,8 +28,6 @@ import store from 'app/core/store'; import { ExpressionDatasourceUID } from 'app/features/expressions/types'; import { QueryOptions, QueryTransaction } from 'app/types/explore'; -import { config } from '../config'; - import { getNextRefIdChar } from './query'; export const DEFAULT_UI_STATE = { @@ -329,15 +327,6 @@ export const getTimeRange = (timeZone: TimeZone, rawRange: RawTimeRange, fiscalY export const refreshIntervalToSortOrder = (refreshInterval?: string) => RefreshPicker.isLive(refreshInterval) ? LogsSortOrder.Ascending : LogsSortOrder.Descending; -export const convertToWebSocketUrl = (url: string) => { - const protocol = window.location.protocol === 'https:' ? 'wss://' : 'ws://'; - let backend = `${protocol}${window.location.host}${config.appSubUrl}`; - if (backend.endsWith('/')) { - backend = backend.slice(0, -1); - } - return `${backend}${url}`; -}; - export const stopQueryState = (querySubscription: Unsubscribable | undefined) => { if (querySubscription) { querySubscription.unsubscribe(); diff --git a/public/app/plugins/datasource/loki/datasource.ts b/public/app/plugins/datasource/loki/datasource.ts index dd3426bb24b..bb658e24ac2 100644 --- a/public/app/plugins/datasource/loki/datasource.ts +++ b/public/app/plugins/datasource/loki/datasource.ts @@ -41,7 +41,6 @@ import { import { Duration } from '@grafana/lezer-logql'; import { BackendSrvRequest, config, DataSourceWithBackend, getTemplateSrv, TemplateSrv } from '@grafana/runtime'; import { DataQuery } from '@grafana/schema'; -import { convertToWebSocketUrl } from 'app/core/utils/explore'; import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv'; import { queryLogsSample, queryLogsVolume } from '../../../features/logs/logsModel'; @@ -83,7 +82,7 @@ import { isQueryWithError, requestSupportsSplitting, } from './queryUtils'; -import { doLokiChannelStream } from './streaming'; +import { convertToWebSocketUrl, doLokiChannelStream } from './streaming'; import { trackQuery } from './tracking'; import { LokiOptions, diff --git a/public/app/plugins/datasource/loki/streaming.test.ts b/public/app/plugins/datasource/loki/streaming.test.ts new file mode 100644 index 00000000000..04a87d20856 --- /dev/null +++ b/public/app/plugins/datasource/loki/streaming.test.ts @@ -0,0 +1,45 @@ +import { convertToWebSocketUrl } from './streaming'; + +jest.mock('@grafana/runtime', () => ({ + ...jest.requireActual('@grafana/runtime'), + config: { + appSubUrl: '/grafana', + }, +})); + +describe('convertToWebSocketUrl', () => { + const { location } = window; + + beforeEach(() => { + // @ts-ignore + delete window.location; + window.location = {} as Location; + }); + + afterEach(() => { + window.location = location; + }); + it('should convert HTTP URL to WebSocket URL', () => { + window.location.protocol = 'http:'; + window.location.host = 'example.com'; + + const httpUrl = '/api/ds/proxy/1/api/v1/tail/loki?query=a'; + const expectedWebSocketUrl = 'ws://example.com/grafana/api/ds/proxy/1/api/v1/tail/loki?query=a'; + + const result = convertToWebSocketUrl(httpUrl); + + expect(result).toBe(expectedWebSocketUrl); + }); + + it('should convert HTTPS URL to WebSocket URL', () => { + window.location.protocol = 'https:'; + window.location.host = 'example.com'; + + const httpsUrl = '/api/ds/proxy/1/api/v1/tail/loki?query=a'; + const expectedWebSocketUrl = 'wss://example.com/grafana/api/ds/proxy/1/api/v1/tail/loki?query=a'; + + const result = convertToWebSocketUrl(httpsUrl); + + expect(result).toBe(expectedWebSocketUrl); + }); +}); diff --git a/public/app/plugins/datasource/loki/streaming.ts b/public/app/plugins/datasource/loki/streaming.ts index 953e74337df..d1db4c4cf03 100644 --- a/public/app/plugins/datasource/loki/streaming.ts +++ b/public/app/plugins/datasource/loki/streaming.ts @@ -8,7 +8,7 @@ import { LoadingState, StreamingDataFrame, } from '@grafana/data'; -import { getGrafanaLiveSrv } from '@grafana/runtime'; +import { getGrafanaLiveSrv, config } from '@grafana/runtime'; import { LokiDatasource } from './datasource'; import { LokiQuery } from './types'; @@ -86,3 +86,12 @@ export function doLokiChannelStream( }) ); } + +export const convertToWebSocketUrl = (url: string) => { + const protocol = window.location.protocol === 'https:' ? 'wss://' : 'ws://'; + let backend = `${protocol}${window.location.host}${config.appSubUrl}`; + if (backend.endsWith('/')) { + backend = backend.slice(0, -1); + } + return `${backend}${url}`; +};