-
Notifications
You must be signed in to change notification settings - Fork 0
Bones of browse/contributors #603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
yossariano
wants to merge
1
commit into
main
Choose a base branch
from
SCC-4987-author-browse-endpoint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,89 @@ | ||
| const { parseParams } = require('../lib/util') | ||
|
|
||
| const ApiRequest = require('./api-request') | ||
| const ElasticQueryContributorsBuilder = require('./elasticsearch/elastic-query-contributors-builder') | ||
|
|
||
| const CONTRIBUTORS_INDEX = process.env.CONTRIBUTORS_INDEX | ||
|
|
||
| const SEARCH_SCOPES = [ | ||
| 'has', | ||
| 'starts_with' | ||
| ] | ||
|
|
||
| const SORT_FIELDS = [ | ||
| 'contributorName', | ||
| 'count', | ||
| 'relevance' | ||
| ] | ||
|
|
||
| // Default sort orders for different search scopes | ||
| const SEARCH_SCOPE_SORT_ORDER = { | ||
| has: 'count', | ||
| starts_with: 'contributorName' | ||
| } | ||
|
|
||
| const parseBrowseParams = function (params) { | ||
| return parseParams(params, { | ||
| q: { type: 'string' }, | ||
| page: { type: 'int', default: 1 }, | ||
| per_page: { type: 'int', default: 50, range: [0, 100] }, | ||
| sort: { type: 'string', range: SORT_FIELDS, default: SEARCH_SCOPE_SORT_ORDER[params.search_scope] || 'termLabel' }, | ||
| sort_direction: { type: 'string', range: ['asc', 'desc'] }, | ||
| search_scope: { type: 'string', range: SEARCH_SCOPES, default: '' } | ||
| }) | ||
| } | ||
|
|
||
| module.exports = function (app, _private = null) { | ||
| app.subjects = {} | ||
|
|
||
| app.subjects.browse = function (params, opts, request) { | ||
| app.logger.debug('Unparsed params: ', params) | ||
| params = parseBrowseParams(params) | ||
|
|
||
| app.logger.debug('Parsed params: ', params) | ||
|
|
||
| const body = buildElasticContributorsBody(params) | ||
|
|
||
| app.logger.debug('Contrbutors#browse', CONTRIBUTORS_INDEX, body) | ||
|
|
||
| return app.esClient.search(body, process.env.SUBJECTS_INDEX) | ||
| .then((resp) => { | ||
| return { | ||
| '@type': 'contributorList', | ||
| page: params.page, | ||
| per_page: params.per_page, | ||
| totalResults: resp.hits?.total?.value, | ||
| contributors: resp.hits?.hits?.map((hit) => { | ||
| // TODO: parse ES response into frontend-friendly format | ||
| return {} | ||
| }) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // For unit testing | ||
| if (_private && typeof _private === 'object') { | ||
| _private.buildElasticContributorsBody = buildElasticContributorsBody | ||
| _private.parseBrowseParams = parseBrowseParams | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Given GET params, returns a plainobject with `from`, `size`, `query`, | ||
| * `sort`, and any other params necessary to perform the ES query based | ||
| * on the GET params. | ||
| * | ||
| * @return {object} An object that can be posted directly to ES | ||
| */ | ||
| const buildElasticContributorsBody = function (params) { | ||
| const body = { | ||
| from: (params.per_page * (params.page - 1)), | ||
| size: params.per_page | ||
| } | ||
|
|
||
| const request = ApiRequest.fromParams(params) | ||
| const builder = ElasticQueryContributorsBuilder.forApiRequest(request) | ||
|
|
||
| body.query = builder.query.toJson() | ||
| return body | ||
| } | ||
This file contains hidden or 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,49 @@ | ||
| const ElasticQuery = require('./elastic-query') | ||
|
|
||
| class ElasticQueryContributorsBuilder { | ||
| constructor (apiRequest) { | ||
| this.request = apiRequest | ||
| this.query = new ElasticQuery() | ||
|
|
||
| switch (this.request.params.search_scope) { | ||
| case 'has': | ||
| this.requireContainingMatch() | ||
| break | ||
| case 'starts_with': | ||
| this.requirePrefixMatch() | ||
| break | ||
| default: | ||
| this.requireTermMatch() | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Match on the start of a preferred term | ||
| */ | ||
| requirePrefixMatch () { | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Require exact term match on preferred term or variants | ||
| */ | ||
| requireTermMatch () { | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Require general "and" match on provided query | ||
| */ | ||
| requireContainingMatch () { | ||
|
|
||
| } | ||
|
|
||
| /** | ||
| * Create a ElasticQueryBuilder for given ApiRequest instance | ||
| */ | ||
| static forApiRequest (request) { | ||
| return new ElasticQueryContributorsBuilder(request) | ||
| } | ||
| } | ||
|
|
||
| module.exports = ElasticQueryContributorsBuilder |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might help to avoid some bugs down the line if the strings here are constants. Right now all of these strings are defined in multiple places.