Skip to content

Commit

Permalink
Inline parsing discover url
Browse files Browse the repository at this point in the history
  • Loading branch information
maryia-lapata committed Mar 11, 2020
1 parent 3fc7d7f commit a2dd603
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 170 deletions.
1 change: 1 addition & 0 deletions x-pack/plugins/graph/public/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ export function initGraphApp(angularModule, deps) {

const store = createGraphStore({
basePath: getBasePath(),
addBasePath,
indexPatternProvider: $scope.indexPatternProvider,
indexPatterns: $route.current.locals.indexPatterns,
createWorkspace: (indexPattern, exploreControls) => {
Expand Down
106 changes: 0 additions & 106 deletions x-pack/plugins/graph/public/state_management/kibana_parsed_url.ts

This file was deleted.

4 changes: 2 additions & 2 deletions x-pack/plugins/graph/public/state_management/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function createMockGraphStore({
} as unknown) as GraphWorkspaceSavedObject;

const mockedDeps: jest.Mocked<GraphStoreDependencies> = {
basePath: 'basepath',
addBasePath: jest.fn((url: string) => url),
changeUrl: jest.fn(),
chrome: ({
setBreadcrumbs: jest.fn(),
Expand Down Expand Up @@ -83,7 +83,7 @@ export function createMockGraphStore({
};
const sagaMiddleware = createSagaMiddleware();

const rootReducer = createRootReducer(mockedDeps.basePath);
const rootReducer = createRootReducer(mockedDeps.addBasePath);
const initializedRootReducer = (state: GraphState | undefined, action: AnyAction) =>
rootReducer(state || (initialStateOverwrites as GraphState), action);

Expand Down
37 changes: 0 additions & 37 deletions x-pack/plugins/graph/public/state_management/prepend_path.ts

This file was deleted.

8 changes: 4 additions & 4 deletions x-pack/plugins/graph/public/state_management/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export interface GraphState {
}

export interface GraphStoreDependencies {
basePath: string;
addBasePath: (url: string) => string;
indexPatternProvider: IndexPatternProvider;
indexPatterns: IndexPatternSavedObject[];
createWorkspace: (index: string, advancedSettings: AdvancedSettings) => void;
Expand All @@ -65,10 +65,10 @@ export interface GraphStoreDependencies {
I18nContext: I18nStart['Context'];
}

export function createRootReducer(basePath: string) {
export function createRootReducer(addBasePath: (url: string) => string) {
return combineReducers({
fields: fieldsReducer,
urlTemplates: urlTemplatesReducer(basePath),
urlTemplates: urlTemplatesReducer(addBasePath),
advancedSettings: advancedSettingsReducer,
datasource: datasourceReducer,
metaData: metaDataReducer,
Expand All @@ -91,7 +91,7 @@ function registerSagas(sagaMiddleware: SagaMiddleware<object>, deps: GraphStoreD
export const createGraphStore = (deps: GraphStoreDependencies) => {
const sagaMiddleware = createSagaMiddleware();

const rootReducer = createRootReducer(deps.basePath);
const rootReducer = createRootReducer(deps.addBasePath);

const store = createStore(rootReducer, applyMiddleware(sagaMiddleware));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import { outlinkEncoders } from '../helpers/outlink_encoders';
import { UrlTemplate } from '../types';

describe('url_templates', () => {
const addBasePath = (url: string) => url;

describe('reducer', () => {
it('should create a default template as soon as datasource is known', () => {
const templates = urlTemplatesReducer('basepath')(
const templates = urlTemplatesReducer(addBasePath)(
[],
requestDatasource({
type: 'indexpattern',
Expand All @@ -28,7 +30,7 @@ describe('url_templates', () => {
});

it('should keep non-default templates when switching datasource', () => {
const templates = urlTemplatesReducer('basepath')(
const templates = urlTemplatesReducer(addBasePath)(
[
{
description: 'default template',
Expand All @@ -52,7 +54,7 @@ describe('url_templates', () => {
});

it('should remove isDefault flag when saving a template even if it is spreaded in', () => {
const templates = urlTemplatesReducer('basepath')(
const templates = urlTemplatesReducer(addBasePath)(
[
{
description: 'abc',
Expand Down
39 changes: 21 additions & 18 deletions x-pack/plugins/graph/public/state_management/url_templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import { reducerWithInitialState } from 'typescript-fsa-reducers/dist';
import { i18n } from '@kbn/i18n';
import rison from 'rison-node';
import { takeEvery, select } from 'redux-saga/effects';
import { KibanaParsedUrl } from './kibana_parsed_url';
import { format, parse } from 'url';
import { GraphState, GraphStoreDependencies } from './store';
import { UrlTemplate } from '../types';
import { reset } from './global';
import { setDatasource, IndexpatternDatasource, requestDatasource } from './datasource';
import { outlinkEncoders } from '../helpers/outlink_encoders';
import { urlTemplatePlaceholder } from '../helpers/url_template';
import { matchesOne } from './helpers';
import { modifyUrl } from '../../../../../src/core/utils';

const actionCreator = actionCreatorFactory('x-pack/graph/urlTemplates');

Expand All @@ -32,30 +33,32 @@ const initialTemplates: UrlTemplatesState = [];

function generateDefaultTemplate(
datasource: IndexpatternDatasource,
basePath: string
addBasePath: (url: string) => string
): UrlTemplate {
const kUrl = new KibanaParsedUrl({
appId: 'kibana',
basePath,
appPath: '/discover',
});

kUrl.addQueryParameter(
'_a',
rison.encode({
const appPath = modifyUrl('/discover', parsed => {
parsed.query._a = rison.encode({
columns: ['_source'],
index: datasource.id,
interval: 'auto',
query: { language: 'kuery', query: urlTemplatePlaceholder },
sort: ['_score', 'desc'],
})
);
});
});
const parsedAppPath = parse(`/app/kibana#${appPath}`, true, true);
const formattedAppPath = format({
protocol: parsedAppPath.protocol,
host: parsedAppPath.host,
pathname: parsedAppPath.pathname,
query: parsedAppPath.query,
hash: parsedAppPath.hash,
});

// replace the URI encoded version of the tag with the unescaped version
// so it can be found with String.replace, regexp, etc.
const discoverUrl = kUrl
.getRootRelativePath()
.replace(encodeURIComponent(urlTemplatePlaceholder), urlTemplatePlaceholder);
const discoverUrl = addBasePath(formattedAppPath).replace(
encodeURIComponent(urlTemplatePlaceholder),
urlTemplatePlaceholder
);

return {
url: discoverUrl,
Expand All @@ -68,15 +71,15 @@ function generateDefaultTemplate(
};
}

export const urlTemplatesReducer = (basePath: string) =>
export const urlTemplatesReducer = (addBasePath: (url: string) => string) =>
reducerWithInitialState(initialTemplates)
.case(reset, () => initialTemplates)
.cases([requestDatasource, setDatasource], (templates, datasource) => {
if (datasource.type === 'none') {
return initialTemplates;
}
const customTemplates = templates.filter(template => !template.isDefault);
return [...customTemplates, generateDefaultTemplate(datasource, basePath)];
return [...customTemplates, generateDefaultTemplate(datasource, addBasePath)];
})
.case(loadTemplates, (_currentTemplates, newTemplates) => newTemplates)
.case(saveTemplate, (templates, { index: indexToUpdate, template: updatedTemplate }) => {
Expand Down

0 comments on commit a2dd603

Please sign in to comment.