DEV: Move reports to concerns for autoloading (#11066)

DEV: Move reports to concerns for autoloading (#11066)

diff --git a/app/models/concerns/reports/bookmarks.rb b/app/models/concerns/reports/bookmarks.rb
new file mode 100644
index 0000000..c520855
--- /dev/null
+++ b/app/models/concerns/reports/bookmarks.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+module Reports::Bookmarks
+  extend ActiveSupport::Concern
+
+  class_methods do
+    def report_bookmarks(report)
+      report.icon = 'bookmark'
+
+      category_filter = report.filters.dig(:category)
+      report.add_filter('category', default: category_filter)
+
+      report.data = []
+      Bookmark.count_per_day(
+        category_id: category_filter,
+        start_date: report.start_date,
+        end_date: report.end_date
+      ).each do |date, count|
+        report.data << { x: date, y: count }
+      end
+      add_counts report, Bookmark, 'bookmarks.created_at'
+    end
+  end
+end
diff --git a/app/models/concerns/reports/consolidated_page_views.rb b/app/models/concerns/reports/consolidated_page_views.rb
new file mode 100644
index 0000000..35757d7
--- /dev/null
+++ b/app/models/concerns/reports/consolidated_page_views.rb
@@ -0,0 +1,49 @@
+# frozen_string_literal: true
+
+module Reports::ConsolidatedPageViews
+  extend ActiveSupport::Concern
+
+  class_methods do
+    def report_consolidated_page_views(report)
+      filters = %w[
+        page_view_logged_in
+        page_view_anon
+        page_view_crawler
+      ]
+
+      report.modes = [:stacked_chart]
+
+      tertiary = ColorScheme.hex_for_name('tertiary') || '0088cc'
+      danger = ColorScheme.hex_for_name('danger') || 'e45735'
+
+      requests = filters.map do |filter|
+        color = report.rgba_color(tertiary)
+
+        if filter == "page_view_anon"
+          color = report.lighten_color(tertiary, 0.25)
+        end
+
+        if filter == "page_view_crawler"
+          color = report.rgba_color(danger, 0.75)
+        end
+
+        {
+          req: filter,
+          label: I18n.t("reports.consolidated_page_views.xaxis.#{filter}"),
+          color: color,
+          data: ApplicationRequest.where(req_type: ApplicationRequest.req_types[filter])
+        }
+      end
+
+      requests.each do |request|
+        request[:data] = request[:data].where('date >= ? AND date <= ?', report.start_date, report.end_date)
+          .order(date: :asc)
+          .group(:date)
+          .sum(:count)
+          .map { |date, count| { x: date, y: count } }
+      end
+
+      report.data = requests
+    end
+  end
+end
diff --git a/app/models/concerns/reports/daily_engaged_users.rb b/app/models/concerns/reports/daily_engaged_users.rb
new file mode 100644
index 0000000..9e4dfdb
--- /dev/null
+++ b/app/models/concerns/reports/daily_engaged_users.rb
@@ -0,0 +1,38 @@
+# frozen_string_literal: true
+
+module Reports::DailyEngagedUsers
+  extend ActiveSupport::Concern
+
+  class_methods do
+    def report_daily_engaged_users(report)
+      report.average = true
+
+      report.data = []
+
+      data = UserAction.count_daily_engaged_users(report.start_date, report.end_date)
+
+      if report.facets.include?(:prev30Days)
+        prev30DaysData = UserAction.count_daily_engaged_users(report.start_date - 30.days, report.start_date)
+        report.prev30Days = prev30DaysData.sum { |k, v| v }
+      end
+
+      if report.facets.include?(:total)
+        report.total = UserAction.count_daily_engaged_users
+      end
+
+      if report.facets.include?(:prev_period)
+        prev_data = UserAction.count_daily_engaged_users(report.prev_start_date, report.prev_end_date)
+
+        prev = prev_data.sum { |k, v| v }
+        if prev > 0
+          prev = prev / ((report.end_date - report.start_date) / 1.day)
+        end
+        report.prev_period = prev
+      end
+
+      data.each do |key, value|
+        report.data << { x: key, y: value }
+      end
+    end
+  end
+end
diff --git a/app/models/concerns/reports/dau_by_mau.rb b/app/models/concerns/reports/dau_by_mau.rb
new file mode 100644
index 0000000..69956c8
--- /dev/null
+++ b/app/models/concerns/reports/dau_by_mau.rb
@@ -0,0 +1,57 @@
+# frozen_string_literal: true
+
+module Reports::DauByMau
+  extend ActiveSupport::Concern
+
+  class_methods do
+    def report_dau_by_mau(report)
+      report.labels = [
+        {
+          type: :date,
+          property: :x,
+          title: I18n.t("reports.default.labels.day")
+        },
+        {
+          type: :percent,
+          property: :y,
+          title: I18n.t("reports.default.labels.percent")
+        },
+      ]
+
+      report.average = true
+      report.percent = true
+
+      data_points = UserVisit.count_by_active_users(report.start_date, report.end_date)
+
+      report.data = []
+
+      compute_dau_by_mau = Proc.new { |data_point|
+        if data_point["mau"] == 0
+          0
+        else
+          ((data_point["dau"].to_f / data_point["mau"].to_f) * 100).ceil(2)
+        end
+      }
+
+      dau_avg = Proc.new { |start_date, end_date|
+        data_points = UserVisit.count_by_active_users(start_date, end_date)
+        if !data_points.empty?
+          sum = data_points.sum { |data_point| compute_dau_by_mau.call(data_point) }
+          (sum.to_f / data_points.count.to_f).ceil(2)
+        end
+      }
+
+      data_points.each do |data_point|
+        report.data << { x: data_point["date"], y: compute_dau_by_mau.call(data_point) }
+      end
+
+      if report.facets.include?(:prev_period)
+        report.prev_period = dau_avg.call(report.prev_start_date, report.prev_end_date)
+      end
+
+      if report.facets.include?(:prev30Days)
+        report.prev30Days = dau_avg.call(report.start_date - 30.days, report.start_date)
+      end
+    end
+  end
+end
diff --git a/app/models/concerns/reports/emails.rb b/app/models/concerns/reports/emails.rb
new file mode 100644
index 0000000..42a97a1
--- /dev/null
+++ b/app/models/concerns/reports/emails.rb
@@ -0,0 +1,11 @@
+# frozen_string_literal: true
+
+module Reports::Emails
+  extend ActiveSupport::Concern
+
+  class_methods do
+    def report_emails(report)
+      report_about report, EmailLog
+    end
+  end
+end
diff --git a/app/models/concerns/reports/flags.rb b/app/models/concerns/reports/flags.rb
new file mode 100644
index 0000000..8c8cd84
--- /dev/null
+++ b/app/models/concerns/reports/flags.rb
@@ -0,0 +1,36 @@
+# frozen_string_literal: true
+
+module Reports::Flags
+  extend ActiveSupport::Concern
+
+  class_methods do
+    def report_flags(report)
+      category_id, include_subcategories = report.add_category_filter
+
+      report.icon = 'flag'
+      report.higher_is_better = false
+
+      basic_report_about(
+        report,
+        ReviewableFlaggedPost,
+        :count_by_date,
+        report.start_date,
+        report.end_date,
+        category_id,
+        include_subcategories
+      )
+
+      countable = ReviewableFlaggedPost.scores_with_topics
+
+      if category_id
+        if include_subcategories
+          countable = countable.where('topics.category_id IN (?)', Category.subcategory_ids(category_id))
+        else
+          countable = countable.where('topics.category_id = ?', category_id)
+        end
+      end
+
+      add_counts report, countable, 'reviewable_scores.created_at'
+    end
+  end
+end
diff --git a/app/models/concerns/reports/flags_status.rb b/app/models/concerns/reports/flags_status.rb
new file mode 100644
index 0000000..1349f13
--- /dev/null
+++ b/app/models/concerns/reports/flags_status.rb
@@ -0,0 +1,178 @@
+# frozen_string_literal: true
+
+module Reports::FlagsStatus
+  extend ActiveSupport::Concern
+
+  class_methods do
+    def report_flags_status(report)
+      report.modes = [:table]
+
+      report.labels = [
+        {
+          type: :post,
+          properties: {
+            topic_id: :topic_id,
+            number: :post_number,
+            truncated_raw: :post_type
+          },
+          title: I18n.t("reports.flags_status.labels.flag")
+        },
+        {
+          type: :user,

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

GitHub sha: c750a02f

This commit appears in #11066 which was approved by jjaffeux. It was merged by markvanlan.