Class: SourceDataPoint
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- SourceDataPoint
- 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
Belongs to collapse
-
#source ⇒ Source
The campaign these metrics belong to.
Class Method Summary collapse
-
.bulk_record!(source_id:, date:, metrics:, batch_id: nil) ⇒ void
Upserts a day's metrics for one campaign source.
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
Methods included from Models::AfterCommittable
Methods included from Models::EventPublishable
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).
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
#source ⇒ Source
Returns the campaign these metrics belong to.
15 |
# File 'app/models/source_data_point.rb', line 15 belongs_to :source |