Skip to content

Commit edf8ab1

Browse files
authored
Merge pull request #209 from alphagov/2449-add-source-filter-to-conversation
Add ability to filter questions in Admin UI by source
2 parents 57ec9ef + 76be755 commit edf8ab1

16 files changed

Lines changed: 201 additions & 28 deletions

app/controllers/admin/questions_controller.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def filter_params
2020
params.permit(
2121
:search,
2222
:status,
23+
:source,
2324
{ start_date_params: %i[day month year], end_date_params: %i[day month year] },
2425
:answer_feedback_useful,
2526
:question_routing_label,

app/controllers/api/v0/conversations_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class Api::V0::ConversationsController < ApplicationController
44
before_action :find_conversation, only: %i[show update answer answer_feedback]
55

66
def create
7-
conversation = Conversation.new(signon_user: current_user)
7+
conversation = Conversation.new(signon_user: current_user, source: :api)
88
form = Form::CreateQuestion.new(question_params.merge(conversation:))
99

1010
if form.valid?
@@ -70,7 +70,7 @@ def answer_feedback
7070
def find_conversation
7171
@conversation = Conversation
7272
.includes(questions: { answer: %i[sources feedback] })
73-
.where(signon_user_id: current_user.id)
73+
.where(signon_user_id: current_user.id, source: :api)
7474
.find(params[:conversation_id])
7575
end
7676

app/controllers/conversations_controller.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ def user_question_params
111111
def find_conversation
112112
return if cookies[:conversation_id].blank?
113113

114-
@conversation = Conversation.includes(:user).active.find_by!(id: cookies[:conversation_id], user: current_early_access_user)
114+
@conversation = Conversation.includes(:user)
115+
.active
116+
.find_by!(id: cookies[:conversation_id], user: current_early_access_user, source: :web)
115117
set_conversation_cookie(@conversation)
116118
rescue ActiveRecord::RecordNotFound
117119
cookies.delete(:conversation_id)

app/helpers/admin/questions_helper.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def question_show_summary_list_rows(question, answer, question_number, total_que
8585
end
8686

8787
signon_user = conversation.signon_user
88-
if signon_user.present?
88+
if signon_user.present? && conversation.source_api?
8989
rows << {
9090
field: "API user",
9191
value: safe_join([
@@ -97,6 +97,11 @@ def question_show_summary_list_rows(question, answer, question_number, total_que
9797
}
9898
end
9999

100+
rows << {
101+
field: "Source",
102+
value: conversation.source_api? ? "API" : conversation.source.humanize,
103+
}
104+
100105
rows << if answer.present?
101106
[
102107
{

app/models/admin/filters/questions_filter.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
class Admin::Filters::QuestionsFilter < Admin::Filters::BaseFilter
22
attribute :status
33
attribute :search
4+
attribute :source
45
attribute :start_date_params, default: {}
56
attribute :end_date_params, default: {}
67
attribute :conversation_id
@@ -30,6 +31,7 @@ def results
3031
.left_outer_joins(:answer)
3132
scope = search_scope(scope)
3233
scope = status_scope(scope)
34+
scope = source_scope(scope)
3335
scope = start_date_scope(scope)
3436
scope = end_date_scope(scope)
3537
scope = answer_feedback_useful_scope(scope)
@@ -93,6 +95,12 @@ def status_scope(scope)
9395
end
9496
end
9597

98+
def source_scope(scope)
99+
return scope if source.blank?
100+
101+
scope.joins(:conversation).where(conversations: { source: })
102+
end
103+
96104
def start_date_scope(scope)
97105
return scope if errors[:start_date_params].present? || start_date.nil?
98106

@@ -126,7 +134,7 @@ def user_scope(scope)
126134
def signon_user_scope(scope)
127135
return scope if signon_user_id.blank? || user_id.present?
128136

129-
scope.joins(:conversation).where(conversation: { signon_user_id: signon_user_id })
137+
scope.joins(:conversation).where(conversation: { signon_user_id: signon_user_id, source: :api })
130138
end
131139

132140
def question_routing_label_scope(scope)

app/models/conversation.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ class Conversation < ApplicationRecord
66

77
scope :active, -> { where(Question.active.where("questions.conversation_id = conversations.id").arel.exists) }
88

9+
enum :source,
10+
{
11+
api: "api",
12+
web: "web",
13+
},
14+
prefix: true
15+
916
def questions_for_showing_conversation
1017
Question.where(conversation: self)
1118
.includes(answer: %i[feedback sources])

app/views/admin/questions/_question_filters.html.erb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,30 @@
7474
end,
7575
} %>
7676

77+
<%= render "govuk_publishing_components/components/select", {
78+
id: "source",
79+
name: "source",
80+
label: "Source",
81+
heading_size: "s",
82+
full_width: true,
83+
options: [
84+
{
85+
text: "",
86+
value: "",
87+
},
88+
{
89+
text: "Web",
90+
value: "web",
91+
selected: params[:source] == "web",
92+
},
93+
{
94+
text: "API",
95+
value: "api",
96+
selected: params[:source] == "api",
97+
},
98+
],
99+
} %>
100+
77101
<%= render "govuk_publishing_components/components/fieldset", {
78102
legend_text: "Start date",
79103
heading_size: "s",
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class AddSourceToConversations < ActiveRecord::Migration[8.0]
2+
def change
3+
create_enum "conversation_source", %w[web api]
4+
add_column :conversations, :source, :conversation_source, null: false, default: "web"
5+
end
6+
end

db/schema.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[8.0].define(version: 2025_05_01_150559) do
13+
ActiveRecord::Schema[8.0].define(version: 2025_05_06_133220) do
1414
# These are extensions that must be enabled in order to support this database
1515
enable_extension "citext"
1616
enable_extension "pg_catalog.plpgsql"
@@ -19,6 +19,7 @@
1919
# Custom types defined in this database.
2020
# Note that some types may not work with other database engines. Be careful if changing database.
2121
create_enum "answer_status", ["answered", "banned", "clarification", "error_answer_guardrails", "error_answer_service_error", "error_context_length_exceeded", "error_jailbreak_guardrails", "error_non_specific", "error_question_routing_guardrails", "error_timeout", "guardrails_answer", "guardrails_forbidden_terms", "guardrails_jailbreak", "guardrails_question_routing", "unanswerable_llm_cannot_answer", "unanswerable_no_govuk_content", "unanswerable_question_routing"]
22+
create_enum "conversation_source", ["web", "api"]
2223
create_enum "deleted_early_access_user_deletion_type", ["unsubscribe", "admin"]
2324
create_enum "deleted_waiting_list_user_deletion_type", ["unsubscribe", "admin", "promotion"]
2425
create_enum "early_access_user_source", ["admin_added", "instant_signup", "admin_promoted", "delayed_signup"]
@@ -97,6 +98,7 @@
9798
t.datetime "updated_at", null: false
9899
t.uuid "early_access_user_id"
99100
t.uuid "signon_user_id"
101+
t.enum "source", default: "web", null: false, enum_type: "conversation_source"
100102
t.index ["created_at"], name: "index_conversations_on_created_at"
101103
t.index ["early_access_user_id"], name: "index_conversations_on_early_access_user_id"
102104
t.index ["signon_user_id"], name: "index_conversations_on_signon_user_id"

spec/factories/conversation_factory.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,9 @@
3434
create(:question, :with_answer, conversation:)
3535
end
3636
end
37+
38+
trait :api do
39+
source { :api }
40+
end
3741
end
3842
end

0 commit comments

Comments
 (0)