Skip to content

Commit

Permalink
Refactor Azure sync routes and Google sync utils
Browse files Browse the repository at this point in the history
  • Loading branch information
StanGirard committed May 20, 2024
1 parent b70d6f0 commit 101fa12
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
4 changes: 2 additions & 2 deletions backend/modules/sync/controller/azure_sync_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
"https://graph.microsoft.com/Sites.Read.All",
]

client = PublicClientApplication(CLIENT_ID, authority=AUTHORITY)


@azure_sync_router.get(
"/sync/azure/authorize",
Expand All @@ -47,6 +45,7 @@ def authorize_azure(
Returns:
dict: A dictionary containing the authorization URL.
"""
client = PublicClientApplication(CLIENT_ID, authority=AUTHORITY)
logger.debug(f"Authorizing Azure sync for user: {current_user.id}")
state = f"user_id={current_user.id}"
authorization_url = client.get_authorization_request_url(
Expand Down Expand Up @@ -75,6 +74,7 @@ def oauth2callback_azure(request: Request):
Returns:
dict: A dictionary containing a success message.
"""
client = PublicClientApplication(CLIENT_ID, authority=AUTHORITY)
state = request.query_params.get("state")
state_dict = {"state": state}
current_user = state.split("=")[1] # Extract user_id from state
Expand Down
2 changes: 1 addition & 1 deletion backend/modules/sync/repository/sync_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def get_sync_files(self, sync_active_id: int) -> list[SyncsFiles]:
.execute()
)
if response.data:
logger.info("Sync files retrieved successfully: %s", response.data)
# logger.info("Sync files retrieved successfully: %s", response.data)
return [SyncsFiles(**file) for file in response.data]
logger.warning("No sync files found for sync_active_id: %s", sync_active_id)
return []
Expand Down
58 changes: 36 additions & 22 deletions backend/modules/sync/utils/googleutils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from io import BytesIO

from fastapi import UploadFile
Expand Down Expand Up @@ -65,11 +65,9 @@ async def _upload_files(
file_name = file["name"]
mime_type = file["mime_type"]
modified_time = file["last_modified"]
logger.info("Downloading file with file_id: %s", file_id)
logger.info("File last modified on: %s", modified_time)
# Convert Google Docs files to appropriate formats before downloading
if mime_type == "application/vnd.google-apps.document":
logger.info(
logger.debug(
"Converting Google Docs file with file_id: %s to DOCX.", file_id
)
request = service.files().export_media(
Expand All @@ -78,7 +76,7 @@ async def _upload_files(
)
file_name += ".docx"
elif mime_type == "application/vnd.google-apps.spreadsheet":
logger.info(
logger.debug(
"Converting Google Sheets file with file_id: %s to XLSX.",
file_id,
)
Expand All @@ -88,7 +86,7 @@ async def _upload_files(
)
file_name += ".xlsx"
elif mime_type == "application/vnd.google-apps.presentation":
logger.info(
logger.debug(
"Converting Google Slides file with file_id: %s to PPTX.",
file_id,
)
Expand All @@ -97,15 +95,29 @@ async def _upload_files(
mimeType="application/vnd.openxmlformats-officedocument.presentationml.presentation",
)
file_name += ".pptx"
else:
### Elif pdf, txt, md, csv, docx, xlsx, pptx, doc
elif file_name.split(".")[-1] in [
"pdf",
"txt",
"md",
"csv",
"docx",
"xlsx",
"pptx",
"doc",
]:
request = service.files().get_media(fileId=file_id)
else:
logger.warning(
"Skipping unsupported file type: %s for file_id: %s",
mime_type,
file_id,
)
continue

file_data = request.execute()

# Check if the file already exists in the storage
logger.info(
"Checking if file already exists in the storage: %s", file_name
)
if check_file_exists(brain_id, file_name):
logger.info("馃敟 File already exists in the storage: %s", file_name)

Expand Down Expand Up @@ -145,7 +157,6 @@ async def _upload_files(
)

downloaded_files.append(file_name)
logger.info("File downloaded and saved successfully: %s", file_name)
return {"downloaded_files": downloaded_files}
except HttpError as error:
logger.error(
Expand All @@ -161,11 +172,6 @@ async def sync(self, sync_active_id: int, user_id: str):
sync_active_id (int): The ID of the active sync.
user_id (str): The user ID associated with the active sync.
"""
logger.info(
"Checking if Google sync has not been synced for sync_active_id: %s, user_id: %s",
sync_active_id,
user_id,
)

# Retrieve the active sync details
sync_active = self.sync_active_service.get_details_sync_active(sync_active_id)
Expand All @@ -179,11 +185,20 @@ async def sync(self, sync_active_id: int, user_id: str):
last_synced = sync_active.get("last_synced")
sync_interval_minutes = sync_active.get("sync_interval_minutes", 0)
if last_synced:
last_synced_time = datetime.fromisoformat(last_synced)
last_synced_time = datetime.fromisoformat(last_synced).astimezone(
timezone.utc
)
current_time = datetime.now().astimezone()
if current_time - last_synced_time < timedelta(
minutes=sync_interval_minutes
):

# Debug logging to check the values
logger.debug("Last synced time (UTC): %s", last_synced_time)
logger.debug("Current time (local timezone): %s", current_time)

# Convert current_time to UTC for comparison
current_time_utc = current_time.astimezone(timezone.utc)
logger.debug("Current time (UTC): %s", current_time_utc)
time_difference = current_time_utc - last_synced_time
if time_difference < timedelta(minutes=sync_interval_minutes):
logger.info(
"Google sync is not due for sync_active_id: %s", sync_active_id
)
Expand Down Expand Up @@ -217,7 +232,6 @@ async def sync(self, sync_active_id: int, user_id: str):
# Get the folder id from the settings from sync_active
settings = sync_active.get("settings", {})
folders = settings.get("folders", [])
logger.info("Folders: %s", folders)
files = get_google_drive_files(
sync_user["credentials"], folder_id=folders[0] if folders else None
)
Expand Down Expand Up @@ -257,7 +271,7 @@ async def sync(self, sync_active_id: int, user_id: str):
# Update the last_synced timestamp
self.sync_active_service.update_sync_active(
sync_active_id,
SyncsActiveUpdateInput(last_synced=datetime.now().isoformat()),
SyncsActiveUpdateInput(last_synced=datetime.now().astimezone().isoformat()),
)
logger.info(
"Google Drive sync completed for sync_active_id: %s", sync_active_id
Expand Down

0 comments on commit 101fa12

Please sign in to comment.