class ManagerStatisticsBase

Public Class Methods

new(options = nil) click to toggle source

default constructor

Calls superclass method ManagerBase::new
# File lib/mrpin/core/statistic/manager_statistics_base.rb, line 18
def initialize(options = nil)
  super(options)

  @class_statistics_config     = get_class_by_name(CLASS_MAP[EClassType::ECT_STATISTIC_CONFIG])
  @class_statistic_application = get_class_by_name(CLASS_MAP[EClassType::ECT_STATISTIC_APPLICATION])

  @class_player          = get_class_by_name(CLASS_MAP[EClassType::ECT_PLAYER])
  @class_player_purchase = get_class_by_name(CLASS_MAP[EClassType::ECT_PLAYER_PURCHASE])

  @handler_profiler_settings =
      {
          enable: false,
          size:   0
      }

  @handler_profiler_managers = []
end

Public Instance Methods

calculate_application_statistics() click to toggle source
# File lib/mrpin/core/statistic/manager_statistics_base.rb, line 131
def calculate_application_statistics
  return if @manager_platforms.nil?

  today_start = Time.now.utc.midnight.to_i

  platforms_list = @manager_platforms.supported_platforms

  # date
  date           = (Time.now.utc.midnight - 1.second).strftime('%d.%m.%y')

  platforms_list.each do |platform_type|

    # skip if statistic already calculated
    next if @class_statistic_application.where(date: date, platform_type: platform_type).exists?

    # dau
    dau              = @class_player.where(:platform_type_login => platform_type,
                                           :last_login_at       => {'$gt' => (today_start - 1.day).to_i, '$lt' => today_start}).count

    # installs
    installs         = @class_player.where(:platform_type_install => platform_type,
                                           :created_at            => {'$gt' => (today_start - 1.day).to_i, '$lt' => today_start}).count

    # revenue
    platform_revenue = @class_player_purchase.where(:platform_type    => platform_type,
                                                    :is_purchase_hard => true,
                                                    :purchased_at     => {'$gt' => (today_start - 1.day).to_i, '$lt' => today_start},
                                                    :is_test          => false).sum(:price)
    currency_rate    = @manager_platforms.get_currency_rate(platform_type)
    revenue          = currency_rate * platform_revenue

    # paid users
    paid_users       = @class_player_purchase.where(:platform_type    => platform_type,
                                                    :is_purchase_hard => true,
                                                    :purchased_at     => {'$gt' => (today_start - 1.day).to_i, '$lt' => today_start},
                                                    :is_test          => false).distinct(:player_id).count


    # retention
    ret_1d           = retention(platform_type, 1)
    ret_3d           = retention(platform_type, 3)
    ret_7d           = retention(platform_type, 7)
    ret_14d          = retention(platform_type, 14)


    # create record
    statistic        = @class_statistic_application.new

    statistic.date          = date
    statistic.platform_type = platform_type

    statistic.dau        = dau
    statistic.installs   = installs
    statistic.revenue    = revenue.round(1)
    statistic.paid_users = paid_users
    statistic.arpu       = dau == 0 ? 0 : (revenue.to_f / dau).round(2)
    statistic.arppu      = paid_users == 0 ? 0 : (revenue.to_f / paid_users).round(2)
    statistic.ret_1d     = ret_1d.nil? ? nil : ret_1d.round(2)
    statistic.ret_3d     = ret_3d.nil? ? nil : ret_3d.round(2)
    statistic.ret_7d     = ret_7d.nil? ? nil : ret_7d.round(2)
    statistic.ret_14d    = ret_14d.nil? ? nil : ret_14d.round(2)

    statistic.save!
  end
end
is_profiler_enable?() click to toggle source

Properties

# File lib/mrpin/core/statistic/manager_statistics_base.rb, line 8
def is_profiler_enable?
  @handler_profiler_settings[:enable]
end
load_init_data() click to toggle source
Calls superclass method ManagerBase#load_init_data
# File lib/mrpin/core/statistic/manager_statistics_base.rb, line 42
def load_init_data
  super

  init_config
end
post_init() click to toggle source
Calls superclass method ManagerBase#post_init
# File lib/mrpin/core/statistic/manager_statistics_base.rb, line 37
def post_init
  super
end
register_manager_to_profile(manager) click to toggle source
# File lib/mrpin/core/statistic/manager_statistics_base.rb, line 126
def register_manager_to_profile(manager)
  @handler_profiler_managers << manager
end

Private Instance Methods

init_config() click to toggle source
# File lib/mrpin/core/statistic/manager_statistics_base.rb, line 49
def init_config
  @config = @class_statistics_config.first

  assert(!@config.nil?, 'ManagerStatistics. Config is empty')

  assert(!@config.handler_profiler.nil?, 'ManagerStatistics. Config::handler_profiler is empty')

  init_handler_profiler
end
init_handler_profiler() click to toggle source
# File lib/mrpin/core/statistic/manager_statistics_base.rb, line 60
def init_handler_profiler
  new_settings = @config.handler_profiler
  new_settings = Hash[new_settings].symbolize_keys!

  old_settings = @handler_profiler_settings

  new_settings.assert_property!(:enable)
  new_settings.assert_property!(:size)

  if !new_settings[:enable] && new_settings[:enable] != old_settings[:enable]
    # clear stats
    @handler_profiler_managers.each do |manager|
      requests_handlers_map = manager.handlers_map

      handlers = requests_handlers_map.values

      handlers.each do |handler|
        next if handler.requests_count == 0

        handler.reset_handler_stats
      end
    end
  end

  if new_settings[:size] != old_settings[:size]
    # recreate collection if need
    recreate_collection = true

    if old_settings[:size] == 0
      collection          = SystemHandlerProfiler.collection_name.to_s
      collection_size_max = new_settings[:size]

      old_collection_size_max = Utils::Mongo.get_collection_stats_property(collection, 'maxSize') || 0

      old_size_mb = old_collection_size_max / 1_000_000
      new_size_mb = collection_size_max / 1_000_000

      if old_size_mb == new_size_mb
        recreate_collection = false
      end
    end

    if recreate_collection

      old_documents = []

      SystemHandlerProfiler.all.each do |document|
        old_documents << document.as_document
      end

      SystemHandlerProfiler.collection.drop

      collection_name     = SystemHandlerProfiler.collection_name.to_s
      collection_size_max = new_settings[:size]

      Utils::Mongo.create_capped_collection(collection_name, collection_size_max)

      SystemHandlerProfiler.collection.insert(old_documents) if old_documents.size > 0

    end
  end

  @handler_profiler_settings = new_settings
end
retention(platform_type, day) click to toggle source
# File lib/mrpin/core/statistic/manager_statistics_base.rb, line 198
def retention(platform_type, day)
  today_start         = Time.now.utc.midnight.to_i

  # retention interval (yesterday)
  retention_day_end   = today_start
  retention_day_start = retention_day_end - 1.day

  # installs interval (<day> days ago)
  install_day_end     = retention_day_start - day.days
  install_day_start   = install_day_end - 1.day

  installs_count = @class_player.where(:platform_type_install => platform_type,
                                       :created_at            => {'$gt' => install_day_start, '$lt' => install_day_end}).count

  return nil if installs_count == 0

  retention_count = @class_player.where(:platform_type_install => platform_type,
                                        :created_at            => {'$gt' => install_day_start, '$lt' => install_day_end},
                                        :last_login_at         => {'$gt' => retention_day_start, '$lt' => retention_day_end}).count

  retention_count.to_f/installs_count
end