Finished GPX base import logic

This commit is contained in:
João Silva
2023-10-27 21:23:56 +00:00
parent 3e16115f27
commit 6387c592c0
4 changed files with 55 additions and 67 deletions

View File

@@ -93,39 +93,20 @@ async def read_activities_all_pagination(
return results
def determine_activity_type(gpx):
# Default to 'other' if no activity type is specified
activity_type = 'other'
# Check the GPX 'type' element within the 'trk' section
if gpx.tracks:
track = gpx.tracks[0]
if track.type:
if track.type.lower() == "running":
activity_type = 1
elif track.type.lower() == "trail running":
activity_type = 2
else:
activity_type = 1
return activity_type
def extract_activity_datetime(gpx):
# Default to None if no datetime is specified
activity_datetime = None
if gpx.metadata and gpx.metadata.time:
activity_datetime = gpx.metadata.time.isoformat()
return activity_datetime
class CreateActivityRequest(BaseModel):
distance: int
name: str
type: str
starttime: str
endtime: str
city: str
town: str
country: str
waypoints: List[dict]
@router.post("/activities/create")
async def create_activity(
distance: int = Form(...),
name: str = Form(...),
type: str = Form(...),
starttime: str = Form(...),
endtime: str = Form(...),
activity_data: CreateActivityRequest,
token: str = Depends(oauth2_scheme)
):
from . import sessionController
@@ -138,36 +119,35 @@ async def create_activity(
user_id = payload.get("id")
# Convert the 'starttime' string to a datetime
starttime = datetime.strptime(starttime, "%Y-%m-%dT%H:%M:%SZ")
starttime = datetime.strptime(activity_data.starttime, "%Y-%m-%dT%H:%M:%SZ")
# Convert the 'endtime' string to a datetime
endtime = datetime.strptime(endtime, "%Y-%m-%dT%H:%M:%SZ")
endtime = datetime.strptime(activity_data.endtime, "%Y-%m-%dT%H:%M:%SZ")
if type == "running":
auxType=1
elif type == "trail running":
auxType=2
elif type == "VirtualRun":
auxType=3
elif type == "Ride":
auxType=4
elif type == "GravelRide":
auxType=5
elif type == "EBikeRide":
auxType=6
elif type == "VirtualRide":
auxType=7
else:
auxType=10
auxType = 10 # Default value
type_mapping = {
"running": 1,
"trail running": 2,
"VirtualRun": 3,
"Ride": 4,
"GravelRide": 5,
"EBikeRide": 6,
"VirtualRide": 7
}
auxType = type_mapping.get(activity_data.type, 10)
# Create a new Activity record
activity = Activity(
user_id=user_id,
name=name,
distance=distance,
name=activity_data.name,
distance=activity_data.distance,
activity_type=auxType,
start_time=starttime,
end_time=endtime,
created_at=func.now() # Use func.now() to set 'created_at' to the current timestamp
city=activity_data.city,
town=activity_data.town,
country=activity_data.country,
created_at=func.now(), # Use func.now() to set 'created_at' to the current timestamp
waypoints=activity_data.waypoints
)
# Store the Activity record in the database

View File

@@ -83,7 +83,11 @@ CREATE TABLE IF NOT EXISTS `gearguardian`.`activities` (
`activity_type` INT(2) NOT NULL COMMENT 'Gear type (1 - mountain bike, 2 - gravel bike, 3 - road bike, 4 - indoor bike, 5 - road run, 6 - trail run, 7 - indoor run, 8 - indoor swim, 9 - openwater swim, 10 - other)' ,
`start_time` DATETIME NOT NULL COMMENT 'Actvitiy start date (datetime)' ,
`end_time` DATETIME NOT NULL COMMENT 'Actvitiy end date (datetime)' ,
`city` VARCHAR(45) NULL COMMENT 'Activity city (May include spaces)' ,
`town` VARCHAR(45) NULL COMMENT 'Activity town (May include spaces)' ,
`country` VARCHAR(45) NULL COMMENT 'Activity country (May include spaces)' ,
`created_at` DATETIME NOT NULL COMMENT 'Actvitiy creation date (datetime)' ,
`waypoints` LONGTEXT NULL COMMENT 'Store waypoints data',
PRIMARY KEY (`id`) ,
INDEX `FK_user_id_idx` (`user_id` ASC) ,
CONSTRAINT `FK_activity_user`

View File

@@ -6,6 +6,7 @@ from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, Column, Integer, String, Date, DateTime, ForeignKey, LargeBinary, DECIMAL
from dotenv import load_dotenv
from datetime import datetime
from sqlalchemy.dialects.mysql import JSON
# Load the environment variables from config/.env
load_dotenv('config/.env')
@@ -94,30 +95,34 @@ class Activity(Base):
activity_type = Column(Integer, nullable=False, comment='Gear type (1 - mountain bike, 2 - gravel bike, ...)')
start_time = Column(DateTime, nullable=False, comment='Activity start date (datetime)')
end_time = Column(DateTime, nullable=False, comment='Activity end date (datetime)')
city = Column(String(length=45), nullable=True, comment='Activity city (May include spaces)')
town = Column(String(length=45), nullable=True, comment='Activity town (May include spaces)')
country = Column(String(length=45), nullable=True, comment='Activity country (May include spaces)')
created_at = Column(DateTime, nullable=False, comment='Activity creation date (datetime)')
waypoints = Column(JSON, nullable=True, doc='Store waypoints data')
# Define a relationship to the User model
user = relationship('User', back_populates='activities')
# Establish a one-to-many relationship with 'waypoints'
waypoints = relationship('Waypoint', back_populates='activity')
#waypoints = relationship('Waypoint', back_populates='activity')
# Data model for waypoints table using SQLAlchemy's ORM
class Waypoint(Base):
__tablename__ = 'waypoints'
# class Waypoint(Base):
# __tablename__ = 'waypoints'
id = Column(Integer, primary_key=True, autoincrement=True)
activity_id = Column(Integer, ForeignKey('activities.id'), nullable=False, comment='Activity ID that the waypoint belongs')
latitude = Column(DECIMAL(precision=10, scale=6), nullable=True, comment='Latitude with 6 decimal places')
longitude = Column(DECIMAL(precision=10, scale=6), nullable=True, comment='Longitude with 6 decimal places')
elevation = Column(DECIMAL(precision=8, scale=2), nullable=True, comment='Elevation with 2 decimal places')
time = Column(DateTime, nullable=True, comment='Timestamp of the waypoint')
heart_rate = Column(Integer, nullable=True, comment='Heart rate data')
cadence = Column(Integer, nullable=True, comment='Cadence data')
power = Column(Integer, nullable=True, comment='Power data')
# id = Column(Integer, primary_key=True, autoincrement=True)
# activity_id = Column(Integer, ForeignKey('activities.id'), nullable=False, comment='Activity ID that the waypoint belongs')
# latitude = Column(DECIMAL(precision=10, scale=6), nullable=True, comment='Latitude with 6 decimal places')
# longitude = Column(DECIMAL(precision=10, scale=6), nullable=True, comment='Longitude with 6 decimal places')
# elevation = Column(DECIMAL(precision=8, scale=2), nullable=True, comment='Elevation with 2 decimal places')
# time = Column(DateTime, nullable=True, comment='Timestamp of the waypoint')
# heart_rate = Column(Integer, nullable=True, comment='Heart rate data')
# cadence = Column(Integer, nullable=True, comment='Cadence data')
# power = Column(Integer, nullable=True, comment='Power data')
# Define a relationship to the Activity model
activity = relationship('Activity', back_populates='waypoints')
# # Define a relationship to the Activity model
# activity = relationship('Activity', back_populates='waypoints')
# Context manager to get a database session
from contextlib import contextmanager

View File

@@ -1,6 +1,6 @@
from fastapi import FastAPI
from apscheduler.schedulers.background import BackgroundScheduler
from controllers import sessionController, userController, gearController, activitiesController, waypointsController
from controllers import sessionController, userController, gearController, activitiesController
import logging
#from db.db import User # Import your SQLAlchemy session management from db.db
@@ -22,7 +22,6 @@ app.include_router(sessionController.router)
app.include_router(userController.router)
app.include_router(gearController.router)
app.include_router(activitiesController.router)
app.include_router(waypointsController.router)
# Create a background scheduler instance
scheduler = BackgroundScheduler()