@@ -48,13 +48,19 @@ def daily_sql(self, invoice_month, results):
48
48
SELECT DATE(usage_start_time) as usage_date,
49
49
sum(cost) as cost,
50
50
FROM { self .table_name }
51
- WHERE invoice.month = ' { invoice_month } '
52
- AND DATE(_PARTITIONTIME) BETWEEN " { str ( self . start_date ) } " AND " { str ( self . end_date ) } "
51
+ WHERE invoice.month = @ invoice_month
52
+ AND DATE(_PARTITIONTIME) BETWEEN @ start_date AND @ end_date
53
53
GROUP BY usage_date ORDER BY usage_date;
54
54
"""
55
-
55
+ job_config = bigquery .QueryJobConfig (
56
+ query_parameters = [
57
+ bigquery .ScalarQueryParameter ("invoice_month" , "STRING" , invoice_month ),
58
+ bigquery .ScalarQueryParameter ("start_date" , "DATE" , self .start_date ),
59
+ bigquery .ScalarQueryParameter ("end_date" , "DATE" , self .end_date ),
60
+ ]
61
+ )
56
62
LOG .info (daily_query )
57
- rows = self .client .query (daily_query ).result ()
63
+ rows = self .client .query (daily_query , job_config = job_config ).result ()
58
64
daily_dict = {}
59
65
for row in rows :
60
66
daily_dict [str (row ["usage_date" ])] = {"cost" : row .get ("cost" )}
@@ -66,10 +72,17 @@ def monthly_sql(self, invoice_month, results, key):
66
72
monthly_query = f"""
67
73
SELECT sum(cost) as cost,
68
74
FROM { self .table_name }
69
- WHERE DATE(_PARTITIONTIME) BETWEEN " { str ( self . start_date ) } " AND " { str ( self . end_date ) } "
70
- AND invoice.month = ' { invoice_month } '
75
+ WHERE DATE(_PARTITIONTIME) BETWEEN @ start_date AND @ end_date
76
+ AND invoice.month = @ invoice_month
71
77
"""
72
- rows = self .client .query (monthly_query ).result ()
78
+ job_config = bigquery .QueryJobConfig (
79
+ query_parameters = [
80
+ bigquery .ScalarQueryParameter ("invoice_month" , "STRING" , invoice_month ),
81
+ bigquery .ScalarQueryParameter ("start_date" , "DATE" , self .start_date ),
82
+ bigquery .ScalarQueryParameter ("end_date" , "DATE" , self .end_date ),
83
+ ]
84
+ )
85
+ rows = self .client .query (monthly_query , job_config = job_config ).result ()
73
86
for row in rows :
74
87
results [key ] = row [0 ] # TODO: Remove once bigquery is updated.
75
88
metadata = {
@@ -79,30 +92,22 @@ def monthly_sql(self, invoice_month, results, key):
79
92
results [key + "_metadata" ] = metadata
80
93
return results
81
94
82
- def custom_sql (self , query ):
83
- """Takes a custom query and replaces the table_name."""
84
- query = query .replace ("table_name" , self .table_name )
85
- rows = self .client .query (query ).result ()
86
- return rows
87
-
88
95
89
96
@never_cache
90
- @api_view (http_method_names = ["GET" , "POST" ])
97
+ @api_view (http_method_names = ["GET" ])
91
98
@permission_classes ((AllowAny ,))
92
99
@renderer_classes (tuple (api_settings .DEFAULT_RENDERER_CLASSES ))
93
- def bigquery_cost (request ): # noqa: C901
100
+ def bigquery_cost (request , * args , ** kwargs ): # noqa: C901
94
101
"""Returns the invoice monthly cost."""
95
102
params = request .query_params
96
- provider_uuid = params .get ("provider_uuid" )
97
- return_daily = True if "daily" in params .keys () else False
98
-
99
- if provider_uuid is None :
100
- errmsg = "provider_uuid is a required parameter."
101
- return Response ({"Error" : errmsg }, status = status .HTTP_400_BAD_REQUEST )
103
+ provider_uuid = kwargs .get ("source_uuid" )
104
+ return_daily = "daily" in params .keys ()
102
105
103
- provider = Provider .objects .filter (uuid = provider_uuid ).first ()
106
+ provider = Provider .objects .filter (
107
+ uuid = provider_uuid , type__in = [Provider .PROVIDER_GCP , Provider .PROVIDER_GCP_LOCAL ]
108
+ ).first ()
104
109
if not provider :
105
- errmsg = f"The provider_uuid { provider_uuid } does not exist."
110
+ errmsg = f"The *GCP* provider_uuid { provider_uuid } does not exist."
106
111
return Response ({"Error" : errmsg }, status = status .HTTP_400_BAD_REQUEST )
107
112
credentials = provider .authentication .credentials
108
113
data_source = provider .billing_source .data_source
@@ -122,19 +127,13 @@ def bigquery_cost(request): # noqa: C901
122
127
results = {}
123
128
try :
124
129
bq_helper = BigQueryHelper (table_name )
125
- if request .method == "POST" :
126
- data = request .data
127
- query = data .get ("query" )
128
- results = bq_helper .custom_sql (query )
129
- resp_key = "custom_query_results"
130
- else :
131
- for key , invoice_month in mapping .items ():
132
- if return_daily :
133
- results = bq_helper .daily_sql (invoice_month , results )
134
- resp_key = "daily_invoice_cost_mapping"
135
- else :
136
- results = bq_helper .monthly_sql (invoice_month , results , key )
137
- resp_key = "monthly_invoice_cost_mapping"
130
+ for key , invoice_month in mapping .items ():
131
+ if return_daily :
132
+ results = bq_helper .daily_sql (invoice_month , results )
133
+ resp_key = "daily_invoice_cost_mapping"
134
+ else :
135
+ results = bq_helper .monthly_sql (invoice_month , results , key )
136
+ resp_key = "monthly_invoice_cost_mapping"
138
137
except GoogleCloudError as err :
139
138
return Response ({"Error" : err .message }, status = status .HTTP_400_BAD_REQUEST )
140
139
0 commit comments