Class: CatalogDataPoint

Inherits:
ApplicationRecord show all
Includes:
Models::DataPointMetrics
Defined in:
app/models/catalog_data_point.rb

Overview

== Schema Information

Table name: catalog_data_points
Database name: primary

id :bigint not null, primary key
metric_type :enum not null
period :daterange not null
recorded_at :datetime not null
reference :string
value :decimal(15, 4) not null
created_at :datetime not null
updated_at :datetime not null
catalog_id :bigint not null
source_batch_id :uuid

Indexes

idx_catalog_data_points_batch (source_batch_id) WHERE (source_batch_id IS NOT NULL)
idx_catalog_data_points_lookup (catalog_id,metric_type,period) USING gist
idx_catalog_data_points_unique_metric_period (catalog_id, metric_type, period, COALESCE(reference, ''::character varying)) UNIQUE

Foreign Keys

fk_rails_... (catalog_id => catalogs.id) ON DELETE => cascade

Daily time-series of retailer-compliance counts per Catalog, written by
RetailerComplianceReportWorker from Retailer::DailyComplianceReport. Backs
the trend charts and trend arrows on the /retailers dashboards; every metric
is a point-in-time count that can't be reconstructed retroactively, so history
accrues from launch. The generic time-series machinery lives in
Models::DataPointMetrics.

Constant Summary collapse

INVERTED_METRICS =

Every compliance metric except active_public is a problem count where
lower is better; active_public (and the derived health gauge) grow when
things improve.

%w[
  map_violations
  promotions
  price_diverging
  out_of_stock_active
  listing_issues
  scrape_failures
  unreachable_online
  out_of_stock_online
  problem_items
].freeze

Constants included from Models::Schedulable

Models::Schedulable::SIMPLE_FORM_OPTIONS

Instance Attribute Summary

Attributes included from Models::DataPointMetrics

#metric_type, #period, #value

Belongs to collapse

Class Method Summary collapse

Methods included from Models::DataPointMetrics

by_period_start, by_recorded, containing_date, for_metric, for_reference, #inverted_metric?, inverted_metrics, latest, latest_values, overlapping, period_comparison, #period_days, #period_end, #period_start, recent, trend, trend_direction, upsert_data_points!

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Class Method Details

.bulk_record!(catalog:, metrics:, period_start:, period_end:, batch_id: nil) ⇒ void

This method returns an undefined value.

Bulk-record one catalog's compliance metrics for a period (upserts, so a
re-run of the daily worker is idempotent).

Parameters:

  • catalog (Catalog)
  • metrics (Hash{Symbol,String => Numeric})

    metric_type => count

  • period_start (Date)
  • period_end (Date)
  • batch_id (String, nil) (defaults to: nil)

    groups one day's run across catalogs



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/catalog_data_point.rb', line 65

def self.bulk_record!(catalog:, metrics:, period_start:, period_end:, batch_id: nil)
  batch_id ||= SecureRandom.uuid
  now = Time.current
  period_str = "[#{period_start},#{period_end}]"

  rows = metrics.filter_map do |metric_type, value|
    next if value.nil?

    {
      catalog_id: catalog.id,
      metric_type: metric_type.to_s,
      value:,
      period: period_str,
      reference: nil,
      source_batch_id: batch_id,
      recorded_at: now
    }
  end

  upsert_data_points!(foreign_key: :catalog_id, rows:)
end

Instance Method Details

#catalogCatalog

Returns the catalog this data point belongs to.

Returns:

  • (Catalog)

    the catalog this data point belongs to



39
# File 'app/models/catalog_data_point.rb', line 39

belongs_to :catalog