Skip to content

Commit

Permalink
Address performance for #3786
Browse files Browse the repository at this point in the history
  • Loading branch information
benwbrum committed Sep 14, 2023
1 parent 9d24ba6 commit 0c7a18f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
47 changes: 33 additions & 14 deletions app/models/collection_statistic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,39 @@ def get_stats_hash(start_date=nil, end_date=nil)
deeds = DeedType.generate_zero_counts_hash
deeds.merge!(self.deeds.where(timeframe(start_date, end_date)).group('deed_type').count)

stats =
{
:works => self.works.count,
:pages => self.works.joins(:pages).where(timeframe(start_date, end_date, 'pages.created_on')).count,
:subjects => self.articles.where(timeframe(start_date, end_date, 'created_on')).count,
:mentions => self.articles.joins(:page_article_links).where(timeframe(start_date, end_date, 'page_article_links.created_on')).count,
:contributors => self.deeds.where(timeframe(start_date, end_date)).select('user_id').distinct.count,
:pages_transcribed => self.pages.where(status: Page::COMPLETED_STATUSES).where(timeframe(start_date, end_date,'pages.edit_started_at')).count,
:works_transcribed => self.works.joins(:work_statistic).where(work_statistics: { complete: 100 }).where(timeframe(start_date, end_date, 'work_statistics.updated_at')).count,
:pages_incomplete => self.pages.where(status: Page::NEEDS_WORK_STATUSES).where(timeframe(start_date, end_date, 'pages.edit_started_at')).count,
:pages_needing_review => self.pages.where(status: Page::STATUS_NEEDS_REVIEW).where(timeframe(start_date, end_date, 'pages.edit_started_at')).count,
:descriptions => self.works.where(description_status: Work::DescriptionStatus::DESCRIBED).count,
:line_count => self.line_count
}

if start_date || end_date
stats =
{
:works => self.works.count,
:pages => self.works.joins(:pages).where(timeframe(start_date, end_date, 'pages.created_on')).count,
:subjects => self.articles.where(timeframe(start_date, end_date, 'created_on')).count,
:mentions => self.articles.joins(:page_article_links).where(timeframe(start_date, end_date, 'page_article_links.created_on')).count,
:contributors => self.deeds.where(timeframe(start_date, end_date)).select('user_id').distinct.count,
:pages_transcribed => self.pages.where(status: Page::COMPLETED_STATUSES).where(timeframe(start_date, end_date,'pages.edit_started_at')).count,
:works_transcribed => self.works.joins(:work_statistic).where(work_statistics: { complete: 100 }).where(timeframe(start_date, end_date, 'work_statistics.updated_at')).count,
:pages_incomplete => self.pages.where(status: Page::NEEDS_WORK_STATUSES).where(timeframe(start_date, end_date, 'pages.edit_started_at')).count,
:pages_needing_review => self.pages.where(status: Page::STATUS_NEEDS_REVIEW).where(timeframe(start_date, end_date, 'pages.edit_started_at')).count,
:descriptions => self.works.where(description_status: Work::DescriptionStatus::DESCRIBED).count,
:line_count => self.line_count
}
else
stats =
{
:works => self.works.count,
:pages => self.works.joins(:work_statistic).sum(:total_pages),
:subjects => self.articles.count,
:mentions => self.articles.joins(:page_article_links).count,
:contributors => self.deeds.select('user_id').distinct.count,
:pages_transcribed => self.pages.where(status: Page::COMPLETED_STATUSES).count,
:works_transcribed => self.works.joins(:work_statistic).where(work_statistics: { complete: 100 }).count,
:pages_incomplete => self.pages.where(status: Page::NEEDS_WORK_STATUSES).count,
:pages_needing_review => self.works.joins(:work_statistic).sum(:needs_review),
:descriptions => self.works.where(description_status: Work::DescriptionStatus::DESCRIBED).count,
:line_count => self.line_count
}

end

stats.merge(deeds)
end
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20230914142257_revise_index_on_page_works.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ReviseIndexOnPageWorks < ActiveRecord::Migration[6.0]
def change
# remove_index :pages, [:status, :work_id]
add_index :pages, [:status, :work_id, :edit_started_at], order: { edit_started_at: :desc }
end
end
6 changes: 4 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2023_09_12_135655) do
ActiveRecord::Schema.define(version: 2023_09_14_142257) do

create_table "ahoy_activity_summaries", id: :integer, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci", force: :cascade do |t|
t.datetime "date"
Expand Down Expand Up @@ -578,7 +578,7 @@
t.datetime "last_note_updated_at"
t.index ["edit_started_by_user_id"], name: "index_pages_on_edit_started_by_user_id"
t.index ["search_text"], name: "pages_search_text_index", type: :fulltext
t.index ["status", "work_id"], name: "index_pages_on_status_and_work_id"
t.index ["status", "work_id", "edit_started_at"], name: "index_pages_on_status_and_work_id_and_edit_started_at"
t.index ["work_id"], name: "index_pages_on_work_id"
end

Expand Down Expand Up @@ -664,7 +664,9 @@
t.integer "collection_id"
t.integer "work_id"
t.string "search_type"
t.bigint "document_set_id"
t.index ["collection_id"], name: "index_search_attempts_on_collection_id"
t.index ["document_set_id"], name: "index_search_attempts_on_document_set_id"
t.index ["slug"], name: "index_search_attempts_on_slug", unique: true
t.index ["user_id"], name: "index_search_attempts_on_user_id"
t.index ["visit_id"], name: "index_search_attempts_on_visit_id"
Expand Down

0 comments on commit 0c7a18f

Please sign in to comment.