Class: SourceDataPoint

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

Overview

Daily advertising metrics per campaign Source, written by
AdSpendSyncWorker from each provider's reporting API.

Spend parents on sources because revenue already attributes there (see
doc/architecture/SOURCE_ATTRIBUTION.md), so return-on-ad-spend is a join on
one key: sum ad_spend here against invoice revenue for the same
source_id. The generic time-series machinery lives in
Models::DataPointMetrics.

Constant Summary collapse

INVERTED_METRICS =

Spend is the one metric where lower is better at equal return.

%w[ad_spend].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!(source_id:, date:, metrics:, batch_id: nil) ⇒ void

This method returns an undefined value.

Upserts a day's metrics for one campaign source. Re-running a date is
idempotent (unique index on source/metric/period/reference).

Parameters:

  • source_id (Integer)
  • date (Date)

    the day the metrics cover

  • metrics (Hash{Symbol => Numeric})

    e.g. { ad_spend: 12.34, ad_clicks: 5 }

  • batch_id (String, nil) (defaults to: nil)

    uuid grouping one sync run



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/source_data_point.rb', line 28

def self.bulk_record!(source_id:, date:, metrics:, batch_id: nil)
  rows = metrics.compact.map do |metric_type, value|
    {
      source_id: source_id,
      metric_type: metric_type.to_s,
      value: value,
      period: "[#{date},#{date + 1})",
      reference: nil,
      source_batch_id: batch_id,
      recorded_at: Time.current
    }
  end
  upsert_data_points!(foreign_key: :source_id, rows: rows)
end

Instance Method Details

#sourceSource

Returns the campaign these metrics belong to.

Returns:

  • (Source)

    the campaign these metrics belong to



15
# File 'app/models/source_data_point.rb', line 15

belongs_to :source