Loki: Move `convertToWebSocketUrl` from Explore to Loki (#78542)

* Move convertToWebSocketUrl to Loki

* Add tests

* Fix test
pull/78635/head
Ivana Huckova 2 years ago committed by GitHub
parent 5dd5ed9b30
commit fd5f66083c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 11
      public/app/core/utils/explore.ts
  2. 3
      public/app/plugins/datasource/loki/datasource.ts
  3. 45
      public/app/plugins/datasource/loki/streaming.test.ts
  4. 11
      public/app/plugins/datasource/loki/streaming.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();

@ -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,

@ -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);
});
});

@ -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}`;
};

Loading…
Cancel
Save