Skip to content

Commit

Permalink
chore(routes): refactor to avoid require() of es module not supported…
Browse files Browse the repository at this point in the history
… error
  • Loading branch information
gtk-grafana committed Jun 26, 2024
1 parent d91e562 commit 4733a35
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 117 deletions.
2 changes: 1 addition & 1 deletion src/Components/IndexScene/IndexScene.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ import { addLastUsedDataSourceToStorage, getLastUsedDataSourceFromStorage } from
import { ServiceScene } from '../ServiceScene/ServiceScene';
import { LayoutScene } from './LayoutScene';
import { FilterOp } from 'services/filters';
import { SLUGS } from '../../services/routing';
import { getSlug } from '../Pages';
import { ServiceSelectionScene } from '../ServiceSelectionScene/ServiceSelectionScene';
import { LoadingPlaceholder } from '@grafana/ui';
import { locationService } from '@grafana/runtime';
import { SLUGS } from '../../services/routes';

export type LogExplorationMode = 'service_selection' | 'service_details';

Expand Down
2 changes: 1 addition & 1 deletion src/Components/LogExplorationPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getUrlSyncManager, SceneApp, useSceneApp } from '@grafana/scenes';
import { config } from '@grafana/runtime';
import { Redirect } from 'react-router-dom';
import { makeIndexPage, makeRedirectPage } from './Pages';
import { SLUGS } from '../services/routing';
import { SLUGS } from '../services/routes';

const getSceneApp = () =>
new SceneApp({
Expand Down
11 changes: 2 additions & 9 deletions src/Components/Pages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,11 @@ import {
SceneRouteMatch,
SceneTimeRange,
} from '@grafana/scenes';
import {
DRILLDOWN_URL_KEYS,
PLUGIN_BASE_URL,
prefixRoute,
ROUTE_DEFINITIONS,
ROUTES,
SERVICE_URL_KEYS,
SLUGS,
} from '../services/routing';
import { PLUGIN_BASE_URL, prefixRoute } from '../services/routing';
import { PageLayoutType } from '@grafana/data';
import { IndexScene } from './IndexScene/IndexScene';
import { locationService } from '@grafana/runtime';
import { DRILLDOWN_URL_KEYS, ROUTE_DEFINITIONS, ROUTES, SERVICE_URL_KEYS, SLUGS } from '../services/routes';

function getServicesScene() {
const DEFAULT_TIME_RANGE = { from: 'now-15m', to: 'now' };
Expand Down
3 changes: 2 additions & 1 deletion src/Components/ServiceScene/ServiceScene.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { reportAppInteraction, USER_EVENTS_ACTIONS, USER_EVENTS_PAGES } from 'se
import { DetectedLabelsResponse, extractParserAndFieldsFromDataFrame } from 'services/fields';
import { getQueryRunner } from 'services/panel';
import { buildLokiQuery } from 'services/query';
import { buildBreakdownUrl, buildServicesUrl, PLUGIN_ID, ROUTES, SLUGS } from 'services/routing';
import { PLUGIN_ID } from 'services/routing';
import { getExplorationFor, getLokiDatasource } from 'services/scenes';
import {
ALL_VARIABLE_VALUE,
Expand All @@ -44,6 +44,7 @@ import { testIds } from 'services/testIds';
import { sortLabelsByCardinality } from 'services/filters';
import { SERVICE_NAME } from 'Components/ServiceSelectionScene/ServiceSelectionScene';
import { getSlug } from '../Pages';
import { buildBreakdownUrl, buildServicesUrl, ROUTES, SLUGS } from '../../services/routes';

export interface LokiPattern {
pattern: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { SERVICE_NAME } from './ServiceSelectionScene';
import { reportAppInteraction, USER_EVENTS_ACTIONS, USER_EVENTS_PAGES } from 'services/analytics';
import { FilterOp } from 'services/filters';
import { locationService } from '@grafana/runtime';
import { buildBreakdownUrl, ROUTES } from '../../services/routing';
import { buildBreakdownUrl, ROUTES } from '../../services/routes';

export interface SelectServiceButtonState extends SceneObjectState {
service: string;
Expand Down
99 changes: 99 additions & 0 deletions src/services/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { UrlQueryMap, urlUtil } from '@grafana/data';
import {
VAR_DATASOURCE,
VAR_FIELD_GROUP_BY,
VAR_FIELDS,
VAR_FILTERS,
VAR_LABEL_GROUP_BY,
VAR_LINE_FILTER,
VAR_LOGS_FORMAT,
VAR_PATTERNS,
} from './variables';
import { prefixRoute } from './routing';

export enum SLUGS {
explore = 'explore',
logs = 'logs',
labels = 'labels',
patterns = 'patterns',
fields = 'fields',
}

export function encodeParameter(parameter: string): string {
return encodeURIComponent(parameter.replace(/\//g, '---'));
}

export function decodeParameter(parameter: string): string {
return decodeURIComponent(parameter).replace(/---/g, '/');
}

export const ROUTES = {
explore: () => prefixRoute(SLUGS.explore),
logs: (service: string) => prefixRoute(`${SLUGS.explore}/service/${encodeParameter(service)}/${SLUGS.logs}`),
fields: (service: string) => prefixRoute(`${SLUGS.explore}/service/${encodeParameter(service)}/${SLUGS.fields}`),
patterns: (service: string) => prefixRoute(`${SLUGS.explore}/service/${encodeParameter(service)}/${SLUGS.patterns}`),
labels: (service: string) => prefixRoute(`${SLUGS.explore}/service/${encodeParameter(service)}/${SLUGS.labels}`),
};
export const ROUTE_DEFINITIONS: Record<keyof typeof SLUGS, string> = {
explore: prefixRoute(SLUGS.explore),
logs: prefixRoute(`${SLUGS.explore}/service/:service/${SLUGS.logs}`),
fields: prefixRoute(`${SLUGS.explore}/service/:service/${SLUGS.fields}`),
patterns: prefixRoute(`${SLUGS.explore}/service/:service/${SLUGS.patterns}`),
labels: prefixRoute(`${SLUGS.explore}/service/:service/${SLUGS.labels}`),
};

// For redirect back to service, we just want to keep datasource, and timerange
export const SERVICE_URL_KEYS = ['from', 'to', `var-${VAR_DATASOURCE}`];
//@todo why patterns and var-patterns?
export const DRILLDOWN_URL_KEYS = [
'from',
'to',
'mode',
'urlColumns',
'visualizationType',
`selectedLine`,
VAR_PATTERNS,
`var-${VAR_PATTERNS}`,
`var-${VAR_DATASOURCE}`,
`var-${VAR_FILTERS}`,
`var-${VAR_FIELDS}`,
`var-${VAR_FIELD_GROUP_BY}`,
`var-${VAR_LABEL_GROUP_BY}`,
`var-${VAR_DATASOURCE}`,
`var-${VAR_LOGS_FORMAT}`,
`var-${VAR_LINE_FILTER}`,
];

export function buildBreakdownUrl(path: string, extraQueryParams?: UrlQueryMap): string {
return urlUtil.renderUrl(path, buildBreakdownRoute(extraQueryParams));
}

export function buildBreakdownRoute(extraQueryParams?: UrlQueryMap): UrlQueryMap {
return {
...Object.entries(urlUtil.getUrlSearchParams()).reduce<UrlQueryMap>((acc, [key, value]) => {
if (DRILLDOWN_URL_KEYS.includes(key)) {
acc[key] = value;
}

return acc;
}, {}),
...extraQueryParams,
};
}

export function buildServicesUrl(path: string, extraQueryParams?: UrlQueryMap): string {
return urlUtil.renderUrl(path, buildServicesRoute(extraQueryParams));
}

export function buildServicesRoute(extraQueryParams?: UrlQueryMap): UrlQueryMap {
return {
...Object.entries(urlUtil.getUrlSearchParams()).reduce<UrlQueryMap>((acc, [key, value]) => {
if (SERVICE_URL_KEYS.includes(key)) {
acc[key] = value;
}

return acc;
}, {}),
...extraQueryParams,
};
}
101 changes: 0 additions & 101 deletions src/services/routing.ts
Original file line number Diff line number Diff line change
@@ -1,110 +1,9 @@
import pluginJson from '../plugin.json';
import { UrlQueryMap, urlUtil } from '@grafana/data';
import {
VAR_DATASOURCE,
VAR_FIELD_GROUP_BY,
VAR_FIELDS,
VAR_FILTERS,
VAR_LABEL_GROUP_BY,
VAR_LINE_FILTER,
VAR_LOGS_FORMAT,
VAR_PATTERNS,
} from './variables';

export const PLUGIN_ID = pluginJson.id;
export const PLUGIN_BASE_URL = `/a/${PLUGIN_ID}`;

export enum SLUGS {
explore = 'explore',
logs = 'logs',
labels = 'labels',
patterns = 'patterns',
fields = 'fields',
}

export function encodeParameter(parameter: string): string {
return encodeURIComponent(parameter.replace(/\//g, '---'));
}

export function decodeParameter(parameter: string): string {
return decodeURIComponent(parameter).replace(/---/g, '/');
}

export const ROUTES = {
explore: () => prefixRoute(SLUGS.explore),
logs: (service: string) => prefixRoute(`${SLUGS.explore}/service/${encodeParameter(service)}/${SLUGS.logs}`),
fields: (service: string) => prefixRoute(`${SLUGS.explore}/service/${encodeParameter(service)}/${SLUGS.fields}`),
patterns: (service: string) => prefixRoute(`${SLUGS.explore}/service/${encodeParameter(service)}/${SLUGS.patterns}`),
labels: (service: string) => prefixRoute(`${SLUGS.explore}/service/${encodeParameter(service)}/${SLUGS.labels}`),
};

export const ROUTE_DEFINITIONS: Record<keyof typeof SLUGS, string> = {
explore: prefixRoute(SLUGS.explore),
logs: prefixRoute(`${SLUGS.explore}/service/:service/${SLUGS.logs}`),
fields: prefixRoute(`${SLUGS.explore}/service/:service/${SLUGS.fields}`),
patterns: prefixRoute(`${SLUGS.explore}/service/:service/${SLUGS.patterns}`),
labels: prefixRoute(`${SLUGS.explore}/service/:service/${SLUGS.labels}`),
};

export const EXPLORATIONS_ROUTE = `${PLUGIN_BASE_URL}/${SLUGS.explore}`;

// Prefixes the route with the base URL of the plugin
export function prefixRoute(route: string): string {
return `${PLUGIN_BASE_URL}/${route}`;
}

// For redirect back to service, we just want to keep datasource, and timerange
export const SERVICE_URL_KEYS = ['from', 'to', `var-${VAR_DATASOURCE}`];
//@todo why patterns and var-patterns?
export const DRILLDOWN_URL_KEYS = [
'from',
'to',
'mode',
'urlColumns',
'visualizationType',
`selectedLine`,
VAR_PATTERNS,
`var-${VAR_PATTERNS}`,
`var-${VAR_DATASOURCE}`,
`var-${VAR_FILTERS}`,
`var-${VAR_FIELDS}`,
`var-${VAR_FIELD_GROUP_BY}`,
`var-${VAR_LABEL_GROUP_BY}`,
`var-${VAR_DATASOURCE}`,
`var-${VAR_LOGS_FORMAT}`,
`var-${VAR_LINE_FILTER}`,
];

export function buildBreakdownUrl(path: string, extraQueryParams?: UrlQueryMap): string {
return urlUtil.renderUrl(path, buildBreakdownRoute(extraQueryParams));
}

export function buildBreakdownRoute(extraQueryParams?: UrlQueryMap): UrlQueryMap {
return {
...Object.entries(urlUtil.getUrlSearchParams()).reduce<UrlQueryMap>((acc, [key, value]) => {
if (DRILLDOWN_URL_KEYS.includes(key)) {
acc[key] = value;
}

return acc;
}, {}),
...extraQueryParams,
};
}

export function buildServicesUrl(path: string, extraQueryParams?: UrlQueryMap): string {
return urlUtil.renderUrl(path, buildServicesRoute(extraQueryParams));
}

export function buildServicesRoute(extraQueryParams?: UrlQueryMap): UrlQueryMap {
return {
...Object.entries(urlUtil.getUrlSearchParams()).reduce<UrlQueryMap>((acc, [key, value]) => {
if (SERVICE_URL_KEYS.includes(key)) {
acc[key] = value;
}

return acc;
}, {}),
...extraQueryParams,
};
}
4 changes: 2 additions & 2 deletions src/services/scenes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import {
SceneObjectUrlValues,
} from '@grafana/scenes';
import { VAR_DATASOURCE_EXPR, LOG_STREAM_SELECTOR_EXPR } from './variables';
import { EXPLORATIONS_ROUTE } from './routing';
import { IndexScene } from 'Components/IndexScene/IndexScene';
import { ROUTES } from './routes';

export function getExplorationFor(model: SceneObject): IndexScene {
return sceneGraph.getAncestor(model, IndexScene);
Expand All @@ -20,7 +20,7 @@ export function getUrlForExploration(exploration: IndexScene) {
}

export function getUrlForValues(values: SceneObjectUrlValues) {
return urlUtil.renderUrl(EXPLORATIONS_ROUTE, values);
return urlUtil.renderUrl(ROUTES.explore(), values);
}

export function getDataSource(exploration: IndexScene) {
Expand Down
1 change: 0 additions & 1 deletion tests/exploreServices.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { test, expect } from '@grafana/plugin-e2e';
import { ExplorePage } from './fixtures/explore';
import {testIds} from "../src/services/testIds";

test.describe('explore services page', () => {
let explorePage: ExplorePage;
Expand Down

0 comments on commit 4733a35

Please sign in to comment.