mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 17:28:26 -04:00
[indexPatterns/mapping] extract conflict-describing logic
This commit is contained in:
parent
4c96efb6c6
commit
a71dd2c22b
2 changed files with 45 additions and 26 deletions
29
src/ui/public/index_patterns/_conflict_tracker.js
Normal file
29
src/ui/public/index_patterns/_conflict_tracker.js
Normal file
|
@ -0,0 +1,29 @@
|
|||
import { uniq, where, groupBy, mapValues, pluck } from 'lodash';
|
||||
|
||||
export class ConflictTracker {
|
||||
constructor() {
|
||||
this._history = [];
|
||||
}
|
||||
|
||||
trackField(name, type, index) {
|
||||
this._history.push({ name, type, index });
|
||||
}
|
||||
|
||||
describeConflict(name) {
|
||||
const fieldHistory = where(this._history, { name });
|
||||
const entriesByType = groupBy(fieldHistory, 'type');
|
||||
|
||||
return mapValues(entriesByType, (entries) => {
|
||||
const indices = uniq(pluck(entries, 'index'));
|
||||
|
||||
// keep the list short so we don't polute the .kibana index
|
||||
if (indices.length > 10) {
|
||||
const total = indices.length;
|
||||
indices.length = 9;
|
||||
indices.push(`... and ${total - indices.length} others`);
|
||||
}
|
||||
|
||||
return indices;
|
||||
});
|
||||
}
|
||||
};
|
|
@ -1,9 +1,10 @@
|
|||
import _ from 'lodash';
|
||||
import IndexPatternsMapFieldProvider from 'ui/index_patterns/_map_field';
|
||||
import { ConflictTracker } from 'ui/index_patterns/_conflict_tracker';
|
||||
|
||||
export default function transformMappingIntoFields(Private, kbnIndex, config) {
|
||||
let mapField = Private(IndexPatternsMapFieldProvider);
|
||||
|
||||
|
||||
/**
|
||||
* Convert the ES response into the simple map for fields to
|
||||
* mappings which we will cache
|
||||
|
@ -15,7 +16,8 @@ export default function transformMappingIntoFields(Private, kbnIndex, config) {
|
|||
*/
|
||||
return function (response) {
|
||||
let fields = {};
|
||||
let conflictFields = {};
|
||||
const conflictTracker = new ConflictTracker();
|
||||
|
||||
_.each(response, function (index, indexName) {
|
||||
if (indexName === kbnIndex) return;
|
||||
_.each(index.mappings, function (mappings) {
|
||||
|
@ -24,40 +26,23 @@ export default function transformMappingIntoFields(Private, kbnIndex, config) {
|
|||
if (keys.length === 0 || (name[0] === '_') && !_.contains(config.get('metaFields'), name)) return;
|
||||
|
||||
let mapping = mapField(field, name);
|
||||
|
||||
const origType = mapping.type;
|
||||
mapping.conflictDescriptions = {};
|
||||
mapping.conflictDescriptions[origType] = [indexName];
|
||||
// track the name, type and index for every field encountered so that the source
|
||||
// of conflicts can be described later
|
||||
conflictTracker.trackField(name, mapping.type, indexName);
|
||||
|
||||
if (fields[name]) {
|
||||
if (fields[name].type !== mapping.type) {
|
||||
// conflict fields are not available for much except showing in the discover table
|
||||
mapping.type = 'conflict';
|
||||
mapping.indexed = false;
|
||||
// overwrite the entire mapping object to reset all fields
|
||||
fields[name] = { type: 'conflict' };
|
||||
}
|
||||
} else {
|
||||
fields[name] = _.pick(mapping, 'type', 'indexed', 'analyzed', 'doc_values');
|
||||
}
|
||||
if (conflictFields[name]) {
|
||||
mapping.conflictDescriptions = conflictFields[name].conflictDescriptions;
|
||||
if (mapping.conflictDescriptions.hasOwnProperty(origType)) {
|
||||
mapping.conflictDescriptions[origType].push(indexName);
|
||||
} else {
|
||||
mapping.conflictDescriptions[origType] = [indexName];
|
||||
}
|
||||
}
|
||||
fields[name] = _.pick(mapping, 'type', 'indexed', 'analyzed', 'doc_values');
|
||||
conflictFields[name] = _.pick(mapping, 'type', 'indexed', 'analyzed', 'doc_values', 'conflictDescriptions');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
for (let key in conflictFields) {
|
||||
if (!conflictFields.hasOwnProperty(key)) continue;
|
||||
let conflictField = conflictFields[key];
|
||||
if (conflictField.type === 'conflict') {
|
||||
fields[key].conflictDescriptions = conflictField.conflictDescriptions;
|
||||
}
|
||||
}
|
||||
|
||||
config.get('metaFields').forEach(function (meta) {
|
||||
if (fields[meta]) return;
|
||||
|
||||
|
@ -68,6 +53,11 @@ export default function transformMappingIntoFields(Private, kbnIndex, config) {
|
|||
|
||||
return _.map(fields, function (mapping, name) {
|
||||
mapping.name = name;
|
||||
|
||||
if (mapping.type === 'conflict') {
|
||||
mapping.conflictDescriptions = conflictTracker.describeConflict(name);
|
||||
}
|
||||
|
||||
return mapping;
|
||||
});
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue