Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: error handling incase if cairn is down #569

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions openedx/features/sdaia_features/course_progress/api/v1/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,24 @@ def get(self, request):
"""
user = request.user
user_id = user.id
clickhouse_uri = (
f"{settings.CAIRN_CLICKHOUSE_HTTP_SCHEME}://{settings.CAIRN_CLICKHOUSE_USERNAME}:{settings.CAIRN_CLICKHOUSE_PASSWORD}@"
f"{settings.CAIRN_CLICKHOUSE_HOST}:{settings.CAIRN_CLICKHOUSE_HTTP_PORT}/?database={settings.CAIRN_CLICKHOUSE_DATABASE}"
)
query = f"SELECT SUM(duration) as `Watch time` FROM `openedx`.`video_view_segments` WHERE user_id={user_id};"
errors = {}

############ WATCH HOURS ############
try:
clickhouse_uri = (
f"{settings.CAIRN_CLICKHOUSE_HTTP_SCHEME}://{settings.CAIRN_CLICKHOUSE_USERNAME}:{settings.CAIRN_CLICKHOUSE_PASSWORD}@"
f"{settings.CAIRN_CLICKHOUSE_HOST}:{settings.CAIRN_CLICKHOUSE_HTTP_PORT}/?database={settings.CAIRN_CLICKHOUSE_DATABASE}"
)
query = f"SELECT SUM(duration) as `Watch time` FROM `openedx`.`video_view_segments` WHERE user_id={user_id};"
response = requests.get(clickhouse_uri, data=query.encode("utf8"))
watch_time = float(response.content.decode().strip()) / (60 * 60)
except Exception as e:
log.error(
f"Unable to fetch watch for user {user_id} due to this exception: {str(e)}"
f"Unable to fetch watch time for user {user_id} due to this exception: {str(e)}"
)
raise HTTPException(status_code=500, detail=str(e))
watch_time_error = "ERROR: Unable to Connect to Cairn Clickhouse"
errors["watch_hours"] = watch_time_error
watch_time = None

############ PROGRAMS COUNT ############
meter = ProgramProgressMeter(request.site, user, mobile_only=False)
Expand Down Expand Up @@ -156,6 +159,7 @@ def get(self, request):
"score": score,
"user_certificates": user_certificates,
"user_badges": transformed_user_badges,
"errors": errors,
},
)

Expand Down Expand Up @@ -188,21 +192,24 @@ def get(self, request):
user = request.user
users_count = User.objects.all().count()
certificates_count = GeneratedCertificate.objects.all().count()
clickhouse_uri = (
f"{settings.CAIRN_CLICKHOUSE_HTTP_SCHEME}://{settings.CAIRN_CLICKHOUSE_USERNAME}:{settings.CAIRN_CLICKHOUSE_PASSWORD}@"
f"{settings.CAIRN_CLICKHOUSE_HOST}:{settings.CAIRN_CLICKHOUSE_HTTP_PORT}/?database={settings.CAIRN_CLICKHOUSE_DATABASE}"
)
query = f"SELECT SUM(duration) as `Watch time` FROM `openedx`.`video_view_segments`;"
errors = {}

############ TOTAL WATCH HOURS ############
try:
clickhouse_uri = (
f"{settings.CAIRN_CLICKHOUSE_HTTP_SCHEME}://{settings.CAIRN_CLICKHOUSE_USERNAME}:{settings.CAIRN_CLICKHOUSE_PASSWORD}@"
f"{settings.CAIRN_CLICKHOUSE_HOST}:{settings.CAIRN_CLICKHOUSE_HTTP_PORT}/?database={settings.CAIRN_CLICKHOUSE_DATABASE}"
)
query = f"SELECT SUM(duration) as `Watch time` FROM `openedx`.`video_view_segments`;"
response = requests.get(clickhouse_uri, data=query.encode("utf8"))
watch_time = float(response.content.decode().strip()) / (60 * 60)
except Exception as e:
log.error(
f"Unable to fetch total watch hours due to this exception: {str(e)}"
)
raise HTTPException(status_code=500, detail=str(e))
watch_time_error = "ERROR: Unable to Connect to Cairn Clickhouse"
errors["total_watch_time"] = watch_time_error
watch_time = None

############ Response ############
return Response(
Expand All @@ -211,5 +218,6 @@ def get(self, request):
"users_count": users_count,
"certificates_count": certificates_count,
"total_watch_time": watch_time,
"errors": errors,
},
)
Loading