Class: Legion::Extensions::Coldstart::Helpers::Bootstrap

Inherits:
Object
  • Object
show all
Defined in:
lib/legion/extensions/coldstart/helpers/bootstrap.rb

Constant Summary collapse

LOCK =
Monitor.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBootstrap

Returns a new instance of Bootstrap.



27
28
29
30
31
32
33
# File 'lib/legion/extensions/coldstart/helpers/bootstrap.rb', line 27

def initialize
  @started_at = nil
  @observation_count = 0
  @firmware_loaded = false
  @calibration_state = :not_started
  load_from_local
end

Instance Attribute Details

#calibration_stateObject (readonly)

Returns the value of attribute calibration_state.



10
11
12
# File 'lib/legion/extensions/coldstart/helpers/bootstrap.rb', line 10

def calibration_state
  @calibration_state
end

#firmware_loadedObject (readonly)

Returns the value of attribute firmware_loaded.



10
11
12
# File 'lib/legion/extensions/coldstart/helpers/bootstrap.rb', line 10

def firmware_loaded
  @firmware_loaded
end

#observation_countObject (readonly)

Returns the value of attribute observation_count.



10
11
12
# File 'lib/legion/extensions/coldstart/helpers/bootstrap.rb', line 10

def observation_count
  @observation_count
end

#started_atObject (readonly)

Returns the value of attribute started_at.



10
11
12
# File 'lib/legion/extensions/coldstart/helpers/bootstrap.rb', line 10

def started_at
  @started_at
end

Class Method Details

.instanceObject



16
17
18
19
20
# File 'lib/legion/extensions/coldstart/helpers/bootstrap.rb', line 16

def instance
  LOCK.synchronize do
    @instance ||= new
  end
end

.reset!Object



22
23
24
# File 'lib/legion/extensions/coldstart/helpers/bootstrap.rb', line 22

def reset!
  LOCK.synchronize { @instance = nil }
end

Instance Method Details

#begin_imprint(force: false) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/legion/extensions/coldstart/helpers/bootstrap.rb', line 35

def begin_imprint(force: false)
  LOCK.synchronize do
    return if @started_at && !force

    @started_at = Time.now.utc
    @observation_count = 0 if force
    @calibration_state = :imprinting
    save_to_local
  end
end

#current_layerObject



69
70
71
72
73
# File 'lib/legion/extensions/coldstart/helpers/bootstrap.rb', line 69

def current_layer
  return :firmware unless @firmware_loaded

  Imprint.current_layer(started_at: @started_at, observation_count: @observation_count)
end

#current_multiplierObject



65
66
67
# File 'lib/legion/extensions/coldstart/helpers/bootstrap.rb', line 65

def current_multiplier
  Imprint.current_multiplier(started_at: @started_at, observation_count: @observation_count)
end

#imprint_active?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/legion/extensions/coldstart/helpers/bootstrap.rb', line 61

def imprint_active?
  Imprint.imprint_active?(started_at: @started_at, observation_count: @observation_count)
end

#load_firmwareObject



46
47
48
49
50
51
# File 'lib/legion/extensions/coldstart/helpers/bootstrap.rb', line 46

def load_firmware
  LOCK.synchronize do
    @firmware_loaded = true
    save_to_local
  end
end

#progressObject



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/legion/extensions/coldstart/helpers/bootstrap.rb', line 75

def progress
  {
    firmware_loaded:    @firmware_loaded,
    imprint_active:     imprint_active?,
    imprint_progress:   Imprint.imprint_progress(started_at: @started_at, observation_count: @observation_count),
    observation_count:  @observation_count,
    calibration_state:  @calibration_state,
    current_layer:      current_layer,
    current_multiplier: current_multiplier
  }
end

#record_observationObject



53
54
55
56
57
58
59
# File 'lib/legion/extensions/coldstart/helpers/bootstrap.rb', line 53

def record_observation
  LOCK.synchronize do
    @observation_count += 1
    check_calibration_progress
    save_to_local
  end
end