Skip to content

Commit

Permalink
refactor: 💡 remove index pattern route context provider
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Dec 4, 2020
1 parent 2d9e321 commit b6d49f8
Show file tree
Hide file tree
Showing 12 changed files with 335 additions and 471 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import { schema } from '@kbn/config-schema';
import { IndexPatternSpec } from 'src/plugins/data/common';
import { IRouter } from '../../../../../core/server';
import { assertIndexPatternsContext } from './util/assert_index_patterns_context';
import { handleErrors } from './util/handle_errors';
import { fieldSpecSchema, serializedFieldFormatSchema } from './util/schemas';
import type { IndexPatternsServiceProvider } from '../index_patterns_service';
Expand Down Expand Up @@ -69,31 +68,29 @@ export const registerCreateIndexPatternRoute = (
},
},
router.handleLegacyErrors(
handleErrors(
assertIndexPatternsContext(async (ctx, req, res) => {
const savedObjectsClient = ctx.core.savedObjects.client;
const elasticsearchClient = ctx.core.elasticsearch.client.asCurrentUser;
const indexPatternsService = await indexPatternsProvider.createIndexPatternsService(
savedObjectsClient,
elasticsearchClient
);
const body = req.body;
const indexPattern = await indexPatternsService.createAndSave(
body.index_pattern as IndexPatternSpec,
body.override,
!body.refresh_fields
);
handleErrors(async (ctx, req, res) => {
const savedObjectsClient = ctx.core.savedObjects.client;
const elasticsearchClient = ctx.core.elasticsearch.client.asCurrentUser;
const indexPatternsService = await indexPatternsProvider.createIndexPatternsService(
savedObjectsClient,
elasticsearchClient
);
const body = req.body;
const indexPattern = await indexPatternsService.createAndSave(
body.index_pattern as IndexPatternSpec,
body.override,
!body.refresh_fields
);

return res.ok({
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({
index_pattern: indexPattern.toSpec(),
}),
});
})
)
return res.ok({
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({
index_pattern: indexPattern.toSpec(),
}),
});
})
)
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import { schema } from '@kbn/config-schema';
import { IRouter } from '../../../../../core/server';
import { assertIndexPatternsContext } from './util/assert_index_patterns_context';
import { handleErrors } from './util/handle_errors';
import type { IndexPatternsServiceProvider } from '../index_patterns_service';

Expand All @@ -43,25 +42,23 @@ export const registerDeleteIndexPatternRoute = (
},
},
router.handleLegacyErrors(
handleErrors(
assertIndexPatternsContext(async (ctx, req, res) => {
const savedObjectsClient = ctx.core.savedObjects.client;
const elasticsearchClient = ctx.core.elasticsearch.client.asCurrentUser;
const indexPatternsService = await indexPatternsProvider.createIndexPatternsService(
savedObjectsClient,
elasticsearchClient
);
const id = req.params.id;
handleErrors(async (ctx, req, res) => {
const savedObjectsClient = ctx.core.savedObjects.client;
const elasticsearchClient = ctx.core.elasticsearch.client.asCurrentUser;
const indexPatternsService = await indexPatternsProvider.createIndexPatternsService(
savedObjectsClient,
elasticsearchClient
);
const id = req.params.id;

await indexPatternsService.delete(id);
await indexPatternsService.delete(id);

return res.ok({
headers: {
'content-type': 'application/json',
},
});
})
)
return res.ok({
headers: {
'content-type': 'application/json',
},
});
})
)
);
};
107 changes: 52 additions & 55 deletions src/plugins/data/server/index_patterns/routes/fields/update_fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import { schema } from '@kbn/config-schema';
import { IRouter } from '../../../../../../core/server';
import { assertIndexPatternsContext } from '../util/assert_index_patterns_context';
import { handleErrors } from '../util/handle_errors';
import { serializedFieldFormatSchema } from '../util/schemas';
import type { IndexPatternsServiceProvider } from '../../index_patterns_service';
Expand Down Expand Up @@ -65,72 +64,70 @@ export const registerUpdateFieldsRoute = (
},
},
router.handleLegacyErrors(
handleErrors(
assertIndexPatternsContext(async (ctx, req, res) => {
const savedObjectsClient = ctx.core.savedObjects.client;
const elasticsearchClient = ctx.core.elasticsearch.client.asCurrentUser;
const indexPatternsService = await indexPatternsProvider.createIndexPatternsService(
savedObjectsClient,
elasticsearchClient
);
const id = req.params.id;
const {
// eslint-disable-next-line @typescript-eslint/naming-convention
refresh_fields = true,
fields,
} = req.body;
const fieldNames = Object.keys(fields);
handleErrors(async (ctx, req, res) => {
const savedObjectsClient = ctx.core.savedObjects.client;
const elasticsearchClient = ctx.core.elasticsearch.client.asCurrentUser;
const indexPatternsService = await indexPatternsProvider.createIndexPatternsService(
savedObjectsClient,
elasticsearchClient
);
const id = req.params.id;
const {
// eslint-disable-next-line @typescript-eslint/naming-convention
refresh_fields = true,
fields,
} = req.body;
const fieldNames = Object.keys(fields);

if (fieldNames.length < 1) {
throw new Error('No fields provided.');
}
if (fieldNames.length < 1) {
throw new Error('No fields provided.');
}

const indexPattern = await indexPatternsService.get(id);
const indexPattern = await indexPatternsService.get(id);

let changeCount = 0;
for (const fieldName of fieldNames) {
const field = fields[fieldName];
let changeCount = 0;
for (const fieldName of fieldNames) {
const field = fields[fieldName];

if (field.customLabel !== undefined) {
changeCount++;
indexPattern.setFieldCustomLabel(fieldName, field.customLabel);
}
if (field.customLabel !== undefined) {
changeCount++;
indexPattern.setFieldCustomLabel(fieldName, field.customLabel);
}

if (field.count !== undefined) {
changeCount++;
indexPattern.setFieldCount(fieldName, field.count);
}
if (field.count !== undefined) {
changeCount++;
indexPattern.setFieldCount(fieldName, field.count);
}

if (field.format !== undefined) {
changeCount++;
if (field.format) {
indexPattern.setFieldFormat(fieldName, field.format);
} else {
indexPattern.deleteFieldFormat(fieldName);
}
if (field.format !== undefined) {
changeCount++;
if (field.format) {
indexPattern.setFieldFormat(fieldName, field.format);
} else {
indexPattern.deleteFieldFormat(fieldName);
}
}
}

if (changeCount < 1) {
throw new Error('Change set is empty.');
}
if (changeCount < 1) {
throw new Error('Change set is empty.');
}

await indexPatternsService.updateSavedObject(indexPattern);
await indexPatternsService.updateSavedObject(indexPattern);

if (refresh_fields) {
await indexPatternsService.refreshFields(indexPattern);
}
if (refresh_fields) {
await indexPatternsService.refreshFields(indexPattern);
}

return res.ok({
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({
index_pattern: indexPattern.toSpec(),
}),
});
})
)
return res.ok({
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({
index_pattern: indexPattern.toSpec(),
}),
});
})
)
);
};
39 changes: 18 additions & 21 deletions src/plugins/data/server/index_patterns/routes/get_index_pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import { schema } from '@kbn/config-schema';
import { IRouter } from '../../../../../core/server';
import { assertIndexPatternsContext } from './util/assert_index_patterns_context';
import { handleErrors } from './util/handle_errors';
import type { IndexPatternsServiceProvider } from '../index_patterns_service';

Expand All @@ -43,27 +42,25 @@ export const registerGetIndexPatternRoute = (
},
},
router.handleLegacyErrors(
handleErrors(
assertIndexPatternsContext(async (ctx, req, res) => {
const savedObjectsClient = ctx.core.savedObjects.client;
const elasticsearchClient = ctx.core.elasticsearch.client.asCurrentUser;
const indexPatternsService = await indexPatternsProvider.createIndexPatternsService(
savedObjectsClient,
elasticsearchClient
);
const id = req.params.id;
const indexPattern = await indexPatternsService.get(id);
handleErrors(async (ctx, req, res) => {
const savedObjectsClient = ctx.core.savedObjects.client;
const elasticsearchClient = ctx.core.elasticsearch.client.asCurrentUser;
const indexPatternsService = await indexPatternsProvider.createIndexPatternsService(
savedObjectsClient,
elasticsearchClient
);
const id = req.params.id;
const indexPattern = await indexPatternsService.get(id);

return res.ok({
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({
index_pattern: indexPattern.toSpec(),
}),
});
})
)
return res.ok({
headers: {
'content-type': 'application/json',
},
body: JSON.stringify({
index_pattern: indexPattern.toSpec(),
}),
});
})
)
);
};
Loading

0 comments on commit b6d49f8

Please sign in to comment.