Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecote committed Jan 9, 2019
1 parent e7ff9d3 commit bf3b9fb
Show file tree
Hide file tree
Showing 22 changed files with 171 additions and 105 deletions.
Expand Up @@ -56,12 +56,12 @@ describe('findRelationships', () => {
],
})
};
const result = await findRelationships(
const result = await findRelationships({
type,
id,
size,
savedObjectsClient
);
});
expect(result).to.eql([
{ id: '1', title: 'Foo', type: 'visualization' },
{ id: '2', title: 'Bar', type: 'visualization' },
Expand Down Expand Up @@ -114,12 +114,12 @@ describe('findRelationships', () => {
})
};

const result = await findRelationships(
const result = await findRelationships({
type,
id,
size,
savedObjectsClient
);
});
expect(result).to.eql([
{ id: '1', title: 'My Dashboard', type: 'dashboard' },
{ id: '2', title: 'Your Dashboard', type: 'dashboard' },
Expand Down Expand Up @@ -177,12 +177,12 @@ describe('findRelationships', () => {
})
};

const result = await findRelationships(
const result = await findRelationships({
type,
id,
size,
savedObjectsClient
);
});
expect(result).to.eql([
{ id: '1', title: 'Foo', type: 'visualization' },
{ id: '2', title: 'Bar', type: 'visualization' },
Expand Down Expand Up @@ -282,12 +282,12 @@ describe('findRelationships', () => {
}
};

const result = await findRelationships(
const result = await findRelationships({
type,
id,
size,
savedObjectsClient
);
});
expect(result).to.eql([
{ id: '1', type: 'visualization', title: 'Foo' },
{ id: '2', type: 'visualization', title: 'Bar' },
Expand All @@ -298,7 +298,7 @@ describe('findRelationships', () => {

it('should return an empty object for invalid types', async () => {
const type = 'invalid';
const result = await findRelationships(type);
const result = await findRelationships({ type });
expect(result).to.eql({});
});
});
Expand Up @@ -26,17 +26,13 @@ export async function collectReferencesDeep(savedObjectClient, objects) {
const itemsToGet = queue.splice(0, queue.length);
const { saved_objects: savedObjects } = await savedObjectClient.bulkGet(itemsToGet);
result.push(...savedObjects);
// All references will be replaced with references once all saved object types are migrated
const allReferences = [];
savedObjects.forEach((obj) => {
allReferences.push(...(obj.references || []));
allReferences.push(...extractLegacyReferences(obj));
});
for (const reference of allReferences) {
const isInResult = result.findIndex(obj => obj.type === reference.type && obj.id === reference.id) !== -1;
if (isInResult) continue;
const isInQueue = queue.findIndex(obj => obj.type === reference.type && obj.id === reference.id) !== -1;
if (isInQueue) continue;
const references = []
.concat(...savedObjects.map(obj => obj.references || []))
// This line below will be removed once legacy support is removed
.concat(...savedObjects.map(obj => extractLegacyReferences(obj)));
for (const reference of references) {
const isDuplicate = result.concat(queue).some(obj => obj.type === reference.type && obj.id === reference.id);
if (isDuplicate) continue;
queue.push({ type: reference.type, id: reference.id });
}
}
Expand All @@ -56,7 +52,7 @@ function extractLegacyReferences(savedObject) {
panels = [];
}
for (const panel of panels) {
if (panel.type && panel.id) {
if (panel.type === 'visualization' && panel.id) {
legacyReferences.push({ type: panel.type, id: panel.id });
}
}
Expand Down
Expand Up @@ -17,8 +17,8 @@
* under the License.
*/

async function findDashboardRelationships(id, size, savedObjectsClient) {
const dashboard = await savedObjectsClient.get('dashboard', id);
async function findDashboardRelationships({ id, size, namespace, savedObjectsClient }) {
const dashboard = await savedObjectsClient.get('dashboard', id, { namespace });
const visualizations = [];

// TODO: should we handle exceptions here or at the parent level?
Expand All @@ -29,7 +29,8 @@ async function findDashboardRelationships(id, size, savedObjectsClient) {
visualizationIds.slice(0, size).map(id => ({
id,
type: 'visualization',
}))
})),
{ namespace }
);

visualizations.push(
Expand All @@ -49,9 +50,10 @@ async function findDashboardRelationships(id, size, savedObjectsClient) {
return visualizations;
}

async function findVisualizationRelationships(id, size, savedObjectsClient) {
await savedObjectsClient.get('visualization', id);
async function findVisualizationRelationships({ id, size, namespace, savedObjectsClient }) {
await savedObjectsClient.get('visualization', id, { namespace });
const allDashboardsResponse = await savedObjectsClient.find({
namespace,
type: 'dashboard',
fields: ['title', 'panelsJSON'],
});
Expand Down Expand Up @@ -81,20 +83,21 @@ async function findVisualizationRelationships(id, size, savedObjectsClient) {
return dashboards;
}

async function findSavedSearchRelationships(id, size, savedObjectsClient) {
const search = await savedObjectsClient.get('search', id);
async function findSavedSearchRelationships({ id, namespace, savedObjectsClient }) {
const search = await savedObjectsClient.get('search', id, { namespace });

const searchSourceJSON = JSON.parse(search.attributes.kibanaSavedObjectMeta.searchSourceJSON);

const indexPatterns = [];
try {
const indexPattern = await savedObjectsClient.get('index-pattern', searchSourceJSON.index);
const indexPattern = await savedObjectsClient.get('index-pattern', searchSourceJSON.index, { namespace });
indexPatterns.push({ id: indexPattern.id, type: 'index-pattern', title: indexPattern.attributes.title });
} catch (err) {
// Do nothing
}

const allVisualizationsResponse = await savedObjectsClient.find({
namespace,
type: 'visualization',
searchFields: ['savedSearchId'],
search: id,
Expand All @@ -115,16 +118,18 @@ async function findSavedSearchRelationships(id, size, savedObjectsClient) {
return visualizations.concat(indexPatterns);
}

async function findIndexPatternRelationships(id, size, savedObjectsClient) {
await savedObjectsClient.get('index-pattern', id);
async function findIndexPatternRelationships({ id, size, namespace, savedObjectsClient }) {
await savedObjectsClient.get('index-pattern', id, { namespace });
const [allVisualizationsResponse, savedSearchResponse] = await Promise.all([
savedObjectsClient.find({
namespace,
type: 'visualization',
searchFields: ['kibanaSavedObjectMeta.searchSourceJSON'],
search: '*',
fields: [`title`, `kibanaSavedObjectMeta.searchSourceJSON`],
}),
savedObjectsClient.find({
namespace,
type: 'search',
searchFields: ['kibanaSavedObjectMeta.searchSourceJSON'],
search: '*',
Expand Down Expand Up @@ -172,16 +177,16 @@ async function findIndexPatternRelationships(id, size, savedObjectsClient) {
return visualizations.concat(searches);
}

export async function findRelationships(type, id, size, savedObjectsClient) {
export async function findRelationships({ type, id, namespace, size, savedObjectsClient }) {
switch (type) {
case 'dashboard':
return await findDashboardRelationships(id, size, savedObjectsClient);
return await findDashboardRelationships({ id, size, namespace, savedObjectsClient });
case 'visualization':
return await findVisualizationRelationships(id, size, savedObjectsClient);
return await findVisualizationRelationships({ id, size, namespace, savedObjectsClient });
case 'search':
return await findSavedSearchRelationships(id, size, savedObjectsClient);
return await findSavedSearchRelationships({ id, size, namespace, savedObjectsClient });
case 'index-pattern':
return await findIndexPatternRelationships(id, size, savedObjectsClient);
return await findIndexPatternRelationships({ id, size, namespace, savedObjectsClient });
}
return [];
}
Expand Up @@ -38,7 +38,7 @@ export function registerRelationships(server) {
handler: async (req) => {
const type = req.params.type;
const id = req.params.id;
const size = req.query.size || 10;
const size = req.query.size || 10000;
const savedObjectsClient = req.getSavedObjectsClient();

return await savedObjectsClient.findRelationships(type, id, { size });
Expand Down
3 changes: 2 additions & 1 deletion src/server/saved_objects/routes/bulk_get.test.js
Expand Up @@ -59,7 +59,8 @@ describe('POST /api/saved_objects/_bulk_get', () => {
id: 'abc123',
type: 'index-pattern',
title: 'logstash-*',
version: 2
version: 2,
references: [],
}]
};

Expand Down
3 changes: 2 additions & 1 deletion src/server/saved_objects/routes/create.test.js
Expand Up @@ -57,7 +57,8 @@ describe('POST /api/saved_objects/{type}', () => {
const clientResponse = {
type: 'index-pattern',
id: 'logstash-*',
title: 'Testing'
title: 'Testing',
references: [],
};

savedObjectsClient.create.returns(Promise.resolve(clientResponse));
Expand Down
6 changes: 4 additions & 2 deletions src/server/saved_objects/routes/find.test.js
Expand Up @@ -74,13 +74,15 @@ describe('GET /api/saved_objects/_find', () => {
id: 'logstash-*',
title: 'logstash-*',
timeFieldName: '@timestamp',
notExpandable: true
notExpandable: true,
references: [],
}, {
type: 'index-pattern',
id: 'stocks-*',
title: 'stocks-*',
timeFieldName: '@timestamp',
notExpandable: true
notExpandable: true,
references: [],
}
]
};
Expand Down
3 changes: 2 additions & 1 deletion src/server/saved_objects/routes/get.test.js
Expand Up @@ -53,7 +53,8 @@ describe('GET /api/saved_objects/{type}/{id}', () => {
id: 'logstash-*',
title: 'logstash-*',
timeFieldName: '@timestamp',
notExpandable: true
notExpandable: true,
references: [],
};

savedObjectsClient.get.returns(Promise.resolve(clientResponse));
Expand Down
3 changes: 2 additions & 1 deletion src/server/saved_objects/routes/update.test.js
Expand Up @@ -51,7 +51,8 @@ describe('PUT /api/saved_objects/{type}/{id?}', () => {
payload: {
attributes: {
title: 'Testing'
}
},
references: [],
}
};

Expand Down
11 changes: 10 additions & 1 deletion src/server/saved_objects/serialization/index.ts
Expand Up @@ -48,6 +48,15 @@ export interface MigrationVersion {
[type: string]: string;
}

/**
* A reference object to anohter saved object.
*/
export interface SavedObjectReference {
name: string;
type: string;
id: string;
}

/**
* A saved object type definition that allows for miscellaneous, unknown
* properties, as current discussions around security, ACLs, etc indicate
Expand All @@ -62,7 +71,7 @@ export interface SavedObjectDoc {
migrationVersion?: MigrationVersion;
version?: number;
updated_at?: Date;
references?: object[];
references?: SavedObjectReference[];

[rootProp: string]: any;
}
Expand Down
35 changes: 35 additions & 0 deletions src/server/saved_objects/serialization/serialization.test.ts
Expand Up @@ -34,6 +34,24 @@ describe('saved object conversion', () => {
expect(actual).toHaveProperty('type', 'foo');
});

test('it copies the _source.references property to references', () => {
const serializer = new SavedObjectsSerializer(new SavedObjectsSchema());
const actual = serializer.rawToSavedObject({
_id: 'foo:bar',
_source: {
type: 'foo',
references: [{ name: 'ref_0', type: 'index-pattern', id: 'pattern*' }],
},
});
expect(actual).toHaveProperty('references', [
{
name: 'ref_0',
type: 'index-pattern',
id: 'pattern*',
},
]);
});

test('if specified it copies the _source.migrationVersion property to migrationVersion', () => {
const serializer = new SavedObjectsSerializer(new SavedObjectsSchema());
const actual = serializer.rawToSavedObject({
Expand Down Expand Up @@ -391,6 +409,23 @@ describe('saved object conversion', () => {
expect(actual._source).toHaveProperty('type', 'foo');
});

test('it copies the references property to _source.references', () => {
const serializer = new SavedObjectsSerializer(new SavedObjectsSchema());
const actual = serializer.savedObjectToRaw({
id: '1',
type: 'foo',
attributes: {},
references: [{ name: 'ref_0', type: 'index-pattern', id: 'pattern*' }],
});
expect(actual._source).toHaveProperty('references', [
{
name: 'ref_0',
type: 'index-pattern',
id: 'pattern*',
},
]);
});

test('if specified it copies the updated_at property to _source.updated_at', () => {
const serializer = new SavedObjectsSerializer(new SavedObjectsSchema());
const now = new Date();
Expand Down

0 comments on commit bf3b9fb

Please sign in to comment.