Class: LlmCostTracker::PriceSync::RefreshPlanBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/llm_cost_tracker/price_sync/refresh_plan_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(sources:, loader: RegistryLoader.new) ⇒ RefreshPlanBuilder

Returns a new instance of RefreshPlanBuilder.



6
7
8
9
# File 'lib/llm_cost_tracker/price_sync/refresh_plan_builder.rb', line 6

def initialize(sources:, loader: RegistryLoader.new)
  @sources = sources
  @loader = loader
end

Instance Method Details

#call(path:, seed_path:, fetcher:, today:) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/llm_cost_tracker/price_sync/refresh_plan_builder.rb', line 11

def call(path:, seed_path:, fetcher:, today:)
  path = path.to_s
  registry = loader.call(path: path, seed_path: seed_path)
  current_models = registry.fetch("models", {})
  source_results, failed_sources = fetch_all(current_models, fetcher)
  merged, discrepancies = Merger.new.merge(source_results)
  validated = Validator.new.validate_batch(merged, existing_registry: current_models)
  updated_models = apply_changes(current_models, validated.accepted, today)

  PriceSync::RefreshPlan.new(
    path: path,
    registry: registry,
    updated_registry: registry.merge(
      "metadata" => (
        registry["metadata"],
        today,
        refresh_succeeded: source_results.any? { |_source, result| result.prices.any? },
        source_results: source_results
      ),
      "models" => updated_models
    ),
    accepted: validated.accepted,
    changes: price_changes(current_models, updated_models),
    orphaned_models: compute_orphaned(current_models, merged.keys),
    failed_sources: failed_sources,
    discrepancies: discrepancies,
    rejected: validated.rejected,
    flagged: validated.flagged,
    sources_used: source_usage(source_results),
    source_results: source_results
  )
end