Skip to content

Commit

Permalink
Revert "record failed deflections (#6511)" (#6521)
Browse files Browse the repository at this point in the history
This reverts commit 21ba567.
  • Loading branch information
escattone authored Feb 21, 2025
1 parent 9f1e77d commit f2644eb
Show file tree
Hide file tree
Showing 10 changed files with 7 additions and 693 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ <h1 class="sumo-page-heading">{% block headline %}{% endblock %}</h1>
</label>
{{ field }}
{{ field.errors }}
{% elif category_field_attrs and (field.name == 'category') %}
{{ field.as_widget(attrs=category_field_attrs)|safe }}
{% else %}
{{ field|safe }}
{% endif %}
Expand Down
3 changes: 1 addition & 2 deletions kitsune/questions/jinja2/questions/new_question.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
{% set meta = [('robots', 'noindex')] %}

{% set classes = 'new-question' %}
{% set category_field_attrs = {"hx-get": "", "hx-swap": "none"}%}

{% block breadcrumbs %}{% endblock %}

Expand Down Expand Up @@ -40,4 +39,4 @@ <h2 class="sumo-page-heading">
{% block submit_button_value %}
{{ _('Submit') }}
{% endblock %}
{% block submit_button_attrs %}data-event-name="question_submit" data-event-parameters='{{ submit_event_parameters }}'{% endblock %}
{% block submit_button_attrs %}data-event-name="question_submit"{% endblock %}
24 changes: 1 addition & 23 deletions kitsune/questions/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import logging
import re
from datetime import datetime, timedelta
Expand Down Expand Up @@ -233,9 +232,7 @@ def clear_mutable_metadata(self):
This excludes immutable fields: user agent, product, and category.
"""
self.metadata_set.exclude(
name__in=["useragent", "product", "category", "kb_visits_prior"]
).delete()
self.metadata_set.exclude(name__in=["useragent", "product", "category"]).delete()
self._metadata = None

def remove_metadata(self, name):
Expand Down Expand Up @@ -405,25 +402,6 @@ def is_moderated(self):
.exists()
)

@cached_property
def created_after_failed_kb_deflection(self) -> bool:
"""
Returns a boolean indicating whether or not this question was created after its
creator had visited one or more KB articles with the same product and topic.
"""
return self.metadata_set.filter(name="kb_visits_prior").exists()

@cached_property
def kb_visits_prior_to_creation(self) -> list[str]:
"""
Returns the list of KB article URL's visited prior to the creation of this question.
"""
try:
metadata = self.metadata_set.filter(name="kb_visits_prior").get()
except QuestionMetaData.DoesNotExist:
return []
return json.loads(metadata.value)

@property
def my_tags(self):
"""A caching wrapper around self.tags.all()."""
Expand Down
20 changes: 1 addition & 19 deletions kitsune/questions/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
from datetime import datetime, timedelta
from unittest import mock

Expand Down Expand Up @@ -215,21 +214,14 @@ def test_clear_mutable_metadata(self):
category="fix-problems",
useragent="Fyerfocks",
crash_id="7",
kb_visits_prior='["/en-US/kb/stuff", "/en-US/kb/nonsense"]',
)

q.metadata
q.clear_mutable_metadata()
md = q.metadata
assert "crash_id" not in md, "clear_mutable_metadata() didn't clear the cached metadata."
self.assertEqual(
dict(
product="desktop",
category="fix-problems",
useragent="Fyerfocks",
kb_visits_prior='["/en-US/kb/stuff", "/en-US/kb/nonsense"]',
),
md,
dict(product="desktop", category="fix-problems", useragent="Fyerfocks"), md
)

def test_auto_tagging(self):
Expand Down Expand Up @@ -276,16 +268,6 @@ def test_has_beta(self):
assert _has_beta("11.0", {"11.0b7": "2011-06-01"})
assert not _has_beta("10.0", {"11.0b7": "2011-06-01"})

def test_kb_visits_prior(self):
visits = ["/en-US/kb/stuff", "/en-US/kb/nonsense"]
self.question.add_metadata(kb_visits_prior=json.dumps(visits))
self.assertTrue(self.question.created_after_failed_kb_deflection)
self.assertEqual(set(self.question.kb_visits_prior_to_creation), set(visits))

def test_no_kb_visits_prior(self):
self.assertFalse(self.question.created_after_failed_kb_deflection)
self.assertEqual(self.question.kb_visits_prior_to_creation, [])


class QuestionTests(TestCase):
"""Tests for Question model"""
Expand Down
25 changes: 1 addition & 24 deletions kitsune/questions/utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import json
import logging
import re
from typing import Optional

from django.conf import settings
from django.contrib.sessions.backends.base import SessionBase

from kitsune.products.models import Product, Topic
from kitsune.questions.models import Answer, Question
from kitsune.wiki.utils import get_featured_articles as kb_get_featured_articles, has_visited_kb
from kitsune.wiki.utils import get_featured_articles as kb_get_featured_articles


REGEX_NON_WINDOWS_HOME_DIR = re.compile(
Expand Down Expand Up @@ -119,22 +115,3 @@ def remove_pii(data: dict) -> None:
remove_pii(value)
elif isinstance(value, str):
data[key] = remove_home_dir_pii(value)


def get_ga_submit_event_parameters_as_json(
session: Optional[SessionBase] = None,
product: Optional[Product] = None,
topic: Optional[Topic] = None,
) -> str:
"""
Returns a JSON string of the event parameters for the GA4 "question_submit"
event, given the session, product, and/or topic.
"""
data = dict(is_failed_deflection="false")

if session and product:
data["is_failed_deflection"] = str(has_visited_kb(session, product, topic=topic)).lower()
if topic:
data["topics"] = f"/{topic.slug}/"

return json.dumps(data)
45 changes: 2 additions & 43 deletions kitsune/questions/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,7 @@
WatchQuestionForm,
)
from kitsune.questions.models import AAQConfig, Answer, AnswerVote, Question, QuestionVote
from kitsune.questions.utils import (
get_featured_articles,
get_ga_submit_event_parameters_as_json,
get_mobile_product_from_ua,
)
from kitsune.questions.utils import get_featured_articles, get_mobile_product_from_ua
from kitsune.sumo.decorators import ratelimit
from kitsune.sumo.i18n import split_into_language_and_path
from kitsune.sumo.templatetags.jinja_helpers import urlparams
Expand All @@ -74,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, get_kb_visited
from kitsune.wiki.utils import build_topics_data

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

Expand Down Expand Up @@ -607,9 +603,6 @@ def aaq(request, product_slug=None, step=1, is_loginless=False):
user=request.user,
)
context["form"] = zendesk_form
context["submit_event_parameters"] = get_ga_submit_event_parameters_as_json(
request.session, product
)

if zendesk_form.is_valid() and not is_ratelimited(request, "loginless", "3/d"):
try:
Expand Down Expand Up @@ -655,9 +648,6 @@ def aaq(request, product_slug=None, step=1, is_loginless=False):
product=product,
)

if visits := get_kb_visited(request.session, product, question.topic):
question.add_metadata(kb_visits_prior=json.dumps(visits))

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

Expand Down Expand Up @@ -690,17 +680,6 @@ def aaq(request, product_slug=None, step=1, is_loginless=False):
content_type=user_ct,
).order_by("-id")[:IMG_LIMIT]

if form.is_bound and (topic := form.cleaned_data.get("category")):
# We've got invalid POST data, but the topic has been provided.
# Let's set the proper GA4 event parameters on the submit button.
context["submit_event_parameters"] = get_ga_submit_event_parameters_as_json(
request.session, product, topic=topic
)
else:
# We don't know the topic yet, since that's set via the form, so let's
# start by providing default GA4 event parameters for the submit button.
context["submit_event_parameters"] = get_ga_submit_event_parameters_as_json()

return render(request, template, context)


Expand All @@ -712,26 +691,6 @@ def aaq_step2(request, product_slug):
def aaq_step3(request, product_slug):
"""Step 3: Show full question form."""

# This view can be called by htmx to set the GA4 event parameters on the submit
# button of the new-question form whenever the category (topic) menu is changed.
if ("hx-request" in request.headers) and (
request.headers.get("hx-trigger-name") == "category"
):
topic_id = request.GET.get("category")
product = get_object_or_404(Product.active, slug=product_slug)
topic = get_object_or_404(Topic.active.filter(products=product, in_aaq=True), pk=topic_id)
response = HttpResponse(status=204)
response["HX-Trigger"] = json.dumps(
{
"setQuestionSubmitEventParameters": {
"eventParameters": get_ga_submit_event_parameters_as_json(
request.session, product, topic=topic
)
}
}
)
return response

# Since removing the @login_required decorator for MA form
# need to catch unauthenticated, non-MA users here """
referer = request.META.get("HTTP_REFERER", "")
Expand Down
12 changes: 0 additions & 12 deletions kitsune/sumo/static/sumo/js/questions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,6 @@ import AAQSystemInfo from "sumo/js/aaq";
* Scripts for the questions app.
*/

document.addEventListener("DOMContentLoaded", async () => {
const body = document.querySelector("body.new-question");
if (body) {
const submitButton = body.querySelector('#question-form button[type="submit"]');
if (submitButton) {
body.addEventListener('setQuestionSubmitEventParameters', (event) => {
submitButton.dataset.eventParameters = event.detail.eventParameters;
});
}
}
});

// TODO: Figure out how to break out the functionality here into
// testable parts.

Expand Down
Loading

0 comments on commit f2644eb

Please sign in to comment.