Class: OpenC3::LimitsEventTopic
- Defined in:
- lib/openc3/topics/limits_event_topic.rb
Overview
LimitsEventTopic keeps track of not only the
Class Method Summary collapse
-
._active_db_shards(scope:) ⇒ Object
Collect all unique target db_shards from TargetModel.
- .current_set(scope:) ⇒ Object
-
.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.
-
.delete_set(set_name, scope:) ⇒ Object
Removes a limits set from the limits_sets hash and from the current_limits_settings of every item.
- .out_of_limits(scope:) ⇒ Object
-
.process_event(event, telemetry: nil) ⇒ Object
Process a single limits event hash and update the local System accordingly.
-
.read(offset = nil, count: 100, scope:) ⇒ Object
Remove the JSON encoding to return hashes directly.
-
.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'.
-
.sync_system(scope:) ⇒ Object
Update the local System based on overall state.
-
.sync_system_thread_body(scope:, block_ms: nil) ⇒ Object
Update the local system based on limits events (standalone read loop).
- .write(event, scope:) ⇒ Object
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
161 162 163 |
# File 'lib/openc3/topics/limits_event_topic.rb', line 161 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
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/openc3/topics/limits_event_topic.rb', line 167 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 |
.delete_set(set_name, scope:) ⇒ Object
Removes a limits set from the limits_sets hash and from the current_limits_settings of every item. Note that the set is not removed from the TargetModel packet definitions; that is cleaned up on the next plugin install. Running microservices will continue to hold the set in memory until they restart and resync from current_limits_settings.
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/openc3/topics/limits_event_topic.rb', line 200 def self.delete_set(set_name, scope:) set_name = set_name.to_s limits_settings = Store.hgetall("#{scope}__current_limits_settings") # Collect all changed items and write them back in a single hmset to # avoid a Redis round trip per item (this hash can be large) updates = {} limits_settings.each do |item, settings| settings = JSON.parse(settings, allow_nan: true, create_additions: true) if settings.key?(set_name) settings.delete(set_name) updates[item] = JSON.generate(settings, allow_nan: true) end end Store.hmset("#{scope}__current_limits_settings", *updates.flatten) unless updates.empty? Store.hdel("#{scope}__limits_sets", set_name) end |
.out_of_limits(scope:) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/openc3/topics/limits_event_topic.rb', line 141 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.
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/openc3/topics/limits_event_topic.rb', line 254 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_STATE_COLOR' 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 System.limits.set_state_color(target_name, packet_name, item_name, event['state_name'], event['color']) 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
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/openc3/topics/limits_event_topic.rb', line 120 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.(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'
157 158 159 |
# File 'lib/openc3/topics/limits_event_topic.rb', line 157 def self.sets(scope:) Store.hgetall("#{scope}__limits_sets") end |
.sync_system(scope:) ⇒ Object
Update the local System based on overall state
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 249 250 |
# File 'lib/openc3/topics/limits_event_topic.rb', line 218 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 if limits_set == 'state_colors' settings.each do |state_name, color| System.limits.set_state_color(target_name, packet_name, item_name, state_name, color) end next end 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.
311 312 313 314 315 316 317 318 |
# File 'lib/openc3/topics/limits_event_topic.rb', line 311 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# 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. # When the event originates from a stored packet in a non-PROCESS mode # (LOG or DISABLE), skip updating current_limits so that historical # data does not affect the real-time overall limits state or the # out_of_limits API response. unless event[:suppress_stored] field = "#{event[:target_name]}__#{event[:packet_name]}__#{event[:item_name]}" Store.hset("#{scope}__current_limits", field, event[:new_limits_state]) end 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_STATE_COLOR # Persist the state color so it survives a decom microservice restart (applied by sync_system) 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['state_colors'] ||= {} limits_settings['state_colors'][event[:state_name].to_s] = event[:color].to_s 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 |