-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Removed flask-selfdoc from project. * Update score_nodes.py added relatedPersonalValues to the GET response of the /feed endpoint to include all the personal values associated with each climate change impact for user's feed. * run linting * optional parameter to skip recaptcha * change spelling * remove timedelta * Update installation.md instructions how to free up port 5000 on macs. * Cm 499 delete account and data (#501) * Black formatting * Extra test to ensure deleted user can't login * added api documentation * #499 ondelete="SET NULL" or "CASCADE" * #499 explicit ondelete action for user foreign keys * Change route method from POST to DELETE * switch POST documentation to DELETE in app/static/Climate-Mind_bundled.yml --------- Co-authored-by: Daniil Mashkin <[email protected]> Co-authored-by: Daniil Mashkin <[email protected]> * Analytics endpoint (#504) * Initial commit for #503 * Modified 2 files * Fixing some bugs * Remove init not needed Remove * Fixed bugs * clearer datetime string format documentation. * Fixed bug with date time * Lint * #503 unittests fixed --------- Co-authored-by: Daniil Mashkin <[email protected]> --------- Co-authored-by: Jason Hutson <[email protected]> Co-authored-by: Svenstar74 <[email protected]> Co-authored-by: Daniil Mashkin <[email protected]> Co-authored-by: Daniil Mashkin <[email protected]>
- Loading branch information
1 parent
6b2d435
commit 6a807ba
Showing
10 changed files
with
305 additions
and
24 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
db.session.add(event_to_add) | ||
db.session.commit() | ||
except: | ||
raise DatabaseError( | ||
message="An error occurred while logging a user analytics event." | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from app.analytics import bp | ||
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, | ||
) | ||
from app.models import AnalyticsData | ||
|
||
|
||
@bp.route("/analytics", methods=["POST"]) | ||
@cross_origin() | ||
def post_user_a_event(): | ||
""" | ||
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 = AnalyticsSchema() | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from datetime import datetime, timedelta, timezone | ||
|
||
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, | ||
PasswordResetLinkFactory, | ||
ScoresFactory, | ||
) | ||
from app.models import AnalyticsData, Users | ||
|
||
|
||
@pytest.mark.integration | ||
def test_add_event(client, accept_json): | ||
session = SessionsFactory() | ||
session_header = [("X-Session-Id", session.session_uuid)] | ||
ok_data = { | ||
"category": faker.pystr(20, 50), | ||
"action": faker.pystr(20, 50), | ||
"label": faker.pystr(20, 50), | ||
"eventValue": faker.pystr(20, 50), | ||
"eventTimestamp": str(faker.date_time()), | ||
"pageUrl": faker.pystr(20, 255), | ||
} | ||
|
||
url = url_for("analytics.post_user_a_event") | ||
response = client.post( | ||
url, | ||
headers=session_header, | ||
json=ok_data, | ||
) | ||
assert response.status_code == 201, "User event logged." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters