Class: CodeSunset::Event

Inherits:
ApplicationRecord show all
Defined in:
app/models/code_sunset/event.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.apply_filters(filters = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/code_sunset/event.rb', line 24

def apply_filters(filters = {})
  scoped = all
  normalized = filters.to_h.symbolize_keys

  scoped = scoped.where(app_env: normalized[:app_env]) if normalized[:app_env].present?
  scoped = scoped.where(plan: normalized[:plan]) if normalized[:plan].present?
  if normalized[:org_id].present?
    scoped = apply_identity_filter(scoped, :org_id, :hashed_org_id, normalized[:org_id])
  end
  if normalized[:user_id].present?
    scoped = apply_identity_filter(scoped, :user_id, :hashed_user_id, normalized[:user_id])
  end
  scoped = scoped.where(paid_org: true) if truthy?(normalized[:paid_only])
  scoped = scoped.where.not(internal_org: true) if truthy?(normalized[:exclude_internal])
  scoped = (scoped, normalized)

  from_time = parse_time(normalized[:from])&.beginning_of_day
  to_time = parse_time(normalized[:to])&.end_of_day

  scoped = scoped.where(occurred_at: from_time..) if from_time
  scoped = scoped.where(occurred_at: ..to_time) if to_time
  scoped
end

.create_from_payload(payload) ⇒ Object



15
16
17
18
19
20
21
22
# File 'app/models/code_sunset/event.rb', line 15

def create_from_payload(payload)
  attrs = sanitize_payload(payload)

  transaction do
    CodeSunset::Feature.find_or_create_by!(key: attrs[:feature_key])
    create!(attrs)
  end
end

.grouped_counts(scope) ⇒ Object



52
53
54
# File 'app/models/code_sunset/event.rb', line 52

def grouped_counts(scope)
  scope.group(:feature_key).count
end

.grouped_distinct_org_counts(scope) ⇒ Object



56
57
58
# File 'app/models/code_sunset/event.rb', line 56

def grouped_distinct_org_counts(scope)
  scope.group(:feature_key).distinct.count(Arel.sql(org_identifier_expression))
end

.grouped_distinct_user_counts(scope) ⇒ Object



60
61
62
# File 'app/models/code_sunset/event.rb', line 60

def grouped_distinct_user_counts(scope)
  scope.group(:feature_key).distinct.count(Arel.sql(user_identifier_expression))
end

.grouped_last_seen(scope) ⇒ Object



48
49
50
# File 'app/models/code_sunset/event.rb', line 48

def grouped_last_seen(scope)
  scope.group(:feature_key).maximum(:occurred_at)
end

.org_identifier_expression ⇒ Object



80
81
82
# File 'app/models/code_sunset/event.rb', line 80

def org_identifier_expression
  "COALESCE(code_sunset_events.hashed_org_id, code_sunset_events.org_id::text)"
end

.top_job_classes(scope, limit: 10) ⇒ Object



68
69
70
# File 'app/models/code_sunset/event.rb', line 68

def top_job_classes(scope, limit: 10)
  grouped_top(scope.where.not(job_class: [nil, ""]), "job_class", limit: limit)
end

.top_orgs(scope, limit: 10) ⇒ Object



72
73
74
# File 'app/models/code_sunset/event.rb', line 72

def top_orgs(scope, limit: 10)
  grouped_top(scope, org_identifier_expression, limit: limit, alias_name: "org_identifier")
end

.top_request_paths(scope, limit: 10) ⇒ Object



64
65
66
# File 'app/models/code_sunset/event.rb', line 64

def top_request_paths(scope, limit: 10)
  grouped_top(scope.where.not(request_path: [nil, ""]), "request_path", limit: limit)
end

.top_users(scope, limit: 10) ⇒ Object



76
77
78
# File 'app/models/code_sunset/event.rb', line 76

def top_users(scope, limit: 10)
  grouped_top(scope, user_identifier_expression, limit: limit, alias_name: "user_identifier")
end

.user_identifier_expression ⇒ Object



84
85
86
# File 'app/models/code_sunset/event.rb', line 84

def user_identifier_expression
  "COALESCE(code_sunset_events.hashed_user_id, code_sunset_events.user_id::text)"
end

Instance Method Details

#display_org_identifier ⇒ Object



174
175
176
# File 'app/models/code_sunset/event.rb', line 174

def display_org_identifier
  hashed_org_id.presence || org_id&.to_s
end

#display_user_identifier ⇒ Object



178
179
180
# File 'app/models/code_sunset/event.rb', line 178

def display_user_identifier
  hashed_user_id.presence || user_id&.to_s
end