Skip to content

Commit

Permalink
Initial commit for #503
Browse files Browse the repository at this point in the history
  • Loading branch information
rodriguesk committed Nov 22, 2023
1 parent 5f88c1a commit dd29361
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/analytics/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from flask import Blueprint

bp = Blueprint("analytics", __name__)

from app.analytics import routes
30 changes: 30 additions & 0 deletions app/analytics/analytics_logging.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import uuid
from app import db
from app.errors.errors import DatabaseError
from app.models import AnalyticsData
from datetime import datetime, timezone


def log_user_a_event(session_uuid, category, action, label, event_value, event_timestamp, page_url):
"""
Log an event in the user a analytics data table.
"""
try:
event_to_add = AnalyticsData()

# event_to_add.event_log_uuid = uuid.uuid4() #this should not be needed as autoincrement is set to true.
event_to_add.session_uuid = session_uuid
event_to_add.category = category
event_to_add.action = action
event_to_add.label = label
event_to_add.value = event_value
event_to_add.event_timestamp = event_timestamp
event_to_add.page_url = page_url
# event_to_add.event_timestamp = datetime.now(timezone.utc)

db.session.add(event_to_add)
db.session.commit()
except:
raise DatabaseError(
message="An error occurred while logging a user analytics event."
)
47 changes: 47 additions & 0 deletions app/analytics/routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from app.analytics.analytics_logging import log_user_a_event
from app.common.uuid import validate_uuid, uuidType, check_uuid_in_db
from flask_cors import cross_origin
from flask import request, jsonify

import uuid
from datetime import datetime
from app.analytics.schemas import (
AnalyticsSchema,
)


@bp.route("/analytics", methods=["POST"])
@cross_origin()
def post_user_a_event(conversation_uuid):
"""
Logs a user a event in the analytics_data table for analytics tracking.
The required request body must include category, action, label, session_uuid, event_timestamp, value, page_url
Session uuid validation are included for accurate logging.
Parameters
==========
(implicitly session_uuid), category, action, label, session_uuid, event_timestamp, value, page_url
Returns
==========
JSON - success message
"""

session_uuid = request.headers.get("X-Session-Id")
session_uuid = validate_uuid(session_uuid, uuidType.SESSION)
check_uuid_in_db(session_uuid, uuidType.SESSION)

json_data = request.get_json(force=True, silent=True)
schema = LoggedUserDeleteAccountSchema()
result_data = schema.load(json_data)
log_user_a_event(session_uuid,result_data["category"],result_data["action"],result_data["label"],result_data["event_value"],result_data["event_timestamp"],result_data["page_url"])

response = {"message": "User event logged."}

return jsonify(response), 201



17 changes: 17 additions & 0 deletions app/analytics/schemas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from marshmallow import (
Schema,
fields,
validates_schema,
ValidationError,
)

from app.common.schemas import CamelCaseSchema


class AnalyticsSchema(CamelCaseSchema, Schema):
category = fields.Str(required=True)
action = fields.Str(required=True)
label = fields.Str(required=True)
event_value = fields.Str(required=True)
event_timestamp = fields.Datetime(required=True)
page_url = fields.Str(required=True)
41 changes: 41 additions & 0 deletions app/analytics/tests/test_analytics_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from datetime import datetime, timedelta

import pytest
import typing
from flask import url_for
from flask.testing import FlaskClient
from flask_jwt_extended import create_access_token
from freezegun import freeze_time
from mock import mock

from app.factories import (
UsersFactory,
faker,
SessionsFactory,
)
from app.models import AnalyticsData, Users


@pytest.mark.integration
def test_add_event(client_with_user_and_header):
client, user, session_header, current_password = client_with_user_and_header

url = url_for("analytics.post_user_a_event")
headers = session_header + accept_json
response = client.post(
url,
headers=headers,
json={
"category": faker.pystr(20, 50),
"action": faker.pystr(20, 50),
"label": faker.pystr(20, 50),
"eventValue": faker.pystr(20, 50),
"eventTimestamp": faker.date_time(),
"pageUrl": faker.pystr(20, 255),
},
)

assert response.status_code == 201, "User event logged."



0 comments on commit dd29361

Please sign in to comment.