59 lines
1.8 KiB
Python
59 lines
1.8 KiB
Python
|
|
import logging
|
|
import sys
|
|
import os
|
|
|
|
# Ensure we can import from src
|
|
# In container, app root handles this differently.
|
|
# If running from /app, we need /app/backend to see 'src'
|
|
sys.path.append('/app/backend')
|
|
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
from src.utils.config import config
|
|
from src.models.activity import Activity
|
|
from src.services.parsers import extract_points_from_file
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
logger = logging.getLogger(__name__)
|
|
|
|
def backfill_location():
|
|
db_string = config.DATABASE_URL
|
|
db = create_engine(db_string)
|
|
Session = sessionmaker(bind=db)
|
|
session = Session()
|
|
|
|
try:
|
|
activities = session.query(Activity).filter(
|
|
Activity.file_content != None,
|
|
Activity.start_lat == None
|
|
).all()
|
|
|
|
logger.info(f"Scanning {len(activities)} activities for backfill...")
|
|
|
|
count = 0
|
|
for act in activities:
|
|
try:
|
|
points = extract_points_from_file(act.file_content, act.file_type)
|
|
if points and len(points) > 0:
|
|
first_point = points[0]
|
|
# points are [lon, lat] or [lon, lat, ele]
|
|
act.start_lng = first_point[0]
|
|
act.start_lat = first_point[1]
|
|
count += 1
|
|
|
|
if count % 100 == 0:
|
|
session.commit()
|
|
logger.info(f"Processed {count} activities...")
|
|
except Exception as e:
|
|
logger.error(f"Failed to parse activity {act.id}: {e}")
|
|
|
|
session.commit()
|
|
logger.info(f"Backfill complete. Updated {count} activities.")
|
|
|
|
finally:
|
|
session.close()
|
|
|
|
if __name__ == "__main__":
|
|
backfill_location()
|