Skip to content

Commit

Permalink
Patch for resolve #2
Browse files Browse the repository at this point in the history
Fix broken link on empty template

Improve MarkdownTemplates using SavedObjects

Restore cache logic

Using scanner as v5.6 style
  • Loading branch information
sw-jung committed Feb 22, 2018
1 parent d753730 commit 9fff7f3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 59 deletions.
4 changes: 3 additions & 1 deletion public/doc_view/markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import './markdown.less';
import defaultTemplate from './no_template.html';
import { DocViewsRegistryProvider } from 'ui/registry/doc_views';
import { convertDocToVars } from '../libs/convert_doc_to_vars';
import { compileTemplate } from '../libs/compile_template';

const compiledDefaultTemplate = compileTemplate(defaultTemplate);
DocViewsRegistryProvider.register(markdownTemplates => {
return {
title: 'Markdown',
Expand All @@ -18,7 +20,7 @@ DocViewsRegistryProvider.register(markdownTemplates => {
link: ($scope, elem) => {
const { indexPattern, hit } = $scope;
markdownTemplates.get(indexPattern.title)
.then(template => template.isEmpty() ? defaultTemplate : template.render(convertDocToVars(indexPattern, hit)))
.then(template => template.isEmpty() ? compiledDefaultTemplate($scope) : template.render(convertDocToVars(indexPattern, hit)))
.then(renderedHtml => elem.empty().append(renderedHtml));
}
}
Expand Down
83 changes: 30 additions & 53 deletions public/libs/markdown_templates.js
Original file line number Diff line number Diff line change
@@ -1,70 +1,47 @@
import { uiModules } from 'ui/modules';
import { Scanner } from 'ui/utils/scanner';
import { SavedObjectsClientProvider } from 'ui/saved_objects';
import { MarkdownTemplate } from '../libs/markdown_template';

export function MarkdownTemplatesProvider(esAdmin, kbnIndex) {
const index = kbnIndex;
const type = 'markdown_template';
export function MarkdownTemplatesProvider(kbnIndex, esAdmin, Private) {
const self = this;
const type = 'markdown_template';
const scanner = new Scanner(esAdmin, {
index: kbnIndex,
type
});
const savedObjectsClient = Private(SavedObjectsClientProvider);
const cache = {};

self.list = q => {
const allHits = [];
return esAdmin.search({
index,
type,
q,
size: 100,
ignoreUnavailable: true
})
.then(function getMoreUntilDone(resp) {
allHits.push(...resp.hits.hits);
if (resp.hits.total <= allHits.length) return allHits;
return esAdmin.scroll({
scrollId: resp._scroll_id
}).then(getMoreUntilDone);
});
self.list = queryString => {
return scanner.scanAndMap(queryString, {
pageSize: 1000,
docCount: Infinity
}).then(({ hits }) => hits);
};

self.get = id => {
return Promise.resolve(cache[id])
.then((cached) => {
return cached || esAdmin.getSource({
index,
type,
ignore: 404,
id: id
}).then((source = {}) => {
const { template, options } = source;
return cache[id] = new MarkdownTemplate(template, options);
});
});
return Promise.resolve(cache[id] || savedObjectsClient.get(type, id)
.then(({ attributes }) => {
const { template, options } = attributes;
return cache[id] = new MarkdownTemplate(template, options);
}));
};

self.save = (id, docTemplate, options) => {
return esAdmin.index({
index,
type,
id: id,
body: docTemplate,
refresh: options ? options.refresh : true
}).then(() => delete cache[id]);
self.save = (id, docTemplate) => {
return savedObjectsClient.create(type, docTemplate, {
id,
overwrite: true
})
.then(() => delete cache[id]);
};

self.delete = ids => {
ids = [].concat(ids);
return esAdmin.deleteByQuery({
index,
type,
body: {
query: {
ids: {
values: ids
}
}
},
refresh: true,
ignoreUnavailable: true
}).then(() => ids.forEach(id => delete cache[id]));
return Promise.all([].concat(ids)
.map(id => {
return savedObjectsClient.delete(type, id)
.then(() => delete cache[id]);
}));
};
}

Expand Down
5 changes: 0 additions & 5 deletions public/management/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,6 @@ uiRoutes
notify.error(e);
});
}))
.then(() => {
return esAdmin.indices.refresh({
index: kbnIndex
});
})
.then($scope.getData)
.catch(notify.error);
};
Expand Down

0 comments on commit 9fff7f3

Please sign in to comment.