DEV: Move chart helper to module so it is easier to debug

DEV: Move chart helper to module so it is easier to debug

diff --git a/lib/chart_helper.rb b/lib/chart_helper.rb
new file mode 100644
index 0000000..cc4b43e
--- /dev/null
+++ b/lib/chart_helper.rb
@@ -0,0 +1,119 @@
+# frozen_string_literal: true
+
+module ::DevAdditions
+  class ChartHelper
+    def self.refresh_bench_topic(post)
+      return if post.post_number == 1
+      return if !SiteSetting.dev_additions_benchmark_topic_ids.split(",").include?(post.topic_id.to_s)
+
+      rows = DB.query(<<~SQL, topic_id: post.topic_id)
+      SELECT raw, post_number, username
+      FROM posts p
+      LEFT JOIN users u on u.id = p.user_id
+      WHERE p.topic_id = :topic_id AND p.deleted_at IS NULL
+      SQL
+
+      benches = {}
+
+      rows.each do |row|
+        desc = nil
+        bench_parsed = nil
+        skip_chart = false
+
+        row.raw.each_line do |line|
+          desc = line.strip if !desc && line.present?
+
+          if line.include? "on 1 core"
+            bench_parsed = :single
+          elsif line.match?(/on \d+ core/)
+            bench_parsed = :multi
+          elsif line.include?("<!-- skip chart -->")
+            skip_chart = true
+          elsif bench_parsed
+            match = line.match(/(.*): (\S+)(.*)(op\/s|seconds)$/)
+            if match
+              name = match[1]
+              duration = match[2].to_f
+              if duration > 0
+                durations = (benches["#{name}#{" (mp)" if bench_parsed == :multi}"] ||= [])
+                durations << {
+                  duration: duration,
+                  type: match[4] == "seconds" ? :secs : :ops,
+                  post_number: row.post_number,
+                  description: desc,
+                  variance: (match[3] || "").strip,
+                  username: row.username,
+                  skip_chart: skip_chart
+                }
+              end
+            end
+          end
+
+        end
+      end
+
+      markdown = +""
+
+      benches.each do |name, metrics|
+        result_type = metrics[0][:type]
+        type = result_type == :ops ? "op/s (higher is better)" : "seconds (lower is better)"
+
+        markdown << <<~MD
+        [chart borderColors="#f45,#c83,#d76" title="#{name.gsub('"', "")}" type="horizontalBar" xAxisTitle="#{type}" backgroundColors="#4cc"]
+        MD
+
+        metrics.sort! do |a, b|
+          if a[:type] == :ops
+            b[:duration] <=> a[:duration]
+          else
+            a[:duration] <=> b[:duration]
+          end
+        end
+
+        metrics.each do |row|
+          if !row[:skip_chart]
+            markdown << <<~MD
+            #{row[:description]} | #{row[:duration]}
+            MD
+          end
+        end
+
+        markdown << "[/chart]\n\n"
+
+        markdown << <<~MD
+        | desc | user | #{ result_type == :ops ? "op/s" : "seconds" } | variance | percent |
+        |-----|-----|----:|----:|----:|
+        MD
+
+        top_duration = metrics[0][:duration]
+        metrics.each do |row|
+          percent = ((top_duration - (top_duration - row[:duration])) / top_duration) * 100
+          percent = "#{percent.round(2)}%"
+          desc = row[:description].gsub("|", "\\|")
+          user_link = "[#{row[:username]}](#{post.topic.url}/#{row[:post_number]})"
+
+          markdown << <<~MD
+          #{desc}|#{user_link}|#{row[:duration]}|#{row[:variance]}|#{percent}|
+          MD
+        end
+      end
+
+      op = Post.find_by(topic_id: post.topic_id, post_number: 1)
+      raw = op.raw.split("<!--AUTO-->", 3)
+      raw[1] = markdown
+      raw[2] ||= ""
+
+      revisor = PostRevisor.new(op)
+      new_raw = +"" << raw[0]
+      new_raw << "\n" if new_raw[-1] != "\n"
+      new_raw << "<!--AUTO-->\n"
+      new_raw << markdown
+      new_raw << "<!--AUTO-->"
+      if raw[2]
+        new_raw << "\n" if raw[0] != "\n"
+        new_raw << raw[2]
+      end
+      revisor.revise!(Discourse.system_user, raw: new_raw)
+    end
+  end
+end
diff --git a/plugin.rb b/plugin.rb
index 46bca26..ac9fc93 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -19,6 +19,7 @@ after_initialize do
     "../jobs/scheduled/check_logs_game.rb",
     "../jobs/scheduled/check_cakeday.rb",
     "../jobs/scheduled/gift_exchange.rb",
+    "../lib/chart_helper.rb",
   ].each { |path| require File.expand_path(path, __FILE__) }
 
   register_editable_user_custom_field(DevAdditions::MANAGER_CUSTOM_FIELD)
@@ -34,125 +35,11 @@ after_initialize do
     )
   end
 
-  def refresh_bench_topic(post)
-    return if post.post_number == 1
-    return if !SiteSetting.dev_additions_benchmark_topic_ids.split(",").include?(post.topic_id.to_s)
-
-    rows = DB.query(<<~SQL, topic_id: post.topic_id)
-      SELECT raw, post_number, username
-      FROM posts p
-      LEFT JOIN users u on u.id = p.user_id
-      WHERE p.topic_id = :topic_id AND p.deleted_at IS NULL
-    SQL
-
-    benches = {}
-
-    rows.each do |row|
-      desc = nil
-      bench_parsed = nil
-      skip_chart = false
-
-      row.raw.each_line do |line|
-        desc = line.strip if !desc && line.present?
-
-        if line.include? "on 1 core"
-          bench_parsed = :single
-        elsif line.match?(/on \d+ core/)
-          bench_parsed = :multi
-        elsif line.include?("<!-- skip chart -->")
-          skip_chart = true
-        elsif bench_parsed
-          match = line.match(/(.*): (\S+)(.*)(op\/s|seconds)$/)
-          if match
-            name = match[1]
-            duration = match[2].to_f
-            if duration > 0
-              durations = (benches["#{name}#{" (mp)" if bench_parsed == :multi}"] ||= [])
-              durations << {
-                duration: duration,
-                type: match[4] == "seconds" ? :secs : :ops,
-                post_number: row.post_number,
-                description: desc,
-                variance: (match[3] || "").strip,
-                username: row.username,
-                skip_chart: skip_chart
-              }
-            end
-          end
-        end
-
-      end
-    end
-
-    markdown = +""
-
-    benches.each do |name, metrics|
-      result_type = metrics[0][:type]
-      type = result_type == :ops ? "op/s (higher is better)" : "seconds (lower is better)"
-
-      markdown << <<~MD
-        [chart borderColors="#f45,#c83,#d76" title="#{name.gsub('"', "")}" type="horizontalBar" xAxisTitle="#{type}" backgroundColors="#4cc"]
-      MD
-
-      metrics.sort! do |a, b|
-        if a[:type] == :ops
-          b[:duration] <=> a[:duration]
-        else
-          a[:duration] <=> b[:duration]
-        end
-      end
-
-      metrics.each do |row|
-        if !row[:skip_chart]
-          markdown << <<~MD
-            #{row[:description]} | #{row[:duration]}
-          MD
-        end
-      end
-
-      markdown << "[/chart]\n\n"
-
-      markdown << <<~MD
-        | desc | user | #{ result_type == :ops ? "op/s" : "seconds" } | variance | percent |
-        |-----|-----|----:|----:|----:|
-      MD
-
-      top_duration = metrics[0][:duration]
-      metrics.each do |row|
-        percent = ((top_duration - (top_duration - row[:duration])) / top_duration) * 100
-        percent = "#{percent.round(2)}%"
-        desc = row[:description].gsub("|", "\\|")
-        user_link = "[#{row[:username]}](#{post.topic.url}/#{row[:post_number]})"
-
-        markdown << <<~MD
-          #{desc}|#{user_link}|#{row[:duration]}|#{row[:variance]}|#{percent}|
-        MD
-      end
-    end
-
-    op = Post.find_by(topic_id: post.topic_id, post_number: 1)
-    raw = op.raw.split("<!--AUTO-->", 3)
-    raw[1] = markdown
-    raw[2] ||= ""
-
-    revisor = PostRevisor.new(op)
-    new_raw = +"" << raw[0]
-    new_raw << "\n" if new_raw[-1] != "\n"
-    new_raw << "<!--AUTO-->\n"
-    new_raw << markdown
-    new_raw << "<!--AUTO-->"
-    if raw[2]
-      new_raw << "\n" if raw[0] != "\n"
-      new_raw << raw[2]
-    end
-    revisor.revise!(Discourse.system_user, raw: new_raw)
-  end
-
   DiscourseEvent.on(:post_edited) do |post|
-    refresh_bench_topic(post)

[... diff too long, it was truncated ...]

GitHub sha: 86dcb663