Class: OpenC3::SystemHealthJson

Inherits:
Migration show all
Defined in:
lib/openc3/migrations/20260731000000_system_health_json.rb

Overview

The system_health setting was originally stored as a Ruby Hash. Local mode writes settings to disk with File.write and reads them back as a String, so the Hash round tripped into a Ruby inspect String which nothing can parse. Store it as a JSON string like every other setting and repair existing data.

Class Method Summary collapse

Class Method Details

.repair(data) ⇒ Hash

Returns the existing settings if they can be recovered, otherwise defaults.

Returns:

  • (Hash)

    the existing settings if they can be recovered, otherwise defaults



22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/openc3/migrations/20260731000000_system_health_json.rb', line 22

def self.repair(data)
  return ScopeModel::SYSTEM_HEALTH_DEFAULTS if data.nil?
  return data if Hash === data
  return ScopeModel::SYSTEM_HEALTH_DEFAULTS unless String === data
  begin
    parsed = JSON.parse(data)
  rescue JSON::ParserError
    # Ruby inspect format, e.g. {"cpu" => {"redThreshold" => 90, ... => nil}}
    # Ruby 3.3 and earlier had no spaces around the rocket. All the values are
    # numbers, booleans, or nil so there's nothing to accidentally rewrite.
    parsed = JSON.parse(data.gsub(/\s*=>\s*/, ': ').gsub(/\bnil\b/, 'null')) rescue nil
  end
  Hash === parsed ? parsed : ScopeModel::SYSTEM_HEALTH_DEFAULTS
end

.runObject



13
14
15
16
17
18
19
# File 'lib/openc3/migrations/20260731000000_system_health_json.rb', line 13

def self.run
  setting = SettingModel.get(name: 'system_health')
  data = repair(setting && setting['data'])
  SettingModel.set({ name: 'system_health', data: JSON.generate(data) }, scope: 'DEFAULT')
  # Repair the local mode file as well or sync_settings puts the bad data back
  LocalMode.save_setting('DEFAULT', 'system_health', JSON.generate(data))
end