Skip to content
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

Merged
merged 9 commits into from
Feb 22, 2022

Conversation

philvarner
Copy link
Collaborator

Related Issue(s): #137

Proposed Changes:

  1. support open-ended datetime intervals (e.g., ../{datetime}, /{datetime}, {datetime}/.., or {datetime}/
  2. fix a few camelcase violations

PR Checklist:

  • I have added my changes to the CHANGELOG or a CHANGELOG entry is not required.

Copy link
Member

@matthewhanson matthewhanson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM other than the failing test that is still using the old snake case function create_indices

@@ -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('/')) {
Copy link
Contributor

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.

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const mapping = (index === 'collections' ? collectionsMapping : itemsMapping)
const mapping = index === 'collections' ? collectionsMapping : itemsMapping

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this change was already made?

@@ -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('/')) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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)
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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

src/lib/es.js Outdated Show resolved Hide resolved
@marchuffnagle marchuffnagle merged commit 071b75e into main Feb 22, 2022
@marchuffnagle marchuffnagle deleted the pv/open-ended-datetime-ranges branch February 22, 2022 22:19
@marchuffnagle
Copy link
Contributor

Closes #137

philvarner pushed a commit that referenced this pull request Feb 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants