mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Merge pull request #16189 from grafana/hugoh/explore-graphite-url-not-working
Added target as isMetric property
This commit is contained in:
@@ -157,49 +157,72 @@ export function buildQueryTransaction(
|
|||||||
|
|
||||||
export const clearQueryKeys: (query: DataQuery) => object = ({ key, refId, ...rest }) => rest;
|
export const clearQueryKeys: (query: DataQuery) => object = ({ key, refId, ...rest }) => rest;
|
||||||
|
|
||||||
const isMetricSegment = (segment: { [key: string]: string }) => segment.hasOwnProperty('expr');
|
const metricProperties = ['expr', 'target', 'datasource'];
|
||||||
|
const isMetricSegment = (segment: { [key: string]: string }) =>
|
||||||
|
metricProperties.some(prop => segment.hasOwnProperty(prop));
|
||||||
const isUISegment = (segment: { [key: string]: string }) => segment.hasOwnProperty('ui');
|
const isUISegment = (segment: { [key: string]: string }) => segment.hasOwnProperty('ui');
|
||||||
|
|
||||||
export function parseUrlState(initial: string | undefined): ExploreUrlState {
|
enum ParseUrlStateIndex {
|
||||||
let uiState = DEFAULT_UI_STATE;
|
RangeFrom = 0,
|
||||||
|
RangeTo = 1,
|
||||||
|
Datasource = 2,
|
||||||
|
SegmentsStart = 3,
|
||||||
|
}
|
||||||
|
|
||||||
if (initial) {
|
enum ParseUiStateIndex {
|
||||||
try {
|
Graph = 0,
|
||||||
const parsed = JSON.parse(decodeURI(initial));
|
Logs = 1,
|
||||||
if (Array.isArray(parsed)) {
|
Table = 2,
|
||||||
if (parsed.length <= 3) {
|
Strategy = 3,
|
||||||
throw new Error('Error parsing compact URL state for Explore.');
|
}
|
||||||
}
|
|
||||||
const range = {
|
|
||||||
from: parsed[0],
|
|
||||||
to: parsed[1],
|
|
||||||
};
|
|
||||||
const datasource = parsed[2];
|
|
||||||
let queries = [];
|
|
||||||
|
|
||||||
parsed.slice(3).forEach(segment => {
|
export const safeParseJson = (text: string) => {
|
||||||
if (isMetricSegment(segment)) {
|
if (!text) {
|
||||||
queries = [...queries, segment];
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
if (isUISegment(segment)) {
|
|
||||||
uiState = {
|
|
||||||
showingGraph: segment.ui[0],
|
|
||||||
showingLogs: segment.ui[1],
|
|
||||||
showingTable: segment.ui[2],
|
|
||||||
dedupStrategy: segment.ui[3],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return { datasource, queries, range, ui: uiState };
|
|
||||||
}
|
|
||||||
return parsed;
|
|
||||||
} catch (e) {
|
|
||||||
console.error(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return { datasource: null, queries: [], range: DEFAULT_RANGE, ui: uiState };
|
|
||||||
|
try {
|
||||||
|
return JSON.parse(decodeURI(text));
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export function parseUrlState(initial: string | undefined): ExploreUrlState {
|
||||||
|
const parsed = safeParseJson(initial);
|
||||||
|
const errorResult = { datasource: null, queries: [], range: DEFAULT_RANGE, ui: DEFAULT_UI_STATE };
|
||||||
|
|
||||||
|
if (!parsed) {
|
||||||
|
return errorResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Array.isArray(parsed)) {
|
||||||
|
return parsed;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parsed.length <= ParseUrlStateIndex.SegmentsStart) {
|
||||||
|
console.error('Error parsing compact URL state for Explore.');
|
||||||
|
return errorResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
const range = {
|
||||||
|
from: parsed[ParseUrlStateIndex.RangeFrom],
|
||||||
|
to: parsed[ParseUrlStateIndex.RangeTo],
|
||||||
|
};
|
||||||
|
const datasource = parsed[ParseUrlStateIndex.Datasource];
|
||||||
|
const parsedSegments = parsed.slice(ParseUrlStateIndex.SegmentsStart);
|
||||||
|
const queries = parsedSegments.filter(segment => isMetricSegment(segment));
|
||||||
|
const uiState = parsedSegments.filter(segment => isUISegment(segment))[0];
|
||||||
|
const ui = uiState
|
||||||
|
? {
|
||||||
|
showingGraph: uiState.ui[ParseUiStateIndex.Graph],
|
||||||
|
showingLogs: uiState.ui[ParseUiStateIndex.Logs],
|
||||||
|
showingTable: uiState.ui[ParseUiStateIndex.Table],
|
||||||
|
dedupStrategy: uiState.ui[ParseUiStateIndex.Strategy],
|
||||||
|
}
|
||||||
|
: DEFAULT_UI_STATE;
|
||||||
|
|
||||||
|
return { datasource, queries, range, ui };
|
||||||
}
|
}
|
||||||
|
|
||||||
export function serializeStateToUrlParam(urlState: ExploreUrlState, compact?: boolean): string {
|
export function serializeStateToUrlParam(urlState: ExploreUrlState, compact?: boolean): string {
|
||||||
|
|||||||
Reference in New Issue
Block a user