Skip to content

Commit

Permalink
Survey and telemetry consent status reset on new session /w initializ…
Browse files Browse the repository at this point in the history
…ation method refactor
  • Loading branch information
wielas committed Nov 17, 2023
1 parent 0be00a3 commit db5fa1e
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 40 deletions.
4 changes: 2 additions & 2 deletions flask_monitoringdashboard/core/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ def __init__(self):
self.telemetry_consent = False
self.telemetry_session = 0
self.telemetry_headers = {
'X-Parse-Application-Id': 'zwfDL1t45KjnXSF5ELGQajShV6eJiaKVmRFaQjUb',
'X-Parse-REST-API-Key': 'Kr9nhaAKx04hJypCJzz5BziqbKf9Yq5Q7HsWDpI9',
'X-Parse-Application-Id': '4nHPABwkHqOZzNrFduzNyKH8q7wmPFdOWvajfWU2',
'X-Parse-REST-API-Key': 'zjv0WLI2K3UvpfzrfG4sPA6EykYyzZM4KxQk07Hs',
'Content-Type': 'application/json'
}
83 changes: 49 additions & 34 deletions flask_monitoringdashboard/core/telemetry.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import datetime
import requests

from collections import Counter

from sqlalchemy import func
from sqlalchemy.exc import NoResultFound, MultipleResultsFound, SQLAlchemyError

Expand All @@ -12,6 +10,7 @@


def get_telemetry_user(session):
"""Return a telemetry user object"""
try:
return session.query(TelemetryUser).one()

Expand All @@ -26,6 +25,41 @@ def get_telemetry_user(session):
get_telemetry_user(session)


def collect_general_telemetry_data(session, telemetry_user):
"""
Collects telemetry data and posts it to the remote database
"""
# collect endpoint and blueprint data
endpoints = session.query(Endpoint.name).all()
blueprints = set(get_blueprint(endpoint) for endpoint, in endpoints)
no_of_endpoints = len(endpoints)
no_of_blueprints = len(blueprints)

# collect monitoring levels
counts = (
session.query(Endpoint.monitor_level, func.count(Endpoint.monitor_level))
.group_by(Endpoint.monitor_level)
.all()
)
counts_dict = dict(counts)
level_zeros_count = counts_dict.get(0, 0)
level_ones_count = counts_dict.get(1, 0)
level_twos_count = counts_dict.get(2, 0)
level_threes_count = counts_dict.get(3, 0)

data = {'endpoints': no_of_endpoints,
'blueprints': no_of_blueprints,
'time_accessed': telemetry_user.last_accessed.strftime('%Y-%m-%d %H:%M:%S'),
'monitoring_0': level_zeros_count,
'monitoring_1': level_ones_count,
'monitoring_2': level_twos_count,
'monitoring_3': level_threes_count,
}

# post user data
post_to_back('UserSession', **data)


def initialize_telemetry_session(session):
"""
Initializes the monitoring session by creating or updating an entry
Expand All @@ -42,6 +76,14 @@ def initialize_telemetry_session(session):
telemetry_user.last_accessed = datetime.datetime.utcnow()
session.commit()

# reset telemetry and survey prompt if declined in previous session
if telemetry_user.monitoring_consent == 2:
telemetry_user.monitoring_consent = 1
session.commit()
if telemetry_user.survey_filled == 2:
telemetry_user.survey_filled = 1
session.commit()

# check if telemetry's been agreed on
telemetry_config.telemetry_consent = True if telemetry_user.monitoring_consent == 3 else False

Expand All @@ -50,38 +92,8 @@ def initialize_telemetry_session(session):
telemetry_config.telemetry_initialized = True
telemetry_config.telemetry_session = telemetry_user.times_accessed

# collect user data
endpoints = session.query(Endpoint.name).all()
print(endpoints)
print(type(endpoints))
print(type(endpoints[0]))
print(type(endpoints[0].name))
blueprints = set(get_blueprint(endpoint) for endpoint, in endpoints)
no_of_endpoints = len(endpoints)
no_of_blueprints = len(blueprints)
counts = (
session.query(Endpoint.monitor_level, func.count(Endpoint.monitor_level))
.group_by(Endpoint.monitor_level)
.all()
)
# collect monitoring levels
counts_dict = dict(counts)
level_zeros_count = counts_dict.get(0, 0)
level_ones_count = counts_dict.get(1, 0)
level_twos_count = counts_dict.get(2, 0)
level_threes_count = counts_dict.get(3, 0)

data = {'endpoints': no_of_endpoints,
'blueprints': no_of_blueprints,
'time_accessed': telemetry_user.last_accessed.strftime('%Y-%m-%d %H:%M:%S'),
'monitoring_0': level_zeros_count,
'monitoring_1': level_ones_count,
'monitoring_2': level_twos_count,
'monitoring_3': level_threes_count,
}

# post user data
post_to_back('UserSession', **data)
# collect the data and send it to the remote database
collect_general_telemetry_data(session, telemetry_user)

except (MultipleResultsFound, NoResultFound) as e:
print(e)
Expand All @@ -94,6 +106,9 @@ def initialize_telemetry_session(session):


def post_to_back(class_name='Endpoints', **kwargs):
"""
Function to send telemetry data to remote database
"""
if telemetry_config.telemetry_consent:
back4app_endpoint = f'https://parseapi.back4app.com/classes/{class_name}'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ export function TelemetryController($scope, $http, $window) {
// Configuration for HTTP requests to Back4App
var config = {
headers: {
'X-Parse-Application-Id': 'zwfDL1t45KjnXSF5ELGQajShV6eJiaKVmRFaQjUb',
'X-Parse-REST-API-Key': 'Kr9nhaAKx04hJypCJzz5BziqbKf9Yq5Q7HsWDpI9',
'X-Parse-Application-Id': '4nHPABwkHqOZzNrFduzNyKH8q7wmPFdOWvajfWU2',
'X-Parse-REST-API-Key': 'zjv0WLI2K3UvpfzrfG4sPA6EykYyzZM4KxQk07Hs',
'Content-Type': 'application/json'
}
};
Expand Down
2 changes: 1 addition & 1 deletion flask_monitoringdashboard/static/js/app.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion flask_monitoringdashboard/views/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def survey_has_been_filled():
def get_is_telemetry_answered():
with session_scope() as session:
telemetry_user = get_telemetry_user(session)
res = True if telemetry_user.monitoring_consent == 3 else False
res = True if telemetry_user.monitoring_consent == (2, 3) else False
return {'is_telemetry_answered': res}


Expand Down

0 comments on commit db5fa1e

Please sign in to comment.