Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
akinaka
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
12
Issues
12
List
Boards
Labels
Service Desk
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Container Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
olindata
akinaka
Commits
79ec3a73
Commit
79ec3a73
authored
Mar 16, 2019
by
Nazrul Kamaruddin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added multi-headers with AWS account id as value.
parent
df4cc4e3
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
52 additions
and
18 deletions
+52
-18
akinaka_reporting/billing_summary/billing_queries.py
akinaka_reporting/billing_summary/billing_queries.py
+52
-18
No files found.
akinaka_reporting/billing_summary/billing_queries.py
View file @
79ec3a73
...
...
@@ -4,35 +4,69 @@ import time
import
datetime
import
sys
import
tabulate
import
json
class
BillingQueries
():
def
__init__
(
self
,
region
,
assume_role_arn
):
self
.
costexplorer
=
akinaka_libs
.
costexplorer
.
CostExplorer
(
region
,
assume_role_arn
)
def
days_estimates
(
self
,
from_days_ago
):
output
=
'screen'
try
:
response
=
self
.
costexplorer
.
get_bill_estimates
(
from_days_ago
)
data
=
response
[
'ResultsByTime'
]
message
=
"
\n
"
except
Exception
as
e
:
print
(
"Billing estimates is not available: {}"
.
format
(
e
)
)
message
=
"Billing estimates is not available: {}"
.
format
(
e
)
return
e
results
=
[]
if
len
(
data
)
==
1
:
amount
=
float
(
data
[
0
][
'Total'
][
'UnblendedCost'
][
'Amount'
])
unit
=
data
[
0
][
'Total'
][
'UnblendedCost'
][
'Unit'
]
date_today
=
datetime
.
datetime
.
now
().
strftime
(
"%Y-%m-%d"
)
results
.
append
([
date_today
,
"{} {:.2f}"
.
format
(
unit
,
amount
)])
message
=
"
\n
Today's estimated bill
\n
"
else
:
if
output
is
'screen'
and
len
(
data
)
>
0
:
# Generate the table rows
rows
=
[]
dimens_size
=
0
if
len
(
data
)
==
1
:
message
+=
"Estimated bill for today
\n
"
else
:
message
+=
"Estimated bill for the past {} days
\n
"
.
format
(
str
(
len
(
data
)))
for
d
in
data
:
unit
=
d
[
'Total'
][
'UnblendedCost'
][
'Unit'
]
amount
=
float
(
d
[
'Total'
][
'UnblendedCost'
][
'Amount'
])
results
.
append
([
d
[
'TimePeriod'
][
'End'
],
"{} {:.2f}"
.
format
(
unit
,
amount
)])
message
=
"
\n
Estimated bill for the past {} days
\n
"
.
format
(
str
(
len
(
data
)))
message
+=
tabulate
.
tabulate
(
results
,
headers
=
[
"Date"
,
"Total"
],
tablefmt
=
'psql'
)
message
+=
"
\n
"
row
=
[]
if
len
(
data
)
>
1
:
row
.
append
(
d
[
'TimePeriod'
][
'End'
])
else
:
row
.
append
(
d
[
'TimePeriod'
][
'Start'
])
# Get the length of the last row. Use that data to form the total length of the table.
if
len
(
d
[
'Groups'
])
>
dimens_size
:
dimens_size
=
len
(
d
[
'Groups'
])
# Group the list of account IDs
account_ids
=
[]
for
g
in
d
[
'Groups'
]:
account_ids
.
append
(
g
[
'Keys'
])
total_amount
=
0
total_unit
=
"USD"
for
g
in
d
[
'Groups'
]:
amount
=
float
(
g
[
'Metrics'
][
'UnblendedCost'
][
'Amount'
])
unit
=
g
[
'Metrics'
][
'UnblendedCost'
][
'Unit'
]
total_amount
+=
amount
total_unit
=
unit
row
.
append
(
"{} {:.2f}"
.
format
(
unit
,
amount
))
if
(
dimens_size
>
1
):
row
.
append
(
"{} {:.2f}"
.
format
(
total_unit
,
total_amount
))
rows
.
append
(
row
)
# Generate the table headers.
headers
=
[]
headers
.
append
(
"Date / AWS Account ID"
)
for
th
in
range
(
dimens_size
):
headers
.
append
(
account_ids
[
th
][
0
])
if
(
dimens_size
>
1
):
headers
.
append
(
"Total Amount"
)
print
(
message
)
return
message
\ No newline at end of file
# Generate the table
message
+=
tabulate
.
tabulate
(
rows
,
headers
=
headers
,
tablefmt
=
'psql'
)
message
+=
"
\n
"
print
(
message
)
return
message
return
data
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment