Class: Gjallarhorn::History

Inherits:
Object
  • Object
show all
Defined in:
lib/gjallarhorn/history.rb

Overview

Deployment history tracking and management

Handles tracking of deployment history in a local JSON file, providing functionality to record deployments and query history.

Since:

  • 0.1.0

Constant Summary collapse

DEFAULT_HISTORY_DIR =

Default directory for storing history files

Since:

  • 0.1.0

File.expand_path("~/.gjallarhorn")
DEFAULT_HISTORY_FILE =

Default filename for history storage

Since:

  • 0.1.0

"history.json"

Instance Method Summary collapse

Constructor Details

#initialize(history_dir: DEFAULT_HISTORY_DIR) ⇒ History

Initialize history manager

Since:

  • 0.1.0



24
25
26
27
28
# File 'lib/gjallarhorn/history.rb', line 24

def initialize(history_dir: DEFAULT_HISTORY_DIR)
  @history_dir = history_dir
  @history_file = File.join(@history_dir, DEFAULT_HISTORY_FILE)
  ensure_history_directory
end

Instance Method Details

#available_versions(environment, limit: 10) ⇒ Array<String>

Get available versions for rollback

Since:

  • 0.1.0



92
93
94
95
96
97
98
# File 'lib/gjallarhorn/history.rb', line 92

def available_versions(environment, limit: 10)
  successful_deployments = get_history(environment: environment, limit: 50)
    .select { |record| record["status"] == "success" }
    .uniq { |record| record["image"] }

  successful_deployments.first(limit).map { |record| record["image"] }
end

#clear_history(environment: nil) ⇒ void

This method returns an undefined value.

Clear history for a specific environment or all environments

Since:

  • 0.1.0



104
105
106
107
108
109
110
111
# File 'lib/gjallarhorn/history.rb', line 104

def clear_history(environment: nil)
  if environment
    history_data = load_history.reject { |record| record["environment"] == environment }
    save_history(history_data)
  else
    save_history([])
  end
end

#ensure_history_directoryvoid (private)

This method returns an undefined value.

Ensure the history directory exists

Since:

  • 0.1.0



141
142
143
# File 'lib/gjallarhorn/history.rb', line 141

def ensure_history_directory
  FileUtils.mkdir_p(@history_dir) unless Dir.exist?(@history_dir)
end

#get_history(environment: nil, limit: 20) ⇒ Array<Hash>

Get deployment history for a specific environment

Since:

  • 0.1.0



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/gjallarhorn/history.rb', line 62

def get_history(environment: nil, limit: 20)
  history_data = load_history

  # Filter by environment if specified
  if environment
    history_data = history_data.select { |record| record["environment"] == environment }
  end

  # Sort by timestamp (most recent first) and limit results
  history_data.sort_by { |record| record["timestamp"] }.reverse.first(limit)
end

#last_successful_deployment(environment) ⇒ Hash?

Get the last successful deployment for an environment

Since:

  • 0.1.0



78
79
80
81
82
83
84
85
# File 'lib/gjallarhorn/history.rb', line 78

def last_successful_deployment(environment)
  history_data = load_history
  
  history_data
    .select { |record| record["environment"] == environment && record["status"] == "success" }
    .sort_by { |record| record["timestamp"] }
    .last
end

#load_historyArray<Hash> (private)

Load history data from file

Since:

  • 0.1.0



148
149
150
151
152
153
154
# File 'lib/gjallarhorn/history.rb', line 148

def load_history
  return [] unless File.exist?(@history_file)

  JSON.parse(File.read(@history_file))
rescue JSON::ParserError, Errno::ENOENT
  []
end

#record_deployment(environment:, image:, status:, strategy: nil, error: nil) ⇒ void

This method returns an undefined value.

Record a deployment attempt

Since:

  • 0.1.0



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/gjallarhorn/history.rb', line 38

def record_deployment(environment:, image:, status:, strategy: nil, error: nil)
  deployment_record = {
    timestamp: Time.now.utc.iso8601,
    environment: environment,
    image: image,
    status: status,
    strategy: strategy,
    error: error
  }.compact

  history_data = load_history
  history_data << deployment_record

  # Keep only the last 100 deployments to prevent file from growing too large
  history_data = history_data.last(100) if history_data.length > 100

  save_history(history_data)
end

#save_history(history_data) ⇒ void (private)

This method returns an undefined value.

Save history data to file

Since:

  • 0.1.0



160
161
162
# File 'lib/gjallarhorn/history.rb', line 160

def save_history(history_data)
  File.write(@history_file, JSON.pretty_generate(history_data))
end

#statistics(environment: nil) ⇒ Hash

Get deployment statistics

Since:

  • 0.1.0



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/gjallarhorn/history.rb', line 117

def statistics(environment: nil)
  history_data = load_history
  
  if environment
    history_data = history_data.select { |record| record["environment"] == environment }
  end

  total = history_data.length
  successful = history_data.count { |record| record["status"] == "success" }
  failed = history_data.count { |record| record["status"] == "failed" }

  {
    total_deployments: total,
    successful_deployments: successful,
    failed_deployments: failed,
    success_rate: total > 0 ? (successful.to_f / total * 100).round(2) : 0
  }
end