From 72271e9fca2fc69167e4862f64a356c7f0b01f77 Mon Sep 17 00:00:00 2001 From: Iain-S <25081046+Iain-S@users.noreply.github.com> Date: Thu, 16 May 2024 10:47:04 +0100 Subject: [PATCH] Simplify retrieve_data function --- usage_function/pyproject.toml | 2 +- usage_function/utils/usage.py | 10 +++------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/usage_function/pyproject.toml b/usage_function/pyproject.toml index 2e446f5..bddcbf9 100644 --- a/usage_function/pyproject.toml +++ b/usage_function/pyproject.toml @@ -6,8 +6,8 @@ authors = [] packages = [ {include = "usage"}, {include = "utils"}, -# {include = "costmanagement"}, {include = "monthly_usage"}, + # {include = "costmanagement"}, ] [tool.poetry.dependencies] diff --git a/usage_function/utils/usage.py b/usage_function/utils/usage.py index 415a8c8..4732623 100644 --- a/usage_function/utils/usage.py +++ b/usage_function/utils/usage.py @@ -108,9 +108,8 @@ def retrieve_usage(usage_data): else: usage_item.amortised_cost = 0.0 - if usage_item.id in all_items: - existing_item = all_items[usage_item.id] - # Add to the existing item + if existing_item := all_items.get(usage_item.id): + # Update the existing item existing_item.quantity += usage_item.quantity existing_item.effective_price += usage_item.effective_price existing_item.cost += usage_item.cost @@ -118,9 +117,6 @@ def retrieve_usage(usage_data): existing_item.total_cost += usage_item.total_cost existing_item.unit_price += usage_item.unit_price - # Update the dict entry - all_items[usage_item.id] = existing_item - else: all_items[usage_item.id] = usage_item @@ -132,7 +128,7 @@ def retrieve_usage(usage_data): datetime.now() - started_processing_at, ) - return list(all_items.values()) + return all_item_list def retrieve_and_send_usage(hostname_or_ip, usage_data):