From c9a0edd01f5e07660f7ef34e41b7f4438f9c86c5 Mon Sep 17 00:00:00 2001 From: Maxime Orefice Date: Thu, 13 Mar 2025 15:18:59 +0100 Subject: [PATCH 1/2] Add classification escalation --- notifier/.rubocop.yml | 2 +- notifier/spec/table_size_feedback_spec.rb | 14 +++--- notifier/spec/table_size_service_spec.rb | 23 +++++++++- notifier/table_size_service.rb | 55 +++++++++++++++++++++++ notifier/templates/table_sizes_table.erb | 2 +- 5 files changed, 86 insertions(+), 10 deletions(-) diff --git a/notifier/.rubocop.yml b/notifier/.rubocop.yml index 51789bd0..f47462ca 100644 --- a/notifier/.rubocop.yml +++ b/notifier/.rubocop.yml @@ -12,7 +12,7 @@ Performance/OpenStruct: Enabled: false RSpec/MultipleMemoizedHelpers: - Max: 12 + Max: 13 # This is due to a bug in RuboCop. # I filed an issue with the maintainer of RuboCop: https://github.com/rubocop/rubocop/issues/13458 diff --git a/notifier/spec/table_size_feedback_spec.rb b/notifier/spec/table_size_feedback_spec.rb index 9ccadc51..aadfe634 100644 --- a/notifier/spec/table_size_feedback_spec.rb +++ b/notifier/spec/table_size_feedback_spec.rb @@ -4,15 +4,9 @@ require 'spec_helper' RSpec.describe TableSizeFeedback do describe "#render_table_size_summary" do - before do - allow(Date).to receive(:current).and_return('2025-02-27') - end - - subject { described_class.new(data).render_table_size_summary } - + let(:fixed_date) { "2025-02-27" } let(:path) { 'spec/fixtures/table-sizes-output.json' } let(:data) { JSON.parse(File.read(path)).first } - let(:markdown_table) do <<~TEXT ## Summary @@ -58,6 +52,12 @@ RSpec.describe TableSizeFeedback do TEXT end + before do + allow(Time).to receive_message_chain(:now, :strftime).with("%Y-%m-%d").and_return(fixed_date) + end + + subject { described_class.new(data).render_table_size_summary } + it "returns a markdown table" do expect(subject).to eql markdown_table end diff --git a/notifier/spec/table_size_service_spec.rb b/notifier/spec/table_size_service_spec.rb index 65a86cd9..2faa8ae0 100644 --- a/notifier/spec/table_size_service_spec.rb +++ b/notifier/spec/table_size_service_spec.rb @@ -82,10 +82,14 @@ RSpec.describe TableSizeService do end context 'when issue already exists' do - let(:issue_double) { instance_double('issue', iid: 1, state: 'opened', labels: 'label') } + let(:issue_double) do + instance_double('issue', iid: 1, state: 'opened', labels: 'label', description: 'description') + end + let(:issues) { instance_double('issues', any?: true, first: issue_double) } let(:gitlab_response) { [issue_double] } let(:state_param) { { state: 'opened' } } + let(:note_response) { instance_double('note', noteable_iid: 1) } include_examples 'when making gitlab issues request', state: 'opened' @@ -97,6 +101,23 @@ RSpec.describe TableSizeService do ) execute end + + context 'when classification has escalated' do + before do + allow(service).to receive(:classification_escalated?).with(issue_double, anything).and_return(true) + allow(gitlab_client).to receive(:create_issue_note).and_return(note_response) + end + + it 'adds a note about classification escalation' do + expect(gitlab_client).to receive(:create_issue_note).with( + project_path, + issue_double.iid, + include('Table Size Classification Escalated') + ) + + execute + end + end end context 'when closed issue exists' do diff --git a/notifier/table_size_service.rb b/notifier/table_size_service.rb index 83fb6b82..81b86740 100644 --- a/notifier/table_size_service.rb +++ b/notifier/table_size_service.rb @@ -10,6 +10,12 @@ class TableSizeService large: 'severity::3', medium: 'severity::4' }.freeze + CLASSIFICATION_RANKING = { + medium: 1, + large: 2, + over_limit: 3 + }.freeze + CLASSIFICATION_PATTERN = /\|\s*`([^`]+)`\s*\|\s*$/.freeze Issue = Struct.new(:table_metadata) do def title @@ -43,6 +49,7 @@ class TableSizeService next if already_closed?(gitlab_issue.first) response = edit_issue(gitlab_issue.first, issue.description, table_metadata) + add_note(gitlab_issue.first, table_metadata) if classification_escalated?(gitlab_issue.first, table_metadata) puts "Issue updated successfully for #{table_metadata['table_name']} " \ "at https://gitlab.com/gitlab-org/gitlab/-/issues/#{response.iid}\n" @@ -114,4 +121,52 @@ class TableSizeService def search_identifier(identifier) OpenSSL::Digest::SHA256.hexdigest(identifier) end + + def extract_classification_from_description(description) + return nil unless description + return unless match = description.match(CLASSIFICATION_PATTERN) + + match[1] + end + + def classification_escalated?(issue, table_metadata) + old_classification = extract_classification_from_description(issue.description) + new_classification = table_metadata['classification'] + + return false unless old_classification && new_classification + + old_rank = CLASSIFICATION_RANKING[old_classification.to_sym] || 0 + new_rank = CLASSIFICATION_RANKING[new_classification.to_sym] || 0 + + new_rank > old_rank + end + + def add_note(issue, table_metadata) + old_classification = extract_classification_from_description(issue.description) + new_classification = table_metadata['classification'] + + note_text = <<~TEXT + ## 🔔 Table Size Classification Escalated + + The classification for table `uploads` has escalated from `#{old_classification}` to `#{new_classification}`. + + ### Recommended Actions + + - Review recent growth patterns + - Implement retention policies + - Consider data optimization techniques + + Need help? Contact us in [#database-sustainability](https://gitlab.enterprise.slack.com/archives/C08GQHWGM0S). + + cc @gitlab-org/database-team/triage + TEXT + + response = gitlab_client.create_issue_note( + project_path, + issue.iid, + note_text + ) + + puts "Classification escalated for issue https://gitlab.com/gitlab-org/gitlab/-/issues/#{response.noteable_iid}\n" + end end diff --git a/notifier/templates/table_sizes_table.erb b/notifier/templates/table_sizes_table.erb index 5a3a9820..be07dfe0 100644 --- a/notifier/templates/table_sizes_table.erb +++ b/notifier/templates/table_sizes_table.erb @@ -8,7 +8,7 @@ Table `<%= table_metadata['table_name'] %>` is rapidly growing toward our limits | Date | Identifier | Total size | Table size | Index size | Classification | | -------- | -------- | -------- | -------- | -------- | -------- | -| `<%= Date.current %>` | `<%= table_metadata['identifier'] %>` | `<%= table_metadata['total_size'] %>` | `<%= table_metadata['table_size'] %>` | `<%= table_metadata['index_size'] %>` | `<%= table_metadata['classification'] %>` | +| `<%= Time.now.strftime("%Y-%m-%d") %>` | `<%= table_metadata['identifier'] %>` | `<%= table_metadata['total_size'] %>` | `<%= table_metadata['table_size'] %>` | `<%= table_metadata['index_size'] %>` | `<%= table_metadata['classification'] %>` | ## Growth Concerns -- GitLab From c0ed0a5dc0fd0a70b3c1141b7493a3e7a7622e20 Mon Sep 17 00:00:00 2001 From: Maxime Orefice Date: Tue, 25 Mar 2025 08:48:55 +0100 Subject: [PATCH 2/2] apply code review feedback --- notifier/spec/table_size_feedback_spec.rb | 14 +++++++------- notifier/templates/table_sizes_table.erb | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/notifier/spec/table_size_feedback_spec.rb b/notifier/spec/table_size_feedback_spec.rb index aadfe634..9ccadc51 100644 --- a/notifier/spec/table_size_feedback_spec.rb +++ b/notifier/spec/table_size_feedback_spec.rb @@ -4,9 +4,15 @@ require 'spec_helper' RSpec.describe TableSizeFeedback do describe "#render_table_size_summary" do - let(:fixed_date) { "2025-02-27" } + before do + allow(Date).to receive(:current).and_return('2025-02-27') + end + + subject { described_class.new(data).render_table_size_summary } + let(:path) { 'spec/fixtures/table-sizes-output.json' } let(:data) { JSON.parse(File.read(path)).first } + let(:markdown_table) do <<~TEXT ## Summary @@ -52,12 +58,6 @@ RSpec.describe TableSizeFeedback do TEXT end - before do - allow(Time).to receive_message_chain(:now, :strftime).with("%Y-%m-%d").and_return(fixed_date) - end - - subject { described_class.new(data).render_table_size_summary } - it "returns a markdown table" do expect(subject).to eql markdown_table end diff --git a/notifier/templates/table_sizes_table.erb b/notifier/templates/table_sizes_table.erb index be07dfe0..5a3a9820 100644 --- a/notifier/templates/table_sizes_table.erb +++ b/notifier/templates/table_sizes_table.erb @@ -8,7 +8,7 @@ Table `<%= table_metadata['table_name'] %>` is rapidly growing toward our limits | Date | Identifier | Total size | Table size | Index size | Classification | | -------- | -------- | -------- | -------- | -------- | -------- | -| `<%= Time.now.strftime("%Y-%m-%d") %>` | `<%= table_metadata['identifier'] %>` | `<%= table_metadata['total_size'] %>` | `<%= table_metadata['table_size'] %>` | `<%= table_metadata['index_size'] %>` | `<%= table_metadata['classification'] %>` | +| `<%= Date.current %>` | `<%= table_metadata['identifier'] %>` | `<%= table_metadata['total_size'] %>` | `<%= table_metadata['table_size'] %>` | `<%= table_metadata['index_size'] %>` | `<%= table_metadata['classification'] %>` | ## Growth Concerns -- GitLab