Spaces:
Running
Running
Update mtdna_backend.py
Browse files- mtdna_backend.py +11 -12
mtdna_backend.py
CHANGED
@@ -678,37 +678,36 @@ def load_user_usage():
|
|
678 |
# print(f"❌ Failed to save user_usage_log.json to Google Drive: {e}")
|
679 |
def save_user_usage(usage_dict):
|
680 |
try:
|
681 |
-
# Load credentials
|
682 |
creds_dict = json.loads(os.environ["GCP_CREDS_JSON"])
|
683 |
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
|
684 |
creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scope)
|
685 |
client = gspread.authorize(creds)
|
686 |
|
687 |
-
# Open or create the sheet
|
688 |
spreadsheet = client.open("user_usage_log")
|
689 |
sheet = spreadsheet.sheet1
|
690 |
|
691 |
-
# Convert
|
692 |
df_new = pd.DataFrame(list(usage_dict.items()), columns=["email", "usage_count"])
|
|
|
693 |
|
694 |
-
#
|
695 |
existing_data = sheet.get_all_values()
|
696 |
-
if existing_data:
|
697 |
df_old = pd.DataFrame(existing_data[1:], columns=existing_data[0])
|
|
|
|
|
698 |
else:
|
699 |
df_old = pd.DataFrame(columns=["email", "usage_count"])
|
700 |
|
701 |
-
# Merge
|
702 |
-
|
703 |
-
|
704 |
-
df_old.update(df_new)
|
705 |
-
df_combined = df_old.reset_index()
|
706 |
|
707 |
-
# Write
|
708 |
sheet.clear()
|
709 |
sheet.update([df_combined.columns.tolist()] + df_combined.values.tolist())
|
710 |
-
print("✅ Saved user usage to user_usage_log sheet.")
|
711 |
|
|
|
712 |
except Exception as e:
|
713 |
print(f"❌ Failed to save user usage to Google Sheets: {e}")
|
714 |
|
|
|
678 |
# print(f"❌ Failed to save user_usage_log.json to Google Drive: {e}")
|
679 |
def save_user_usage(usage_dict):
|
680 |
try:
|
|
|
681 |
creds_dict = json.loads(os.environ["GCP_CREDS_JSON"])
|
682 |
scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
|
683 |
creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scope)
|
684 |
client = gspread.authorize(creds)
|
685 |
|
|
|
686 |
spreadsheet = client.open("user_usage_log")
|
687 |
sheet = spreadsheet.sheet1
|
688 |
|
689 |
+
# Step 1: Convert new usage to DataFrame
|
690 |
df_new = pd.DataFrame(list(usage_dict.items()), columns=["email", "usage_count"])
|
691 |
+
df_new["email"] = df_new["email"].str.strip().str.lower()
|
692 |
|
693 |
+
# Step 2: Try to read old data
|
694 |
existing_data = sheet.get_all_values()
|
695 |
+
if existing_data and len(existing_data) >= 2:
|
696 |
df_old = pd.DataFrame(existing_data[1:], columns=existing_data[0])
|
697 |
+
df_old["email"] = df_old["email"].str.strip().str.lower()
|
698 |
+
df_old["usage_count"] = pd.to_numeric(df_old["usage_count"], errors="coerce").fillna(0).astype(int)
|
699 |
else:
|
700 |
df_old = pd.DataFrame(columns=["email", "usage_count"])
|
701 |
|
702 |
+
# Step 3: Merge new and old usage
|
703 |
+
df_combined = pd.concat([df_old, df_new])
|
704 |
+
df_combined = df_combined.groupby("email", as_index=False).sum(numeric_only=True)
|
|
|
|
|
705 |
|
706 |
+
# Step 4: Write to sheet
|
707 |
sheet.clear()
|
708 |
sheet.update([df_combined.columns.tolist()] + df_combined.values.tolist())
|
|
|
709 |
|
710 |
+
print("✅ Saved user usage to user_usage_log sheet.")
|
711 |
except Exception as e:
|
712 |
print(f"❌ Failed to save user usage to Google Sheets: {e}")
|
713 |
|