Class: GroupMetrics::WeeklyCount

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/group_metrics/weekly_count.rb

Overview

Tabulate count of records (within a study week) for a Group.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fetch(group_id, timestamp_column = "created_at") ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/group_metrics/weekly_count.rb', line 11

def self.fetch(group_id, timestamp_column = "created_at")
  pg_timezone = ActiveSupport::TimeZone[Time.zone.name].tzinfo.name

  joins(memberships: :participant)
    .select(<<-SQL
      ( FLOOR ( EXTRACT ( EPOCH FROM (
        ( #{connection.quote_table_name(table_name)}.#{connection.quote_table_name(timestamp_column)}
          AT TIME ZONE 'UTC' ) AT TIME ZONE #{connection.quote(pg_timezone)}
        - memberships.start_date ) ) / 604800 ) + 1
      )::int AS week, COUNT(1)
    SQL
           )
    .merge(Participant.not_moderator)
    .merge(Membership.where(group_id: group_id))
    .group(:week)
end

Instance Method Details

#countObject



32
33
34
# File 'app/models/group_metrics/weekly_count.rb', line 32

def count
  attributes["count"]
end

#weekObject



28
29
30
# File 'app/models/group_metrics/weekly_count.rb', line 28

def week
  attributes["week"]
end