Skip to content

Commit d3aead8

Browse files
bengreenbankshortcutsmillotpFluf22
authored
refactor(specs): new predict segment condition syntax (#1202)
Co-authored-by: Clément Vannicatte <[email protected]> Co-authored-by: Pierre Millot <[email protected]> Co-authored-by: Thomas Raffray <[email protected]>
1 parent a602d1f commit d3aead8

File tree

6 files changed

+361
-16
lines changed

6 files changed

+361
-16
lines changed

generators/src/main/java/com/algolia/codegen/AlgoliaJavaGenerator.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,33 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
120120

121121
@Override
122122
public String toEnumVarName(String value, String datatype) {
123-
if ("String".equals(datatype) && !value.matches("[A-Z0-9_]+")) {
123+
// when it's not a string, we don't want to change the name of the variable generated
124+
if (!"String".equals(datatype)) {
125+
return super.toEnumVarName(value, datatype);
126+
}
127+
128+
// predict has some enums that are operators, we internally convert them to prevent wrong
129+
// assumptions from the generator/templates.
130+
switch (value) {
131+
case "<":
132+
return "LT";
133+
case ">":
134+
return "GT";
135+
case "=":
136+
return "EQ";
137+
case "<=":
138+
return "LTE";
139+
case ">=":
140+
return "GTE";
141+
case "!=":
142+
return "NEQ";
143+
}
144+
145+
if (!value.matches("[A-Z0-9_]+")) {
124146
// convert camelCase77String to CAMEL_CASE_77_STRING
125147
return value.replaceAll("-", "_").replaceAll("(.+?)([A-Z]|[0-9])", "$1_$2").toUpperCase(Locale.ROOT);
126148
}
149+
127150
return super.toEnumVarName(value, datatype);
128151
}
129152
}

generators/src/main/java/com/algolia/codegen/cts/tests/ParametersWithDataType.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,11 +560,22 @@ private IJsonSchemaValidationProperties findMatchingOneOf(Object param, CodegenM
560560
String paramType = inferDataType(param, maybeMatch, null);
561561
maybeMatch.dataType = paramType;
562562

563+
boolean hasFloat = false;
563564
for (String oneOfName : model.oneOf) {
564565
if (oneOfName.equals(paramType)) {
565566
return maybeMatch;
566567
}
568+
if (oneOfName.equals("Float") || oneOfName.equals("Double")) {
569+
hasFloat = true;
570+
}
571+
}
572+
573+
// If there is a number, try to use it as other number type, in the order
574+
// Integer, Long, Float, Double
575+
if (hasFloat && (paramType.equals("Integer") || paramType.equals("Long") || paramType.equals("Double"))) {
576+
return maybeMatch;
567577
}
578+
568579
for (CodegenModel oneOf : model.interfaceModels) {
569580
// Somehow the dataType can be in lower case?
570581
if (oneOf.dataType.toLowerCase().equals(paramType.toLowerCase())) {

specs/predict/common/schemas/SegmentsParams.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ createSegmentParams:
77
name:
88
$ref: '#/name'
99
conditions:
10-
$ref: '#/conditions'
10+
$ref: '../../responses/Segment.yml#/segmentParentConditions'
1111

1212
updateSegmentParams:
1313
oneOf:
@@ -30,12 +30,8 @@ segmentConditionsParam:
3030
type: object
3131
properties:
3232
conditions:
33-
$ref: '#/conditions'
33+
$ref: '../../responses/Segment.yml#/segmentParentConditions'
3434

3535
name:
3636
type: string
3737
description: The name or description of the segment.
38-
39-
conditions:
40-
type: string
41-
description: The filters that define the segment, defined in the same way as filters for Rules.

specs/predict/responses/Segment.yml

Lines changed: 240 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,21 @@ createSegmentResponse:
6565
segment:
6666
type: object
6767
required:
68+
- version
6869
- segmentID
6970
- name
7071
- conditions
7172
- segmentStatus
7273
- type
7374
properties:
75+
version:
76+
$ref: '#/segmentVersion'
7477
segmentID:
7578
$ref: '#/segmentID'
7679
name:
77-
type: string
80+
$ref: '#/segmentName'
7881
conditions:
79-
type: string
82+
$ref: '#/segmentParentConditions'
8083
segmentStatus:
8184
$ref: '#/segmentStatus'
8285
type:
@@ -86,25 +89,259 @@ segment:
8689

8790
segmentStatus:
8891
type: string
92+
description: The status of the segment.
8993
enum:
9094
- active
9195
- pending
9296
- failed
9397

9498
segmentType:
9599
type: string
100+
description: The type of the segment.
96101
enum:
97102
- computed
98103
- custom
99104

105+
segmentVersion:
106+
type: number
107+
format: float
108+
description: The segment syntax version.
109+
example: 1.1
110+
100111
segmentID:
101112
type: string
102113
description: The ID of the segment.
103114

115+
segmentName:
116+
type: string
117+
description: The name of the segment.
118+
104119
updatedAt:
105120
type: string
106-
description: The date and time at which the segment was last updated.
121+
description: The date and time at which the segment was last updated (RFC3339).
122+
example: 2021-07-22T12:30:00Z
107123

108124
deletedUntil:
109125
type: string
110126
description: The date and time at which the segment will be re-ingested.
127+
example: 2021-07-22T12:30:00Z
128+
129+
segmentParentConditions:
130+
description: |
131+
The conditions that define which user profiles are included in the segment.
132+
133+
Can contain operands and a maximum of 1 level of nested conditions.
134+
type: object
135+
required:
136+
- operator
137+
- operands
138+
properties:
139+
operator:
140+
$ref: '#/segmentConditionOperator'
141+
operands:
142+
type: array
143+
items:
144+
$ref: '#/segmentParentConditionOperands'
145+
146+
segmentChildConditions:
147+
description: Nested segment conditions that only contain operands.
148+
type: object
149+
required:
150+
- operator
151+
- operands
152+
properties:
153+
operator:
154+
$ref: '#/segmentConditionOperator'
155+
operands:
156+
type: array
157+
items:
158+
$ref: '#/segmentChildConditionOperands'
159+
160+
segmentConditionOperator:
161+
description: Operator used to combine the operands.
162+
type: string
163+
enum:
164+
- AND
165+
- OR
166+
167+
segmentParentConditionOperands:
168+
oneOf:
169+
- $ref: '#/segmentChildConditions'
170+
- $ref: '#/segmentOperandAffinity'
171+
- $ref: '#/segmentOperandFunnelStage'
172+
- $ref: '#/segmentOperandOrderValue'
173+
- $ref: '#/segmentOperandProperty'
174+
175+
segmentChildConditionOperands:
176+
oneOf:
177+
- $ref: '#/segmentOperandAffinity'
178+
- $ref: '#/segmentOperandFunnelStage'
179+
- $ref: '#/segmentOperandOrderValue'
180+
- $ref: '#/segmentOperandProperty'
181+
182+
segmentOperandAffinity:
183+
description: Operand for affinity model predictions.
184+
type: object
185+
required:
186+
- name
187+
- filters
188+
properties:
189+
name:
190+
type: string
191+
pattern: ^predictions\.affinities\.\w+$
192+
example: predictions.affinities.color
193+
filters:
194+
type: array
195+
minItems: 1
196+
items:
197+
$ref: '#/segmentAffinityFilter'
198+
199+
segmentOperandFunnelStage:
200+
description: Operand for funnel stage model predictions.
201+
type: object
202+
required:
203+
- name
204+
- filters
205+
properties:
206+
name:
207+
type: string
208+
pattern: ^predictions\.funnel_stage$
209+
example: predictions.funnel_stage
210+
filters:
211+
type: array
212+
minItems: 1
213+
items:
214+
$ref: '#/segmentFunnelStageFilter'
215+
216+
segmentOperandOrderValue:
217+
description: Operand for order value model predictions.
218+
type: object
219+
required:
220+
- name
221+
- filters
222+
properties:
223+
name:
224+
type: string
225+
pattern: ^predictions\.order_value$
226+
example: predictions.order_value
227+
filters:
228+
type: array
229+
minItems: 1
230+
items:
231+
$ref: '#/segmentOrderValueFilter'
232+
233+
segmentOperandProperty:
234+
description: Operand for user profile properties.
235+
type: object
236+
required:
237+
- name
238+
- filters
239+
properties:
240+
name:
241+
type: string
242+
pattern: ^properties\.(raw|computed|custom)\.\w+$
243+
example: properties.raw.age
244+
filters:
245+
type: array
246+
minItems: 1
247+
items:
248+
$ref: '#/segmentPropertyFilter'
249+
250+
segmentAffinityFilter:
251+
description: Filter for affinity model predictions.
252+
type: object
253+
required:
254+
- value
255+
- probability
256+
properties:
257+
operator:
258+
$ref: '#/segmentFilterOperatorNumerical'
259+
value:
260+
$ref: '#/segmentAffinityFilterValue'
261+
probability:
262+
$ref: '#/segmentFilterProbability'
263+
264+
segmentAffinityFilterValue:
265+
oneOf:
266+
- type: string
267+
- type: number
268+
format: float
269+
- type: array
270+
items:
271+
type: string
272+
- type: boolean
273+
274+
segmentFunnelStageFilter:
275+
description: Filter for funnel stage model predictions.
276+
type: object
277+
required:
278+
- value
279+
- probability
280+
properties:
281+
operator:
282+
$ref: '#/segmentFilterOperatorBoolean'
283+
value:
284+
type: string
285+
probability:
286+
$ref: '#/segmentFilterProbability'
287+
288+
segmentOrderValueFilter:
289+
description: Filter for order value model predictions.
290+
type: object
291+
required:
292+
- value
293+
properties:
294+
operator:
295+
$ref: '#/segmentFilterOperatorNumerical'
296+
value:
297+
type: number
298+
299+
segmentPropertyFilter:
300+
description: Filter for user profile properties.
301+
type: object
302+
required:
303+
- value
304+
properties:
305+
operator:
306+
$ref: '#/segmentFilterOperatorNumerical'
307+
value:
308+
$ref: '#/segmentPropertyFilterValue'
309+
310+
segmentPropertyFilterValue:
311+
allOf:
312+
- $ref: '#/segmentAffinityFilterValue'
313+
314+
segmentFilterOperatorBoolean:
315+
description: The operator used on the boolean filter value.
316+
type: string
317+
default: =
318+
enum:
319+
- =
320+
- '!='
321+
322+
segmentFilterOperatorNumerical:
323+
description: The operator used on the numerical filter value.
324+
type: string
325+
default: =
326+
enum:
327+
- =
328+
- '!='
329+
- '>'
330+
- '>='
331+
- <
332+
- <=
333+
334+
segmentFilterProbability:
335+
description: Probability of the filter.
336+
type: object
337+
minProperties: 1
338+
maxProperties: 2
339+
properties:
340+
lt:
341+
type: number
342+
lte:
343+
type: number
344+
gt:
345+
type: number
346+
gte:
347+
type: number

0 commit comments

Comments
 (0)