Class: LittleGhost::Models::Catalog
- Inherits:
-
Object
- Object
- LittleGhost::Models::Catalog
- Defined in:
- lib/little_ghost/models/catalog.rb,
lib/little_ghost/models/catalog/source.rb,
lib/little_ghost/models/catalog/models_dev_source.rb
Overview
Resolves model facts from refreshed data and the snapshot packaged with LittleGhost. Refresh is always explicit.
Defined Under Namespace
Classes: ModelsDevSource, Source
Constant Summary collapse
- ARRAY_ATTRIBUTES =
:nodoc:
%i[input_modalities output_modalities supported_parameters].freeze
- SNAPSHOT_PATH =
Path to the offline model metadata snapshot packaged with the gem.
File.("../data/model_catalog.json", __dir__)
- MAX_CATALOG_BYTES =
:nodoc:
25 * 1024 * 1024
Instance Method Summary collapse
-
#details(target) ⇒ Object
Returns immutable normalized details for
target. -
#initialize(sources: [], snapshot_path: SNAPSHOT_PATH, clock: -> { Time.now.utc }) ⇒ Catalog
constructor
Builds a layered catalog from bundled and refreshed facts.
-
#refresh!(target: nil) ⇒ Object
Refreshes one target or all models.
Constructor Details
#initialize(sources: [], snapshot_path: SNAPSHOT_PATH, clock: -> { Time.now.utc }) ⇒ Catalog
Builds a layered catalog from bundled and refreshed facts.
18 19 20 21 22 23 24 |
# File 'lib/little_ghost/models/catalog.rb', line 18 def initialize(sources: [], snapshot_path: SNAPSHOT_PATH, clock: -> { Time.now.utc }) @sources = Array(sources).freeze @snapshot = load_snapshot(snapshot_path) @refreshed = {} @clock = clock @mutex = Mutex.new end |
Instance Method Details
#details(target) ⇒ Object
Returns immutable normalized details for target.
27 28 29 30 31 |
# File 'lib/little_ghost/models/catalog.rb', line 27 def details(target) target = Target.parse(target) records = [@snapshot[target.to_s], @mutex.synchronize { @refreshed[target.to_s] }].compact merge_details(target, records) end |
#refresh!(target: nil) ⇒ Object
Refreshes one target or all models. Failed sources leave the last good catalog untouched and are returned to the caller for reporting.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/little_ghost/models/catalog.rb', line 35 def refresh!(target: nil) requested = target && Target.parse(target) records = {} errors = [] @sources.each do |source| values = source.refresh(target: requested) normalized = normalize_records( values, source.name, attribute_merge_strategies: source.attribute_merge_strategies ) records = records.merge(normalized) { |_key, older, newer| merge_records(older, newer) } rescue => error errors << error end @mutex.synchronize { @refreshed = @refreshed.merge(records) { |_key, older, newer| merge_records(older, newer) } } unless records.empty? {updated: records.keys.freeze, errors: errors.freeze}.freeze end |