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

[COST-4945] Create a new API to return list of EC2-compute instances #5117

Merged
merged 153 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 151 commits
Commits
Show all changes
153 commits
Select commit Hold shift + click to select a range
12e09ce
feat: cleaning up old changes. making first changes to create the API…
bacciotti May 20, 2024
9d85c75
feat: cleaning up old changes. making first changes to create the API…
bacciotti May 20, 2024
adf75a3
Merge branch 'main' into cost-4945_awsec2_compute_api
bacciotti May 21, 2024
b6f3dd9
Merge branch 'main' into cost-4945_awsec2_compute_api
bacciotti May 22, 2024
57ae32d
feat: insert new filters.
bacciotti May 22, 2024
0355c52
Merge branch 'main' into cost-4945_awsec2_compute_api
bacciotti May 23, 2024
bbce41d
feat: insert new filters and order by params.
bacciotti May 22, 2024
f13f9d2
feat: customizing provider map and serializer.
bacciotti May 23, 2024
87e56ed
fix: changing TIME_CHOICES options.
bacciotti May 23, 2024
6c624c1
Merge branch 'main' into cost-4945_awsec2_compute_api
bacciotti May 26, 2024
f970d16
feat: first unit tests.
bacciotti May 27, 2024
b945ffc
feat: wip.
bacciotti May 28, 2024
9f6230a
feat: fixing provider map.
bacciotti May 29, 2024
e21c004
feat: fixing provider map.
bacciotti May 29, 2024
fd34ce8
Merge branch 'main' into cost-4945_awsec2_compute_api
bacciotti May 29, 2024
e863cf9
update ec2 annotations to get all required fields
djnakabaale May 31, 2024
af45f4f
use AWSEC2ComputeQueryParamSerializer
djnakabaale May 31, 2024
1e4b69d
Merge branch 'cost-4945_awsec2_compute_api' of github.com:project-kok…
djnakabaale May 31, 2024
7e5abcb
feat: creating orderby and groupby serializers for ec2
bacciotti May 31, 2024
823bdcf
feat: unit tests for filters
bacciotti May 31, 2024
ff37a9a
Merge branch 'main' into cost-4945_awsec2_compute_api
bacciotti May 31, 2024
f4139ff
feat: removing group_by - not needed on ec2
bacciotti Jun 3, 2024
0e3af90
feat: fixing orderby serializer and starting units tests
bacciotti Jun 3, 2024
1c2fb62
fix: typo
bacciotti Jun 3, 2024
9b5938f
feat: changing usage_hours to usage_amount
bacciotti Jun 3, 2024
1490fe7
Merge branch 'main' into cost-4945_awsec2_compute_api
bacciotti Jun 3, 2024
cfe8144
feat: fixing unit test.
bacciotti Jun 3, 2024
21b38d3
Merge branch 'main' into cost-4945_awsec2_compute_api
bacciotti Jun 4, 2024
2c9bd5d
wip: blocking some filters and unit test.
bacciotti Jun 4, 2024
4d83ae2
feat: unit tests for group by filter
bacciotti Jun 4, 2024
20affa9
flake8 fix
bacciotti Jun 4, 2024
0d0a49d
Merge branch 'main' into cost-4945_awsec2_compute_api
bacciotti Jun 4, 2024
f4e80bb
feat: inserting more filters on validate function.
bacciotti Jun 4, 2024
f3de969
feat: updating validate method to use similar logic and add filters.
bacciotti Jun 4, 2024
bd778f7
fix: changing unit tests for some filters.
bacciotti Jun 4, 2024
4d63213
feat: testing filter combinations and flake8 checks.
bacciotti Jun 4, 2024
4c0af3e
fix: test
bacciotti Jun 4, 2024
bdf5c16
feat: serializer Unit tests and view Unit test fix
bacciotti Jun 5, 2024
c34ef4a
flake8 fix
bacciotti Jun 5, 2024
43fe76d
fix: new approach to satisfy CodeCov
bacciotti Jun 5, 2024
3dfccc4
fix:: getting rid of validate custom method.
bacciotti Jun 5, 2024
bf5a819
fix: commenting tags.
bacciotti Jun 5, 2024
42a95f6
fix: validarte functions, tests.
bacciotti Jun 5, 2024
c8c38f1
handle filter params for specific report type
djnakabaale Jun 5, 2024
cea2a09
transform tags to desired ui format
djnakabaale Jun 7, 2024
4067991
Merge branch 'main' into cost-4945_awsec2_compute_api
lcouzens Jun 10, 2024
8228c98
Merge branch 'main' into cost-4945_awsec2_compute_api
bacciotti Jun 11, 2024
3553d92
default to monthly resolution on the EC2 endpoint
djnakabaale Jun 11, 2024
e6cd155
add special pagination for EC2
djnakabaale Jun 11, 2024
17d1274
use default report type time period settings if exists
djnakabaale Jun 11, 2024
dbf5cf0
fix typo
djnakabaale Jun 11, 2024
a35ef49
fix: fixing parameters validations.
bacciotti Jun 12, 2024
dac9e5c
Merge branch 'main' into cost-4945_awsec2_compute_api
bacciotti Jun 12, 2024
f215976
Merge branch 'main' into cost-4945_awsec2_compute_api
bacciotti Jun 13, 2024
41bbece
Merge branch 'main' into cost-4945_awsec2_compute_api
bacciotti Jun 13, 2024
e9b96fc
Merge branch 'main' into cost-4945_awsec2_compute_api
bacciotti Jun 18, 2024
5059c38
[COST-5141] Fix management command to use continue instead of return.…
myersCody Jun 18, 2024
099ddad
[COST-5128] Process new subs tagging strategy to identify non-convert…
cgoodfred Jun 18, 2024
0359a86
[COST-4745] Add data_transfer_direction to OCP on GCP Trino tables (#…
samdoran Jun 18, 2024
b6a1944
[COST-4741] Add data_transfer_direction for AWS network costs to Trin…
samdoran Jun 18, 2024
564c721
[COST-5168] - Adding new penalty pipeline (#5176)
lcouzens Jun 19, 2024
89fefea
Improve our logging readability (#5178)
lcouzens Jun 19, 2024
edcbf9d
add prometheus metrics for new queues (#5179)
lcouzens Jun 20, 2024
f0f2e1e
add v3.3.0 operator commits (#5143)
maskarb Jun 21, 2024
e29e407
[COST-5124] Improve Trino migration management command (#5163)
samdoran Jun 21, 2024
8cbfd22
Filter accounts by matching criteria during subs processing to preven…
cgoodfred Jun 24, 2024
d5c1c6d
Update tasks.py (#5185)
lcouzens Jun 24, 2024
14ab2e0
clean up grafana dashboard (#5183)
lcouzens Jun 24, 2024
7b32db0
Skip OCPCloud tag SQL if key is present in cache but value is None (#…
cgoodfred Jun 24, 2024
8278f77
[COST-5196] - Send OCP tasks to correct queues (#5187)
lcouzens Jun 25, 2024
afa7deb
[COST-5176] correctly pass context dictionary within log_json functio…
djnakabaale Jun 25, 2024
6b755d1
batch delete S3 files (#5180)
lcouzens Jun 25, 2024
0e6b916
Bump urllib3 from 1.26.18 to 1.26.19 in the pip group across 1 direct…
dependabot[bot] Jun 25, 2024
2d56a40
Add flower as a dev dependency (#5189)
samdoran Jun 26, 2024
14f6e4d
[COST-4844] Serializer update for ordering by storageclass (#5174)
myersCody Jun 27, 2024
2cb6753
Switch to using podman in build_deploy (#5193)
samdoran Jun 27, 2024
554a2d8
skip polling providers still processing (#5181)
lcouzens Jun 28, 2024
3281bf0
[COST-5214] Move TARGETARCH declaration to the top of the Dockerfile …
samdoran Jun 28, 2024
98aaad8
[COST-5213] - fix S3 prepare (#5194)
lcouzens Jun 28, 2024
2aeaf1e
[COST-5214] pass build-arg to docker build command (#5196)
samdoran Jul 1, 2024
da276bd
[COST-5216] Delete filtering optimization (#5197)
myersCody Jul 2, 2024
8a9b415
Revert "[COST-5216] Delete filtering optimization (#5197)" (#5200)
myersCody Jul 2, 2024
b1f8a56
[COST-5226] - Skip S3 delete (daily flow) if we have marked deletion …
lcouzens Jul 2, 2024
72f8346
[COST-5076] upgrade to python 3.11 (#4444)
maskarb Jul 2, 2024
1456d54
[COST-5228] log outside for loop (#5202)
djnakabaale Jul 3, 2024
2818af2
log s3 batch deletes (#5204)
lcouzens Jul 3, 2024
7d88e49
[COST-5219] Correctly report VM usage for metering when billing recor…
cgoodfred Jul 4, 2024
cc465ba
[COST-4745] OCPGCP Network data processing SQL (#5058)
cgoodfred Jul 4, 2024
37a4670
[COST-5198] - split read traffic to read replica db using nginx proxy…
chambridge Jul 8, 2024
4f29b6a
remove unused methods (#5208)
maskarb Jul 8, 2024
823b7d2
Bump certifi in the pip group across 1 directory (#5207)
dependabot[bot] Jul 8, 2024
91aa6ec
chore(image): update and rebuild image (#5203)
github-actions[bot] Jul 8, 2024
739a956
Handle case when resource ID cannot be obtained (#5209)
chambridge Jul 9, 2024
4cb9982
[COST-5148] filter out empty resource ids and SavingsPlanCoveredUsage…
djnakabaale Jul 9, 2024
c45514f
Unpause the csi volume handle sql (#5175)
myersCody Jul 9, 2024
5fdd94d
update linting
djnakabaale Jul 9, 2024
65ae7b6
feat: cleaning up old changes. making first changes to create the API…
bacciotti May 20, 2024
57ec28c
feat: cleaning up old changes. making first changes to create the API…
bacciotti May 20, 2024
03ec3b4
feat: insert new filters.
bacciotti May 22, 2024
a5fbc2c
feat: insert new filters and order by params.
bacciotti May 22, 2024
6270d40
feat: customizing provider map and serializer.
bacciotti May 23, 2024
e4c376d
fix: changing TIME_CHOICES options.
bacciotti May 23, 2024
c9a6b32
feat: first unit tests.
bacciotti May 27, 2024
9e808a0
feat: wip.
bacciotti May 28, 2024
1e8d854
feat: fixing provider map.
bacciotti May 29, 2024
836cd19
feat: fixing provider map.
bacciotti May 29, 2024
3704bde
update ec2 annotations to get all required fields
djnakabaale May 31, 2024
5fe6d65
use AWSEC2ComputeQueryParamSerializer
djnakabaale May 31, 2024
c9ae732
feat: creating orderby and groupby serializers for ec2
bacciotti May 31, 2024
95e5836
feat: unit tests for filters
bacciotti May 31, 2024
83858d5
feat: removing group_by - not needed on ec2
bacciotti Jun 3, 2024
75ad857
feat: fixing orderby serializer and starting units tests
bacciotti Jun 3, 2024
42d3e06
fix: typo
bacciotti Jun 3, 2024
2c96923
feat: changing usage_hours to usage_amount
bacciotti Jun 3, 2024
c5a4bda
feat: fixing unit test.
bacciotti Jun 3, 2024
ef0b49f
wip: blocking some filters and unit test.
bacciotti Jun 4, 2024
2d212b2
feat: unit tests for group by filter
bacciotti Jun 4, 2024
8ffeff0
flake8 fix
bacciotti Jun 4, 2024
c3e64cb
feat: inserting more filters on validate function.
bacciotti Jun 4, 2024
a894488
feat: updating validate method to use similar logic and add filters.
bacciotti Jun 4, 2024
82974ad
fix: changing unit tests for some filters.
bacciotti Jun 4, 2024
c414c77
feat: testing filter combinations and flake8 checks.
bacciotti Jun 4, 2024
4ed48a0
fix: test
bacciotti Jun 4, 2024
b2f35d2
feat: serializer Unit tests and view Unit test fix
bacciotti Jun 5, 2024
1f9d6c3
flake8 fix
bacciotti Jun 5, 2024
09e0d0a
fix: new approach to satisfy CodeCov
bacciotti Jun 5, 2024
8df62d4
fix:: getting rid of validate custom method.
bacciotti Jun 5, 2024
5cfdb22
fix: commenting tags.
bacciotti Jun 5, 2024
ba31663
fix: validarte functions, tests.
bacciotti Jun 5, 2024
df5f986
handle filter params for specific report type
djnakabaale Jun 5, 2024
f90b7a5
transform tags to desired ui format
djnakabaale Jun 7, 2024
2b1d736
default to monthly resolution on the EC2 endpoint
djnakabaale Jun 11, 2024
8f0465b
add special pagination for EC2
djnakabaale Jun 11, 2024
dd33c01
use default report type time period settings if exists
djnakabaale Jun 11, 2024
f7e6051
fix typo
djnakabaale Jun 11, 2024
80b4976
fix: fixing parameters validations.
bacciotti Jun 12, 2024
11700a0
update linting
djnakabaale Jul 9, 2024
d8b2440
Merge branch 'cost-4945_awsec2_compute_api' of github.com:project-kok…
djnakabaale Jul 9, 2024
7aaa6bd
squash commits
djnakabaale Jul 9, 2024
84e8826
Merge branch 'cost-4945_awsec2_compute_api' of github.com:project-kok…
djnakabaale Jul 9, 2024
a82249f
clean up query params and time period settings
djnakabaale Jul 10, 2024
cc26986
do not use filter keyword
djnakabaale Jul 11, 2024
be974bc
more code clean up
djnakabaale Jul 16, 2024
d0ad047
Merge branch 'main' into cost-4945_awsec2_compute_api
bacciotti Jul 16, 2024
17825aa
address feedback
djnakabaale Jul 16, 2024
8547650
more unit tests
djnakabaale Jul 16, 2024
4447066
update openapi spec
djnakabaale Jul 17, 2024
7ddbfa4
clean up and add unit tests
djnakabaale Jul 17, 2024
834a95e
Merge branch 'main' of github.com:project-koku/koku into cost-4945_aw…
djnakabaale Jul 17, 2024
c3d0ab0
move changes to openapi spec to a separate pr
djnakabaale Jul 18, 2024
924a0d3
Merge branch 'main' of github.com:project-koku/koku into cost-4945_aw…
djnakabaale Jul 18, 2024
592ef1c
use serializer choice field and not customer validate method
djnakabaale Jul 18, 2024
45decf9
Merge branch 'main' into cost-4945_awsec2_compute_api
djnakabaale Jul 19, 2024
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
40 changes: 34 additions & 6 deletions koku/api/common/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ def get_limit_parameter(self, request):
return int(request.query_params.get(self.limit_query_param))
return None

def get_paginated_data(self, queryset):
"""Shared logic for paginating the data list."""
if self.limit:
return queryset.get("data", [])[self.offset : self.offset + self.limit]
return queryset.get("data", [])

def paginate_queryset(self, queryset, request, view=None):
"""Override queryset pagination."""
self.count = self.get_count(queryset)
Expand All @@ -157,12 +163,7 @@ def paginate_queryset(self, queryset, request, view=None):
queryset["data"] = []
return queryset

if self.limit:
query_data = queryset.get("data", [])[self.offset : self.offset + self.limit] # noqa
else:
query_data = queryset.get("data", [])

queryset["data"] = query_data
queryset["data"] = self.get_paginated_data(queryset)

return queryset

Expand Down Expand Up @@ -295,3 +296,30 @@ def get_paginated_response(self):
"data": self.data_set,
}
)


class AWSEC2ComputePagination(ReportPagination):
"""Paginator for AWS EC2 compute instances report data."""

def get_count(self, queryset):
"""Special case count for EC2 resource IDs."""
return len(queryset.get("data", [])[0].get("resource_ids", []))

def get_paginated_data(self, queryset):
"""Special case pagination for EC2 resource IDs."""

paginated_data = []

for item in queryset.get("data", []):
resource_ids = item.get("resource_ids", [])
resource_count = len(resource_ids)

# if the current offset is within the range of resource IDs.
if self.offset < resource_count:
paginated_item = item.copy()

# paginate resource IDs from current_offset to the limit
paginated_item["resource_ids"] = resource_ids[self.offset : self.offset + self.limit]
paginated_data.append(paginated_item)

return paginated_data
37 changes: 23 additions & 14 deletions koku/api/query_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
from api.currency.models import ExchangeRateDictionary
from api.query_filter import QueryFilter
from api.query_filter import QueryFilterCollection
from api.report.constants import RESOLUTION_DAILY
from api.report.constants import TIME_SCOPE_UNITS_DAILY
from api.report.constants import TIME_SCOPE_VALUES_DAILY
from api.utils import DateHelper

LOG = logging.getLogger(__name__)
Expand Down Expand Up @@ -67,9 +70,9 @@
self._max_rank = 0

self.time_scope_units = self.parameters.get_filter("time_scope_units")
if self.parameters.get_filter("time_scope_value"):
self.time_scope_value = int(self.parameters.get_filter("time_scope_value"))
# self.time_order = parameters["date"]
self.time_scope_value = (
int(time_scope_value) if (time_scope_value := self.parameters.get_filter("time_scope_value")) else None
)

# self.start_datetime = parameters["start_date"]
# self.end_datetime = parameters["end_date"]
Expand Down Expand Up @@ -187,7 +190,11 @@
(String): The value of how data will be sliced.

"""
return self.parameters.get_filter("resolution", default="daily")

get_default_report_value = self._mapper._report_type_map.get("default_time_period", {}).get(
"resolution", RESOLUTION_DAILY
)
return self.parameters.get_filter("resolution", default=get_default_report_value)

def check_query_params(self, key, in_key):
"""Test if query parameters has a given key and key within it.
Expand All @@ -209,11 +216,12 @@
(String): The value of how data will be sliced.

"""
if self.time_scope_units:
return self.time_scope_units

time_scope_units = self.parameters.get_filter("time_scope_units", default="day")
self.time_scope_units = time_scope_units
if not self.time_scope_units:
get_default_report_value = self._mapper._report_type_map.get("default_time_period", {}).get(

Check warning on line 220 in koku/api/query_handler.py

View check run for this annotation

Codecov / codecov/patch

koku/api/query_handler.py#L220

Added line #L220 was not covered by tests
"time_scope_units", TIME_SCOPE_UNITS_DAILY
)
time_scope_units = self.parameters.get_filter("time_scope_units", default=get_default_report_value)
self.time_scope_units = time_scope_units

Check warning on line 224 in koku/api/query_handler.py

View check run for this annotation

Codecov / codecov/patch

koku/api/query_handler.py#L223-L224

Added lines #L223 - L224 were not covered by tests
return self.time_scope_units

def get_time_scope_value(self):
Expand All @@ -223,11 +231,12 @@
(Integer): time relative value providing query scope

"""
if self.time_scope_value:
return self.time_scope_value

time_scope_value = self.parameters.get_filter("time_scope_value", default=-10)
self.time_scope_value = int(time_scope_value)
if not self.time_scope_value:
get_default_report_value = self._mapper._report_type_map.get("default_time_period", {}).get(

Check warning on line 235 in koku/api/query_handler.py

View check run for this annotation

Codecov / codecov/patch

koku/api/query_handler.py#L235

Added line #L235 was not covered by tests
"time_scope_value", TIME_SCOPE_VALUES_DAILY[0]
)
time_scope_value = self.parameters.get_filter("time_scope_value", default=get_default_report_value)
self.time_scope_value = int(time_scope_value)

Check warning on line 239 in koku/api/query_handler.py

View check run for this annotation

Codecov / codecov/patch

koku/api/query_handler.py#L238-L239

Added lines #L238 - L239 were not covered by tests
return self.time_scope_value

def _get_timeframe(self):
Expand Down
34 changes: 29 additions & 5 deletions koku/api/query_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@
from api.report.constants import AWS_CATEGORY_PREFIX
from api.report.constants import OR_AWS_CATEGORY_PREFIX
from api.report.constants import OR_TAG_PREFIX
from api.report.constants import RESOLUTION_DAILY
from api.report.constants import RESOLUTION_MONTHLY
from api.report.constants import TAG_PREFIX
from api.report.constants import TIME_SCOPE_UNITS_DAILY
from api.report.constants import TIME_SCOPE_UNITS_MONTHLY
from api.report.constants import TIME_SCOPE_VALUES_DAILY
from api.report.constants import TIME_SCOPE_VALUES_MONTHLY
from api.report.constants import URL_ENCODED_SAFE
from api.report.queries import ReportQueryHandler
from api.tags.serializers import month_list
from reporting.models import OCPAllCostLineItemDailySummaryP
from reporting.provider.all.models import EnabledTagKeys
from reporting.provider.aws.models import AWSEnabledCategoryKeys
Expand Down Expand Up @@ -421,21 +426,40 @@ def _set_time_scope_defaults(self):
start_date = self.get_start_date()
end_date = self.get_end_date()
resolution = self.get_filter("resolution")

if not (start_date or end_date):
if not time_scope_value:
time_scope_value = -1 if time_scope_units == "month" else -10
time_scope_value = (
TIME_SCOPE_VALUES_MONTHLY[0]
if time_scope_units == TIME_SCOPE_UNITS_MONTHLY
else TIME_SCOPE_VALUES_DAILY[0]
)
if not time_scope_units:
time_scope_units = "month" if int(time_scope_value) in month_list else "day"
time_scope_units = (
TIME_SCOPE_UNITS_MONTHLY
if time_scope_value in TIME_SCOPE_VALUES_MONTHLY
else TIME_SCOPE_UNITS_DAILY
)
if not resolution:
resolution = "monthly" if int(time_scope_value) in month_list else "daily"
resolution = RESOLUTION_MONTHLY if time_scope_value in TIME_SCOPE_VALUES_MONTHLY else RESOLUTION_DAILY

self.set_filter(
time_scope_value=str(time_scope_value),
time_scope_units=str(time_scope_units),
resolution=str(resolution),
)
else:
if not resolution:
self.set_filter(resolution="daily")
self.set_filter(resolution=RESOLUTION_DAILY)

if self.report_type == "ec2_compute":
self.set_filter(
time_scope_value=(
time_scope_value if time_scope_value in TIME_SCOPE_VALUES_MONTHLY else TIME_SCOPE_VALUES_MONTHLY[0]
),
time_scope_units=TIME_SCOPE_UNITS_MONTHLY,
resolution=RESOLUTION_MONTHLY,
)

def _validate(self, query_params):
"""Validate query parameters.
Expand Down
118 changes: 117 additions & 1 deletion koku/api/report/aws/provider_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from reporting.provider.aws.models import AWSComputeSummaryByAccountP
from reporting.provider.aws.models import AWSComputeSummaryP
from reporting.provider.aws.models import AWSCostEntryLineItemDailySummary
from reporting.provider.aws.models import AWSCostEntryLineItemSummaryByEC2Compute
from reporting.provider.aws.models import AWSCostSummaryByAccountP
from reporting.provider.aws.models import AWSCostSummaryByRegionP
from reporting.provider.aws.models import AWSCostSummaryByServiceP
Expand All @@ -29,7 +30,6 @@
from reporting.provider.aws.models import AWSStorageSummaryByAccountP
from reporting.provider.aws.models import AWSStorageSummaryP


CSV_FIELD_MAP = {"account": "id", "account_alias": "alias"}


Expand Down Expand Up @@ -69,6 +69,9 @@ def __init__(self, provider, report_type, schema_name, cost_type, markup_cost="m
"org_unit_id": {"field": "organizational_unit__org_unit_path", "operation": "icontains"},
"org_unit_single_level": {"field": "organizational_unit__org_unit_id", "operation": "icontains"},
"instance_type": {"field": "instance_type", "operation": "icontains"},
"operating_system": {"field": "operating_system", "operation": "icontains"},
"instance_name": {"field": "instance_name", "operation": "icontains"},
"resource_id": {"field": "resource_id", "operation": "icontains"},
},
"group_by_options": ["service", "account", "region", "az", "product_family", "org_unit_id"],
"tag_column": "tags",
Expand Down Expand Up @@ -270,6 +273,116 @@ def __init__(self, provider, report_type, schema_name, cost_type, markup_cost="m
"sum_columns": ["usage", "cost_total", "infra_total", "sup_total"],
"default_ordering": {"usage": "desc"},
},
"ec2_compute": {
"aggregates": {
"infra_raw": Sum(
Coalesce(F(self.cost_type), Value(0, output_field=DecimalField()))
* Coalesce("exchange_rate", Value(1, output_field=DecimalField()))
),
"infra_usage": Sum(Value(0, output_field=DecimalField())),
"infra_markup": Sum(
Coalesce(F(self.markup_cost), Value(0, output_field=DecimalField()))
* Coalesce("exchange_rate", Value(1, output_field=DecimalField()))
),
"infra_total": Sum(
(
Coalesce(F(self.cost_type), Value(0, output_field=DecimalField()))
+ Coalesce(F(self.markup_cost), Value(0, output_field=DecimalField()))
)
* Coalesce("exchange_rate", Value(1, output_field=DecimalField()))
),
"sup_raw": Sum(Value(0, output_field=DecimalField())),
"sup_usage": Sum(Value(0, output_field=DecimalField())),
"sup_markup": Sum(Value(0, output_field=DecimalField())),
"sup_total": Sum(Value(0, output_field=DecimalField())),
"cost_total": Sum(
(
Coalesce(F(self.cost_type), Value(0, output_field=DecimalField()))
+ Coalesce(F(self.markup_cost), Value(0, output_field=DecimalField()))
)
* Coalesce("exchange_rate", Value(1, output_field=DecimalField()))
),
"cost_raw": Sum(
Coalesce(F(self.cost_type), Value(0, output_field=DecimalField()))
* Coalesce("exchange_rate", Value(1, output_field=DecimalField()))
),
"cost_usage": Sum(Value(0, output_field=DecimalField())),
"cost_markup": Sum(
Coalesce(F(self.markup_cost), Value(0, output_field=DecimalField()))
* Coalesce("exchange_rate", Value(1, output_field=DecimalField()))
),
"usage": Sum("usage_amount"),
},
"aggregate_key": "usage_amount",
"annotations": {
"infra_raw": Sum(
Coalesce(F(self.cost_type), Value(0, output_field=DecimalField()))
* Coalesce("exchange_rate", Value(1, output_field=DecimalField()))
),
"infra_usage": Value(0, output_field=DecimalField()),
"infra_markup": Sum(
Coalesce(F(self.markup_cost), Value(0, output_field=DecimalField()))
* Coalesce("exchange_rate", Value(1, output_field=DecimalField()))
),
"infra_total": Sum(
(
Coalesce(F(self.cost_type), Value(0, output_field=DecimalField()))
+ Coalesce(F(self.markup_cost), Value(0, output_field=DecimalField()))
)
* Coalesce("exchange_rate", Value(1, output_field=DecimalField()))
),
"sup_raw": Value(0, output_field=DecimalField()),
"sup_usage": Value(0, output_field=DecimalField()),
"sup_markup": Value(0, output_field=DecimalField()),
"sup_total": Value(0, output_field=DecimalField()),
"cost_raw": Sum(
Coalesce(F(self.cost_type), Value(0, output_field=DecimalField()))
* Coalesce("exchange_rate", Value(1, output_field=DecimalField()))
),
"cost_usage": Value(0, output_field=DecimalField()),
"cost_markup": Sum(
Coalesce(F(self.markup_cost), Value(0, output_field=DecimalField()))
* Coalesce("exchange_rate", Value(1, output_field=DecimalField()))
),
"cost_total": Sum(
(
Coalesce(F(self.cost_type), Value(0, output_field=DecimalField()))
+ Coalesce(F(self.markup_cost), Value(0, output_field=DecimalField()))
)
* Coalesce("exchange_rate", Value(1, output_field=DecimalField()))
),
# the `currency_annotation` is inserted by the `annotations` property of the query-handler
"cost_units": Coalesce("currency_annotation", Value("USD", output_field=CharField())),
"usage": Sum("usage_amount"),
"usage_units": Coalesce(Max("unit"), Value("Hrs")),
"source_uuid": ArrayAgg(
F("source_uuid"), filter=Q(source_uuid__isnull=False), distinct=True
),
"account_alias": Max("account_alias"),
"account": Max("usage_account_id"),
"instance_name": Max("instance_name"),
"instance_type": Max("instance_type"),
"operating_system": Max("operating_system"),
"region": Max("region"),
"vcpu": Max("vcpu"),
"memory": Max("memory"),
"tags": ArrayAgg(F("tags")),
},
"filter": [{}],
"group_by": ["resource_id"],
"cost_units_key": "currency_code",
"cost_units_fallback": "USD",
"usage_units_key": "unit",
"usage_units_fallback": "Hrs",
"sum_columns": ["usage", "cost_total", "infra_total", "sup_total"],
"default_ordering": {"resource_id": "desc"},
"tables": {"query": AWSCostEntryLineItemSummaryByEC2Compute},
"default_time_period": {
"time_scope_value": "-1",
"time_scope_units": "month",
"resolution": "monthly",
},
},
"storage": {
"aggregates": {
"infra_total": Sum(
Expand Down Expand Up @@ -446,6 +559,9 @@ def __init__(self, provider, report_type, schema_name, cost_type, markup_cost="m
("account", "org_unit_id"): AWSComputeSummaryByAccountP,
("org_unit_id",): AWSComputeSummaryByAccountP,
},
"ec2_compute": {
"default": AWSCostEntryLineItemSummaryByEC2Compute,
},
"storage": {
"default": AWSStorageSummaryP,
("account",): AWSStorageSummaryByAccountP,
Expand Down
38 changes: 37 additions & 1 deletion koku/api/report/aws/query_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,11 @@ def _format_query_response(self):
(Dict): Dictionary response of query params, data, and total

"""
output = self._initialize_response_output(self.parameters)

if self._report_type == "ec2_compute":
self._format_ec2_response()

output = self._initialize_response_output(self.parameters)
output["data"] = self.query_data
output["total"] = self.query_sum

Expand Down Expand Up @@ -556,3 +559,36 @@ def _get_sub_org_units(self, org_unit_list):
except Exception as e:
LOG.error(f"Error getting sub org units: \n{e}")
return []

def _format_ec2_response(self):
"""
Format EC2 response data tansforming tags to the desired UI format.

Example transformation:

Input:
"tags": [
{"Map":"c2"},
{"Name":"instance_name_3"},
]


Output:
"tags": [
{
"key": "Map",
"values": ["c2"]
},
{
"key": "Name",
"values": ["instance_name_3"]
},
]
"""

for item in self.query_data:
for resource in item["resource_ids"]:
resource_values = resource["values"][0]
resource_values["tags"] = [
{"key": key, "values": [value]} for tag in resource_values["tags"] for key, value in tag.items()
]
Loading
Loading