Module: LlmCostTracker::Pricing::Sync

Defined in:
lib/llm_cost_tracker/pricing/sync.rb,
lib/llm_cost_tracker/pricing/sync/fetcher.rb,
lib/llm_cost_tracker/pricing/sync/registry_diff.rb,
lib/llm_cost_tracker/pricing/sync/change_printer.rb,
lib/llm_cost_tracker/pricing/sync/registry_writer.rb

Defined Under Namespace

Modules: ChangePrinter, RegistryDiff Classes: CheckResult, Fetcher, RefreshResult, 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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/llm_cost_tracker/pricing/sync.rb', line 75

def check(path: DEFAULT_OUTPUT_PATH, url: DEFAULT_REMOTE_URL, fetcher: Fetcher.new, today: Date.today)
  current = load_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 = registry_changes(current, remote)

  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



25
26
27
28
29
30
31
32
33
# File 'lib/llm_cost_tracker/pricing/sync.rb', line 25

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

  prices_file = config.prices_file
  return prices_file.to_s if prices_file

  Rails.root.join(DEFAULT_OUTPUT_PATH).to_s
end

.configured_remote_url(env: ENV) ⇒ Object



35
36
37
# File 'lib/llm_cost_tracker/pricing/sync.rb', line 35

def configured_remote_url(env: ENV)
  env["URL"].to_s.strip.presence || DEFAULT_REMOTE_URL
end

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/llm_cost_tracker/pricing/sync.rb', line 39

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

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

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