19arjun89 commited on
Commit
b41322c
·
verified ·
1 Parent(s): f4815d7

Update usage_logging.py

Browse files

Changes to json directory to help with concurrent user access telemetry

Files changed (1) hide show
  1. usage_logging.py +17 -13
usage_logging.py CHANGED
@@ -52,7 +52,7 @@ Author:
52
  Arjun Singh
53
 
54
  Last Updated:
55
- 2026-01-22
56
  """
57
 
58
 
@@ -65,10 +65,13 @@ from huggingface_hub import HfApi, hf_hub_url
65
  import ipaddress
66
  import pycountry
67
  from io import BytesIO
 
68
 
69
  SPACE_URL = "https://huggingface.co/spaces/19arjun89/AI_Recruiting_Agent"
70
  USAGE_DATASET_REPO = "19arjun89/ai_recruiting_agent_usage"
71
- USAGE_JSONL_PATH = "usage/visits.jsonl"
 
 
72
 
73
 
74
  def _hf_api():
@@ -158,32 +161,33 @@ def append_visit_to_dataset(
158
  if not api:
159
  return
160
 
161
- existing = _download_text_if_exists(USAGE_DATASET_REPO, USAGE_JSONL_PATH)
162
-
163
  event = {
164
  "ts_utc": datetime.utcnow().isoformat() + "Z",
165
  "space_url": SPACE_URL,
 
166
  "country": country or "Unknown",
167
  "country_code": (country_code or "").strip().upper(),
168
  "country_source": country_source or "unknown",
169
  "city": city or "",
170
- "event": event_type,
171
  }
172
 
173
- # Drop None values so you never write JSON nulls
174
  if extra_fields:
 
175
  event.update({k: v for k, v in extra_fields.items() if v is not None})
176
 
177
- new_content = (existing.rstrip("\n") + "\n" if existing.strip() else "") + json.dumps(event) + "\n"
178
- print("DEBUG EVENT:", event)
 
 
 
179
  try:
180
  api.upload_file(
181
  repo_id=USAGE_DATASET_REPO,
182
  repo_type="dataset",
183
- path_in_repo=USAGE_JSONL_PATH,
184
- path_or_fileobj=BytesIO(new_content.encode("utf-8")),
185
- commit_message="append visit log",
186
- )
187
  except Exception:
188
  pass
189
 
@@ -197,7 +201,7 @@ def record_visit(request: gr.Request):
197
  city="",
198
  event_type="usage_start",
199
  country_source="header",
200
- country_code=country_hint,
201
  )
202
  return
203
 
 
52
  Arjun Singh
53
 
54
  Last Updated:
55
+ 2026-01-27
56
  """
57
 
58
 
 
65
  import ipaddress
66
  import pycountry
67
  from io import BytesIO
68
+ import uuid
69
 
70
  SPACE_URL = "https://huggingface.co/spaces/19arjun89/AI_Recruiting_Agent"
71
  USAGE_DATASET_REPO = "19arjun89/ai_recruiting_agent_usage"
72
+
73
+ USAGE_EVENTS_DIR = "usage/events"
74
+
75
 
76
 
77
  def _hf_api():
 
161
  if not api:
162
  return
163
 
 
 
164
  event = {
165
  "ts_utc": datetime.utcnow().isoformat() + "Z",
166
  "space_url": SPACE_URL,
167
+ "event": event_type,
168
  "country": country or "Unknown",
169
  "country_code": (country_code or "").strip().upper(),
170
  "country_source": country_source or "unknown",
171
  "city": city or "",
 
172
  }
173
 
 
174
  if extra_fields:
175
+ # Prevent JSON nulls
176
  event.update({k: v for k, v in extra_fields.items() if v is not None})
177
 
178
+ # Unique file path per event (prevents collisions)
179
+ ts = datetime.utcnow().strftime("%Y%m%dT%H%M%S%f")
180
+ uid = uuid.uuid4().hex[:8]
181
+ path_in_repo = f"{USAGE_EVENTS_DIR}/{ts}_{uid}.json"
182
+
183
  try:
184
  api.upload_file(
185
  repo_id=USAGE_DATASET_REPO,
186
  repo_type="dataset",
187
+ path_in_repo=path_in_repo,
188
+ path_or_fileobj=BytesIO(json.dumps(event).encode("utf-8")),
189
+ commit_message=f"log {event_type}",
190
+ )
191
  except Exception:
192
  pass
193
 
 
201
  city="",
202
  event_type="usage_start",
203
  country_source="header",
204
+ country_code=country_hint.strip().upper(),
205
  )
206
  return
207