7777st .divider ()
7878
7979PROJECT_ID = "sipa-adv-c-giggling-wombat"
80- TABLE_ID = f"{ PROJECT_ID } .petroleum_supply.weekly_supply"
80+ TOTAL_SUPPLY_TABLE_ID = f"{ PROJECT_ID } .petroleum_supply.weekly_supply"
81+ PRODUCT_SUPPLY_TABLE_ID = f"{ PROJECT_ID } .petroleum_supply.weekly_supply_by_product"
82+ DEFAULT_PRODUCT_COUNT = 3
8183
8284
8385@st .cache_resource
@@ -95,29 +97,29 @@ def get_bq_client():
9597def load_supply_data () -> pd .DataFrame :
9698 client = get_bq_client ()
9799 query = f"""
98- SELECT week, total_product_supplied
99- FROM `{ TABLE_ID } `
100+ SELECT week, total_supply
101+ FROM `{ TOTAL_SUPPLY_TABLE_ID } `
100102 ORDER BY week
101103 """
102104 df = client .query (query ).to_dataframe ()
103105 df ["week" ] = pd .to_datetime (df ["week" ])
104- df ["total_product_supplied " ] = pd .to_numeric (df ["total_product_supplied " ], errors = "coerce" )
105- df = df .dropna (subset = ["week" , "total_product_supplied " ])
106+ df ["total_supply " ] = pd .to_numeric (df ["total_supply " ], errors = "coerce" )
107+ df = df .dropna (subset = ["week" , "total_supply " ])
106108 return df
107109
108110
109111@st .cache_data (ttl = 60 * 60 )
110112def load_supply_product_data () -> pd .DataFrame :
111113 client = get_bq_client ()
112- query = """
113- SELECT week, product_name , product_supplied
114- FROM `sipa-adv-c-giggling-wombat.petroleum_supply.weekly_supply_by_product `
114+ query = f """
115+ SELECT week, product , product_supplied
116+ FROM `{ PRODUCT_SUPPLY_TABLE_ID } `
115117 ORDER BY week
116118 """
117119 df = client .query (query ).to_dataframe ()
118120 df ["week" ] = pd .to_datetime (df ["week" ])
119121 df ["product_supplied" ] = pd .to_numeric (df ["product_supplied" ], errors = "coerce" )
120- df = df .dropna (subset = ["week" , "product_name " , "product_supplied" ])
122+ df = df .dropna (subset = ["week" , "product " , "product_supplied" ])
121123 return df
122124
123125
@@ -168,27 +170,35 @@ def load_supply_product_data() -> pd.DataFrame:
168170 st .warning ("No data available for the selected date range." )
169171 st .stop ()
170172
171- weekly_by_product = load_supply_product_data ()
173+ try :
174+ weekly_by_product = load_supply_product_data ()
175+ except Exception as e :
176+ st .error (f"Failed to load product-level supply data from BigQuery: { e } " )
177+ st .stop ()
172178
173179filtered_product = weekly_by_product [
174180 (weekly_by_product ["week" ] >= pd .to_datetime (start_week ))
175181 & (weekly_by_product ["week" ] <= pd .to_datetime (end_week ))
176182].copy ()
177183
178- product_options = sorted (filtered_product ["product_name " ].dropna ().unique ().tolist ())
184+ product_options = sorted (filtered_product ["product " ].dropna ().unique ().tolist ())
179185
180186selected_products = st .sidebar .multiselect (
181187 "Select product(s)" ,
182188 options = product_options ,
183- default = product_options [:3 ] if len (product_options ) >= 3 else product_options , # noqa: PLR2004
189+ default = (
190+ product_options [:DEFAULT_PRODUCT_COUNT ]
191+ if len (product_options ) >= DEFAULT_PRODUCT_COUNT
192+ else product_options
193+ ),
184194 key = "product_filter" ,
185195)
186196
187197try :
188198 latest_total = latest_value (
189199 filtered_total ,
190200 date_col = "week" ,
191- value_col = "total_product_supplied " ,
201+ value_col = "total_supply " ,
192202 )
193203except Exception :
194204 latest_total = None
@@ -204,7 +214,7 @@ def load_supply_product_data() -> pd.DataFrame:
204214st .subheader ("Total Product Supplied (Weekly, All Products Summed)" )
205215
206216fig , ax = plt .subplots (figsize = (8 , 4 ))
207- ax .plot (filtered_total ["week" ], filtered_total ["total_product_supplied " ])
217+ ax .plot (filtered_total ["week" ], filtered_total ["total_supply " ])
208218ax .set_xlabel ("Week" )
209219ax .set_ylabel ("Total Product Supplied" )
210220st .pyplot (fig )
@@ -226,13 +236,11 @@ def load_supply_product_data() -> pd.DataFrame:
226236if not selected_products :
227237 st .warning ("Please select at least one product from the sidebar." )
228238else :
229- product_plot_df = filtered_product [
230- filtered_product ["product_name" ].isin (selected_products )
231- ].copy ()
239+ product_plot_df = filtered_product [filtered_product ["product" ].isin (selected_products )].copy ()
232240
233241 fig2 , ax2 = plt .subplots (figsize = (8 , 4 ))
234242 for product in selected_products :
235- temp = product_plot_df [product_plot_df ["product_name " ] == product ]
243+ temp = product_plot_df [product_plot_df ["product " ] == product ]
236244 ax2 .plot (temp ["week" ], temp ["product_supplied" ], label = product )
237245
238246 ax2 .set_xlabel ("Week" )
@@ -242,7 +250,7 @@ def load_supply_product_data() -> pd.DataFrame:
242250
243251 with st .expander ("Show product-level data table" ):
244252 st .dataframe (
245- product_plot_df .sort_values (["product_name " , "week" ], ascending = [True , False ]),
253+ product_plot_df .sort_values (["product " , "week" ], ascending = [True , False ]),
246254 use_container_width = True ,
247255 )
248256
0 commit comments