Refactor calendar sync phases into separate functions for better modularity
This commit is contained in:
@@ -571,92 +571,8 @@ def get_timestamps(adv_data, google_data):
|
|||||||
|
|
||||||
return adv_ts, google_ts
|
return adv_ts, google_ts
|
||||||
|
|
||||||
async def handler(event, context):
|
async def process_new_from_advoware(state, conn, service, calendar_id, kuerzel, advoware):
|
||||||
"""Main event handler for calendar sync."""
|
"""Phase 1: Process new appointments from Advoware to Google."""
|
||||||
logger.info("Starting calendar sync for all employees")
|
|
||||||
|
|
||||||
try:
|
|
||||||
logger.debug("Initializing Advoware API")
|
|
||||||
advoware = AdvowareAPI(context)
|
|
||||||
|
|
||||||
# Alle Mitarbeiter abrufen
|
|
||||||
logger.info("Rufe Advoware Mitarbeiter ab...")
|
|
||||||
employees = await get_advoware_employees(advoware)
|
|
||||||
if not employees:
|
|
||||||
logger.error("Keine Mitarbeiter gefunden. Sync abgebrochen.")
|
|
||||||
return {'status': 500, 'body': {'error': 'Keine Mitarbeiter gefunden'}}
|
|
||||||
|
|
||||||
total_synced = 0
|
|
||||||
for employee in employees:
|
|
||||||
kuerzel = employee.get('kuerzel')
|
|
||||||
if not kuerzel:
|
|
||||||
logger.warning(f"Mitarbeiter ohne Kürzel übersprungen: {employee}")
|
|
||||||
continue
|
|
||||||
|
|
||||||
# DEBUG: Nur für Nutzer AI syncen (für Test der Travel/Prep Zeit)
|
|
||||||
if kuerzel != 'SB':
|
|
||||||
logger.info(f"DEBUG: Überspringe {kuerzel}, nur AI wird gesynct")
|
|
||||||
continue
|
|
||||||
|
|
||||||
logger.info(f"Starting calendar sync for {kuerzel}")
|
|
||||||
|
|
||||||
redis_client = redis.Redis(
|
|
||||||
host=Config.REDIS_HOST,
|
|
||||||
port=int(Config.REDIS_PORT),
|
|
||||||
db=int(Config.REDIS_DB_CALENDAR_SYNC),
|
|
||||||
socket_timeout=Config.REDIS_TIMEOUT_SECONDS
|
|
||||||
)
|
|
||||||
|
|
||||||
try:
|
|
||||||
logger.debug("Initializing Google service")
|
|
||||||
service = await get_google_service()
|
|
||||||
logger.debug(f"Ensuring Google calendar for {kuerzel}")
|
|
||||||
calendar_id = await ensure_google_calendar(service, kuerzel)
|
|
||||||
|
|
||||||
conn = await connect_db()
|
|
||||||
try:
|
|
||||||
# Initialize state
|
|
||||||
state = {
|
|
||||||
'rows': [],
|
|
||||||
'db_adv_index': {},
|
|
||||||
'db_google_index': {},
|
|
||||||
'adv_appointments': [],
|
|
||||||
'adv_map': {},
|
|
||||||
'google_events': [],
|
|
||||||
'google_map': {}
|
|
||||||
}
|
|
||||||
|
|
||||||
async def reload_db_indexes():
|
|
||||||
"""Reload database indexes after DB changes in phases."""
|
|
||||||
state['rows'] = await conn.fetch(
|
|
||||||
"""
|
|
||||||
SELECT * FROM calendar_sync
|
|
||||||
WHERE employee_kuerzel = $1 AND deleted = FALSE
|
|
||||||
""",
|
|
||||||
kuerzel
|
|
||||||
)
|
|
||||||
state['db_adv_index'] = {str(row['advoware_frnr']): row for row in state['rows'] if row['advoware_frnr']}
|
|
||||||
state['db_google_index'] = {}
|
|
||||||
for row in state['rows']:
|
|
||||||
if row['google_event_id']:
|
|
||||||
state['db_google_index'][row['google_event_id']] = row
|
|
||||||
log_operation('debug', "Reloaded indexes", rows=len(state['rows']), adv=len(state['db_adv_index']), google=len(state['db_google_index']))
|
|
||||||
|
|
||||||
async def reload_api_maps():
|
|
||||||
"""Reload API maps after creating new events in phases."""
|
|
||||||
state['adv_appointments'] = await fetch_advoware_appointments(advoware, kuerzel)
|
|
||||||
state['adv_map'] = {str(app['frNr']): app for app in state['adv_appointments'] if app.get('frNr')}
|
|
||||||
state['google_events'] = await fetch_google_events(service, calendar_id)
|
|
||||||
state['google_map'] = {evt['id']: evt for evt in state['google_events']}
|
|
||||||
log_operation('debug', "Reloaded API maps", adv=len(state['adv_map']), google=len(state['google_map']))
|
|
||||||
|
|
||||||
# Initial fetch
|
|
||||||
log_operation('info', "Fetching fresh data from APIs")
|
|
||||||
await reload_api_maps()
|
|
||||||
await reload_db_indexes()
|
|
||||||
log_operation('info', "Fetched existing sync rows", count=len(state['rows']))
|
|
||||||
|
|
||||||
# Phase 1: New from Advoware => Google
|
|
||||||
logger.info("Phase 1: Processing new appointments from Advoware")
|
logger.info("Phase 1: Processing new appointments from Advoware")
|
||||||
for frnr, app in state['adv_map'].items():
|
for frnr, app in state['adv_map'].items():
|
||||||
if frnr not in state['db_adv_index']:
|
if frnr not in state['db_adv_index']:
|
||||||
@@ -675,12 +591,8 @@ async def handler(event, context):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(f"Phase 1: Failed to process new Advoware {frnr}: {e}")
|
logger.warning(f"Phase 1: Failed to process new Advoware {frnr}: {e}")
|
||||||
|
|
||||||
# Reload indexes after Phase 1 changes
|
async def process_new_from_google(state, conn, service, calendar_id, kuerzel, advoware):
|
||||||
await reload_db_indexes()
|
"""Phase 2: Process new events from Google to Advoware."""
|
||||||
# Reload API maps after Phase 1 changes
|
|
||||||
await reload_api_maps()
|
|
||||||
|
|
||||||
# Phase 2: New from Google => Advoware
|
|
||||||
logger.info("Phase 2: Processing new events from Google")
|
logger.info("Phase 2: Processing new events from Google")
|
||||||
for event_id, evt in state['google_map'].items():
|
for event_id, evt in state['google_map'].items():
|
||||||
# For recurring events, check if the master event (recurringEventId) is already synced
|
# For recurring events, check if the master event (recurringEventId) is already synced
|
||||||
@@ -706,12 +618,8 @@ async def handler(event, context):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.warning(f"Phase 2: Failed to process new Google {event_id}: {e}")
|
logger.warning(f"Phase 2: Failed to process new Google {event_id}: {e}")
|
||||||
|
|
||||||
# Reload indexes after Phase 2 changes
|
async def process_deleted_entries(state, conn, service, calendar_id, kuerzel, advoware):
|
||||||
await reload_db_indexes()
|
"""Phase 3: Process deleted entries."""
|
||||||
# Reload API maps after Phase 2 changes
|
|
||||||
await reload_api_maps()
|
|
||||||
|
|
||||||
# Phase 3: Identify deleted entries
|
|
||||||
logger.info("Phase 3: Processing deleted entries")
|
logger.info("Phase 3: Processing deleted entries")
|
||||||
for row in state['rows']:
|
for row in state['rows']:
|
||||||
frnr = row['advoware_frnr']
|
frnr = row['advoware_frnr']
|
||||||
@@ -836,12 +744,8 @@ async def handler(event, context):
|
|||||||
async with conn.transaction():
|
async with conn.transaction():
|
||||||
await conn.execute("UPDATE calendar_sync SET sync_status = 'failed' WHERE sync_id = $1;", row['sync_id'])
|
await conn.execute("UPDATE calendar_sync SET sync_status = 'failed' WHERE sync_id = $1;", row['sync_id'])
|
||||||
|
|
||||||
# Reload indexes after Phase 3 changes
|
async def process_updates(state, conn, service, calendar_id, kuerzel, advoware):
|
||||||
await reload_db_indexes()
|
"""Phase 4: Process updates for existing entries."""
|
||||||
# Reload API maps after Phase 3 changes
|
|
||||||
await reload_api_maps()
|
|
||||||
|
|
||||||
# Phase 4: Update existing entries if changed
|
|
||||||
logger.info("Phase 4: Processing updates for existing entries")
|
logger.info("Phase 4: Processing updates for existing entries")
|
||||||
# Track which master events we've already processed to avoid duplicate updates
|
# Track which master events we've already processed to avoid duplicate updates
|
||||||
processed_master_events = set()
|
processed_master_events = set()
|
||||||
@@ -932,6 +836,116 @@ async def handler(event, context):
|
|||||||
logger.warning(f"Phase 4: Failed to update sync_id {row['sync_id']}: {e}")
|
logger.warning(f"Phase 4: Failed to update sync_id {row['sync_id']}: {e}")
|
||||||
async with conn.transaction():
|
async with conn.transaction():
|
||||||
await conn.execute("UPDATE calendar_sync SET sync_status = 'failed' WHERE sync_id = $1;", row['sync_id'])
|
await conn.execute("UPDATE calendar_sync SET sync_status = 'failed' WHERE sync_id = $1;", row['sync_id'])
|
||||||
|
"""Main event handler for calendar sync."""
|
||||||
|
logger.info("Starting calendar sync for all employees")
|
||||||
|
|
||||||
|
try:
|
||||||
|
logger.debug("Initializing Advoware API")
|
||||||
|
advoware = AdvowareAPI(context)
|
||||||
|
|
||||||
|
# Alle Mitarbeiter abrufen
|
||||||
|
logger.info("Rufe Advoware Mitarbeiter ab...")
|
||||||
|
employees = await get_advoware_employees(advoware)
|
||||||
|
if not employees:
|
||||||
|
logger.error("Keine Mitarbeiter gefunden. Sync abgebrochen.")
|
||||||
|
return {'status': 500, 'body': {'error': 'Keine Mitarbeiter gefunden'}}
|
||||||
|
|
||||||
|
total_synced = 0
|
||||||
|
for employee in employees:
|
||||||
|
kuerzel = employee.get('kuerzel')
|
||||||
|
if not kuerzel:
|
||||||
|
logger.warning(f"Mitarbeiter ohne Kürzel übersprungen: {employee}")
|
||||||
|
continue
|
||||||
|
|
||||||
|
# DEBUG: Nur für Nutzer AI syncen (für Test der Travel/Prep Zeit)
|
||||||
|
if kuerzel != 'SB':
|
||||||
|
logger.info(f"DEBUG: Überspringe {kuerzel}, nur AI wird gesynct")
|
||||||
|
continue
|
||||||
|
|
||||||
|
logger.info(f"Starting calendar sync for {kuerzel}")
|
||||||
|
|
||||||
|
redis_client = redis.Redis(
|
||||||
|
host=Config.REDIS_HOST,
|
||||||
|
port=int(Config.REDIS_PORT),
|
||||||
|
db=int(Config.REDIS_DB_CALENDAR_SYNC),
|
||||||
|
socket_timeout=Config.REDIS_TIMEOUT_SECONDS
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
logger.debug("Initializing Google service")
|
||||||
|
service = await get_google_service()
|
||||||
|
logger.debug(f"Ensuring Google calendar for {kuerzel}")
|
||||||
|
calendar_id = await ensure_google_calendar(service, kuerzel)
|
||||||
|
|
||||||
|
conn = await connect_db()
|
||||||
|
try:
|
||||||
|
# Initialize state
|
||||||
|
state = {
|
||||||
|
'rows': [],
|
||||||
|
'db_adv_index': {},
|
||||||
|
'db_google_index': {},
|
||||||
|
'adv_appointments': [],
|
||||||
|
'adv_map': {},
|
||||||
|
'google_events': [],
|
||||||
|
'google_map': {}
|
||||||
|
}
|
||||||
|
|
||||||
|
async def reload_db_indexes():
|
||||||
|
"""Reload database indexes after DB changes in phases."""
|
||||||
|
state['rows'] = await conn.fetch(
|
||||||
|
"""
|
||||||
|
SELECT * FROM calendar_sync
|
||||||
|
WHERE employee_kuerzel = $1 AND deleted = FALSE
|
||||||
|
""",
|
||||||
|
kuerzel
|
||||||
|
)
|
||||||
|
state['db_adv_index'] = {str(row['advoware_frnr']): row for row in state['rows'] if row['advoware_frnr']}
|
||||||
|
state['db_google_index'] = {}
|
||||||
|
for row in state['rows']:
|
||||||
|
if row['google_event_id']:
|
||||||
|
state['db_google_index'][row['google_event_id']] = row
|
||||||
|
log_operation('debug', "Reloaded indexes", rows=len(state['rows']), adv=len(state['db_adv_index']), google=len(state['db_google_index']))
|
||||||
|
|
||||||
|
async def reload_api_maps():
|
||||||
|
"""Reload API maps after creating new events in phases."""
|
||||||
|
state['adv_appointments'] = await fetch_advoware_appointments(advoware, kuerzel)
|
||||||
|
state['adv_map'] = {str(app['frNr']): app for app in state['adv_appointments'] if app.get('frNr')}
|
||||||
|
state['google_events'] = await fetch_google_events(service, calendar_id)
|
||||||
|
state['google_map'] = {evt['id']: evt for evt in state['google_events']}
|
||||||
|
log_operation('debug', "Reloaded API maps", adv=len(state['adv_map']), google=len(state['google_map']))
|
||||||
|
|
||||||
|
# Initial fetch
|
||||||
|
log_operation('info', "Fetching fresh data from APIs")
|
||||||
|
await reload_api_maps()
|
||||||
|
await reload_db_indexes()
|
||||||
|
log_operation('info', "Fetched existing sync rows", count=len(state['rows']))
|
||||||
|
|
||||||
|
# Phase 1: New from Advoware => Google
|
||||||
|
await process_new_from_advoware(state, conn, service, calendar_id, kuerzel, advoware)
|
||||||
|
|
||||||
|
# Reload indexes after Phase 1 changes
|
||||||
|
await reload_db_indexes()
|
||||||
|
# Reload API maps after Phase 1 changes
|
||||||
|
await reload_api_maps()
|
||||||
|
|
||||||
|
# Phase 2: New from Google => Advoware
|
||||||
|
await process_new_from_google(state, conn, service, calendar_id, kuerzel, advoware)
|
||||||
|
|
||||||
|
# Reload indexes after Phase 2 changes
|
||||||
|
await reload_db_indexes()
|
||||||
|
# Reload API maps after Phase 2 changes
|
||||||
|
await reload_api_maps()
|
||||||
|
|
||||||
|
# Phase 3: Identify deleted entries
|
||||||
|
await process_deleted_entries(state, conn, service, calendar_id, kuerzel, advoware)
|
||||||
|
|
||||||
|
# Reload indexes after Phase 3 changes
|
||||||
|
await reload_db_indexes()
|
||||||
|
# Reload API maps after Phase 3 changes
|
||||||
|
await reload_api_maps()
|
||||||
|
|
||||||
|
# Phase 4: Update existing entries if changed
|
||||||
|
await process_updates(state, conn, service, calendar_id, kuerzel, advoware)
|
||||||
|
|
||||||
# Update last_sync timestamps
|
# Update last_sync timestamps
|
||||||
logger.debug("Updated last_sync timestamps")
|
logger.debug("Updated last_sync timestamps")
|
||||||
|
|||||||
Reference in New Issue
Block a user