Skip to content

Commit

Permalink
collect failed deflections
Browse files Browse the repository at this point in the history
  • Loading branch information
escattone committed Feb 14, 2025
1 parent 0e513e6 commit 0f7dc59
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
1 change: 1 addition & 0 deletions kitsune/questions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class Question(AAQBase):
)
is_locked = models.BooleanField(default=False)
is_archived = models.BooleanField(default=False, null=True)
# is_failed_deflection = models.BooleanField(default=False)
num_votes_past_week = models.PositiveIntegerField(default=0, db_index=True)

marked_as_spam_by = models.ForeignKey(
Expand Down
11 changes: 10 additions & 1 deletion kitsune/questions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
from kitsune.upload.models import ImageAttachment
from kitsune.users.models import Setting
from kitsune.wiki.facets import topics_for
from kitsune.wiki.utils import build_topics_data
from kitsune.wiki.utils import build_topics_data, has_visited_kb

log = logging.getLogger("k.questions")

Expand Down Expand Up @@ -605,6 +605,10 @@ def aaq(request, product_slug=None, step=1, is_loginless=False):
context["form"] = zendesk_form

if zendesk_form.is_valid() and not is_ratelimited(request, "loginless", "3/d"):

if has_visited_kb(request.session, product):
print("RYAN: FAILED DEFLECTION!!")

try:
zendesk_form.send(request.user, product)
email = zendesk_form.cleaned_data["email"]
Expand Down Expand Up @@ -648,6 +652,11 @@ def aaq(request, product_slug=None, step=1, is_loginless=False):
product=product,
)

if has_visited_kb(request.session, question.product, question.topic):
print("RYAN: FAILED DEFLECTION!!")
# question.is_failed_deflection = True
# question.save()

if form.cleaned_data.get("is_spam"):
_add_to_moderation_queue(request, question)

Expand Down
55 changes: 55 additions & 0 deletions kitsune/wiki/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import random
import time
from itertools import chain, islice

import requests
Expand All @@ -18,6 +19,9 @@
from kitsune.wiki.models import Document, Revision


KB_VISITED_DEFAULT_TTL = 60 * 60 * 24


def active_contributors(from_date, to_date=None, locale=None, product=None):
"""Return active KB contributors for the specified parameters.
Expand Down Expand Up @@ -290,3 +294,54 @@ def build_topics_data(request: HttpRequest, product: Product, topics: list[Topic
topics_data.append(topic_data)

return topics_data


def clean_kb_visited(session, ttl=KB_VISITED_DEFAULT_TTL):
"""
Within the given session, remove expired KB-visited slugs.
"""
if not (session and ((kb_visited := session.get("kb-visited")) is not None)):
return None

now = time.time()

# Remove any slugs that have expired.
if expired_slugs := [slug for slug, ts in kb_visited.items() if (now - ts) > ttl]:
for slug in expired_slugs:
del kb_visited[slug]
session.modified = True

return kb_visited


def update_kb_visited(session, doc, ttl=KB_VISITED_DEFAULT_TTL):
"""
Update the "kb_visited" dictionary within the given session.
"""
if (kb_visited := clean_kb_visited(session, ttl=ttl)) is None:
return

# Note that we have visited the given KB article.
kb_visited[doc.original.slug] = time.time()
session.modified = True


def has_visited_kb(session, product, topic=None, ttl=KB_VISITED_DEFAULT_TTL):
"""
Does the given session indicate that the user has visited at least
one KB article associated with the given product and topic.
"""
if (kb_visited := clean_kb_visited(session, ttl=ttl)) is None:
return False

if not (visited_slugs := tuple(kb_visited.keys())):
return False

qs = Document.objects.filter(
locale=settings.WIKI_DEFAULT_LANGUAGE, slug__in=visited_slugs, products=product
)

if topic:
qs = qs.filter(topics=topic)

return qs.exists()
8 changes: 7 additions & 1 deletion kitsune/wiki/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@
send_contributor_notification,
send_reviewed_notification,
)
from kitsune.wiki.utils import get_visible_document_or_404, get_visible_revision_or_404
from kitsune.wiki.utils import (
get_visible_document_or_404,
get_visible_revision_or_404,
update_kb_visited,
)

log = logging.getLogger("k.wiki")

Expand Down Expand Up @@ -309,6 +313,8 @@ def maybe_vary_on_accept_language(response):
and doc.slug != "get-community-support"
)

update_kb_visited(request.session, doc)

data = {
"document": doc,
"is_first_revision": is_first_revision,
Expand Down

0 comments on commit 0f7dc59

Please sign in to comment.