mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Loki: Move convertToWebSocketUrl
from Explore to Loki (#78542)
* Move convertToWebSocketUrl to Loki * Add tests * Fix test
This commit is contained in:
parent
5dd5ed9b30
commit
fd5f66083c
@ -28,8 +28,6 @@ import store from 'app/core/store';
|
|||||||
import { ExpressionDatasourceUID } from 'app/features/expressions/types';
|
import { ExpressionDatasourceUID } from 'app/features/expressions/types';
|
||||||
import { QueryOptions, QueryTransaction } from 'app/types/explore';
|
import { QueryOptions, QueryTransaction } from 'app/types/explore';
|
||||||
|
|
||||||
import { config } from '../config';
|
|
||||||
|
|
||||||
import { getNextRefIdChar } from './query';
|
import { getNextRefIdChar } from './query';
|
||||||
|
|
||||||
export const DEFAULT_UI_STATE = {
|
export const DEFAULT_UI_STATE = {
|
||||||
@ -329,15 +327,6 @@ export const getTimeRange = (timeZone: TimeZone, rawRange: RawTimeRange, fiscalY
|
|||||||
export const refreshIntervalToSortOrder = (refreshInterval?: string) =>
|
export const refreshIntervalToSortOrder = (refreshInterval?: string) =>
|
||||||
RefreshPicker.isLive(refreshInterval) ? LogsSortOrder.Ascending : LogsSortOrder.Descending;
|
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) => {
|
export const stopQueryState = (querySubscription: Unsubscribable | undefined) => {
|
||||||
if (querySubscription) {
|
if (querySubscription) {
|
||||||
querySubscription.unsubscribe();
|
querySubscription.unsubscribe();
|
||||||
|
@ -41,7 +41,6 @@ import {
|
|||||||
import { Duration } from '@grafana/lezer-logql';
|
import { Duration } from '@grafana/lezer-logql';
|
||||||
import { BackendSrvRequest, config, DataSourceWithBackend, getTemplateSrv, TemplateSrv } from '@grafana/runtime';
|
import { BackendSrvRequest, config, DataSourceWithBackend, getTemplateSrv, TemplateSrv } from '@grafana/runtime';
|
||||||
import { DataQuery } from '@grafana/schema';
|
import { DataQuery } from '@grafana/schema';
|
||||||
import { convertToWebSocketUrl } from 'app/core/utils/explore';
|
|
||||||
import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv';
|
import { getTimeSrv, TimeSrv } from 'app/features/dashboard/services/TimeSrv';
|
||||||
|
|
||||||
import { queryLogsSample, queryLogsVolume } from '../../../features/logs/logsModel';
|
import { queryLogsSample, queryLogsVolume } from '../../../features/logs/logsModel';
|
||||||
@ -83,7 +82,7 @@ import {
|
|||||||
isQueryWithError,
|
isQueryWithError,
|
||||||
requestSupportsSplitting,
|
requestSupportsSplitting,
|
||||||
} from './queryUtils';
|
} from './queryUtils';
|
||||||
import { doLokiChannelStream } from './streaming';
|
import { convertToWebSocketUrl, doLokiChannelStream } from './streaming';
|
||||||
import { trackQuery } from './tracking';
|
import { trackQuery } from './tracking';
|
||||||
import {
|
import {
|
||||||
LokiOptions,
|
LokiOptions,
|
||||||
|
45
public/app/plugins/datasource/loki/streaming.test.ts
Normal file
45
public/app/plugins/datasource/loki/streaming.test.ts
Normal file
@ -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,
|
LoadingState,
|
||||||
StreamingDataFrame,
|
StreamingDataFrame,
|
||||||
} from '@grafana/data';
|
} from '@grafana/data';
|
||||||
import { getGrafanaLiveSrv } from '@grafana/runtime';
|
import { getGrafanaLiveSrv, config } from '@grafana/runtime';
|
||||||
|
|
||||||
import { LokiDatasource } from './datasource';
|
import { LokiDatasource } from './datasource';
|
||||||
import { LokiQuery } from './types';
|
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…
Reference in New Issue
Block a user