Skip to content

Commit

Permalink
db/python/tables/bq/billing_base.py create DATE_FORMAT constant
Browse files Browse the repository at this point in the history
  • Loading branch information
violetbrina committed Oct 7, 2024
1 parent 7eadaac commit 697bb5d
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions db/python/tables/bq/billing_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
STORAGE = 'S'
COMPUTE = 'C'

DATE_FORMAT = '%Y-%m-%d'


def abbrev_cost_category(cost_category: str) -> str:
"""abbreviate cost category"""
Expand All @@ -49,8 +51,8 @@ def prepare_time_periods(
# Based on specified time period, add the corresponding column
if query.time_periods == BillingTimePeriods.DAY:
return TimeGroupingDetails(
field=f'FORMAT_DATE("%Y-%m-%d", {time_column.value}) as day',
formula='PARSE_DATE("%Y-%m-%d", day) as day',
field=f'FORMAT_DATE("{DATE_FORMAT}", {time_column.value}) as day',
formula=f'PARSE_DATE("{DATE_FORMAT}", day) as day',
separator=',',
)

Expand Down Expand Up @@ -150,12 +152,12 @@ def _query_to_partitioned_filter(
# initial partition filter
billing_filter.day = GenericBQFilter[datetime](
gte=(
datetime.strptime(query.start_date, '%Y-%m-%d')
datetime.strptime(query.start_date, DATE_FORMAT)
if query.start_date
else None
),
lte=(
datetime.strptime(query.end_date, '%Y-%m-%d')
datetime.strptime(query.end_date, DATE_FORMAT)
if query.end_date
else None
),
Expand Down Expand Up @@ -290,16 +292,16 @@ async def _execute_running_cost_query(
# This is to optimise the query, BQ view is partitioned by day
# and not by invoice month
start_day_date, last_day_date = get_invoice_month_range(invoice_month_date)
start_day = start_day_date.strftime('%Y-%m-%d')
last_day = last_day_date.strftime('%Y-%m-%d')
start_day = start_day_date.strftime(DATE_FORMAT)
last_day = last_day_date.strftime(DATE_FORMAT)

# start_day and last_day are in to optimise the query
query_params = [
bigquery.ScalarQueryParameter('start_day', 'STRING', start_day),
bigquery.ScalarQueryParameter('last_day', 'STRING', last_day),
]

current_day = datetime.now().strftime('%Y-%m-%d')
current_day = datetime.now().strftime(DATE_FORMAT)
is_current_month = last_day >= current_day
last_loaded_day = None

Expand Down

0 comments on commit 697bb5d

Please sign in to comment.