Class: RhcParamSet

Inherits:
ApplicationRecord show all
Defined in:
app/models/rhc_param_set.rb

Overview

== Schema Information

Table name: rhc_param_sets
Database name: primary

id :integer not null, primary key
bom_version :integer
computed_at :datetime
computed_boms :jsonb
md5_hash_key :string not null
processed_parameters :jsonb
created_at :datetime
updated_at :datetime

Indexes

index_rhc_param_sets_on_md5_hash_key (md5_hash_key) UNIQUE

Constant Summary collapse

BOM_VERSION =

BOM format version - bump when output format changes to force recomputation
v1: Initial version
v2: Added 'features' array to BOM items (feature_1-5 from matview v04)
v3: Added 'recommended' flag to underlayment accessories
v4: Fixed missing product data for snowmelt-powermat/omnimat/ecomat URLs

5
FRESHNESS_DURATION =

How long cached BOMs remain valid before recomputation

24.hours

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#processed_parametersObject (readonly)



33
# File 'app/models/rhc_param_set.rb', line 33

validates :processed_parameters, presence: true

Class Method Details

.generate_md5_hash_key(processed_parameters) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'app/models/rhc_param_set.rb', line 84

def self.generate_md5_hash_key(processed_parameters)
  # Sort our params the same way
  pp = (processed_parameters || {}).sort.to_h.with_indifferent_access
  # Remove a key
  pp.delete(:share_key)
  # Jsonify and md5.  Note that in the database because we use a jsonb field
  # the json will be sorted by key length for query optimization. but as long as
  # consistently do our md5 from a sorted key set we are good.
  Digest::MD5.hexdigest(pp.to_json)
end

.invalidate_all_boms!Object

Class method to invalidate all cached BOMs (e.g., after price changes)



76
77
78
# File 'app/models/rhc_param_set.rb', line 76

def self.invalidate_all_boms!
  where.not(computed_boms: nil).update_all(computed_boms: nil, computed_at: nil, bom_version: nil)
end

Instance Method Details

#boms_fresh?Boolean

Check if cached BOMs are still valid

Returns:

  • (Boolean)


63
64
65
66
67
68
# File 'app/models/rhc_param_set.rb', line 63

def boms_fresh?
  computed_boms.present? &&
    bom_version == BOM_VERSION &&
    computed_at.present? &&
    computed_at > FRESHNESS_DURATION.ago
end

#calculate_quote(context_user:) ⇒ RoomConfiguration::CalculateQuote::Result

Returns cached BOMs if fresh, otherwise recomputes and caches

Parameters:

  • context_user (User)

    Required for catalog_id fallback

Returns:

  • (RoomConfiguration::CalculateQuote::Result)


40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/rhc_param_set.rb', line 40

def calculate_quote(context_user:)
  options = processed_parameters.deep_symbolize_keys
  options[:catalog_id] ||= context_user.customer.catalog.id
  options[:currency] ||= context_user.customer.catalog.currency

  if boms_fresh?
    Rails.logger.debug { "RhcParamSet#calculate_quote: returning cached BOMs for #{md5_hash_key}" }
    build_cached_result(options)
  else
    Rails.logger.info { "RhcParamSet#calculate_quote: computing fresh BOMs for #{md5_hash_key}" }
    compute_and_cache_boms(options)
  end
end

#invalidate_boms!Object

Clear cached BOMs (useful for invalidation)



71
72
73
# File 'app/models/rhc_param_set.rb', line 71

def invalidate_boms!
  update_columns(computed_boms: nil, computed_at: nil, bom_version: nil)
end

#recompute_boms!(context_user:) ⇒ Object

Force recomputation of BOMs, bypassing cache



55
56
57
58
59
60
# File 'app/models/rhc_param_set.rb', line 55

def recompute_boms!(context_user:)
  options = processed_parameters.deep_symbolize_keys
  options[:catalog_id] ||= context_user.customer.catalog.id
  options[:currency] ||= context_user.customer.catalog.currency
  compute_and_cache_boms(options)
end

#store_md5_hash_keyObject



80
81
82
# File 'app/models/rhc_param_set.rb', line 80

def store_md5_hash_key
  self.md5_hash_key = self.class.generate_md5_hash_key(processed_parameters)
end