예제 #1
0
def genTopoJSON():
    """
    Generates a new TopoJSON file using all stored Strava activities and uploads to S3 Bucket, replaces existing file.
    Called by Strava Activity admin page inputs.
    """
    # Create topojson file
    application.logger.debug(f"Received request to generate a new TopoJSON")
    topoJSON = DBQueriesStrava.createStravaPublicActTopoJSON()
    # Upload topoJSON to AWS S3
    StravaAWSS3.uploadToS3(topoJSON)
    application.logger.debug(f"New TopoJSON has been generated")
    return Response(status=200)
def singleActivityProcessing(client, actID):
    """
    Processes a single Strava Activity by placing the full activity in the database, making a simplified and masked public
    version, and by creating a privacy masked stream CSV which is added to a S3 Bucket. Finally a TopoJSON of the
    public activities is generated and uploaded to the S3 Bucket.

    @param client: stravalib client instance with valid access token
    @param actID: Int. ID of Strava Activity to be processed
    @return: Email. Message states if process was successful or failed
    """

    try:
        # Wait 45 minutes before processing update, this allows time for user to update any ride details before they
        #  are processed, in particular changing details uploaded from Wahoo
        # Check if in development mode, if not wait 45 minutes
        if application.config['ENV'] != "development":
            time.sleep(2700)
        application.logger.debug("Getting full activity details")
        # Get all activity details for newly created activity, including stream data
        activity = getFullDetails(client, actID)
        application.logger.debug("Inserting activity details")
        # Insert original, non-masked, coordinates and attribute details into Postgres/PostGIS
        DBQueriesStrava.insertOriginalAct(activity['act'])
        # Calculate masked, publicly sharable, activities and insert into Postgres masked table
        application.logger.debug("Processing and inserting masked geometries")
        DBQueriesStrava.processActivitiesPublic(activity["act"]["actId"])
        # Handle CSV stream processing
        generateAndUploadCSVStream(client, actID, activity)
        # Create topojson file
        topoJSON = DBQueriesStrava.createStravaPublicActTopoJSON()
        # Upload topoJSON to AWS S3
        StravaAWSS3.uploadToS3(topoJSON)
        # Send success email
        errorEmail.sendSuccessEmail(
            "Webhook Activity Update",
            f'The strava activity: {activity["act"]["actId"]}'
            f' has been processed, the activity can be'
            f' viewed on Strava at: '
            f'https://www.strava.com/activities/{activity["act"]["actId"]}')
        application.logger.debug("Strava activity has been processed!")
    except Exception as e:
        application.logger.error(
            f"Handling and inserting new webhook activity inside a thread failed with the error {e}"
        )
        errorEmail.sendErrorEmail(
            script="Webhook Activity Threaded Task Update",
            exceptiontype=e.__class__.__name__,
            body=e)
        # Raise another exception, this will signal the route function to return an error 500
        raise ()