Class: Aidp::Harness::ProviderMetrics

Inherits:
Object
  • Object
show all
Includes:
RescueLogging
Defined in:
lib/aidp/harness/provider_metrics.rb

Overview

Persists provider metrics and rate limit information to disk Enables the provider dashboard to display real-time state

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RescueLogging

__log_rescue_impl, log_rescue, #log_rescue

Constructor Details

#initialize(project_dir) ⇒ ProviderMetrics

Returns a new instance of ProviderMetrics.



17
18
19
20
21
22
23
# File 'lib/aidp/harness/provider_metrics.rb', line 17

def initialize(project_dir)
  # Store metrics at the repository root so different worktrees/modes share state
  @project_dir = Aidp::Util.find_project_root(project_dir)
  @metrics_file = File.join(@project_dir, ".aidp", "provider_metrics.yml")
  @rate_limit_file = File.join(@project_dir, ".aidp", "provider_rate_limits.yml")
  ensure_directory
end

Instance Attribute Details

#metrics_fileObject (readonly)

Returns the value of attribute metrics_file.



15
16
17
# File 'lib/aidp/harness/provider_metrics.rb', line 15

def metrics_file
  @metrics_file
end

#project_dirObject (readonly)

Returns the value of attribute project_dir.



15
16
17
# File 'lib/aidp/harness/provider_metrics.rb', line 15

def project_dir
  @project_dir
end

#rate_limit_fileObject (readonly)

Returns the value of attribute rate_limit_file.



15
16
17
# File 'lib/aidp/harness/provider_metrics.rb', line 15

def rate_limit_file
  @rate_limit_file
end

Instance Method Details

#clearObject

Clear all persisted metrics



78
79
80
81
# File 'lib/aidp/harness/provider_metrics.rb', line 78

def clear
  File.delete(@metrics_file) if File.exist?(@metrics_file)
  File.delete(@rate_limit_file) if File.exist?(@rate_limit_file)
end

#load_metricsObject

Load provider metrics from disk



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/aidp/harness/provider_metrics.rb', line 38

def load_metrics
  return {} unless File.exist?(@metrics_file)

  data = YAML.safe_load_file(@metrics_file, permitted_classes: [Time, Date, Symbol], aliases: true)
  return {} unless data.is_a?(Hash)

  # Convert ISO8601 strings back to Time objects
  deserialize_metrics(data)
rescue => e
  log_rescue(e, component: "provider_metrics", action: "load_metrics", fallback: {})
  {}
end

#load_rate_limitsObject

Load rate limit information from disk



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/aidp/harness/provider_metrics.rb', line 64

def load_rate_limits
  return {} unless File.exist?(@rate_limit_file)

  data = YAML.safe_load_file(@rate_limit_file, permitted_classes: [Time, Date, Symbol], aliases: true)
  return {} unless data.is_a?(Hash)

  # Convert ISO8601 strings back to Time objects
  deserialize_rate_limits(data)
rescue => e
  log_rescue(e, component: "provider_metrics", action: "load_rate_limits", fallback: {})
  {}
end

#save_metrics(metrics_hash) ⇒ Object

Save provider metrics to disk



26
27
28
29
30
31
32
33
34
35
# File 'lib/aidp/harness/provider_metrics.rb', line 26

def save_metrics(metrics_hash)
  return if metrics_hash.nil? || metrics_hash.empty?

  # Convert Time objects to ISO8601 strings for YAML serialization
  serializable_metrics = serialize_metrics(metrics_hash)

  File.write(@metrics_file, YAML.dump(serializable_metrics))
rescue => e
  log_rescue(e, component: "provider_metrics", action: "save_metrics", fallback: nil)
end

#save_rate_limits(rate_limit_hash) ⇒ Object

Save rate limit information to disk



52
53
54
55
56
57
58
59
60
61
# File 'lib/aidp/harness/provider_metrics.rb', line 52

def save_rate_limits(rate_limit_hash)
  return if rate_limit_hash.nil? || rate_limit_hash.empty?

  # Convert Time objects to ISO8601 strings for YAML serialization
  serializable_rate_limits = serialize_rate_limits(rate_limit_hash)

  File.write(@rate_limit_file, YAML.dump(serializable_rate_limits))
rescue => e
  log_rescue(e, component: "provider_metrics", action: "save_rate_limits", fallback: nil)
end