-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add app to display discover entry context
- Loading branch information
1 parent
4556ac3
commit f80a169
Showing
12 changed files
with
300 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import _ from 'lodash'; | ||
|
||
import {addComputedFields} from './utils/fields'; | ||
import {createAnchorQuery} from './utils/queries'; | ||
|
||
|
||
async function fetchAnchor(es, indexPattern, uid, sort) { | ||
const indices = await indexPattern.toIndexList(); | ||
const response = await es.search({ | ||
index: indices, | ||
body: addComputedFields(indexPattern, createAnchorQuery(uid, sort)), | ||
}); | ||
|
||
if (_.get(response, ['hits', 'total'], 0) < 1) { | ||
throw new Error('Failed to load anchor row.'); | ||
} | ||
|
||
return _.assign({}, response.hits.hits[0], { | ||
$$_isAnchor: true, | ||
}); | ||
} | ||
|
||
|
||
export { | ||
fetchAnchor, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import _ from 'lodash'; | ||
|
||
import {addComputedFields} from './utils/fields'; | ||
import {getDocumentUid} from './utils/ids'; | ||
import {createSuccessorsQuery} from './utils/queries.js'; | ||
import {reverseQuerySort} from './utils/sorting'; | ||
|
||
|
||
async function fetchContext(es, indexPattern, anchorDocument, sort, size) { | ||
const indices = await indexPattern.toIndexList(); | ||
const anchorUid = getDocumentUid(anchorDocument._type, anchorDocument._id); | ||
const successorsQuery = addComputedFields( | ||
indexPattern, | ||
createSuccessorsQuery(anchorUid, anchorDocument.sort, sort, size) | ||
); | ||
const predecessorsQuery = reverseQuerySort(successorsQuery); | ||
|
||
const response = await es.msearch({ | ||
body: [ | ||
{index: indices}, | ||
predecessorsQuery, | ||
{index: indices}, | ||
successorsQuery, | ||
], | ||
}); | ||
|
||
const predecessors = _.get(response, ['responses', 0, 'hits', 'hits'], []); | ||
const successors = _.get(response, ['responses', 1, 'hits', 'hits'], []); | ||
|
||
predecessors.reverse(); | ||
|
||
return { | ||
predecessors, | ||
successors, | ||
}; | ||
} | ||
|
||
|
||
export { | ||
fetchContext, | ||
}; |
17 changes: 17 additions & 0 deletions
17
src/core_plugins/kibana/public/context/api/utils/fields.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import _ from 'lodash'; | ||
|
||
|
||
function addComputedFields(indexPattern, query) { | ||
const computedFields = indexPattern.getComputedFields(); | ||
|
||
return _.assign({}, query, { | ||
script_fields: computedFields.scriptFields, | ||
docvalue_fields: computedFields.docvalueFields, | ||
stored_fields: computedFields.storedFields, | ||
}); | ||
}; | ||
|
||
|
||
export { | ||
addComputedFields, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
function getDocumentUid(type, id) { | ||
return `${type}#${id}`; | ||
} | ||
|
||
|
||
export { | ||
getDocumentUid, | ||
}; |
29 changes: 29 additions & 0 deletions
29
src/core_plugins/kibana/public/context/api/utils/queries.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
function createAnchorQuery(uid, contextSort) { | ||
return { | ||
_source: true, | ||
query: { | ||
terms: { | ||
_uid: [uid], | ||
}, | ||
}, | ||
sort: [contextSort], | ||
}; | ||
}; | ||
|
||
function createSuccessorsQuery(anchorUid, anchorSortValues, contextSort, size) { | ||
return { | ||
_source: true, | ||
query: { | ||
match_all: {}, | ||
}, | ||
size, | ||
sort: [ contextSort, { _uid: 'asc' } ], | ||
search_after: anchorSortValues.concat([ anchorUid ]), | ||
}; | ||
}; | ||
|
||
|
||
export { | ||
createAnchorQuery, | ||
createSuccessorsQuery, | ||
}; |
35 changes: 35 additions & 0 deletions
35
src/core_plugins/kibana/public/context/api/utils/sorting.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import _ from 'lodash'; | ||
|
||
|
||
function reverseQuerySort(query) { | ||
return _.assign({}, query, { | ||
sort: _.get(query, 'sort', []).map(reverseSortDirective), | ||
}); | ||
} | ||
|
||
function reverseSortDirective(sortDirective) { | ||
if (_.isString(sortDirective)) { | ||
return { | ||
[sortDirective]: (sortDirective === '_score' ? 'asc' : 'desc'), | ||
}; | ||
} else if (_.isObject(sortDirective)) { | ||
return _.mapValues(sortDirective, reverseSortDirection); | ||
} else { | ||
return sortDirective; | ||
} | ||
} | ||
|
||
function reverseSortDirection(sortDirection) { | ||
if (_.isObject(sortDirection)) { | ||
return sortDirection.order = reverseSortDirection(sortDirection.order); | ||
} else { | ||
return (sortDirection === 'asc' ? 'desc' : 'asc'); | ||
} | ||
} | ||
|
||
|
||
export { | ||
reverseQuerySort, | ||
reverseSortDirection, | ||
reverseSortDirective, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<div class="app-container"> | ||
<kbn-top-nav name="context" config="topNavMenu"> | ||
<!-- Transcluded elements. --> | ||
<div data-transclude-slots> | ||
<!-- Breadcrumbs. --> | ||
<div data-transclude-slot="topLeftCorner" class="localBreadcrumbs"> | ||
<div class="localBreadcrumb"> | ||
<span class="localBreadcrumb">Context</span> | ||
<span ng-bind="contextApp.indexPattern.id" class="localBreadcrumb"></span> | ||
<span ng-bind="contextApp.anchorUid" class="localBreadcrumb"></span> | ||
</div> | ||
</div> | ||
</div> | ||
</kbn-top-nav> | ||
|
||
<div class="container-fluid" role="main"> | ||
<div class="row"> | ||
<div class="col-md-12"> | ||
<div class="discover-table" fixed-scroll> | ||
<doc-table | ||
hits="contextApp.rows" | ||
index-pattern="contextApp.indexPattern" | ||
sorting="contextApp.sort" | ||
columns="contextApp.columns" | ||
infinite-scroll="true" | ||
> | ||
</doc-table> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import _ from 'lodash'; | ||
|
||
import uiModules from 'ui/modules'; | ||
import contextAppTemplate from './app.html'; | ||
import {fetchAnchor} from './api/anchor'; | ||
import {fetchContext} from './api/context'; | ||
|
||
|
||
const module = uiModules.get('apps/context', [ | ||
'kibana', | ||
'ngRoute', | ||
]); | ||
|
||
module.directive('contextApp', function ContextApp() { | ||
return { | ||
bindToController: true, | ||
controller: ContextAppController, | ||
controllerAs: 'contextApp', | ||
restrict: 'E', | ||
scope: { | ||
anchorUid: '=', | ||
columns: '=', | ||
indexPattern: '=', | ||
size: '=', | ||
sort: '=', | ||
}, | ||
template: contextAppTemplate, | ||
}; | ||
}); | ||
|
||
function ContextAppController($q, es) { | ||
this.anchorRow = null; | ||
this.rows = []; | ||
|
||
this.initialize = () => { | ||
this.actions.reload(); | ||
}; | ||
|
||
this.actions = { | ||
fetchAnchorRow: () => ( | ||
$q.resolve() | ||
.then(() => ( | ||
fetchAnchor(es, this.indexPattern, this.anchorUid, _.zipObject([this.sort])) | ||
)) | ||
.then(anchorRow => ( | ||
this.anchorRow = anchorRow | ||
)) | ||
), | ||
fetchContextRows: () => ( | ||
$q.resolve(this.anchorRowPromise) | ||
.then(anchorRow => ( | ||
fetchContext(es, this.indexPattern, anchorRow, _.zipObject([this.sort]), this.size) | ||
)) | ||
.then(({predecessors, successors}) => { | ||
this.predecessorRows = predecessors; | ||
this.successorRows = successors; | ||
}) | ||
.then(() => ( | ||
this.rows = [].concat(this.predecessorRows, [this.anchorRow], this.successorRows) | ||
)) | ||
), | ||
reload: () => { | ||
this.anchorRowPromise = this.actions.fetchAnchorRow(); | ||
this.contextRowsPromise = this.actions.fetchContextRows(); | ||
|
||
$q.all([ | ||
this.anchorRowPromise, | ||
this.contextRowsPromise, | ||
]); | ||
}, | ||
}; | ||
|
||
this.initialize(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import uiRoutes from 'ui/routes'; | ||
import './app'; | ||
import {getDocumentUid} from './api/utils/ids'; | ||
|
||
|
||
uiRoutes | ||
.when('/context/:indexPattern/:type/:id', { | ||
controller: ContextAppRouteController, | ||
controllerAs: 'contextAppRoute', | ||
resolve: { | ||
indexPattern: function ($route, courier, savedSearches) { | ||
return courier.indexPatterns.get($route.current.params.indexPattern); | ||
}, | ||
}, | ||
template: ( | ||
`<context-app | ||
anchor-uid="contextAppRoute.anchorUid" | ||
columns="['_source']" | ||
index-pattern="contextAppRoute.indexPattern" | ||
size="5" | ||
sort="['@timestamp', 'desc']" | ||
>` | ||
), | ||
}); | ||
|
||
|
||
function ContextAppRouteController($routeParams, indexPattern) { | ||
this.anchorUid = getDocumentUid($routeParams.type, $routeParams.id); | ||
this.indexPattern = indexPattern; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters