-
Notifications
You must be signed in to change notification settings - Fork 35
Fix backend tests, API routes, and non-deterministic hashing #604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -107,7 +107,10 @@ def generate_visit_hash(visit_data: dict) -> str: | |
| # Normalize check_in_time to ISO format string for determinism | ||
| check_in_time = visit_data.get('check_in_time') | ||
| if isinstance(check_in_time, datetime): | ||
| check_in_time_str = check_in_time.isoformat() | ||
| # Normalize timestamp to UTC and remove microseconds for consistent hashing across databases | ||
| if check_in_time.tzinfo is None: | ||
| check_in_time = check_in_time.replace(tzinfo=timezone.utc) | ||
| check_in_time_str = check_in_time.astimezone(timezone.utc).replace(microsecond=0).strftime('%Y-%m-%dT%H:%M:%S') | ||
| else: | ||
| check_in_time_str = str(check_in_time) if check_in_time else "" | ||
|
Comment on lines
109
to
115
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Verify whether generate_visit_hash() is primarily called with string timestamps
# and confirm the current branch behavior around datetime/string normalization.
set -euo pipefail
echo "== generate_visit_hash call sites =="
rg -n -C3 --type=py '\bgenerate_visit_hash\s*\('
echo
echo "== check_in_time values built with isoformat() (string path) =="
rg -n -C3 --type=py "['\"]check_in_time['\"]\s*:\s*.*isoformat\s*\("
echo
echo "== current normalization branch in geofencing_service =="
rg -n -C4 --type=py 'if isinstance\(check_in_time, datetime\):|check_in_time_str = str\(check_in_time\) if check_in_time else ""'Repository: RohanExploit/VishwaGuru Length of output: 3380 Parse ISO string timestamps before hashing to guarantee deterministic behavior. The call site at Suggested fix check_in_time = visit_data.get('check_in_time')
- if isinstance(check_in_time, datetime):
- # Normalize timestamp to UTC and remove microseconds for consistent hashing across databases
- if check_in_time.tzinfo is None:
- check_in_time = check_in_time.replace(tzinfo=timezone.utc)
- check_in_time_str = check_in_time.astimezone(timezone.utc).replace(microsecond=0).strftime('%Y-%m-%dT%H:%M:%S')
+ normalized_dt = None
+ if isinstance(check_in_time, datetime):
+ normalized_dt = check_in_time
+ elif isinstance(check_in_time, str) and check_in_time:
+ try:
+ normalized_dt = datetime.fromisoformat(check_in_time.replace("Z", "+00:00"))
+ except ValueError:
+ normalized_dt = None
+
+ if normalized_dt is not None:
+ # Normalize to UTC and remove microseconds for deterministic hashing
+ if normalized_dt.tzinfo is None:
+ normalized_dt = normalized_dt.replace(tzinfo=timezone.utc)
+ check_in_time_str = normalized_dt.astimezone(timezone.utc).replace(microsecond=0).strftime('%Y-%m-%dT%H:%M:%S')
else:
check_in_time_str = str(check_in_time) if check_in_time else ""🤖 Prompt for AI Agents |
||
|
|
||
|
|
||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
generate_visit_hash()only strips microseconds whencheck_in_timeis adatetime. In the codebase, callers (e.g. the field officer check-in router) passcheck_in_timeas an ISO string (datetime.isoformat()), so this branch will still include microseconds and the hash can still change after a DB round-trip (SQLite truncation). Consider normalizing string timestamps too (e.g., parse ISO8601 -> convert/assume UTC -> drop microseconds -> re-serialize), and keep the serialized format consistent (including an explicit UTC offset/Zif you intend UTC).