Skip to content

Commit

Permalink
Implement combine_itemz
Browse files Browse the repository at this point in the history
  • Loading branch information
Iain-S committed Sep 23, 2024
1 parent 39f7408 commit eaf3fd8
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 3 deletions.
31 changes: 31 additions & 0 deletions usage_function/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,13 @@ def test_combine_items(self) -> None:
)
self.assertEqual(expected, existing_item)


class TestCombineItems(TestCase):
"""Tests for the utils.usage.combine_itemz function."""

# todo: can we combine effective price?
# todo: warnable fields (ones we presume are the same but could not be).

def test_combine_itemz_1(self) -> None:
"""Check that we sum the costs."""
items_a = [
Expand All @@ -325,6 +332,22 @@ def test_combine_itemz_1(self) -> None:
total_cost=1,
subscription_id=UUID(int=0),
),
models.Usage(
id="someid",
date=date.today(),
amortised_cost=1,
total_cost=1,
subscription_id=UUID(int=0),
reservation_id="somereservation",
),
models.Usage(
id="someid",
date=date.today(),
amortised_cost=1,
total_cost=1,
subscription_id=UUID(int=0),
reservation_id="somereservation",
),
]

actual = utils.usage.combine_itemz(items_a)
Expand All @@ -337,6 +360,14 @@ def test_combine_itemz_1(self) -> None:
total_cost=2,
subscription_id=UUID(int=0),
),
models.Usage(
id="someid",
date=date.today(),
amortised_cost=2,
total_cost=2,
subscription_id=UUID(int=0),
reservation_id="somereservation",
),
]
self.assertListEqual(expected, actual)

Expand Down
37 changes: 34 additions & 3 deletions usage_function/utils/usage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Utils for collecting and sending Azure usage data."""

import copy
import logging
from datetime import datetime, timedelta
from functools import lru_cache
Expand Down Expand Up @@ -103,9 +104,39 @@ def combine_items(item_to_update: models.Usage, other_item: models.Usage) -> Non
item_to_update.cost += other_item.cost


def combine_itemz(a):
"""Sample docstring."""
pass
def combine_itemz(items: list[models.Usage]) -> list[models.Usage]:
"""Combine usage items.
If two or more usage items share all the same values,
ignoring cost, total_cost, amortised_cost, and quantity,
combine them into one item.
"""
combinable_fields = {
"cost",
"amortised_cost",
"total_cost",
"quantity",
}

ret_list = []
for item in items:
curr_item_fields_dict = item.model_dump(exclude=combinable_fields)

match_found = False
for idx, ret_item in enumerate(ret_list):
existing_fields_dict = ret_item.model_dump(exclude=combinable_fields)

if existing_fields_dict == curr_item_fields_dict:
# item can be combined.
match_found = True
break

if match_found:
combine_items(ret_list[idx], item)
else:
# insert item into ret_list
ret_list.append(copy.deepcopy(item))
return ret_list


def retrieve_usage(
Expand Down

0 comments on commit eaf3fd8

Please sign in to comment.