buzzbandit commited on
Commit
f20149e
·
verified ·
1 Parent(s): 9588681

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -11
app.py CHANGED
@@ -48,31 +48,58 @@ def fetch_yata(force_refresh=False):
48
 
49
 
50
  def query_inventory(item_term="", country_name="", refresh=False):
51
- data, last_update = fetch_yata(force_refresh=refresh)
52
  stocks = data.get("stocks", {})
53
  rows = []
54
 
55
- for cdata in stocks.values():
56
- cname = cdata.get("countryName", "")
57
- for item_name, item_data in cdata.get("items", {}).items():
58
- match_item = item_term.lower() in item_name.lower() if item_term else True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  match_country = country_name.lower() in cname.lower() if country_name else True
60
  if match_item and match_country:
61
  rows.append({
62
  "Country": cname,
63
- "Item": item_name,
64
- "Quantity": item_data["quantity"],
65
- "Cost": item_data["cost"],
 
66
  })
67
 
68
  if not rows:
69
- return pd.DataFrame([{"Result": "No inventory found for that query."}]), f"Last update: {last_update}"
70
 
71
  df = pd.DataFrame(rows)
72
  df = df.sort_values(by=["Country", "Item"])
73
  total_quantity = df["Quantity"].sum()
74
- df.loc[len(df)] = {"Country": "—", "Item": "TOTAL", "Quantity": total_quantity, "Cost": ""}
75
- return df, f"Last update: {last_update}"
 
 
 
76
 
77
  def run_query(item, country, refresh):
78
  df, timestamp = query_inventory(item, country, refresh)
 
48
 
49
 
50
  def query_inventory(item_term="", country_name="", refresh=False):
51
+ data, _ = fetch_yata(force_refresh=refresh)
52
  stocks = data.get("stocks", {})
53
  rows = []
54
 
55
+ # Mapping of country codes to readable names (you can expand this)
56
+ COUNTRY_NAMES = {
57
+ "arg": "Argentina",
58
+ "mex": "Mexico",
59
+ "can": "Canada",
60
+ "uk": "United Kingdom",
61
+ "jap": "Japan",
62
+ "sou": "South Africa",
63
+ "swi": "Switzerland",
64
+ "uae": "United Arab Emirates",
65
+ "chi": "China",
66
+ "haw": "Hawaii"
67
+ }
68
+
69
+ for country_code, cdata in stocks.items():
70
+ cname = COUNTRY_NAMES.get(country_code, country_code.upper())
71
+
72
+ # Handle per-country 'update' timestamp
73
+ update_ts = cdata.get("update")
74
+ update_str = (
75
+ time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(update_ts))
76
+ if update_ts else "Unknown"
77
+ )
78
+
79
+ for item in cdata.get("stocks", []):
80
+ iname = item.get("name", "")
81
+ match_item = item_term.lower() in iname.lower() if item_term else True
82
  match_country = country_name.lower() in cname.lower() if country_name else True
83
  if match_item and match_country:
84
  rows.append({
85
  "Country": cname,
86
+ "Item": iname,
87
+ "Quantity": item.get("quantity", 0),
88
+ "Cost": item.get("cost", 0),
89
+ "Updated": update_str
90
  })
91
 
92
  if not rows:
93
+ return pd.DataFrame([{"Result": "No inventory found for that query."}]), "No update available"
94
 
95
  df = pd.DataFrame(rows)
96
  df = df.sort_values(by=["Country", "Item"])
97
  total_quantity = df["Quantity"].sum()
98
+ df.loc[len(df)] = {"Country": "—", "Item": "TOTAL", "Quantity": total_quantity, "Cost": "", "Updated": ""}
99
+ last_update_display = f"Data last updated per country — most recent: {max(r['Updated'] for r in rows if r['Updated'] != 'Unknown')}"
100
+
101
+ return df, last_update_display
102
+
103
 
104
  def run_query(item, country, refresh):
105
  df, timestamp = query_inventory(item, country, refresh)