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

Allow to pass precission for every field in Aggregator #24

Merged
merged 1 commit into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
20 changes: 13 additions & 7 deletions ooui/helpers/aggregated.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@


class Aggregator:
def __init__(self, data, field_definitions):
def __init__(self, data, field_definitions, precisions=None):
self.data = data
self.field_definitions = field_definitions
self.precisions = precisions or {}

def process(self):
results = {}
for field, functions in self.field_definitions.items():
precision = self.precisions.get(field)
values = [item[field] for item in self.data if field in item]
results[field] = {}
if 'sum' in functions:
results[field]['sum'] = sum(values)
result = sum(values)
results[field]['sum'] = precision and round(result, precision) or result
if 'count' in functions:
results[field]['count'] = len(values)
results[field]['count'] = round(len(values), precision)
if 'avg' in functions:
results[field]['avg'] = sum(values) / float(len(values)) if values else 0
result = sum(values) / float(len(values)) if values else 0
results[field]['avg'] = precision and round(result, precision) or result
if 'max' in functions:
results[field]['max'] = max(values)
result = max(values) if values else 0
results[field]['max'] = precision and round(result, precision) or result
if 'min' in functions:
results[field]['min'] = min(values)
return results
result = min(values) if values else 0
results[field]['min'] = precision and round(result, precision) or result
return results
20 changes: 20 additions & 0 deletions spec/aggregated_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,23 @@
expect(results['value']['avg']).to(equal(15))
expect(results['value']['max']).to(equal(20))
expect(results['value']['min']).to(equal(10))

with context('Using precision in aggregation'):

with it('should use the precision for the field'):
data = [{'value': 10.1235}, {'value': 20.1233}, {'value': 30.1238}]
field_definitions = {'value': ['sum']}
precisions = {'value': 3}
aggregator = Aggregator(data, field_definitions, precisions)
results = aggregator.process()

expect(results['value']['sum']).to(equal(60.371))


with it('not should round if is an integer'):
data = [{'value': 10}, {'value': 20}, {'value': 30}]
field_definitions = {'value': ['sum']}
aggregator = Aggregator(data, field_definitions)
results = aggregator.process()

expect(str(results['value']['sum'])).to(equal(str(60)))
Loading