2020-07-09 11:08:59 -04:00
|
|
|
# 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
|
|
|
|
|
2020-07-16 20:09:37 -04:00
|
|
|
validates :event_id, :project_id, :v_collector, :v_etl, presence: true
|
|
|
|
|
2020-07-09 11:08:59 -04:00
|
|
|
# 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)
|
|
|
|
}
|
2020-07-27 05:09:42 -04:00
|
|
|
|
2020-07-30 11:09:40 -04:00
|
|
|
def self.count_by_graph(graph, days)
|
|
|
|
group(graph).timerange(days).count
|
|
|
|
end
|
|
|
|
|
2020-07-27 05:09:42 -04:00
|
|
|
def as_json_wo_empty
|
|
|
|
as_json.compact
|
|
|
|
end
|
2020-07-09 11:08:59 -04:00
|
|
|
end
|