script/embeddable_stats.rb
# This script will print CSV string describing all the activities that include selected embeddable type.
# It's meant to be run by developer in rails console directly (can be copy-pasted to staging or production console).
# It iterates over activities instead of the embeddable models as e.g. on LARA staging there are 300k Labbooks,
# but only 13k activities. Probably there are many stale objects not assigned to any page/activity.
This script will print CSV string describing all the activities that include selected embeddable type.
It's meant to be run by developer in rails console directly (can be copy-pasted to staging or production console).
It iterates over activities instead of the embeddable models as e.g. on LARA staging there are 300k Labbooks,
but only 13k activities. Probably there are many stale objects not assigned to any page/activity.
This script will print CSV string describing all the activities that include selected embeddable type . It ' s meant to be run by developer in rails console directly ( can be copy - pasted to staging or production console ) . It iterates over activities instead of the embeddable models as e . g . on LARA staging there are 300k Labbooks but only 13k activities . Probably there are many stale objects not assigned to any page / activity .
def print_embeddable_stats(embeddable_type, host = "authoring.concord.org")
  ActiveRecord::Base.logger = nil 
  stats = {}
  count = 0
  LightweightActivity.find_each do |activity|
    count += 1
    if count % 1000 == 0
      print "." 
    end
    activity.pages.pluck(:id).each do |page_id|
      embeddable_count = PageItem.where(interactive_page_id: page_id, embeddable_type: embeddable_type).count
      if embeddable_count > 0
        unless stats[activity.name]
          runs = activity.runs
          stats[activity.name] = {
            act_name: activity.name,
            act_id: activity.id,
            act_url: Rails.application.routes.url_helpers.activity_url(activity, host: host),
            runs_count: runs.count,
            last_run: runs.count > 0 ? activity.runs.order(:updated_at).last.updated_at : "never",
            embeddable_count: embeddable_count
          }
        else
          stats[activity.name][:embeddable_count] += embeddable_count
        end
      end
    end
  end
  puts 
  puts "activity name\tactivity id\tactivity url\truns count\tlast run\t#{embeddable_type} count"
  stats.each do |key, s|
    puts "#{s[:act_name]}\t#{s[:act_id]}\t#{s[:act_url]}\t#{s[:runs_count]}\t#{s[:last_run]}\t#{s[:embeddable_count]}"
  end
  nil
end
def print_embeddable_stats ( embeddable_type , host = "authoring.concord.org" ) ActiveRecord :: Base . logger = nil stats = { } count = 0 LightweightActivity . find_each do | activity | count += 1 if count % 1000 == 0 print "." end activity . pages . pluck ( :id ) . each do | page_id | embeddable_count = PageItem . where ( interactive_page_id : page_id , embeddable_type : embeddable_type ) . count if embeddable_count > 0 unless stats [ activity . name ] runs = activity . runs stats [ activity . name ] = { act_name : activity . name , act_id : activity . id , act_url : Rails . application . routes . url_helpers . activity_url ( activity , host : host ) , runs_count : runs . count , last_run : runs . count > 0 ? activity . runs . order ( :updated_at ) . last . updated_at : "never" , embeddable_count : embeddable_count } else stats [ activity . name ] [ :embeddable_count ] += embeddable_count end end end end puts puts "activity name\tactivity id\tactivity url\truns count\tlast run\t#{embeddable_type} count" stats . each do | key , s | puts "#{s[:act_name]}\t#{s[:act_id]}\t#{s[:act_url]}\t#{s[:runs_count]}\t#{s[:last_run]}\t#{s[:embeddable_count]}" end nil end
This script will print CSV string describing all the activities that include selected embeddable type.
This script will print CSV string describing all the activities that include selected embeddable type .
# disable SQL logging # print something to see some progress # new line after last .
embeddable_type
<not_specific>
host
<not_specific>