Class: AccountLoginStatisticsJob

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job
Defined in:
app/sidekiq/account_login_statistics_job.rb

Instance Method Summary collapse

Instance Method Details

#account_login_stats_sqlObject (private)



30
31
32
33
34
35
36
# File 'app/sidekiq/account_login_statistics_job.rb', line 30

def 
  <<-SQL.squish
    SELECT
      #{count_column_sql_statements}
    FROM account_login_stats
  SQL
end

#count_column_sql_statementsObject (private)



38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/sidekiq/account_login_statistics_job.rb', line 38

def count_column_sql_statements
  SAML::User::LOGIN_TYPES.map do |type|
    type = SAML::User::MHV_MAPPED_CSID if type == SAML::User::MHV_ORIGINAL_CSID
    %(
      COUNT(#{type}_at) FILTER (WHERE #{type}_at IS NOT NULL) AS "account_login_stats.total_#{type}_accounts",
      COUNT(#{type}_at) FILTER (WHERE #{type}_at > $1) AS "account_login_stats.#{type}_past_year",
      COUNT(#{type}_at) FILTER (WHERE #{type}_at > $2) AS "account_login_stats.#{type}_past_month",
      COUNT(#{type}_at) FILTER (WHERE #{type}_at > $3) AS "account_login_stats.#{type}_past_week",
      COUNT(#{type}_at) FILTER (WHERE #{type}_at > $4) AS "account_login_stats.#{type}_past_day"
    )
  end.join(', ')
end

#execute_sql(sql, *args) ⇒ Object (private)



24
25
26
27
28
# File 'app/sidekiq/account_login_statistics_job.rb', line 24

def execute_sql(sql, *args)
  ActiveRecord::Base.connection_pool.with_connection do |c|
    c.raw_connection.exec_params(sql, args).to_a
  end
end

#performObject



6
7
8
9
10
# File 'app/sidekiq/account_login_statistics_job.rb', line 6

def perform
  total_stats.each do |metric, count|
    StatsD.gauge(metric, count)
  end
end

#total_statsObject (private)



14
15
16
17
18
19
20
21
22
# File 'app/sidekiq/account_login_statistics_job.rb', line 14

def total_stats
  execute_sql(
    ,
    1.year.ago,
    1.month.ago,
    1.week.ago,
    1.day.ago
  ).first
end