Skip to content

Commit 141e40b

Browse files
committed
Admin: classification runs page
1 parent 2847608 commit 141e40b

6 files changed

Lines changed: 273 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
class Admin::ClassificationRunsController < Admin::BaseController
4+
PER_PAGE = 50
5+
SORTS = { "duration" => "duration_s DESC NULLS LAST, id DESC", "tokens" => "input_tokens DESC NULLS LAST, id DESC" }.freeze
6+
REQUEUEABLE = %w[failed timeout].freeze
7+
8+
def active_admin_section
9+
:classification_runs
10+
end
11+
12+
def index
13+
@sort = SORTS.key?(params[:sort]) ? params[:sort] : nil
14+
@status = ClassificationRun::STATUSES.include?(params[:status]) ? params[:status] : nil
15+
@phase = ClassificationRun::PHASES.include?(params[:phase]) ? params[:phase] : nil
16+
@kind = Classify::KIND_TAGS.include?(params[:kind]) ? params[:kind] : nil
17+
18+
runs = ClassificationRun.includes(:topic, :feature_group)
19+
runs = runs.where(status: @status) if @status
20+
runs = runs.where(phase: @phase) if @phase
21+
runs = runs.where(kind: @kind) if @kind
22+
@runs = runs.order(@sort ? Arel.sql(SORTS[@sort]) : { created_at: :desc }).page(params[:page]).per(PER_PAGE)
23+
24+
@status_counts = ClassificationRun.group(:status).count
25+
@kind_counts = ClassificationRun.done.group(:kind).count
26+
@queue_counts = Classify::Queue.counts
27+
@totals = ClassificationRun.pick(Arel.sql("sum(input_tokens), sum(cached_input_tokens), sum(output_tokens), avg(duration_s)"))
28+
end
29+
30+
def show
31+
@run = ClassificationRun.includes(:topic, :feature_group).find(params[:id])
32+
result = @run.result || {}
33+
@linked = Topic.where(id: Array(result["join_linked"])).index_by(&:id)
34+
end
35+
36+
def requeue
37+
run = ClassificationRun.find(params[:id])
38+
unless REQUEUEABLE.include?(run.status)
39+
return redirect_to admin_classification_run_path(run), alert: "Only failed or timed out runs can be requeued"
40+
end
41+
42+
run.destroy!
43+
redirect_to admin_classification_runs_path, notice: "Run #{run.id} removed, thread re-queued"
44+
end
45+
end
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
- content_for :title, "Admin - Classification runs"
2+
- filters = { status: @status, phase: @phase, kind: @kind, sort: @sort }.compact
3+
4+
.settings-page
5+
h1 Classification runs
6+
p
7+
- @queue_counts.each do |phase, c|
8+
span.commit-sidebar-meta = "#{phase}: #{c[:remaining]} remaining, #{c[:claimed]} claimed, #{c[:done]} done "
9+
p
10+
- @status_counts.sort.each do |status, n|
11+
span.commit-sidebar-meta = "#{status}: #{n} "
12+
p
13+
- @kind_counts.sort_by { |k, n| [ -n, k.to_s ] }.each do |kind, n|
14+
span.commit-sidebar-meta = "#{kind || '-'}: #{n} "
15+
- if @totals&.first
16+
p.commit-sidebar-meta
17+
= "Tokens in/cached/out: #{@totals[0]}/#{@totals[1]}/#{@totals[2]}, average #{@totals[3].to_f.round(1)}s per run"
18+
p
19+
= link_to "newest", admin_classification_runs_path(filters.except(:sort))
20+
| /
21+
= link_to "slowest", admin_classification_runs_path(filters.merge(sort: "duration"))
22+
| /
23+
= link_to "most tokens", admin_classification_runs_path(filters.merge(sort: "tokens"))
24+
p
25+
| Status:
26+
- ClassificationRun::STATUSES.each do |s|
27+
= " "
28+
= link_to s, admin_classification_runs_path(filters.merge(status: s))
29+
| / Phase:
30+
- ClassificationRun::PHASES.each do |ph|
31+
= " "
32+
= link_to ph, admin_classification_runs_path(filters.merge(phase: ph))
33+
| / Kind:
34+
- Classify::KIND_TAGS.each do |k|
35+
= " "
36+
= link_to k, admin_classification_runs_path(filters.merge(kind: k))
37+
- if filters.except(:sort).any?
38+
| /
39+
= link_to "clear", admin_classification_runs_path(filters.slice(:sort))
40+
41+
table.email-table
42+
thead
43+
tr
44+
th Run
45+
th Thread
46+
th Phase
47+
th Status
48+
th Result
49+
th Group
50+
th Model
51+
th Worker
52+
th Duration
53+
th Tokens in/cached/out
54+
tbody
55+
- @runs.each do |run|
56+
tr
57+
td = link_to run.id, admin_classification_run_path(run)
58+
td = link_to run.topic.title, topic_path(run.topic)
59+
td = run.phase
60+
td = run.failure_kind.present? ? "#{run.status} (#{run.failure_kind})" : run.status
61+
td = [ run.kind, run.size, ("enhancement" if run.enhancement), ("not-a-bug" if run.not_a_bug) ].compact.join(" ")
62+
td
63+
- if run.feature_group
64+
= link_to run.feature_group.name, admin_feature_group_path(run.feature_group)
65+
td = [ run.model, run.effort ].compact.join(" / ")
66+
td = run.worker
67+
td = run.duration_s ? "#{run.duration_s.round}s" : ""
68+
td = "#{run.input_tokens}/#{run.cached_input_tokens}/#{run.output_tokens}"
69+
= paginate @runs
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
- content_for :title, "Admin - Classification run #{@run.id}"
2+
- result = @run.result || {}
3+
- answer = result["group"] || {}
4+
5+
.settings-page
6+
h1 = "Run #{@run.id}: #{@run.topic.title}"
7+
p
8+
= link_to "open thread", topic_path(@run.topic)
9+
| /
10+
= link_to "all runs", admin_classification_runs_path
11+
table.email-table
12+
tr
13+
th Status
14+
td = @run.failure_kind.present? ? "#{@run.status} (#{@run.failure_kind})" : @run.status
15+
tr
16+
th Phase / worker
17+
td = "#{@run.phase} / #{@run.worker}"
18+
tr
19+
th Model / effort / prompt
20+
td = "#{@run.model} / #{@run.effort} / #{@run.prompt_version}"
21+
tr
22+
th Claimed / finished
23+
td = [ @run.claimed_at, @run.finished_at ].map { |t| t ? absolute_time_display(t) : "-" }.join(" / ")
24+
tr
25+
th Messages seen
26+
td = @run.message_count
27+
tr
28+
th Duration
29+
td = @run.duration_s ? "#{@run.duration_s.round}s" : ""
30+
tr
31+
th Tokens in/cached/out
32+
td = "#{@run.input_tokens}/#{@run.cached_input_tokens}/#{@run.output_tokens}"
33+
- if @run.error.present?
34+
tr
35+
th Error
36+
td
37+
pre = @run.error
38+
- if Admin::ClassificationRunsController::REQUEUEABLE.include?(@run.status)
39+
= button_to "Requeue", requeue_admin_classification_run_path(@run), method: :post, class: "button-secondary button-small", form: { data: { turbo_confirm: "Delete this run so the thread is picked up again?" } }
40+
41+
- if result.any?
42+
h2 Answer
43+
table.email-table
44+
tr
45+
th Kind / size
46+
td = [ result["kind"], result["size"] ].compact.join(" / ")
47+
tr
48+
th Enhancement / not a bug
49+
td = "#{result['enhancement'].inspect} / #{result['not_a_bug'].inspect}"
50+
tr
51+
th Feature name
52+
td = result["feature_name"]
53+
tr
54+
th Group
55+
td
56+
- if @run.feature_group
57+
= link_to @run.feature_group.name, admin_feature_group_path(@run.feature_group)
58+
- elsif answer["new_name"].present?
59+
= "proposed: #{answer['new_name']}"
60+
- else
61+
| none
62+
- if answer["new_summary"].present?
63+
tr
64+
th Proposed summary
65+
td = answer["new_summary"]
66+
tr
67+
th Joined linked threads
68+
td
69+
- ids = Array(result["join_linked"])
70+
- if ids.empty?
71+
| none
72+
- else
73+
- ids.each do |id|
74+
div
75+
- if (t = @linked[id])
76+
= link_to t.title, topic_path(t)
77+
- else
78+
= "topic #{id}"
79+
tr
80+
th Confidence
81+
td = result["confidence"]
82+
h2 Evidence
83+
ul
84+
- Array(result["evidence"]).each do |line|
85+
li = line

‎app/views/layouts/admin.html.slim‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,8 @@
5151
= link_to admin_patchset_runs_path, class: admin_nav_link_class(:patchset_runs) do
5252
i.fas.fa-project-diagram
5353
span Patchset Runs
54+
= link_to admin_classification_runs_path, class: admin_nav_link_class(:classification_runs) do
55+
i.fas.fa-tags
56+
span Classification Runs
5457

5558
= render template: 'layouts/application'

‎config/routes.rb‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
resources :patchset_runs, only: [ :index, :show ] do
66
post :requeue, on: :member
77
end
8+
resources :classification_runs, only: [ :index, :show ] do
9+
post :requeue, on: :member
10+
end
811
resources :users, only: [ :index ] do
912
member do
1013
post :toggle_admin
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
require "rails_helper"
2+
3+
RSpec.describe "Admin::ClassificationRuns", type: :request do
4+
let(:group) { create(:feature_group, name: "FOR PORTION OF") }
5+
let(:linked) { create(:topic, title: "Earlier temporal thread") }
6+
let(:result) do
7+
{ "kind" => "feature", "size" => "major", "enhancement" => false, "not_a_bug" => false,
8+
"group" => { "id" => group.id, "new_name" => nil, "new_summary" => nil }, "join_linked" => [ linked.id ],
9+
"feature_name" => "UPDATE/DELETE FOR PORTION OF", "evidence" => [ "[001] adds FOR PORTION OF" ], "confidence" => "high" }
10+
end
11+
12+
before { create(:release_tag, name: Classify::Scope::WINDOW_TAG, released_at: 3.years.ago) }
13+
14+
it "redirects non-admins" do
15+
sign_in_as(create(:user, admin: false))
16+
get admin_classification_runs_path
17+
expect(response).to redirect_to(root_path)
18+
end
19+
20+
describe "as admin" do
21+
before { sign_in_as(create(:user, admin: true)) }
22+
23+
let!(:done) do
24+
create(:classification_run, topic: create(:topic, title: "Temporal update"), phase: "backfill", kind: "feature", size: "major",
25+
enhancement: false, feature_group: group, model: "gpt-6.2-sol", effort: "medium",
26+
input_tokens: 4321, cached_input_tokens: 100, output_tokens: 50, duration_s: 12.0, result:)
27+
end
28+
let!(:failed) do
29+
create(:classification_run, topic: create(:topic, title: "Broken run"), status: "failed", failure_kind: "schema",
30+
error: "schema: missing kind", input_tokens: 10)
31+
end
32+
33+
it "lists runs with queue and status counts" do
34+
get admin_classification_runs_path(sort: "tokens")
35+
expect(response).to have_http_status(:ok)
36+
expect(response.body).to include("Temporal update", "Broken run", "4321", "FOR PORTION OF", "major", "schema")
37+
expect(response.body).to include("backfill:", "remaining", "done: 1", "failed: 1")
38+
expect(response.body.index("Temporal update")).to be < response.body.index("Broken run")
39+
end
40+
41+
it "filters by status" do
42+
get admin_classification_runs_path(status: "failed")
43+
expect(response.body).to include("Broken run")
44+
expect(response.body).not_to include("Temporal update")
45+
end
46+
47+
it "shows the answer of a run" do
48+
get admin_classification_run_path(done)
49+
expect(response).to have_http_status(:ok)
50+
expect(response.body).to include("UPDATE/DELETE FOR PORTION OF", "[001] adds FOR PORTION OF", "Earlier temporal thread", "gpt-6.2-sol / medium")
51+
expect(response.body).not_to include("Requeue")
52+
end
53+
54+
it "shows the error and requeues a failed run" do
55+
get admin_classification_run_path(failed)
56+
expect(response.body).to include("schema: missing kind", "Requeue")
57+
post requeue_admin_classification_run_path(failed)
58+
expect(ClassificationRun.exists?(failed.id)).to be(false)
59+
expect(flash[:notice]).to be_present
60+
end
61+
62+
it "does not requeue a done run" do
63+
post requeue_admin_classification_run_path(done)
64+
expect(ClassificationRun.exists?(done.id)).to be(true)
65+
expect(flash[:alert]).to be_present
66+
end
67+
end
68+
end

0 commit comments

Comments
 (0)