Module: LlmCostTracker::PriceSync

Defined in:
lib/llm_cost_tracker/price_sync.rb,
lib/llm_cost_tracker/price_sync/fetcher.rb,
lib/llm_cost_tracker/price_sync/registry_diff.rb,
lib/llm_cost_tracker/price_sync/registry_loader.rb,
lib/llm_cost_tracker/price_sync/registry_writer.rb

Defined Under Namespace

Modules: RegistryDiff Classes: CheckResult, Fetcher, RefreshResult, RegistryLoader, RegistryWriter

Constant Summary collapse

DEFAULT_OUTPUT_PATH =
"config/llm_cost_tracker_prices.yml"
DEFAULT_REMOTE_URL =
"https://raw.githubusercontent.com/sergey-homenko/llm_cost_tracker/main/lib/llm_cost_tracker/prices.json"
SUPPORTED_SCHEMA_VERSION =
1

Class Method Summary collapse

Class Method Details

.check(path: DEFAULT_OUTPUT_PATH, url: DEFAULT_REMOTE_URL, fetcher: Fetcher.new, today: Date.today) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/llm_cost_tracker/price_sync.rb', line 52

def check(path: DEFAULT_OUTPUT_PATH, url: DEFAULT_REMOTE_URL, fetcher: Fetcher.new, today: Date.today)
  current = load_current_registry(path)
  response = fetcher.get(url, etag: current.dig("metadata", "source_version"))

  if response.not_modified
    return CheckResult.new(
      path: path,
      source_url: url,
      source_version: response.source_version,
      changes: {},
      up_to_date: true
    )
  end

  remote = normalize_remote_registry(response.body, url: url, response: response, today: today)
  changes = RegistryDiff.call(current.fetch("models", {}), remote.fetch("models", {}))

  CheckResult.new(
    path: path,
    source_url: url,
    source_version: response.source_version,
    changes: changes,
    up_to_date: changes.empty?
  )
end

.configured_output_path(env: ENV, config: LlmCostTracker.configuration) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/llm_cost_tracker/price_sync.rb', line 23

def configured_output_path(env: ENV, config: LlmCostTracker.configuration)
  output = env["OUTPUT"].to_s.strip
  return output unless output.empty?

  prices_file = config.prices_file
  return prices_file.to_s if prices_file

  default_output_path
end

.configured_remote_url(env: ENV) ⇒ Object



33
34
35
36
# File 'lib/llm_cost_tracker/price_sync.rb', line 33

def configured_remote_url(env: ENV)
  url = env["URL"].to_s.strip
  url.empty? ? DEFAULT_REMOTE_URL : url
end

.refresh(path: DEFAULT_OUTPUT_PATH, url: DEFAULT_REMOTE_URL, preview: false, fetcher: Fetcher.new, today: Date.today) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/llm_cost_tracker/price_sync.rb', line 38

def refresh(path: DEFAULT_OUTPUT_PATH, url: DEFAULT_REMOTE_URL, preview: false, fetcher: Fetcher.new,
            today: Date.today)
  current = load_current_registry(path)
  response = fetcher.get(url, etag: current.dig("metadata", "source_version"))

  if response.not_modified
    return refresh_result(path, url, response, current, current, written: false, not_modified: true)
  end

  remote = normalize_remote_registry(response.body, url: url, response: response, today: today)
  RegistryWriter.new.call(path: path, registry: remote) unless preview
  refresh_result(path, url, response, current, remote, written: !preview, not_modified: false)
end