Class: OpenC3::LimitsEventTopic

Inherits:
Topic show all
Defined in:
lib/openc3/topics/limits_event_topic.rb

Overview

LimitsEventTopic keeps track of not only the <SCOPE>__openc3_limits_events topic but also the ancillary key value stores. The LIMITS_CHANGE event updates the <SCOPE>__current_limits key. The LIMITS_SET event updates the <SCOPE>__limits_sets. The LIMITS_SETTINGS event updates the <SCOPE>__current_limits_settings. While this isn’t a clean separation of topics (streams) and models (key-value) it helps maintain consistency as the topic and model are linked.

Class Method Summary collapse

Methods inherited from Topic

all_same_db_shard?, clear_topics, del, get_cnt, get_last_offset, get_newest_message, get_oldest_message, group_topics_by_db_shard, method_missing, read_topics, trim_topic, update_topic_offsets, write_ack, write_topic

Class Method Details

._active_db_shards(scope:) ⇒ Object

Collect all unique target db_shards from TargetModel



29
30
31
32
33
34
35
36
# File 'lib/openc3/topics/limits_event_topic.rb', line 29

def self._active_db_shards(scope:)
  db_shards = Set.new([0])
  Store.hgetall("#{scope}__openc3_targets").each do |_name, json|
    parsed = JSON.parse(json, allow_nan: true, create_additions: true)
    db_shards << (parsed['db_shard'] || 0).to_i
  end
  db_shards
end

.current_set(scope:) ⇒ Object



142
143
144
# File 'lib/openc3/topics/limits_event_topic.rb', line 142

def self.current_set(scope:)
  sets(scope: scope).key('true') || "DEFAULT"
end

.delete(target_name, packet_name = nil, scope:) ⇒ Object

Cleanups up the current_limits and current_limits_settings keys for a target or target/packet combination



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/openc3/topics/limits_event_topic.rb', line 148

def self.delete(target_name, packet_name = nil, scope:)
  limits = Store.hgetall("#{scope}__current_limits")
  limits.each do |item, _limits_state|
    if packet_name
      if item =~ /^#{target_name}__#{packet_name}__/
        Store.hdel("#{scope}__current_limits", item)
      end
    else
      if item =~ /^#{target_name}__/
        Store.hdel("#{scope}__current_limits", item)
      end
    end
  end

  limits_settings = Store.hgetall("#{scope}__current_limits_settings")
  limits_settings.each do |item, _limits_settings|
    if packet_name
      if item =~ /^#{target_name}__#{packet_name}__/
        Store.hdel("#{scope}__current_limits_settings", item)
      end
    else
      if item =~ /^#{target_name}__/
        Store.hdel("#{scope}__current_limits_settings", item)
      end
    end
  end
end

.out_of_limits(scope:) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
# File 'lib/openc3/topics/limits_event_topic.rb', line 122

def self.out_of_limits(scope:)
  out_of_limits = []
  limits = Store.hgetall("#{scope}__current_limits")
  limits.each do |item, limits_state|
    if %w(RED RED_HIGH RED_LOW YELLOW YELLOW_HIGH YELLOW_LOW).include?(limits_state)
      target_name, packet_name, item_name = item.split('__')
      out_of_limits << [target_name, packet_name, item_name, limits_state]
    end
  end
  out_of_limits
end

.process_event(event, telemetry: nil) ⇒ Object

Process a single limits event hash and update the local System accordingly. Called inline by DecomMicroservice when reading from the limits events topic.



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/openc3/topics/limits_event_topic.rb', line 207

def self.process_event(event, telemetry: nil)
  telemetry ||= System.telemetry.all
  case event['type']
  when 'LIMITS_CHANGE'
    # Ignore
  when 'LIMITS_SETTINGS'
    target_name = event['target_name']
    packet_name = event['packet_name']
    item_name = event['item_name']
    target = telemetry[target_name]
    if target
      packet = target[packet_name]
      if packet
        enabled = ConfigParser.handle_true_false_nil(event['enabled'])
        persistence = event['persistence']
        System.limits.set(target_name, packet_name, item_name,
            event['red_low'], event['yellow_low'], event['yellow_high'], event['red_high'],
            event['green_low'], event['green_high'], event['limits_set'], persistence, enabled)
      end
    end

  when 'LIMITS_ENABLE_STATE'
    target_name = event['target_name']
    packet_name = event['packet_name']
    item_name = event['item_name']
    target = telemetry[target_name]
    if target
      packet = target[packet_name]
      if packet
        enabled = ConfigParser.handle_true_false_nil(event['enabled'])
        if enabled
          System.limits.enable(target_name, packet_name, item_name)
        else
          System.limits.disable(target_name, packet_name, item_name)
        end
      end
    end

  when 'LIMITS_SET'
    System.limits_set = event['set']
  end
end

.read(offset = nil, count: 100, scope:) ⇒ Object

Remove the JSON encoding to return hashes directly



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/openc3/topics/limits_event_topic.rb', line 101

def self.read(offset = nil, count: 100, scope:)
  final_result = []
  topic = "#{scope}__openc3_limits_events"
  if offset
    result = Topic.read_topics([topic], [offset], nil, count)
    if not result.empty?
      # result is a hash with the topic key followed by an array of results
      # This returns just the array of arrays [[offset, hash], [offset, hash], ...]
      final_result = result[topic]
    end
  else
    result = Topic.get_newest_message(topic)
    final_result = [result] if result
  end
  parsed_result = []
  final_result.each do |offset, hash|
    parsed_result << [offset, JSON.parse(hash['event'], allow_nan: true, create_additions: true)]
  end
  return parsed_result
end

.sets(scope:) ⇒ Hash{String => String}

Returns all the limits sets as keys with the value ‘true’ or ‘false’ where only the active set is ‘true’

Returns:

  • (Hash{String => String})

    Set name followed by 'true' if enabled else 'false'



138
139
140
# File 'lib/openc3/topics/limits_event_topic.rb', line 138

def self.sets(scope:)
  Store.hgetall("#{scope}__limits_sets")
end

.sync_system(scope:) ⇒ Object

Update the local System based on overall state



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/openc3/topics/limits_event_topic.rb', line 177

def self.sync_system(scope:)
  all_limits_settings = Store.hgetall("#{scope}__current_limits_settings")
  telemetry = System.telemetry.all
  all_limits_settings.each do |item, limits_settings|
    target_name, packet_name, item_name = item.split('__')
    target = telemetry[target_name]
    if target
      packet = target[packet_name]
      if packet
        limits_settings = JSON.parse(limits_settings, allow_nan: true, create_additions: true)
        enabled = limits_settings['enabled']
        persistence = limits_settings['persistence_setting']
        limits_settings.each do |limits_set, settings|
          next unless Hash === settings
          System.limits.set(target_name, packet_name, item_name, settings['red_low'], settings['yellow_low'], settings['yellow_high'], settings['red_high'], settings['green_low'], settings['green_high'], limits_set.to_s.intern, persistence, enabled)
        end
        if not enabled.nil?
          if enabled
            System.limits.enable(target_name, packet_name, item_name)
          else
            System.limits.disable(target_name, packet_name, item_name)
          end
        end
      end
    end
  end
end

.sync_system_thread_body(scope:, block_ms: nil) ⇒ Object

Update the local system based on limits events (standalone read loop). Still available for non-decom consumers that need to sync limits.



252
253
254
255
256
257
258
259
# File 'lib/openc3/topics/limits_event_topic.rb', line 252

def self.sync_system_thread_body(scope:, block_ms: nil)
  telemetry = System.telemetry.all
  topics = ["#{scope}__openc3_limits_events"]
  Topic.read_topics(topics, nil, block_ms) do |_topic, _msg_id, event, _redis|
    event = JSON.parse(event['event'], allow_nan: true, create_additions: true)
    process_event(event, telemetry: telemetry)
  end
end

.write(event, scope:) ⇒ Object



38
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/openc3/topics/limits_event_topic.rb', line 38

def self.write(event, scope:)
  case event[:type]
  when :LIMITS_CHANGE
    # The current_limits hash keeps only the current limits state of items
    # It is used by the API to determine the overall limits state
    field = "#{event[:target_name]}__#{event[:packet_name]}__#{event[:item_name]}"
    Store.hset("#{scope}__current_limits", field, event[:new_limits_state])

  when :LIMITS_SETTINGS
    # Limits updated in limits_api.rb to avoid circular reference to TargetModel
    unless sets(scope: scope).has_key?(event[:limits_set])
      Store.hset("#{scope}__limits_sets", event[:limits_set], 'false')
    end

    field = "#{event[:target_name]}__#{event[:packet_name]}__#{event[:item_name]}"
    limits_settings = Store.hget("#{scope}__current_limits_settings", field)
    if limits_settings
      limits_settings = JSON.parse(limits_settings, allow_nan: true, create_additions: true)
    else
      limits_settings = {}
    end
    limits = {}
    limits['red_low'] = event[:red_low]
    limits['yellow_low'] = event[:yellow_low]
    limits['yellow_high'] = event[:yellow_high]
    limits['red_high'] = event[:red_high]
    limits['green_low'] = event[:green_low] if event[:green_low] && event[:green_high]
    limits['green_high'] = event[:green_high] if event[:green_low] && event[:green_high]
    limits_settings[event[:limits_set].to_s] = limits
    limits_settings['persistence_setting'] = event[:persistence] if event[:persistence]
    limits_settings['enabled'] = event[:enabled] if not event[:enabled].nil?
    Store.hset("#{scope}__current_limits_settings", field, JSON.generate(limits_settings, allow_nan: true))

  when :LIMITS_ENABLE_STATE
    field = "#{event[:target_name]}__#{event[:packet_name]}__#{event[:item_name]}"
    limits_settings = Store.hget("#{scope}__current_limits_settings", field)
    if limits_settings
      limits_settings = JSON.parse(limits_settings, allow_nan: true, create_additions: true)
    else
      limits_settings = {}
    end
    limits_settings['enabled'] = event[:enabled]
    Store.hset("#{scope}__current_limits_settings", field, JSON.generate(limits_settings, allow_nan: true))

  when :LIMITS_SET
    sets = sets(scope: scope)
    raise "Set '#{event[:set]}' does not exist!" unless sets.key?(event[:set])

    # Set all existing sets to "false"
    sets = sets.transform_values! { |_key, _value| "false" }
    sets[event[:set]] = "true" # Enable the requested set
    Store.hmset("#{scope}__limits_sets", *sets)
  else
    raise "Invalid limits event type '#{event[:type]}'"
  end

  # Write to all active db_shards so each decom microservice can read limits events inline
  _active_db_shards(scope: scope).each do |db_shard|
    Topic.write_topic("#{scope}__openc3_limits_events", {event: JSON.generate(event, allow_nan: true)}, '*', 1000, db_shard: db_shard)
  end
end