Skip to content
Merged
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
26 changes: 26 additions & 0 deletions app/analytics/UserAnalytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ def __init__(self):
self.assets_path = os.environ.get("ASSETS_PATH")
self.user_file_path = os.environ.get("USER_FILE_PATH")
self.geo_lite_db_path = os.environ.get("GEOLITE_DB_PATH")

# TODO: come back and fix this
# if self.geo_lite_db_path is None:
# raise Exception("GEOLITE_DB_PATH environment variable is not set.")

if self.user_file_path is None:
raise Exception("USER_FILE_PATH environment variable is not set.")

Expand Down Expand Up @@ -103,6 +108,27 @@ def profile_preference_selections(self):
preference_values = [preference_data[field] for field in preference_fields if field in preference_data]

return profile_values, preference_values

def count_stringeny_attributes(self):
preferences = self.get_preferences_data()

dealbreaker_cats = {
"physical": ["age_dealbreaker", "height_dealbreaker"],
"identity": ["ethnicity_dealbreaker", "religion_dealbreaker", "politics_dealbreaker"],
"lifestyle": ["smoking_dealbreaker", "drinking_dealbreaker", "marijuana_dealbreaker", "drugs_dealbreaker"],
"career": ["education_attained_dealbreaker"],
"future_plans": ["children_dealbreaker", "family_plans_dealbreaker"]
}
# initialize counters
display_counts = defaultdict(lambda: {"true": 0, "false": 0})

for category, fields in dealbreaker_cats.items():
for field in fields:
if field in preferences:
display_value = preferences[field]
display_counts[category]["true" if display_value else "false"] += 1
return dict(display_counts)


def count_displayed_attributes(self):
profile_data = self.get_profile_data()
Expand Down
62 changes: 60 additions & 2 deletions app/pages/UserPage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,63 @@

from analytics.UserAnalytics import UserAnalytics

BLUE = "#3BAAC4"
REDISH = "#C4553B"

def stringency_vs_flexibility():
dealbreaker_counts = UserAnalytics().count_stringeny_attributes()

category_labels = {
"physical": "Age & Height",
"identity": "Identity & Demographics",
"career": "Work & Education",
"lifestyle": "Lifestyle & Habits",
"future_plans": "Dating Preferences & Intentions"
}

# extract the category keys and T/F counts to pass to the graphs
cat_keys = [category_labels[cat] for cat in dealbreaker_counts.keys()]
true_counts = [dealbreaker_counts[cat]['true'] for cat in dealbreaker_counts]
false_counts = [dealbreaker_counts[cat]['false'] for cat in dealbreaker_counts]

fig = go.Figure()

fig.add_trace(go.Bar(
x=cat_keys,
y=true_counts,
name="Dealbreaker (True)",
marker_color=REDISH
))

fig.add_trace(go.Bar(
x=cat_keys,
y=false_counts,
name="Open (False)",
marker_color=BLUE
))

fig.update_layout(
title="Dating Preferences: Dealbreakers vs. Open Choices",
xaxis_title="Preference Category",
yaxis_title="Number of Dealbreakers",
barmode='group',
template="plotly_white"
)
return dmc.Card(
children=[
dmc.Space(h=10),
dmc.Text("How strict or open is this person in their dating preferences?", weight=700, size="xl"),
dmc.Space(h=10),
dmc.Text("This bar chart compares the number of 'dealbreakers' versus 'open' preferences across different dating categories, highlighting which factors are most important or flexible in the user's online dating criteria.", size="md"),
dmc.Space(h=10),
dcc.Graph(figure=fig)
],
withBorder=True,
shadow="sm",
radius="md",
style={"height": "520px"},
)

def geolocation():
df = UserAnalytics().collect_location_from_ip()
fig = px.scatter_geo(
Expand Down Expand Up @@ -93,14 +150,14 @@ def disclosure_vs_privacy():
x=cat_keys,
y=true_counts,
name="Displayed (True)",
marker_color='blue'
marker_color=BLUE
))

fig.add_trace(go.Bar(
x=cat_keys,
y=false_counts,
name="Not Displayed (False)",
marker_color='red'
marker_color=REDISH
))

fig.update_layout(
Expand Down Expand Up @@ -237,5 +294,6 @@ def create_user_summary_card():
disclosure_vs_privacy(),
potential_misalignments(),
geolocation(),
stringency_vs_flexibility(),
dmc.Space(h=50)
])
14 changes: 13 additions & 1 deletion tests/analytics/test_UserAnalytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
"distance_miles_max": 50,
"age_min": 98,
"age_max": 99,
"age_dealbreaker": true,
"height_dealbreaker": false,
"ethnicity_preference": "[Open to All]",
"ethnicity_dealbreaker": false,
"religion_preference": "[Open to All]",
Expand Down Expand Up @@ -115,13 +117,16 @@
}
'''
COUNT_DISPLAYED_ATTRIB_OUTPUT = {'identity': {'true': 2, 'false': 4}, 'lifestyle': {'true': 2, 'false': 3}, 'career': {'true': 2, 'false': 1}, 'future_plans': {'true': 1, 'false': 3}}
STRINGENCY_COUNTS = {'physical': {'true': 1, 'false': 1}, 'identity': {'true': 0, 'false': 3}, 'lifestyle': {'true': 0, 'false': 4}, 'career': {'true': 0, 'false': 1}, 'future_plans': {'true': 0, 'false': 2}}
GEOLITE_DB_PATH = 'data/db_path.mmdb'

#########################################################################################
# pytest fixtures
#########################################################################################
@pytest.fixture
def user_analytics(monkeypatch):
monkeypatch.setenv("USER_FILE_PATH", USER_FILE_PATH)
monkeypatch.setenv("GEOLITE_DB_PATH", GEOLITE_DB_PATH)

with patch("builtins.open", mock_open(read_data=USER_DATA)) as mock_file, \
patch("json.load", return_value=json.loads(USER_DATA)) as mock_json_load:
Expand Down Expand Up @@ -219,4 +224,11 @@ def test_profile_preference_selections(user_analytics):
# TODO: this needs to be mocked out and better tests added
# def test_collect_location_from_ip(user_analytics):
# result = user_analytics.collect_location_from_ip()
# assert result is not None
# assert result is not None

def test_count_stringeny_attributes(user_analytics):
results = user_analytics.count_stringeny_attributes()
print(results)

assert list(results.keys()) == ['physical', 'identity', 'lifestyle', 'career', 'future_plans']
assert results == STRINGENCY_COUNTS