VyLala commited on
Commit
b339d38
·
verified ·
1 Parent(s): 774460d

Update mtdna_backend.py

Browse files
Files changed (1) hide show
  1. mtdna_backend.py +54 -24
mtdna_backend.py CHANGED
@@ -512,25 +512,41 @@ def hash_user_id(user_input):
512
  # except (json.JSONDecodeError, ValueError):
513
  # print("⚠️ Warning: user_usage.json is corrupted or invalid. Resetting.")
514
  # return {} # fallback to empty dict
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
515
  def load_user_usage():
516
  try:
517
- creds_dict = json.loads(os.environ["GCP_CREDS_JSON"])
518
- scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
519
- creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scope)
520
- client = gspread.authorize(creds)
521
 
522
- sheet = client.open("user_usage_log").sheet1
523
- data = sheet.get_all_records() # Assumes columns: email, usage_count
 
 
 
 
 
524
 
525
- usage = {}
526
- for row in data:
527
- email = row.get("email", "").strip().lower()
528
- count = int(row.get("usage_count", 0))
529
- if email:
530
- usage[email] = count
531
- return usage
532
  except Exception as e:
533
- print(f"⚠️ Failed to load user usage from Google Sheets: {e}")
534
  return {}
535
 
536
 
@@ -539,21 +555,35 @@ def load_user_usage():
539
  # with open(USER_USAGE_TRACK_FILE, "w") as f:
540
  # json.dump(usage, f, indent=2)
541
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
542
  def save_user_usage(usage_dict):
543
  try:
544
- creds_dict = json.loads(os.environ["GCP_CREDS_JSON"])
545
- scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
546
- creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scope)
547
- client = gspread.authorize(creds)
 
 
 
548
 
549
- sheet = client.open("user_usage_log").sheet1
550
- sheet.clear() # clear old contents first
551
 
552
- # Write header + rows
553
- rows = [["email", "usage_count"]] + [[email, count] for email, count in usage_dict.items()]
554
- sheet.update(rows)
555
  except Exception as e:
556
- print(f"❌ Failed to save user usage to Google Sheets: {e}")
557
 
558
 
559
  # def increment_usage(user_id, num_samples=1):
 
512
  # except (json.JSONDecodeError, ValueError):
513
  # print("⚠️ Warning: user_usage.json is corrupted or invalid. Resetting.")
514
  # return {} # fallback to empty dict
515
+ # def load_user_usage():
516
+ # try:
517
+ # creds_dict = json.loads(os.environ["GCP_CREDS_JSON"])
518
+ # scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
519
+ # creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scope)
520
+ # client = gspread.authorize(creds)
521
+
522
+ # sheet = client.open("user_usage_log").sheet1
523
+ # data = sheet.get_all_records() # Assumes columns: email, usage_count
524
+
525
+ # usage = {}
526
+ # for row in data:
527
+ # email = row.get("email", "").strip().lower()
528
+ # count = int(row.get("usage_count", 0))
529
+ # if email:
530
+ # usage[email] = count
531
+ # return usage
532
+ # except Exception as e:
533
+ # print(f"⚠️ Failed to load user usage from Google Sheets: {e}")
534
+ # return {}
535
  def load_user_usage():
536
  try:
537
+ parent_id = get_or_create_drive_folder("mtDNA-Location-Classifier")
538
+ iterate3_id = get_or_create_drive_folder("iterate3", parent_id=parent_id)
 
 
539
 
540
+ found = find_drive_file("user_usage_log.json", parent_folder_id=iterate3_id)
541
+ if not found:
542
+ return {} # not found, start fresh
543
+
544
+ file_id = found[0]["id"]
545
+ content = download_drive_file_content(file_id)
546
+ return json.loads(content.strip()) if content.strip() else {}
547
 
 
 
 
 
 
 
 
548
  except Exception as e:
549
+ print(f"⚠️ Failed to load user_usage_log.json from Google Drive: {e}")
550
  return {}
551
 
552
 
 
555
  # with open(USER_USAGE_TRACK_FILE, "w") as f:
556
  # json.dump(usage, f, indent=2)
557
 
558
+ # def save_user_usage(usage_dict):
559
+ # try:
560
+ # creds_dict = json.loads(os.environ["GCP_CREDS_JSON"])
561
+ # scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive']
562
+ # creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scope)
563
+ # client = gspread.authorize(creds)
564
+
565
+ # sheet = client.open("user_usage_log").sheet1
566
+ # sheet.clear() # clear old contents first
567
+
568
+ # # Write header + rows
569
+ # rows = [["email", "usage_count"]] + [[email, count] for email, count in usage_dict.items()]
570
+ # sheet.update(rows)
571
+ # except Exception as e:
572
+ # print(f"❌ Failed to save user usage to Google Sheets: {e}")
573
  def save_user_usage(usage_dict):
574
  try:
575
+ parent_id = get_or_create_drive_folder("mtDNA-Location-Classifier")
576
+ iterate3_id = get_or_create_drive_folder("iterate3", parent_id=parent_id)
577
+
578
+ import tempfile
579
+ tmp_path = os.path.join(tempfile.gettempdir(), "user_usage_log.json")
580
+ with open(tmp_path, "w") as f:
581
+ json.dump(usage_dict, f, indent=2)
582
 
583
+ upload_file_to_drive(tmp_path, "user_usage_log.json", parent_folder_id=iterate3_id)
 
584
 
 
 
 
585
  except Exception as e:
586
+ print(f"❌ Failed to save user_usage_log.json to Google Drive: {e}")
587
 
588
 
589
  # def increment_usage(user_id, num_samples=1):