Skip to content

Commit

Permalink
fix: error handling incase if cairn is down (#569)
Browse files Browse the repository at this point in the history
Co-authored-by: Muhammad Faraz  Maqsood <[email protected]>
  • Loading branch information
Faraz32123 and Muhammad Faraz Maqsood authored Jul 18, 2024
1 parent ebd31d9 commit 4c2e90c
Showing 1 changed file with 21 additions and 13 deletions.
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,
},
)

0 comments on commit 4c2e90c

Please sign in to comment.