Skip to content

Commit

Permalink
Add first_value and last_value aggregations and allow winsorization.
Browse files Browse the repository at this point in the history
  • Loading branch information
chasdevs authored Oct 28, 2024
2 parents 55f32b6 + 6c5680c commit c871add
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
4 changes: 2 additions & 2 deletions eppo_metrics_sync/schema/eppo_metric_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
},
"operation": {
"description": "Which aggregation to apply to the fact",
"enum": ["sum", "count", "distinct_entity", "threshold", "conversion", "retention", "count_distinct"]
"enum": ["sum", "count", "distinct_entity", "threshold", "conversion", "retention", "count_distinct", "last_value", "first_value"]
},
"filters": {
"description": "Optional fact property filters to apply",
Expand Down Expand Up @@ -254,7 +254,7 @@
},
"operation": {
"description": "How to aggregate fact",
"enum": ["sum", "count", "distinct_entity", "count_distinct"]
"enum": ["sum", "count", "distinct_entity", "count_distinct", "last_value", "first_value"]
},
"filters": {
"description": "Optional fact property filters to apply",
Expand Down
4 changes: 2 additions & 2 deletions eppo_metrics_sync/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ def aggregation_is_valid(aggregation):
error_message = []

if aggregation['operation'] not in ['sum', 'count', 'count_distinct', 'distinct_entity', 'threshold', 'retention',
'conversion']:
'conversion', 'last_value', 'first_value']:
error_message.append(
'Invalid aggregation operation: ' + aggregation['operation']
)

# can only winsorize sum or count metrics
if aggregation['operation'] not in ['sum', 'count']:
if aggregation['operation'] not in ['sum', 'count', 'last_value', 'first_value']:
if [name for name in winsorization_parameters if name in aggregation]:
error_message.append(
'Cannot winsorize a metric with operation ' + aggregation['operation']
Expand Down
13 changes: 13 additions & 0 deletions tests/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,16 @@ def test_count_distinct():

res = aggregation_is_valid(test_agg)
assert res == None

def test_last_value():
res = aggregation_is_valid({'operation': 'last_value'})
assert res == None

def test_first_value():
res = aggregation_is_valid({'operation': 'first_value'})
assert res == None

def test_valid_yaml():
eppo_metrics_sync = EppoMetricsSync(directory=None)
eppo_metrics_sync.load_eppo_yaml(path='tests/yaml/valid/purchases.yaml')
eppo_metrics_sync.validate()
9 changes: 8 additions & 1 deletion tests/yaml/valid/purchases.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,11 @@ metrics:
operation: equals
values:
- "UNITED STATES"

- name: Last Purchase Amount
entity: User
description: The last purchase amount with winsorization
numerator:
fact_name: Purchase
operation: last_value
winsorization_lower_percentile: 0.05
winsorization_upper_percentile: 0.95

0 comments on commit c871add

Please sign in to comment.