Fix Advoware timestamp parsing bug causing sync loops
- Change .replace(tzinfo=BERLIN_TZ) to BERLIN_TZ.localize() for correct pytz TZ handling - Update Phase 4 to use proper TZ localization for adv_ts - Add documentation section on correct Advoware timestamp handling - Add .gitignore, GOOGLE_SETUP_README.md, and check_db.py utility
This commit is contained in:
@@ -256,5 +256,33 @@ Cron-Step für regelmäßige Ausführung.
|
||||
- Timestamps: Fehlende in Google können zu Fallback führen.
|
||||
- Performance: Bei vielen Terminen könnte Paginierung helfen.
|
||||
|
||||
## Korrekter Umgang mit Advoware-Timestamps
|
||||
|
||||
### Problemstellung
|
||||
Advoware-Timestamps (z.B. `'zuletztGeaendertAm'`) werden in Berlin-Zeit geliefert, aber das Parsing mit `datetime.datetime.fromisoformat(...).replace(tzinfo=BERLIN_TZ)` führte zu falschen Offsets (z.B. 53 Minuten Unterschied), da `replace(tzinfo=...)` auf naive datetime nicht korrekt mit pytz-TZ-Objekten funktioniert. Dies verursachte Endlosschleifen in Phase 4, da `adv_ts` falsch hochgesetzt wurde.
|
||||
|
||||
### Lösung
|
||||
Verwende `BERLIN_TZ.localize(naive_datetime)` statt `.replace(tzinfo=BERLIN_TZ)`:
|
||||
- `localize()` setzt die TZ korrekt auf pytz-TZ-Objekte.
|
||||
- Beispiel:
|
||||
```python
|
||||
naive = datetime.datetime.fromisoformat('2025-10-23T14:18:36.245')
|
||||
adv_ts = BERLIN_TZ.localize(naive) # Ergebnis: 2025-10-23 14:18:36.245+02:00
|
||||
```
|
||||
- Dies stellt sicher, dass Timestamps korrekt in UTC konvertiert werden (z.B. 12:18 UTC) und Vergleiche in Phase 4 funktionieren.
|
||||
|
||||
### Implementierung
|
||||
- In `calendar_sync_event_step.py`, Phase 4:
|
||||
```python
|
||||
adv_ts = BERLIN_TZ.localize(datetime.datetime.fromisoformat(adv_data['zuletztGeaendertAm']))
|
||||
```
|
||||
- Für Google-Timestamps: `.astimezone(BERLIN_TZ)` bleibt korrekt.
|
||||
- Alle Timestamps werden zu UTC normalisiert für DB-Speicherung und Vergleiche.
|
||||
|
||||
### Vermeidung von Fehlern
|
||||
- Niemals `.replace(tzinfo=pytz_tz)` verwenden – immer `tz.localize(naive)`.
|
||||
- Teste Parsing: `BERLIN_TZ.localize(datetime.datetime.fromisoformat(ts)).astimezone(pytz.utc)` sollte korrekte UTC ergeben.
|
||||
- Bei anderen TZ: Gleiche Regel anwenden.
|
||||
|
||||
## Erweiterungen
|
||||
|
||||
|
||||
@@ -500,7 +500,7 @@ async def handler(event, context):
|
||||
new_frnr = await safe_create_advoware_appointment(advoware, standardize_appointment_data(google_map[event_id], 'google'), employee_kuerzel, row['advoware_write_allowed'])
|
||||
if new_frnr and str(new_frnr) != 'None':
|
||||
async with conn.transaction():
|
||||
await conn.execute("UPDATE calendar_sync SET advoware_frnr = $1, sync_status = 'synced' WHERE sync_id = $2;", int(new_frnr), row['sync_id'])
|
||||
await conn.execute("UPDATE calendar_sync SET advoware_frnr = $1, sync_status = 'synced', last_sync = $3 WHERE sync_id = $2;", int(new_frnr), row['sync_id'], datetime.datetime.now(BERLIN_TZ))
|
||||
logger.info(f"Phase 3: Recreated Advoware appointment {new_frnr} for sync_id {row['sync_id']}")
|
||||
else:
|
||||
logger.warning(f"Phase 3: Failed to recreate Advoware for sync_id {row['sync_id']}, frNr is None")
|
||||
@@ -557,7 +557,7 @@ async def handler(event, context):
|
||||
try:
|
||||
new_event_id = await create_google_event(service, calendar_id, standardize_appointment_data(adv_map[frnr], 'advoware'))
|
||||
async with conn.transaction():
|
||||
await conn.execute("UPDATE calendar_sync SET google_event_id = $1, sync_status = 'synced' WHERE sync_id = $2;", new_event_id, row['sync_id'])
|
||||
await conn.execute("UPDATE calendar_sync SET google_event_id = $1, sync_status = 'synced', last_sync = $3 WHERE sync_id = $2;", new_event_id, row['sync_id'], datetime.datetime.now(BERLIN_TZ))
|
||||
logger.info(f"Phase 3: Recreated Google event {new_event_id} for sync_id {row['sync_id']}")
|
||||
except Exception as e:
|
||||
logger.warning(f"Phase 3: Failed to recreate Google for sync_id {row['sync_id']}: {e}")
|
||||
@@ -591,7 +591,7 @@ async def handler(event, context):
|
||||
if strategy == 'source_system_wins':
|
||||
if row['source_system'] == 'advoware':
|
||||
# Check for changes in source (Advoware) or unauthorized changes in target (Google)
|
||||
adv_ts = datetime.datetime.fromisoformat(adv_data['zuletztGeaendertAm']).astimezone(BERLIN_TZ)
|
||||
adv_ts = BERLIN_TZ.localize(datetime.datetime.fromisoformat(adv_data['zuletztGeaendertAm']))
|
||||
google_ts_str = google_data.get('updated', '')
|
||||
google_ts = datetime.datetime.fromisoformat(google_ts_str.rstrip('Z')).astimezone(BERLIN_TZ) if google_ts_str else None
|
||||
if adv_ts > row['last_sync']:
|
||||
@@ -609,7 +609,8 @@ async def handler(event, context):
|
||||
# Check for changes in source (Google) or unauthorized changes in target (Advoware)
|
||||
google_ts_str = google_data.get('updated', '')
|
||||
google_ts = datetime.datetime.fromisoformat(google_ts_str.rstrip('Z')).astimezone(BERLIN_TZ) if google_ts_str else None
|
||||
adv_ts = datetime.datetime.fromisoformat(adv_data['zuletztGeaendertAm']).astimezone(BERLIN_TZ)
|
||||
adv_ts = BERLIN_TZ.localize(datetime.datetime.fromisoformat(adv_data['zuletztGeaendertAm']))
|
||||
logger.debug(f"Phase 4: Checking sync_id {row['sync_id']}: adv_ts={adv_ts}, google_ts={google_ts}, last_sync={row['last_sync']}")
|
||||
if google_ts and google_ts > row['last_sync']:
|
||||
await safe_update_advoware_appointment(advoware, frnr, google_std, row['advoware_write_allowed'], row['employee_kuerzel'])
|
||||
async with conn.transaction():
|
||||
@@ -619,7 +620,7 @@ async def handler(event, context):
|
||||
logger.warning(f"Phase 4: Unauthorized change in Advoware frNr {frnr}, resetting to Google event {event_id}")
|
||||
await safe_update_advoware_appointment(advoware, frnr, google_std, row['advoware_write_allowed'], row['employee_kuerzel'])
|
||||
async with conn.transaction():
|
||||
await conn.execute("UPDATE calendar_sync SET sync_status = 'synced', last_sync = $2 WHERE sync_id = $1;", row['sync_id'], google_ts)
|
||||
await conn.execute("UPDATE calendar_sync SET sync_status = 'synced', last_sync = $2 WHERE sync_id = $1;", row['sync_id'], datetime.datetime.now(BERLIN_TZ))
|
||||
logger.info(f"Phase 4: Reset Advoware frNr {frnr} to Google event {event_id}")
|
||||
elif strategy == 'last_change_wins':
|
||||
adv_ts = await get_advoware_timestamp(advoware, frnr)
|
||||
|
||||
Reference in New Issue
Block a user