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

[GS-8094] feat: step type contract updated to use min and max instead of … #257

Merged
merged 5 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/seven-sheep-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'contexture-elasticsearch': major
'contexture-client': major
---

`step` type contract updated to use `min` and `max` instead of `range` so it can be consistent with `number` type
3 changes: 2 additions & 1 deletion packages/client/src/exampleTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ export default F.stampKey('type', {
},
defaults: {
field: null,
range: [0, 1000],
min: 0,
max: 1000,
JBezerra marked this conversation as resolved.
Show resolved Hide resolved
steps: [0, 1000, 5000, 10000, 25000, 50000, 1000000],
JBezerra marked this conversation as resolved.
Show resolved Hide resolved
context: {
recordsCount: 0,
Expand Down
27 changes: 9 additions & 18 deletions packages/provider-elasticsearch/src/example-types/filters/step.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,31 @@
import _ from 'lodash/fp.js'
import { pickSafeNumbers } from '../../utils/futil.js'

const getMinAndMax = (range) => {
const min = range[0]
const max = range[range.length - 1]
return { min, max }
}

let filter = ({ field, range }) => {
const { min, max } = getMinAndMax(range)
return {
range: { [field]: pickSafeNumbers({ gte: min, lte: max }) },
}
}
let filter = ({ field, min, max }) => ({
range: { [field]: pickSafeNumbers({ gte: min, lte: max }) },
})

let buildQuery = (field, range) => {
let buildQuery = (field, min, max) => {
return {
aggs: {
rangeFilter: {
filter: filter({ field, range }),
filter: filter({ field, min, max }),
},
},
}
}

let result = async (node, search) => {
let { field, range } = node
const result = await search(buildQuery(field, range))
let { field, min, max } = node
const result = await search(buildQuery(field, min, max))
const recordsCount = result.aggregations.rangeFilter.doc_count
return { recordsCount }
}

export default {
hasValue: ({ range }) => !_.isEmpty(range),
hasValue: ({ min }) => !_.isNil(min),
filter,
validContext: ({ range }) => !_.isEmpty(range),
validContext: ({ min }) => !_.isNil(min),
result,
buildQuery,
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ describe('step', () => {
const input = {
type: 'step',
field: 'test',
range: [0, 500],
min: 0,
max: 500,
steps: [0, 500, 1000],
}

Expand All @@ -24,7 +25,8 @@ describe('step', () => {
it('removes upper bound when upper bound is null', () => {
const inputWithNewRange = {
...input,
range: [0, null],
min: 0,
max: null,
}
let expectedValue = { range: { test: { gte: 0 } } }
expect(filter(inputWithNewRange)).toEqual(expectedValue)
Expand All @@ -38,7 +40,7 @@ describe('step', () => {
},
},
}
let output = buildQuery(input.field, input.range)
let output = buildQuery(input.field, input.min, input.max)
expect(output).toEqual(expectedDSL)
})
})
Loading