Skip to content

Commit

Permalink
refactor(specs): new predict segment condition syntax (#1202)
Browse files Browse the repository at this point in the history
Co-authored-by: Clément Vannicatte <vannicattec@gmail.com>
Co-authored-by: Pierre Millot <pierre.millot@algolia.com>
Co-authored-by: Thomas Raffray <Fluf22@users.noreply.github.com>
  • Loading branch information
4 people committed Feb 13, 2023
1 parent a602d1f commit d3aead8
Show file tree
Hide file tree
Showing 6 changed files with 361 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,33 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo

@Override
public String toEnumVarName(String value, String datatype) {
if ("String".equals(datatype) && !value.matches("[A-Z0-9_]+")) {
// when it's not a string, we don't want to change the name of the variable generated
if (!"String".equals(datatype)) {
return super.toEnumVarName(value, datatype);
}

// predict has some enums that are operators, we internally convert them to prevent wrong
// assumptions from the generator/templates.
switch (value) {
case "<":
return "LT";
case ">":
return "GT";
case "=":
return "EQ";
case "<=":
return "LTE";
case ">=":
return "GTE";
case "!=":
return "NEQ";
}

if (!value.matches("[A-Z0-9_]+")) {
// convert camelCase77String to CAMEL_CASE_77_STRING
return value.replaceAll("-", "_").replaceAll("(.+?)([A-Z]|[0-9])", "$1_$2").toUpperCase(Locale.ROOT);
}

return super.toEnumVarName(value, datatype);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -560,11 +560,22 @@ private IJsonSchemaValidationProperties findMatchingOneOf(Object param, CodegenM
String paramType = inferDataType(param, maybeMatch, null);
maybeMatch.dataType = paramType;

boolean hasFloat = false;
for (String oneOfName : model.oneOf) {
if (oneOfName.equals(paramType)) {
return maybeMatch;
}
if (oneOfName.equals("Float") || oneOfName.equals("Double")) {
hasFloat = true;
}
}

// If there is a number, try to use it as other number type, in the order
// Integer, Long, Float, Double
if (hasFloat && (paramType.equals("Integer") || paramType.equals("Long") || paramType.equals("Double"))) {
return maybeMatch;
}

for (CodegenModel oneOf : model.interfaceModels) {
// Somehow the dataType can be in lower case?
if (oneOf.dataType.toLowerCase().equals(paramType.toLowerCase())) {
Expand Down
8 changes: 2 additions & 6 deletions specs/predict/common/schemas/SegmentsParams.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ createSegmentParams:
name:
$ref: '#/name'
conditions:
$ref: '#/conditions'
$ref: '../../responses/Segment.yml#/segmentParentConditions'

updateSegmentParams:
oneOf:
Expand All @@ -30,12 +30,8 @@ segmentConditionsParam:
type: object
properties:
conditions:
$ref: '#/conditions'
$ref: '../../responses/Segment.yml#/segmentParentConditions'

name:
type: string
description: The name or description of the segment.

conditions:
type: string
description: The filters that define the segment, defined in the same way as filters for Rules.
243 changes: 240 additions & 3 deletions specs/predict/responses/Segment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,21 @@ createSegmentResponse:
segment:
type: object
required:
- version
- segmentID
- name
- conditions
- segmentStatus
- type
properties:
version:
$ref: '#/segmentVersion'
segmentID:
$ref: '#/segmentID'
name:
type: string
$ref: '#/segmentName'
conditions:
type: string
$ref: '#/segmentParentConditions'
segmentStatus:
$ref: '#/segmentStatus'
type:
Expand All @@ -86,25 +89,259 @@ segment:

segmentStatus:
type: string
description: The status of the segment.
enum:
- active
- pending
- failed

segmentType:
type: string
description: The type of the segment.
enum:
- computed
- custom

segmentVersion:
type: number
format: float
description: The segment syntax version.
example: 1.1

segmentID:
type: string
description: The ID of the segment.

segmentName:
type: string
description: The name of the segment.

updatedAt:
type: string
description: The date and time at which the segment was last updated.
description: The date and time at which the segment was last updated (RFC3339).
example: 2021-07-22T12:30:00Z

deletedUntil:
type: string
description: The date and time at which the segment will be re-ingested.
example: 2021-07-22T12:30:00Z

segmentParentConditions:
description: |
The conditions that define which user profiles are included in the segment.
Can contain operands and a maximum of 1 level of nested conditions.
type: object
required:
- operator
- operands
properties:
operator:
$ref: '#/segmentConditionOperator'
operands:
type: array
items:
$ref: '#/segmentParentConditionOperands'

segmentChildConditions:
description: Nested segment conditions that only contain operands.
type: object
required:
- operator
- operands
properties:
operator:
$ref: '#/segmentConditionOperator'
operands:
type: array
items:
$ref: '#/segmentChildConditionOperands'

segmentConditionOperator:
description: Operator used to combine the operands.
type: string
enum:
- AND
- OR

segmentParentConditionOperands:
oneOf:
- $ref: '#/segmentChildConditions'
- $ref: '#/segmentOperandAffinity'
- $ref: '#/segmentOperandFunnelStage'
- $ref: '#/segmentOperandOrderValue'
- $ref: '#/segmentOperandProperty'

segmentChildConditionOperands:
oneOf:
- $ref: '#/segmentOperandAffinity'
- $ref: '#/segmentOperandFunnelStage'
- $ref: '#/segmentOperandOrderValue'
- $ref: '#/segmentOperandProperty'

segmentOperandAffinity:
description: Operand for affinity model predictions.
type: object
required:
- name
- filters
properties:
name:
type: string
pattern: ^predictions\.affinities\.\w+$
example: predictions.affinities.color
filters:
type: array
minItems: 1
items:
$ref: '#/segmentAffinityFilter'

segmentOperandFunnelStage:
description: Operand for funnel stage model predictions.
type: object
required:
- name
- filters
properties:
name:
type: string
pattern: ^predictions\.funnel_stage$
example: predictions.funnel_stage
filters:
type: array
minItems: 1
items:
$ref: '#/segmentFunnelStageFilter'

segmentOperandOrderValue:
description: Operand for order value model predictions.
type: object
required:
- name
- filters
properties:
name:
type: string
pattern: ^predictions\.order_value$
example: predictions.order_value
filters:
type: array
minItems: 1
items:
$ref: '#/segmentOrderValueFilter'

segmentOperandProperty:
description: Operand for user profile properties.
type: object
required:
- name
- filters
properties:
name:
type: string
pattern: ^properties\.(raw|computed|custom)\.\w+$
example: properties.raw.age
filters:
type: array
minItems: 1
items:
$ref: '#/segmentPropertyFilter'

segmentAffinityFilter:
description: Filter for affinity model predictions.
type: object
required:
- value
- probability
properties:
operator:
$ref: '#/segmentFilterOperatorNumerical'
value:
$ref: '#/segmentAffinityFilterValue'
probability:
$ref: '#/segmentFilterProbability'

segmentAffinityFilterValue:
oneOf:
- type: string
- type: number
format: float
- type: array
items:
type: string
- type: boolean

segmentFunnelStageFilter:
description: Filter for funnel stage model predictions.
type: object
required:
- value
- probability
properties:
operator:
$ref: '#/segmentFilterOperatorBoolean'
value:
type: string
probability:
$ref: '#/segmentFilterProbability'

segmentOrderValueFilter:
description: Filter for order value model predictions.
type: object
required:
- value
properties:
operator:
$ref: '#/segmentFilterOperatorNumerical'
value:
type: number

segmentPropertyFilter:
description: Filter for user profile properties.
type: object
required:
- value
properties:
operator:
$ref: '#/segmentFilterOperatorNumerical'
value:
$ref: '#/segmentPropertyFilterValue'

segmentPropertyFilterValue:
allOf:
- $ref: '#/segmentAffinityFilterValue'

segmentFilterOperatorBoolean:
description: The operator used on the boolean filter value.
type: string
default: =
enum:
- =
- '!='

segmentFilterOperatorNumerical:
description: The operator used on the numerical filter value.
type: string
default: =
enum:
- =
- '!='
- '>'
- '>='
- <
- <=

segmentFilterProbability:
description: Probability of the filter.
type: object
minProperties: 1
maxProperties: 2
properties:
lt:
type: number
lte:
type: number
gt:
type: number
gte:
type: number
Loading

0 comments on commit d3aead8

Please sign in to comment.