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

Add GSR detailed analytics and fitness analytics #306

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
21 changes: 20 additions & 1 deletion backend/gsr_booking/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import datetime

from django.contrib.auth import get_user_model
from django.db.models import Prefetch, Q
from django.http import HttpResponseForbidden
Expand Down Expand Up @@ -253,7 +255,24 @@ def post(self, request):
request.user.booking_groups.filter(name="Penn Labs").first(),
)

record_analytics(Metric.GSR_BOOK, request.user.username)
try:
date_format = "%Y-%m-%dT%H:%M:%S%z"
start_date_obj = datetime.strptime(start, date_format)
end_date_obj = datetime.strptime(end, date_format)

gsr_analytic = (
Metric.GSR_BOOK + ".",
str(room_id).replace(" ", "").upper() + ".",
str(room_name).replace(" ", "").upper(),
)

elapsed_minutes = (end_date_obj - start_date_obj).total_seconds() / 60
record_analytics(str(gsr_analytic) + ".start", request.user.username, start)
record_analytics(
str(gsr_analytic) + ".duration", request.user.username, str(elapsed_minutes)
)
except (APIError, ValueError):
pass

return Response({"detail": "success"})
except APIError as e:
Expand Down
23 changes: 23 additions & 0 deletions backend/penndata/analytics.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from enum import Enum
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wasn't sure if it was good practice to use the pennmobile analytics.py file for penndata analytics calls (though they're essentially the same file.)


from analytics.analytics import AnalyticsTxn, LabsAnalytics, Product


try:
AnalyticsEngine = LabsAnalytics()
except Exception as e:
print("Error initializing AnalyticsEngine: ", e)
AnalyticsEngine = None


class Metric(str, Enum):
FITNESS = "fitness"


# TODO: Support multiple data objects in a single transaction
def record_analytics(metric: str, username=None, value="1"):
if not AnalyticsEngine:
print("AnalyticsEngine not initialized")
return
txn = AnalyticsTxn(Product.MOBILE_BACKEND, None, data=[{"key": metric, "value": value}])
AnalyticsEngine.submit(txn)
3 changes: 3 additions & 0 deletions backend/penndata/management/commands/get_fitness_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.core.management.base import BaseCommand
from django.utils import timezone

from penndata.analytics import Metric, record_analytics
from penndata.models import FitnessRoom, FitnessSnapshot


Expand Down Expand Up @@ -56,6 +57,8 @@ def get_usages():
count = int(cells[1].getText())
capacity = float(cells[2].getText().strip("%"))
usages[location] = {"count": count, "capacity": capacity}
analytic_string = (Metric.FITNESS + ".", str(location).replace(" ", "").upper())
record_analytics(analytic_string, None, str(count))
except ValueError:
pass
else:
Expand Down
5 changes: 3 additions & 2 deletions backend/pennmobile/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ class Metric(str, Enum):
PORTAL_POLL_VOTED = "portal.poll.voted"


def record_analytics(metric: Metric, username=None):
# TODO: Support multiple data objects in a single transaction
def record_analytics(metric: str, username=None, value="1"):
if not AnalyticsEngine:
print("AnalyticsEngine not initialized")
return
txn = AnalyticsTxn(Product.MOBILE_BACKEND, username, data=[{"key": metric, "value": "1"}])
txn = AnalyticsTxn(Product.MOBILE_BACKEND, username, data=[{"key": metric, "value": value}])
AnalyticsEngine.submit(txn)
Loading