From b7ae1dd43e2b9181ffad038c9a188859c36d282c Mon Sep 17 00:00:00 2001 From: Paul Arterburn Date: Wed, 4 Sep 2024 14:42:31 -0600 Subject: [PATCH] Remove ai query for perf gains --- app/models/admin_stats.rb | 50 ++++++++++++++++----------------- app/views/admin/stats.html.haml | 50 ++++++++++++++++----------------- 2 files changed, 50 insertions(+), 50 deletions(-) diff --git a/app/models/admin_stats.rb b/app/models/admin_stats.rb index 6cc6fd4a..e8eaa358 100644 --- a/app/models/admin_stats.rb +++ b/app/models/admin_stats.rb @@ -78,38 +78,38 @@ def users_created_since(date) # User.free_only.where("created_at >= ?", date) # end - # def upgraded_users_since(date) - # pro_users = [] - # User.pro_only.includes(:payments).order("payments.created_at ASC").each do | user| - # first_payment = user.payments.order('payments.date').try(:first).try(:date) - # if first_payment.present? && first_payment > date - # pro_users << user - # end - # end - # pro_users - # end + def upgraded_users_since(date) + pro_users = [] + User.pro_only.includes(:payments).order("payments.created_at ASC").each do | user| + first_payment = user.payments.order('payments.date').try(:first).try(:date) + if first_payment.present? && first_payment > date + pro_users << user + end + end + pro_users + end # def bounced_users_since(date) # User.where("emails_bounced > 0").where("updated_at >= ?", date) # end - # def entries_per_day_for(user) - # (entry_count_for(user) / account_age_for(user).to_f).to_f.round(1) - # rescue ZeroDivisionError - # 0 - # end + def entries_per_day_for(user) + (entry_count_for(user) / account_age_for(user).to_f).to_f.round(1) + rescue ZeroDivisionError + 0 + end - # def paid_status_for(user) - # entries_per_day = entries_per_day_for(user) + def paid_status_for(user) + entries_per_day = entries_per_day_for(user) - # if entry_count_for(user) == 0 - # "danger" - # elsif entries_per_day <= 0.2 - # "warning" - # else - # "great" - # end - # end + if entry_count_for(user) == 0 + "danger" + elsif entries_per_day <= 0.2 + "warning" + else + "great" + end + end private diff --git a/app/views/admin/stats.html.haml b/app/views/admin/stats.html.haml index 5169deb8..99e88b01 100644 --- a/app/views/admin/stats.html.haml +++ b/app/views/admin/stats.html.haml @@ -26,13 +26,13 @@ =link_to admin_photos_path do %strong #{format_number(photos_count)} %span{style: "margin-left: 10px;"} #{number_to_percentage(photos_count.to_f/all_count.to_f*100, precision: 0)} - .row - .col-md-3 - %strong All Entries using DabbleMeGPT - - ai_entries = all_entries.with_ai_responses - .col-md-2 - %strong #{format_number(ai_entries.count)} - %span{style: "margin-left: 10px;"} #{format_number(ai_entries.pluck(:user_id).uniq.count)} users + -# .row + -# .col-md-3 + -# %strong All Entries using DabbleMeGPT + -# - ai_entries = all_entries.with_ai_responses + -# .col-md-2 + -# %strong #{format_number(ai_entries.count)} + -# %span{style: "margin-left: 10px;"} #{format_number(ai_entries.pluck(:user_id).uniq.count)} users .col-md-12 %hr @@ -132,24 +132,24 @@ = column_chart @dashboard.payments_by_month(1.year.ago), discrete: true %br - -# .col-md-12 - -# - upgrades = @dashboard.upgraded_users_since(90.days.ago) - -# %h3 #{pluralize(format_number(upgrades.count), "Upgrade")} from the last 90 days - -# %p - -# %table.table.table-striped.table-hover - -# %tr - -# %th Email - -# %th Upgraded - -# %th Paid - -# %th Entries - -# %th Per day - -# - upgrades.each do |user| - -# %tr{:class => @dashboard.paid_status_for(user)} - -# %td= user.email - -# %td= l(user.payments.first.date.to_date, format: :month_day) - -# %td= user.payments.sum(:amount) - -# %td= user.entries.count - -# %td= @dashboard.entries_per_day_for(user) + .col-md-12 + - upgrades = @dashboard.upgraded_users_since(90.days.ago) + %h3 #{pluralize(format_number(upgrades.count), "Upgrade")} from the last 90 days + %p + %table.table.table-striped.table-hover + %tr + %th Email + %th Upgraded + %th Paid + %th Entries + %th Per day + - upgrades.each do |user| + %tr{:class => @dashboard.paid_status_for(user)} + %td= user.email + %td= l(user.payments.first.date.to_date, format: :month_day) + %td= user.payments.sum(:amount) + %td= user.entries.count + %td= @dashboard.entries_per_day_for(user) -# .col-md-12 -# %hr