-
Notifications
You must be signed in to change notification settings - Fork 28
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
support open-ended datetime intervals, fix camelcase violations #180
Changes from 2 commits
4c80363
3dda8dc
233d139
cf94101
e11f869
6707a43
0c1a503
14d0c24
ef855fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,20 +43,28 @@ function buildDatetimeQuery(parameters) { | |
let dateQuery | ||
const { datetime } = parameters | ||
if (datetime) { | ||
const dataRange = datetime.split('/') | ||
if (dataRange.length === 2) { | ||
if (!datetime.includes('/')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's more verbose, but consider breaking this function apart into something like this. I think it makes it easier to understand what's going on. /**
* @param {string} datetime
* @returns {unknown}
*/
const buildDatetimeRange = (datetime) => {
const [start, end, ...rest] = datetime.split('/')
if (rest.length > 0) {
return new Error('datetime value is invalid, contains more than one forward slash')
}
if (!start && !end) {
return new Error(
'datetime value is invalid, at least one end of the interval must be closed'
)
}
if (start === '..' && end === '..') {
return new Error(
'datetime value is invalid, at least one end of the interval must be closed'
)
}
let range
if (start && end) range = { gte: start, lte: end }
else if (start) range = { gte: start }
else range = { lte: end }
return {
range: {
'properties.datetime': range
}
}
}
/**
* @param {string} datetime
* @returns {unknown}
*/
const buildDatetimeTerm = (datetime) => ({
term: {
'properties.datetime': datetime
}
})
/**
* @param {string} datetime
* @returns {boolean}
*/
const isExactDatetime = (datetime) => !datetime.includes('/')
/**
* @param {Object} params
* @param {string} params.datetime
* @returns {unknown|Error|undefined}
*/
const buildDatetimeQuery = ({ datetime }) => {
if (!datetime) return undefined
return isExactDatetime(datetime)
? buildDatetimeTerm(datetime)
: buildDatetimeRange(datetime)
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I chose not to do too much work on this right now because we need to significantly improve the datetime validation #175 |
||
dateQuery = { | ||
range: { | ||
'properties.datetime': { | ||
gte: dataRange[0], | ||
lte: dataRange[1] | ||
} | ||
term: { | ||
'properties.datetime': datetime | ||
} | ||
} | ||
} else { | ||
dateQuery = { | ||
term: { | ||
'properties.datetime': datetime | ||
const [start, end, ...rest] = datetime.split('/') | ||
if (rest.length) { | ||
dateQuery = new Error('datetime value is invalid, contains more than one forward slash') | ||
} else if ((!start && !end) || (start === '..' && start === end)) { | ||
dateQuery = new Error( | ||
'datetime value is invalid, at least one end of the interval must be closed' | ||
) | ||
} else { | ||
const datetimeRange = {} | ||
if (start && start !== '..') datetimeRange.gte = start | ||
if (end && end !== '..') datetimeRange.lte = end | ||
dateQuery = { | ||
range: { | ||
'properties.datetime': datetimeRange | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -123,7 +131,9 @@ function buildQuery(parameters) { | |
} | ||
|
||
const datetimeQuery = buildDatetimeQuery(parameters) | ||
if (datetimeQuery) { | ||
if (datetimeQuery instanceof Error) { | ||
throw datetimeQuery | ||
} else if (datetimeQuery) { | ||
philvarner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
must.push(datetimeQuery) | ||
} | ||
|
||
|
@@ -344,5 +354,6 @@ module.exports = { | |
getCollections, | ||
search, | ||
editPartialItem, | ||
constructSearchParams | ||
constructSearchParams, | ||
buildDatetimeQuery | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -5,8 +5,8 @@ const { createAWSConnection, awsCredsifyAll } = require('@acuris/aws-es-connecti | |||||
const elasticsearch = require('@elastic/elasticsearch') | ||||||
const logger = console //require('./logger') | ||||||
|
||||||
const collections_mapping = require('../../fixtures/collections')() | ||||||
const items_mapping = require('../../fixtures/items')() | ||||||
const collectionsMapping = require('../../fixtures/collections')() | ||||||
const itemsMapping = require('../../fixtures/items')() | ||||||
|
||||||
let _esClient | ||||||
|
||||||
|
@@ -54,10 +54,10 @@ async function esClient() { | |||||
return _esClient | ||||||
} | ||||||
|
||||||
async function create_index(index) { | ||||||
async function createIndex(index) { | ||||||
const client = await esClient() | ||||||
const exists = await client.indices.exists({ index }) | ||||||
const mapping = (index === 'collections' ? collections_mapping : items_mapping) | ||||||
const mapping = (index === 'collections' ? collectionsMapping : itemsMapping) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this change was already made? |
||||||
if (!exists.body) { | ||||||
logger.info(`${index} does not exist, creating...`) | ||||||
try { | ||||||
|
@@ -73,5 +73,5 @@ async function create_index(index) { | |||||
|
||||||
module.exports = { | ||||||
client: esClient, | ||||||
create_index | ||||||
createIndex | ||||||
} |
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.
Rather than negating this and then having an else clause, it would be a bit easier to read if it said
if (datetime.includes('/')) {
and then the conditional orders were swapped.