summaryrefslogtreecommitdiff
path: root/app/models/product_analytics_event.rb
blob: d2026d3b33392820c8cf3f64b618fab206852954 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# frozen_string_literal: true

class ProductAnalyticsEvent < ApplicationRecord
  self.table_name = 'product_analytics_events_experimental'

  # Ignore that the partition key :project_id is part of the formal primary key
  self.primary_key = :id

  belongs_to :project

  validates :event_id, :project_id, :v_collector, :v_etl, presence: true

  # There is no default Rails timestamps in the table.
  # collector_tstamp is a timestamp when a collector recorded an event.
  scope :order_by_time, -> { order(collector_tstamp: :desc) }

  # If we decide to change this scope to use date_trunc('day', collector_tstamp),
  # we should remember that a btree index on collector_tstamp will be no longer effective.
  scope :timerange, ->(duration, today = Time.zone.today) {
    where('collector_tstamp BETWEEN ? AND ? ', today - duration + 1, today + 1)
  }

  scope :by_category_and_action, ->(category, action) { where(se_category: category, se_action: action) }

  def self.count_by_graph(graph, days)
    group(graph).timerange(days).count
  end

  def self.count_collector_tstamp_by_day(days)
    group("DATE_TRUNC('day', collector_tstamp)")
      .reorder('date_trunc_day_collector_tstamp')
      .timerange(days)
      .count
  end

  def as_json_wo_empty
    as_json.compact
  end
end