From 4467b51d8685da4f215c3e40cabc1dfa69e1244d Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Fri, 1 Dec 2023 13:00:37 +0100 Subject: [PATCH 1/6] Add checked_by and tagged_by columns to codetagssamples --- app/models/training_data/code_tags_sample.rb | 2 ++ .../20231201115848_add_tagged_by_to_samples.rb | 7 +++++++ .../20231201115917_add_checked_by_to_samples.rb | 7 +++++++ db/schema.rb | 14 ++++++++++++-- 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20231201115848_add_tagged_by_to_samples.rb create mode 100644 db/migrate/20231201115917_add_checked_by_to_samples.rb diff --git a/app/models/training_data/code_tags_sample.rb b/app/models/training_data/code_tags_sample.rb index 91790c4060..9c4df11b22 100644 --- a/app/models/training_data/code_tags_sample.rb +++ b/app/models/training_data/code_tags_sample.rb @@ -9,6 +9,8 @@ class TrainingData::CodeTagsSample < ApplicationRecord belongs_to :exercise, optional: true belongs_to :solution, optional: true belongs_to :locked_by, class_name: "User", optional: true + belongs_to :tagged_by, class_name: "User", optional: true + belongs_to :checked_by, class_name: "User", optional: true enum dataset: { training: 0, validation: 1 } enum status: { untagged: 0, machine_tagged: 1, human_tagged: 2, community_checked: 3, admin_checked: 4 } diff --git a/db/migrate/20231201115848_add_tagged_by_to_samples.rb b/db/migrate/20231201115848_add_tagged_by_to_samples.rb new file mode 100644 index 0000000000..6d490907f5 --- /dev/null +++ b/db/migrate/20231201115848_add_tagged_by_to_samples.rb @@ -0,0 +1,7 @@ +class AddTaggedByToSamples < ActiveRecord::Migration[7.0] + def change + return if Rails.env.production? + + add_column :training_data_code_tags_samples, :tagged_by_id, :bigint, null: true + end +end diff --git a/db/migrate/20231201115917_add_checked_by_to_samples.rb b/db/migrate/20231201115917_add_checked_by_to_samples.rb new file mode 100644 index 0000000000..1f4b8812e5 --- /dev/null +++ b/db/migrate/20231201115917_add_checked_by_to_samples.rb @@ -0,0 +1,7 @@ +class AddCheckedByToSamples < ActiveRecord::Migration[7.0] + def change + return if Rails.env.production? + + add_column :training_data_code_tags_samples, :checked_by_id, :bigint, null: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 8bb4887509..73a45ab51d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1,4 +1,4 @@ - # This file is auto-generated from the current state of the database. Instead +# This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_11_24_100011) do +ActiveRecord::Schema[7.0].define(version: 2023_12_01_115917) do create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false @@ -817,6 +817,14 @@ t.index ["user_id"], name: "index_scratchpad_pages_on_user_id" end + create_table "site_tags", charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t| + t.string "tag", null: false + t.string "description", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["tag"], name: "index_site_tags_on_tag", unique: true + end + create_table "site_updates", charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t| t.string "type", null: false t.string "uniqueness_key", null: false @@ -1206,6 +1214,8 @@ t.datetime "locked_until" t.bigint "locked_by_id" t.text "llm_tags" + t.bigint "tagged_by_id" + t.bigint "checked_by_id" t.index ["exercise_id"], name: "index_training_data_code_tags_samples_on_exercise_id" t.index ["solution_id"], name: "index_training_data_code_tags_samples_on_solution_id" t.index ["track_id"], name: "index_training_data_code_tags_samples_on_track_id" From f21f8739d8854ea1bbfeb76d97c92a952138b696 Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Fri, 1 Dec 2023 14:01:04 +0100 Subject: [PATCH 2/6] Add two new fields --- .../code_tags_sample/update_tags.rb | 20 +++++++++- app/models/training_data/code_tags_sample.rb | 4 +- ...848_add_community_checked_by_to_samples.rb | 7 ++++ ...20231201115848_add_tagged_by_to_samples.rb | 7 ---- ...1115917_add_admin_checked_by_to_samples.rb | 7 ++++ ...0231201115917_add_checked_by_to_samples.rb | 7 ---- db/schema.rb | 8 +++- .../code_tags_sample/update_tags_test.rb | 37 +++++++++++++++++++ 8 files changed, 78 insertions(+), 19 deletions(-) create mode 100644 db/migrate/20231201115848_add_community_checked_by_to_samples.rb delete mode 100644 db/migrate/20231201115848_add_tagged_by_to_samples.rb create mode 100644 db/migrate/20231201115917_add_admin_checked_by_to_samples.rb delete mode 100644 db/migrate/20231201115917_add_checked_by_to_samples.rb diff --git a/app/commands/training_data/code_tags_sample/update_tags.rb b/app/commands/training_data/code_tags_sample/update_tags.rb index 8cd703ce4c..a19f889428 100644 --- a/app/commands/training_data/code_tags_sample/update_tags.rb +++ b/app/commands/training_data/code_tags_sample/update_tags.rb @@ -6,8 +6,26 @@ class TrainingData::CodeTagsSample::UpdateTags def call sample.with_lock do sample.lock_for_editing!(user) - sample.update!(tags: TrainingData::CodeTagsSample::NormalizeTags.(tags), status:) + sample.update!( + tags: TrainingData::CodeTagsSample::NormalizeTags.(tags), + status:, + community_checked_by:, + admin_checked_by: + ) sample.unlock! end end + + private + def community_checked_by + return user if sample.status == :machine_tagged || sample.status == :human_tagged + + sample.community_checked_by + end + + def admin_checked_by + return user if sample.status == :community_checked + + sample.admin_checked_by + end end diff --git a/app/models/training_data/code_tags_sample.rb b/app/models/training_data/code_tags_sample.rb index 9c4df11b22..64fc28b3c8 100644 --- a/app/models/training_data/code_tags_sample.rb +++ b/app/models/training_data/code_tags_sample.rb @@ -9,8 +9,8 @@ class TrainingData::CodeTagsSample < ApplicationRecord belongs_to :exercise, optional: true belongs_to :solution, optional: true belongs_to :locked_by, class_name: "User", optional: true - belongs_to :tagged_by, class_name: "User", optional: true - belongs_to :checked_by, class_name: "User", optional: true + belongs_to :community_checked_by, class_name: "User", optional: true + belongs_to :admin_checked_by, class_name: "User", optional: true enum dataset: { training: 0, validation: 1 } enum status: { untagged: 0, machine_tagged: 1, human_tagged: 2, community_checked: 3, admin_checked: 4 } diff --git a/db/migrate/20231201115848_add_community_checked_by_to_samples.rb b/db/migrate/20231201115848_add_community_checked_by_to_samples.rb new file mode 100644 index 0000000000..97400de438 --- /dev/null +++ b/db/migrate/20231201115848_add_community_checked_by_to_samples.rb @@ -0,0 +1,7 @@ +class AddCommunityCheckedByToSamples < ActiveRecord::Migration[7.0] + def change + return if Rails.env.production? + + add_reference :training_data_code_tags_samples, :community_checked_by, null: true, foreign_key: { to_table: :users }, index: { name: "index_code_tags_samples_on_community_tagged_by_id" }, if_not_exists: true + end +end diff --git a/db/migrate/20231201115848_add_tagged_by_to_samples.rb b/db/migrate/20231201115848_add_tagged_by_to_samples.rb deleted file mode 100644 index 6d490907f5..0000000000 --- a/db/migrate/20231201115848_add_tagged_by_to_samples.rb +++ /dev/null @@ -1,7 +0,0 @@ -class AddTaggedByToSamples < ActiveRecord::Migration[7.0] - def change - return if Rails.env.production? - - add_column :training_data_code_tags_samples, :tagged_by_id, :bigint, null: true - end -end diff --git a/db/migrate/20231201115917_add_admin_checked_by_to_samples.rb b/db/migrate/20231201115917_add_admin_checked_by_to_samples.rb new file mode 100644 index 0000000000..106786455d --- /dev/null +++ b/db/migrate/20231201115917_add_admin_checked_by_to_samples.rb @@ -0,0 +1,7 @@ +class AddAdminCheckedByToSamples < ActiveRecord::Migration[7.0] + def change + return if Rails.env.production? + + add_reference :training_data_code_tags_samples, :admin_checked_by, null: true, foreign_key: { to_table: :users }, index: { name: "index_code_tags_samples_on_admin_tagged_by_id" }, if_not_exists: true + end +end diff --git a/db/migrate/20231201115917_add_checked_by_to_samples.rb b/db/migrate/20231201115917_add_checked_by_to_samples.rb deleted file mode 100644 index 1f4b8812e5..0000000000 --- a/db/migrate/20231201115917_add_checked_by_to_samples.rb +++ /dev/null @@ -1,7 +0,0 @@ -class AddCheckedByToSamples < ActiveRecord::Migration[7.0] - def change - return if Rails.env.production? - - add_column :training_data_code_tags_samples, :checked_by_id, :bigint, null: true - end -end diff --git a/db/schema.rb b/db/schema.rb index 73a45ab51d..d4902a8293 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1214,8 +1214,10 @@ t.datetime "locked_until" t.bigint "locked_by_id" t.text "llm_tags" - t.bigint "tagged_by_id" - t.bigint "checked_by_id" + t.bigint "community_checked_by_id" + t.bigint "admin_checked_by_id" + t.index ["admin_checked_by_id"], name: "index_code_tags_samples_on_admin_tagged_by_id" + t.index ["community_checked_by_id"], name: "index_code_tags_samples_on_community_tagged_by_id" t.index ["exercise_id"], name: "index_training_data_code_tags_samples_on_exercise_id" t.index ["solution_id"], name: "index_training_data_code_tags_samples_on_solution_id" t.index ["track_id"], name: "index_training_data_code_tags_samples_on_track_id" @@ -1694,6 +1696,8 @@ add_foreign_key "track_concept_contributorships", "track_concepts" add_foreign_key "track_concept_contributorships", "users" add_foreign_key "track_concepts", "tracks" + add_foreign_key "training_data_code_tags_samples", "users", column: "admin_checked_by_id" + add_foreign_key "training_data_code_tags_samples", "users", column: "community_checked_by_id" add_foreign_key "user_acquired_badges", "badges" add_foreign_key "user_acquired_badges", "users" add_foreign_key "user_activities", "exercises" diff --git a/test/commands/training_data/code_tags_sample/update_tags_test.rb b/test/commands/training_data/code_tags_sample/update_tags_test.rb index 195c0206e2..6304faa141 100644 --- a/test/commands/training_data/code_tags_sample/update_tags_test.rb +++ b/test/commands/training_data/code_tags_sample/update_tags_test.rb @@ -82,4 +82,41 @@ class TrainingData::CodeTagsSample::UpdateTagsTest < ActiveSupport::TestCase refute_nil sample.locked_until assert_equal lock_user, sample.locked_by end + + %i[machine_tagged human_tagged].each do |status| + test "sets community_checked_by to user when current status is #{status}" do + user = create :user + tags = ['construct:if'] + sample = create(:training_data_code_tags_sample, status:, locked_by: nil, locked_until: nil) + + TrainingData::CodeTagsSample::UpdateTags.(sample, tags, :human_tagged, user) + + assert_equal user, sample.community_checked_by + assert_nil sample.admin_checked_by + end + end + + test "sets admin_checked_by to user when current status is community_checked" do + user = create :user + community_checked_by_user = create :user + tags = ['construct:if'] + sample = create(:training_data_code_tags_sample, status: :community_checked, community_checked_by: community_checked_by_user, + locked_by: nil, locked_until: nil) + + TrainingData::CodeTagsSample::UpdateTags.(sample, tags, :admin_checked, user) + + assert_equal user, sample.admin_checked_by + end + + test "does not change community_checked_by user when setting admin_checked_by user" do + user = create :user + community_checked_by_user = create :user + tags = ['construct:if'] + sample = create(:training_data_code_tags_sample, status: :community_checked, community_checked_by: community_checked_by_user, + locked_by: nil, locked_until: nil) + + TrainingData::CodeTagsSample::UpdateTags.(sample, tags, :admin_checked, user) + + assert_equal community_checked_by_user, sample.community_checked_by + end end From 0fa758a8ea5d8e3c54136a5bbe99ee2579b8dce8 Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Fri, 1 Dec 2023 14:08:04 +0100 Subject: [PATCH 3/6] Fix bug --- app/commands/training_data/code_tags_sample/update_tags.rb | 4 ++-- .../training_data/code_tags_sample/update_tags_test.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/commands/training_data/code_tags_sample/update_tags.rb b/app/commands/training_data/code_tags_sample/update_tags.rb index a19f889428..bb457e0999 100644 --- a/app/commands/training_data/code_tags_sample/update_tags.rb +++ b/app/commands/training_data/code_tags_sample/update_tags.rb @@ -18,13 +18,13 @@ def call private def community_checked_by - return user if sample.status == :machine_tagged || sample.status == :human_tagged + return user if status == :community_checked sample.community_checked_by end def admin_checked_by - return user if sample.status == :community_checked + return user if status == :admin_checked sample.admin_checked_by end diff --git a/test/commands/training_data/code_tags_sample/update_tags_test.rb b/test/commands/training_data/code_tags_sample/update_tags_test.rb index 6304faa141..f851748f2b 100644 --- a/test/commands/training_data/code_tags_sample/update_tags_test.rb +++ b/test/commands/training_data/code_tags_sample/update_tags_test.rb @@ -89,7 +89,7 @@ class TrainingData::CodeTagsSample::UpdateTagsTest < ActiveSupport::TestCase tags = ['construct:if'] sample = create(:training_data_code_tags_sample, status:, locked_by: nil, locked_until: nil) - TrainingData::CodeTagsSample::UpdateTags.(sample, tags, :human_tagged, user) + TrainingData::CodeTagsSample::UpdateTags.(sample, tags, :community_checked, user) assert_equal user, sample.community_checked_by assert_nil sample.admin_checked_by From 56ce1eaa0bf621d457e50005a458a925a3831c01 Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Fri, 1 Dec 2023 14:08:38 +0100 Subject: [PATCH 4/6] Update db/schema.rb --- db/schema.rb | 8 -------- 1 file changed, 8 deletions(-) diff --git a/db/schema.rb b/db/schema.rb index d4902a8293..eace5bc432 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -817,14 +817,6 @@ t.index ["user_id"], name: "index_scratchpad_pages_on_user_id" end - create_table "site_tags", charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t| - t.string "tag", null: false - t.string "description", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.index ["tag"], name: "index_site_tags_on_tag", unique: true - end - create_table "site_updates", charset: "utf8mb4", collation: "utf8mb4_unicode_ci", force: :cascade do |t| t.string "type", null: false t.string "uniqueness_key", null: false From e233a1cfaddf9574d32e1cc8d8fcb13454bb3e8f Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Fri, 1 Dec 2023 14:09:46 +0100 Subject: [PATCH 5/6] Fix typoe --- .../20231201115848_add_community_checked_by_to_samples.rb | 2 +- db/migrate/20231201115917_add_admin_checked_by_to_samples.rb | 2 +- db/schema.rb | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/db/migrate/20231201115848_add_community_checked_by_to_samples.rb b/db/migrate/20231201115848_add_community_checked_by_to_samples.rb index 97400de438..f03430d2e3 100644 --- a/db/migrate/20231201115848_add_community_checked_by_to_samples.rb +++ b/db/migrate/20231201115848_add_community_checked_by_to_samples.rb @@ -2,6 +2,6 @@ class AddCommunityCheckedByToSamples < ActiveRecord::Migration[7.0] def change return if Rails.env.production? - add_reference :training_data_code_tags_samples, :community_checked_by, null: true, foreign_key: { to_table: :users }, index: { name: "index_code_tags_samples_on_community_tagged_by_id" }, if_not_exists: true + add_reference :training_data_code_tags_samples, :community_checked_by, null: true, foreign_key: { to_table: :users }, index: { name: "index_code_tags_samples_on_community_checked_by_id" }, if_not_exists: true end end diff --git a/db/migrate/20231201115917_add_admin_checked_by_to_samples.rb b/db/migrate/20231201115917_add_admin_checked_by_to_samples.rb index 106786455d..65216609d4 100644 --- a/db/migrate/20231201115917_add_admin_checked_by_to_samples.rb +++ b/db/migrate/20231201115917_add_admin_checked_by_to_samples.rb @@ -2,6 +2,6 @@ class AddAdminCheckedByToSamples < ActiveRecord::Migration[7.0] def change return if Rails.env.production? - add_reference :training_data_code_tags_samples, :admin_checked_by, null: true, foreign_key: { to_table: :users }, index: { name: "index_code_tags_samples_on_admin_tagged_by_id" }, if_not_exists: true + add_reference :training_data_code_tags_samples, :admin_checked_by, null: true, foreign_key: { to_table: :users }, index: { name: "index_code_tags_samples_on_admin_checked_by_id" }, if_not_exists: true end end diff --git a/db/schema.rb b/db/schema.rb index eace5bc432..81b6bb510e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1208,8 +1208,8 @@ t.text "llm_tags" t.bigint "community_checked_by_id" t.bigint "admin_checked_by_id" - t.index ["admin_checked_by_id"], name: "index_code_tags_samples_on_admin_tagged_by_id" - t.index ["community_checked_by_id"], name: "index_code_tags_samples_on_community_tagged_by_id" + t.index ["admin_checked_by_id"], name: "index_code_tags_samples_on_admin_checked_by_id" + t.index ["community_checked_by_id"], name: "index_code_tags_samples_on_community_checked_by_id" t.index ["exercise_id"], name: "index_training_data_code_tags_samples_on_exercise_id" t.index ["solution_id"], name: "index_training_data_code_tags_samples_on_solution_id" t.index ["track_id"], name: "index_training_data_code_tags_samples_on_track_id" From 3655e0a9fb509e9a7c4694219d1e99f7a0f5a4fd Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Fri, 1 Dec 2023 14:12:38 +0100 Subject: [PATCH 6/6] Improve test naming --- .../code_tags_sample/update_tags_test.rb | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/test/commands/training_data/code_tags_sample/update_tags_test.rb b/test/commands/training_data/code_tags_sample/update_tags_test.rb index f851748f2b..47fd2e2e7a 100644 --- a/test/commands/training_data/code_tags_sample/update_tags_test.rb +++ b/test/commands/training_data/code_tags_sample/update_tags_test.rb @@ -83,20 +83,18 @@ class TrainingData::CodeTagsSample::UpdateTagsTest < ActiveSupport::TestCase assert_equal lock_user, sample.locked_by end - %i[machine_tagged human_tagged].each do |status| - test "sets community_checked_by to user when current status is #{status}" do - user = create :user - tags = ['construct:if'] - sample = create(:training_data_code_tags_sample, status:, locked_by: nil, locked_until: nil) + test "sets community_checked_by to user when new status is community_checked" do + user = create :user + tags = ['construct:if'] + sample = create(:training_data_code_tags_sample, status: :machine_tagged, locked_by: nil, locked_until: nil) - TrainingData::CodeTagsSample::UpdateTags.(sample, tags, :community_checked, user) + TrainingData::CodeTagsSample::UpdateTags.(sample, tags, :community_checked, user) - assert_equal user, sample.community_checked_by - assert_nil sample.admin_checked_by - end + assert_equal user, sample.community_checked_by + assert_nil sample.admin_checked_by end - test "sets admin_checked_by to user when current status is community_checked" do + test "sets admin_checked_by to user when new status is admin_checked" do user = create :user community_checked_by_user = create :user tags = ['construct:if']