openc3 7.5.0 → 7.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (175) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +0 -0
  3. data/bin/openc3cli +82 -5
  4. data/data/config/_params.yaml +3 -0
  5. data/data/config/command_modifiers.yaml +5 -3
  6. data/data/config/default_plugins.yaml +1 -0
  7. data/data/config/interface_modifiers.yaml +17 -1
  8. data/data/config/item_modifiers.yaml +24 -1
  9. data/data/config/microservice.yaml +15 -1
  10. data/data/config/parameter_modifiers.yaml +4 -4
  11. data/data/config/plugins.yaml +511 -3
  12. data/data/config/settings.yaml +17 -0
  13. data/data/config/target.yaml +53 -0
  14. data/data/config/telemetry.yaml +58 -2
  15. data/data/config/widgets.yaml +30 -0
  16. data/lib/openc3/accessors/accessor.rb +14 -2
  17. data/lib/openc3/accessors/binary_accessor.rb +25 -5
  18. data/lib/openc3/api/CLAUDE.md +1 -0
  19. data/lib/openc3/api/api.rb +1 -0
  20. data/lib/openc3/api/cmd_api.rb +9 -1
  21. data/lib/openc3/api/interface_api.rb +7 -2
  22. data/lib/openc3/api/limits_api.rb +236 -195
  23. data/lib/openc3/api/router_api.rb +7 -2
  24. data/lib/openc3/api/settings_api.rb +4 -1
  25. data/lib/openc3/api/shared_resource_api.rb +107 -0
  26. data/lib/openc3/api/tlm_api.rb +229 -61
  27. data/lib/openc3/interfaces/http_client_interface.rb +84 -27
  28. data/lib/openc3/interfaces/protocols/preidentified_protocol.rb +6 -2
  29. data/lib/openc3/interfaces/tcpip_server_interface.rb +205 -125
  30. data/lib/openc3/io/json_api.rb +2 -10
  31. data/lib/openc3/io/json_api_object.rb +2 -10
  32. data/lib/openc3/logs/log_writer.rb +9 -8
  33. data/lib/openc3/logs/packet_log_writer.rb +8 -3
  34. data/lib/openc3/microservices/decom_common.rb +4 -1
  35. data/lib/openc3/microservices/decom_microservice.rb +73 -5
  36. data/lib/openc3/microservices/interface_decom_common.rb +16 -1
  37. data/lib/openc3/microservices/interface_microservice.rb +50 -11
  38. data/lib/openc3/microservices/limits_group_microservice.rb +513 -0
  39. data/lib/openc3/microservices/log_microservice.rb +22 -8
  40. data/lib/openc3/microservices/microservice.rb +8 -2
  41. data/lib/openc3/microservices/multi_microservice.rb +76 -22
  42. data/lib/openc3/microservices/queue_microservice.rb +29 -24
  43. data/lib/openc3/migrations/20260704000000_default_bridge.rb +1 -2
  44. data/lib/openc3/migrations/20261103000001_limits_group_backfill.rb +193 -0
  45. data/lib/openc3/models/bridge_model.rb +3 -3
  46. data/lib/openc3/models/cvt_model.rb +10 -3
  47. data/lib/openc3/models/host_interface_microservice_model.rb +4 -4
  48. data/lib/openc3/models/interface_model.rb +27 -7
  49. data/lib/openc3/models/microservice_group.rb +167 -0
  50. data/lib/openc3/models/microservice_model.rb +88 -2
  51. data/lib/openc3/models/panel_model.rb +109 -0
  52. data/lib/openc3/models/plugin_model.rb +1006 -26
  53. data/lib/openc3/models/plugin_store_model.rb +6 -1
  54. data/lib/openc3/models/protected_storage_model.rb +64 -0
  55. data/lib/openc3/models/queue_model.rb +19 -10
  56. data/lib/openc3/models/scope_model.rb +80 -3
  57. data/lib/openc3/models/setting_model.rb +2 -2
  58. data/lib/openc3/models/shared_resource_model.rb +381 -0
  59. data/lib/openc3/models/target_model.rb +333 -74
  60. data/lib/openc3/operators/microservice_operator.rb +76 -9
  61. data/lib/openc3/operators/operator.rb +29 -0
  62. data/lib/openc3/packets/commands.rb +16 -16
  63. data/lib/openc3/packets/limits.rb +2 -0
  64. data/lib/openc3/packets/packet.rb +75 -4
  65. data/lib/openc3/packets/packet_config.rb +152 -13
  66. data/lib/openc3/packets/packet_item.rb +48 -22
  67. data/lib/openc3/packets/packet_item_limits.rb +67 -6
  68. data/lib/openc3/packets/parsers/limits_parser.rb +69 -2
  69. data/lib/openc3/packets/parsers/packet_item_parser.rb +6 -0
  70. data/lib/openc3/packets/parsers/packet_parser.rb +45 -0
  71. data/lib/openc3/packets/parsers/state_parser.rb +1 -1
  72. data/lib/openc3/script/CLAUDE.md +1 -0
  73. data/lib/openc3/script/api_shared.rb +27 -17
  74. data/lib/openc3/script/calendar.rb +18 -6
  75. data/lib/openc3/script/commands.rb +11 -9
  76. data/lib/openc3/script/critical_cmd.rb +22 -2
  77. data/lib/openc3/script/extract.rb +52 -5
  78. data/lib/openc3/script/metadata.rb +30 -4
  79. data/lib/openc3/script/plugins.rb +8 -0
  80. data/lib/openc3/script/script.rb +9 -25
  81. data/lib/openc3/script/web_socket_api.rb +23 -17
  82. data/lib/openc3/system/system.rb +9 -3
  83. data/lib/openc3/top_level.rb +17 -0
  84. data/lib/openc3/topics/command_topic.rb +7 -1
  85. data/lib/openc3/topics/decom_interface_topic.rb +9 -3
  86. data/lib/openc3/topics/discovery_topic.rb +84 -0
  87. data/lib/openc3/topics/limits_event_topic.rb +317 -11
  88. data/lib/openc3/topics/router_topic.rb +6 -2
  89. data/lib/openc3/topics/system_events_topic.rb +9 -6
  90. data/lib/openc3/topics/telemetry_decom_topic.rb +12 -3
  91. data/lib/openc3/topics/topic.rb +9 -1
  92. data/lib/openc3/utilities/authentication.rb +67 -16
  93. data/lib/openc3/utilities/bucket.rb +5 -0
  94. data/lib/openc3/utilities/cli_generator.rb +117 -25
  95. data/lib/openc3/utilities/client_ip_header_filter.rb +38 -0
  96. data/lib/openc3/utilities/config_overlay.rb +24 -1
  97. data/lib/openc3/utilities/cosmos_rails_formatter.rb +18 -10
  98. data/lib/openc3/utilities/hardware_discovery.rb +279 -0
  99. data/lib/openc3/utilities/limits_rollup.rb +81 -0
  100. data/lib/openc3/utilities/local_mode.rb +48 -5
  101. data/lib/openc3/utilities/questdb_client.rb +256 -54
  102. data/lib/openc3/utilities/reingest_job.rb +12 -3
  103. data/lib/openc3/utilities/running_script.rb +206 -49
  104. data/lib/openc3/utilities/script.rb +24 -12
  105. data/lib/openc3/utilities/script_env.rb +164 -0
  106. data/lib/openc3/utilities/script_message_signing.rb +133 -0
  107. data/lib/openc3/utilities/secrets.rb +5 -0
  108. data/lib/openc3/utilities/store_autoload.rb +35 -2
  109. data/lib/openc3/utilities/store_queued.rb +5 -1
  110. data/lib/openc3/version.rb +5 -5
  111. data/templates/command_validator/command_validator.py +9 -9
  112. data/templates/{tool_react/pnpm-workspace.yaml → panel/.prettierrc.cjs} +7 -5
  113. data/templates/panel/Rakefile +24 -0
  114. data/templates/panel/package.json +17 -0
  115. data/templates/{tool_angular → panel}/pnpm-workspace.yaml +5 -0
  116. data/templates/panel/src/Panel.js +52 -0
  117. data/templates/panel/src/Panel.vue +60 -0
  118. data/templates/panel/src/panelSdk.js +24 -0
  119. data/templates/panel/vite-Panel.config.js +43 -0
  120. data/templates/tool_vue/package.json +3 -3
  121. data/templates/widget/package.json +2 -2
  122. metadata +24 -56
  123. data/templates/tool_angular/.editorconfig +0 -16
  124. data/templates/tool_angular/.gitignore +0 -44
  125. data/templates/tool_angular/.vscode/extensions.json +0 -4
  126. data/templates/tool_angular/.vscode/launch.json +0 -20
  127. data/templates/tool_angular/.vscode/tasks.json +0 -42
  128. data/templates/tool_angular/angular.json +0 -111
  129. data/templates/tool_angular/extra-webpack.config.js +0 -8
  130. data/templates/tool_angular/package.json +0 -48
  131. data/templates/tool_angular/src/app/app-routing.module.ts +0 -15
  132. data/templates/tool_angular/src/app/app.component.html +0 -22
  133. data/templates/tool_angular/src/app/app.component.scss +0 -18
  134. data/templates/tool_angular/src/app/app.component.spec.ts +0 -29
  135. data/templates/tool_angular/src/app/app.component.ts +0 -52
  136. data/templates/tool_angular/src/app/app.module.ts +0 -30
  137. data/templates/tool_angular/src/app/custom-overlay-container.ts +0 -17
  138. data/templates/tool_angular/src/app/empty-route/empty-route.component.ts +0 -7
  139. data/templates/tool_angular/src/app/openc3-api.d.ts +0 -1
  140. data/templates/tool_angular/src/assets/.gitkeep +0 -0
  141. data/templates/tool_angular/src/environments/environment.prod.ts +0 -3
  142. data/templates/tool_angular/src/environments/environment.ts +0 -16
  143. data/templates/tool_angular/src/favicon.ico +0 -0
  144. data/templates/tool_angular/src/index.html +0 -13
  145. data/templates/tool_angular/src/main.single-spa.ts +0 -40
  146. data/templates/tool_angular/src/single-spa/asset-url.ts +0 -12
  147. data/templates/tool_angular/src/single-spa/single-spa-props.ts +0 -8
  148. data/templates/tool_angular/src/styles.scss +0 -1
  149. data/templates/tool_angular/tsconfig.app.json +0 -13
  150. data/templates/tool_angular/tsconfig.json +0 -33
  151. data/templates/tool_angular/tsconfig.spec.json +0 -14
  152. data/templates/tool_react/.eslintrc +0 -7
  153. data/templates/tool_react/.gitignore +0 -71
  154. data/templates/tool_react/.prettierignore +0 -8
  155. data/templates/tool_react/babel.config.json +0 -29
  156. data/templates/tool_react/jest.config.js +0 -12
  157. data/templates/tool_react/package.json +0 -51
  158. data/templates/tool_react/src/openc3-tool_name.js +0 -24
  159. data/templates/tool_react/src/root.component.js +0 -88
  160. data/templates/tool_react/src/root.component.test.js +0 -9
  161. data/templates/tool_react/webpack.config.js +0 -27
  162. data/templates/tool_svelte/.gitignore +0 -71
  163. data/templates/tool_svelte/.prettierignore +0 -8
  164. data/templates/tool_svelte/.prettierrc.js +0 -5
  165. data/templates/tool_svelte/babel.config.js +0 -12
  166. data/templates/tool_svelte/build/smui.css +0 -1
  167. data/templates/tool_svelte/jest.config.js +0 -9
  168. data/templates/tool_svelte/package.json +0 -49
  169. data/templates/tool_svelte/pnpm-workspace.yaml +0 -20
  170. data/templates/tool_svelte/rollup.config.js +0 -74
  171. data/templates/tool_svelte/src/App.svelte +0 -42
  172. data/templates/tool_svelte/src/App.test.js +0 -9
  173. data/templates/tool_svelte/src/theme/_smui-theme.scss +0 -25
  174. data/templates/tool_svelte/src/tool_name.js +0 -17
  175. data/templates/widget/LICENSE.md +0 -20
@@ -25,17 +25,185 @@ module OpenC3
25
25
  # While this isn't a clean separation of topics (streams) and models (key-value)
26
26
  # it helps maintain consistency as the topic and model are linked.
27
27
  class LimitsEventTopic < Topic
28
+ LIMITS_GROUP_PACKET = 'LIMITS_GROUP'.freeze
29
+
30
+ # Limits groups are stored in <SCOPE>__limits_groups under <TARGET>__<GROUP>.
31
+ # Group names can't contain a double underscore so the key is unambiguous.
32
+ #
33
+ # @return [String] The store key of the group
34
+ def self.limits_group_key(target_name, group_name)
35
+ "#{target_name}__#{group_name}"
36
+ end
37
+
38
+ # @return [Array(String, String)] The target name and group name of a store key.
39
+ # Groups stored before they were owned by a target have no target name.
40
+ def self.split_limits_group_key(key)
41
+ target_name, group_name = key.split('__', 2)
42
+ return [nil, key] unless group_name
43
+
44
+ [target_name, group_name]
45
+ end
46
+
47
+ # Finds the store key of a limits group from the arguments passed to the
48
+ # limits group APIs: either a target name and group name or just a group name.
49
+ # A group name alone must only be defined by one target. It can also be the
50
+ # TARGET__GROUP key returned by get_limits_groups for a name several targets share.
51
+ #
52
+ # @param target_name [String, nil] Target which defines the group
53
+ # @param group_name [String] Group name
54
+ # @return [String] The store key of the group
55
+ def self.find_limits_group(target_name, group_name, scope:)
56
+ group_name = group_name.to_s.upcase
57
+ keys = Store.hkeys("#{scope}__limits_groups")
58
+ if target_name
59
+ target_name = target_name.to_s.upcase
60
+ key = limits_group_key(target_name, group_name)
61
+ return key if keys.include?(key)
62
+
63
+ raise "LIMITS_GROUP #{target_name} #{group_name} undefined. Ensure target #{target_name} contains the line: LIMITS_GROUP #{group_name}"
64
+ end
65
+
66
+ # An exact match is a TARGET__GROUP key or a group stored before groups were owned by a target
67
+ return group_name if keys.include?(group_name)
68
+
69
+ matches = keys.select { |key| split_limits_group_key(key)[1] == group_name }
70
+ case matches.length
71
+ when 0
72
+ raise "LIMITS_GROUP #{group_name} undefined. Ensure your telemetry definition contains the line: LIMITS_GROUP #{group_name}"
73
+ when 1
74
+ matches[0]
75
+ else
76
+ targets = matches.map { |key| split_limits_group_key(key)[0] }.sort
77
+ raise "LIMITS_GROUP #{group_name} is defined by targets #{targets.join(', ')}. Specify the target name and the group name, e.g. ('#{targets[0]}', '#{group_name}')."
78
+ end
79
+ end
80
+
81
+ # @return [String, nil] The store key of the group named by a limits group event.
82
+ # Events without a target name (written before groups were owned by a target)
83
+ # name a group defined by only one target.
84
+ def self.limits_group_event_key(event, scope:)
85
+ target_name = event[:target_name] || event['target_name']
86
+ group_name = event[:group_name] || event['group_name']
87
+ return limits_group_key(target_name, group_name) if target_name
88
+
89
+ begin
90
+ find_limits_group(nil, group_name, scope: scope)
91
+ rescue RuntimeError
92
+ nil
93
+ end
94
+ end
95
+
96
+ # @return [Boolean] Whether a limits group event applies to the items of nested
97
+ # groups. Only events which say otherwise (cascade: false) don't.
98
+ def self.limits_group_event_cascade?(event)
99
+ cascade = event.key?(:cascade) ? event[:cascade] : event['cascade']
100
+ cascade != false
101
+ end
102
+
103
+ # Returns the items in a limits group grouped by target and packet, or nil if the
104
+ # group is undefined. Reads the store directly to avoid a circular reference to TargetModel.
105
+ #
106
+ # @param key [String] Store key of the group (see limits_group_key)
107
+ # @param cascade [Boolean] Whether to include the items of nested groups
108
+ # @return [Hash{String => Hash{String => Array<String>}}, nil]
109
+ def self.limits_group_items(key, cascade: true, scope:)
110
+ return nil unless key
111
+
112
+ groups = Store.hgetall("#{scope}__limits_groups")
113
+ return nil unless groups[key]
114
+
115
+ items = {}
116
+ _expand_limits_group(key, groups, items, Set.new, Set.new, cascade)
117
+ items
118
+ end
119
+
120
+ # Returns the store keys of a group and, with cascade, of every defined group
121
+ # nested below it. These are the groups whose own enabled flag a
122
+ # LIMITS_GROUP_ENABLE_STATE event sets or clears (see limits_group_settings).
123
+ #
124
+ # @param key [String] Store key of the group (see limits_group_key)
125
+ # @param cascade [Boolean] Whether to include the nested groups
126
+ # @return [Array<String>] Group keys, empty if the group is undefined
127
+ def self.limits_group_keys(key, cascade: true, scope:)
128
+ return [] unless key
129
+
130
+ groups = Store.hgetall("#{scope}__limits_groups")
131
+ return [] unless groups[key]
132
+ return [key] unless cascade
133
+
134
+ visited = Set.new
135
+ _expand_limits_group(key, groups, {}, visited, Set.new, true)
136
+ visited.select { |group| groups[group] }
137
+ end
138
+
139
+ # A group can be disabled itself (by disable_limits_group with cascade false)
140
+ # independent of its members. Groups without a setting are enabled.
141
+ #
142
+ # @return [Hash{String => Hash}] Settings keyed by group store key, e.g. { 'enabled' => false }
143
+ def self.limits_group_settings(scope:)
144
+ Store.hgetall("#{scope}__limits_group_settings").transform_values do |json|
145
+ JSON.parse(json, allow_nan: true, create_additions: true)
146
+ end
147
+ end
148
+
149
+ # Nested groups are members named <TARGET> LIMITS_GROUP <GROUP>. They have no limits
150
+ # of their own so expand them into the items of the child group, unless cascade
151
+ # is false in which case they're skipped. Children which aren't defined (yet) are
152
+ # ignored and each group is only expanded once which also guards against cycles.
153
+ #
154
+ # @param seen [Set<Array(String, String, String)>] Items already added
155
+ def self._expand_limits_group(key, groups, items, visited, seen, cascade = true)
156
+ return if visited.include?(key)
157
+
158
+ visited << key
159
+ json = groups[key]
160
+ return unless json
161
+
162
+ JSON.parse(json, allow_nan: true, create_additions: true).each do |target_name, packet_name, item_name|
163
+ if packet_name == LIMITS_GROUP_PACKET
164
+ _expand_limits_group(limits_group_key(target_name, item_name), groups, items, visited, seen, cascade) if cascade
165
+ # A Set rather than include? on the item list since groups can contain many thousands of items
166
+ elsif seen.add?([target_name, packet_name, item_name])
167
+ ((items[target_name] ||= {})[packet_name] ||= []) << item_name
168
+ end
169
+ end
170
+ end
171
+
172
+ # How long the set of active db_shards is cached. Every limits event is
173
+ # written to every active db_shard, and looking them up reads and parses every
174
+ # target, which is too slow to do per event when many items change state.
175
+ # Only LIMITS_CHANGE events use the cache, so a target added on a new db_shard
176
+ # gets them within this time. Decom microservices ignore LIMITS_CHANGE events,
177
+ # and the events that change their limits always look up the current db_shards.
178
+ ACTIVE_DB_SHARDS_CACHE_TIMEOUT = 10 # seconds
179
+ @active_db_shards_cache = {}
180
+
28
181
  # Collect all unique target db_shards from TargetModel
29
- def self._active_db_shards(scope:)
182
+ # @param use_cache [Boolean] Whether to return the cached db_shards if they haven't timed out.
183
+ # The db_shards are cached either way.
184
+ def self._active_db_shards(scope:, use_cache: true)
185
+ now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
186
+ cached = @active_db_shards_cache[scope]
187
+ return cached[1] if use_cache and cached and now < cached[0]
188
+
30
189
  db_shards = Set.new([0])
31
190
  Store.hgetall("#{scope}__openc3_targets").each do |_name, json|
32
191
  parsed = JSON.parse(json, allow_nan: true, create_additions: true)
33
192
  db_shards << (parsed['db_shard'] || 0).to_i
34
193
  end
194
+ db_shards.freeze
195
+ @active_db_shards_cache[scope] = [now + ACTIVE_DB_SHARDS_CACHE_TIMEOUT, db_shards]
35
196
  db_shards
36
197
  end
37
198
 
38
- def self.write(event, scope:)
199
+ def self.clear_active_db_shards_cache
200
+ @active_db_shards_cache = {}
201
+ end
202
+
203
+ # @param publish [Boolean] Whether to write the event to the limits events stream.
204
+ # When false only the store is updated, which a decom microservice uses while
205
+ # resyncing so the resync can't flood the stream it is recovering from.
206
+ def self.write(event, scope:, publish: true)
39
207
  case event[:type]
40
208
  when :LIMITS_CHANGE
41
209
  # The current_limits hash keeps only the current limits state of items
@@ -84,6 +252,46 @@ module OpenC3
84
252
  end
85
253
  limits_settings['enabled'] = event[:enabled]
86
254
  Store.hset("#{scope}__current_limits_settings", field, JSON.generate(limits_settings, allow_nan: true))
255
+ clear_disabled_current_limits([field], scope: scope) unless ConfigParser.handle_true_false_nil(event[:enabled])
256
+
257
+ when :LIMITS_GROUP_ENABLE_STATE
258
+ # Groups can contain many thousands of items so read and write the settings in bulk
259
+ fields = []
260
+ key = limits_group_event_key(event, scope: scope)
261
+ cascade = limits_group_event_cascade?(event)
262
+ # Only disabling with cascade false disables the group itself, so a group of
263
+ # only nested groups can be disabled on its own. Otherwise the group, and with
264
+ # cascade every group below it, follows its members again: with cascade they're
265
+ # all changed too, and a later change to any member then shows in the group.
266
+ group_keys = limits_group_keys(key, cascade: cascade, scope: scope)
267
+ unless group_keys.empty?
268
+ if !cascade and !event[:enabled]
269
+ Store.hset("#{scope}__limits_group_settings", key, JSON.generate({ 'enabled' => false }, allow_nan: true))
270
+ else
271
+ Store.hdel("#{scope}__limits_group_settings", *group_keys)
272
+ end
273
+ end
274
+ (limits_group_items(key, cascade: cascade, scope: scope) || {}).each do |target_name, packets|
275
+ packets.each do |packet_name, item_names|
276
+ item_names.each { |item_name| fields << "#{target_name}__#{packet_name}__#{item_name}" }
277
+ end
278
+ end
279
+ unless fields.empty?
280
+ all_settings = Store.hmget("#{scope}__current_limits_settings", *fields)
281
+ updates = {}
282
+ fields.each_with_index do |field, index|
283
+ limits_settings = all_settings[index]
284
+ if limits_settings
285
+ limits_settings = JSON.parse(limits_settings, allow_nan: true, create_additions: true)
286
+ else
287
+ limits_settings = {}
288
+ end
289
+ limits_settings['enabled'] = event[:enabled]
290
+ updates[field] = JSON.generate(limits_settings, allow_nan: true)
291
+ end
292
+ Store.hmset("#{scope}__current_limits_settings", *updates.flatten)
293
+ clear_disabled_current_limits(fields, scope: scope) unless ConfigParser.handle_true_false_nil(event[:enabled])
294
+ end
87
295
 
88
296
  when :LIMITS_STATE_COLOR
89
297
  # Persist the state color so it survives a decom microservice restart (applied by sync_system)
@@ -95,7 +303,8 @@ module OpenC3
95
303
  limits_settings = {}
96
304
  end
97
305
  limits_settings['state_colors'] ||= {}
98
- limits_settings['state_colors'][event[:state_name].to_s] = event[:color].to_s
306
+ # A cleared color is saved as nil (not "") so sync_system clears it
307
+ limits_settings['state_colors'][event[:state_name].to_s] = event[:color]&.to_s
99
308
  Store.hset("#{scope}__current_limits_settings", field, JSON.generate(limits_settings, allow_nan: true))
100
309
 
101
310
  when :LIMITS_SET
@@ -110,9 +319,14 @@ module OpenC3
110
319
  raise "Invalid limits event type '#{event[:type]}'"
111
320
  end
112
321
 
113
- # Write to all active db_shards so each decom microservice can read limits events inline
114
- _active_db_shards(scope: scope).each do |db_shard|
115
- Topic.write_topic("#{scope}__openc3_limits_events", {event: JSON.generate(event, allow_nan: true)}, '*', 1000, db_shard: db_shard)
322
+ return unless publish
323
+
324
+ # Write to all active db_shards so each decom microservice can read limits events inline.
325
+ # Only LIMITS_CHANGE events are frequent enough to need the cache. The rest change
326
+ # decom limits so they must reach a decom on a db_shard that was just added.
327
+ msg_hash = { event: JSON.generate(event, allow_nan: true) }
328
+ _active_db_shards(scope: scope, use_cache: event[:type] == :LIMITS_CHANGE).each do |db_shard|
329
+ Topic.write_topic("#{scope}__openc3_limits_events", msg_hash, '*', 1000, db_shard: db_shard)
116
330
  end
117
331
  end
118
332
 
@@ -190,6 +404,13 @@ module OpenC3
190
404
  end
191
405
  end
192
406
  end
407
+
408
+ # Removing the target removes its groups so a reinstalled target, or a new
409
+ # group with the same name, doesn't start disabled
410
+ unless packet_name
411
+ groups = Store.hkeys("#{scope}__limits_group_settings").select { |group| group.start_with?("#{target_name}__") }
412
+ Store.hdel("#{scope}__limits_group_settings", *groups) unless groups.empty?
413
+ end
193
414
  end
194
415
 
195
416
  # Removes a limits set from the limits_sets hash and from the
@@ -214,10 +435,35 @@ module OpenC3
214
435
  Store.hdel("#{scope}__limits_sets", set_name)
215
436
  end
216
437
 
438
+ # Returns true if limits events written after previous_id were trimmed from the
439
+ # stream before they were read. The stream keeps ~1000 entries so a flood of
440
+ # LIMITS_CHANGE events can trim events a slow reader hasn't processed yet.
441
+ # Stream IDs always increase, so if the oldest remaining entry is newer than the
442
+ # last entry read, the entries in between are gone. This can also report a gap
443
+ # when only previous_id itself was trimmed, which just causes an extra resync.
444
+ def self.events_trimmed?(previous_id, scope:, db_shard: 0)
445
+ # 0-0 means the stream was empty when the reader started
446
+ return false if previous_id.nil? or previous_id == '0-0'
447
+ oldest_id = Topic.get_oldest_message("#{scope}__openc3_limits_events", db_shard: db_shard)&.first
448
+ return false unless oldest_id
449
+ (oldest_id.split('-').map(&:to_i) <=> previous_id.split('-').map(&:to_i)) > 0
450
+ end
451
+
452
+ # Reload all limits state from the store, used to recover from missed limits events
453
+ # @param telemetry [Hash] Packets by target to update, default all in System
454
+ def self.resync_system(scope:, telemetry: nil)
455
+ System.limits_set = current_set(scope: scope)
456
+ sync_system(scope: scope, telemetry: telemetry)
457
+ end
458
+
217
459
  # Update the local System based on overall state
218
- def self.sync_system(scope:)
460
+ # @param telemetry [Hash] Packets by target to update, default all in System.
461
+ # Decom microservices sharing a process pass just their own targets because
462
+ # disabling limits calls the target's limits change callback every time.
463
+ def self.sync_system(scope:, telemetry: nil)
219
464
  all_limits_settings = Store.hgetall("#{scope}__current_limits_settings")
220
- telemetry = System.telemetry.all
465
+ telemetry ||= System.telemetry.all
466
+ disabled_fields = []
221
467
  all_limits_settings.each do |item, limits_settings|
222
468
  target_name, packet_name, item_name = item.split('__')
223
469
  target = telemetry[target_name]
@@ -227,14 +473,24 @@ module OpenC3
227
473
  limits_settings = JSON.parse(limits_settings, allow_nan: true, create_additions: true)
228
474
  enabled = limits_settings['enabled']
229
475
  persistence = limits_settings['persistence_setting']
476
+ # Items with change limits (SEQUENCE, CHANGE, STATIC) don't have numeric limits. Saved
477
+ # numeric settings can be left over from before the item definition changed.
478
+ packet_item = packet.items[item_name]
479
+ change_type = packet_item&.limits&.change_type
230
480
  limits_settings.each do |limits_set, settings|
231
481
  next unless Hash === settings
232
482
  if limits_set == 'state_colors'
233
483
  settings.each do |state_name, color|
484
+ # Earlier versions saved a cleared color as "" so treat it as cleared
485
+ color = nil if color.to_s.empty?
234
486
  System.limits.set_state_color(target_name, packet_name, item_name, state_name, color)
235
487
  end
236
488
  next
237
489
  end
490
+ if change_type
491
+ Logger.warn("Ignoring saved #{limits_set} limits settings for #{target_name} #{packet_name} #{item_name} which has #{change_type} limits")
492
+ next
493
+ end
238
494
  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)
239
495
  end
240
496
  if not enabled.nil?
@@ -242,16 +498,34 @@ module OpenC3
242
498
  System.limits.enable(target_name, packet_name, item_name)
243
499
  else
244
500
  System.limits.disable(target_name, packet_name, item_name)
501
+ disabled_fields << item
245
502
  end
246
503
  end
247
504
  end
248
505
  end
249
506
  end
507
+ clear_disabled_current_limits(disabled_fields, scope: scope)
508
+ end
509
+
510
+ # Clear any limits state left in current_limits for disabled items in one pass.
511
+ # Disabling an item only publishes a LIMITS_CHANGE (which clears its state) when
512
+ # the decom microservice has a local state for it. After a decom restart the local
513
+ # state is nil until the next packet, while current_limits can still hold the state
514
+ # from before the restart. An item disabled while the decom wasn't running is the same.
515
+ def self.clear_disabled_current_limits(fields, scope:)
516
+ return if fields.empty?
517
+
518
+ states = Store.hmget("#{scope}__current_limits", *fields)
519
+ updates = []
520
+ fields.each_with_index do |field, index|
521
+ updates.push(field, '') if states[index] and !states[index].empty?
522
+ end
523
+ Store.hmset("#{scope}__current_limits", *updates) unless updates.empty?
250
524
  end
251
525
 
252
526
  # Process a single limits event hash and update the local System accordingly.
253
527
  # Called inline by DecomMicroservice when reading from the limits events topic.
254
- def self.process_event(event, telemetry: nil)
528
+ def self.process_event(event, scope:, telemetry: nil)
255
529
  telemetry ||= System.telemetry.all
256
530
  case event['type']
257
531
  when 'LIMITS_CHANGE'
@@ -289,6 +563,35 @@ module OpenC3
289
563
  end
290
564
  end
291
565
 
566
+ when 'LIMITS_GROUP_ENABLE_STATE'
567
+ enabled = ConfigParser.handle_true_false_nil(event['enabled'])
568
+ key = limits_group_event_key(event, scope: scope)
569
+ (limits_group_items(key, cascade: limits_group_event_cascade?(event), scope: scope) || {}).each do |target_name, packets|
570
+ target = telemetry[target_name]
571
+ next unless target
572
+
573
+ packets.each do |packet_name, item_names|
574
+ packet = target[packet_name]
575
+ next unless packet
576
+
577
+ item_names.each do |item_name|
578
+ next unless packet.items[item_name]
579
+
580
+ # The stream offset has already advanced past this event so isolate
581
+ # failures to a single item rather than skipping the rest of the group
582
+ begin
583
+ if enabled
584
+ System.limits.enable(target_name, packet_name, item_name)
585
+ else
586
+ System.limits.disable(target_name, packet_name, item_name)
587
+ end
588
+ rescue => e
589
+ Logger.error("Error #{enabled ? 'enabling' : 'disabling'} limits for #{target_name} #{packet_name} #{item_name}: #{e.formatted}")
590
+ end
591
+ end
592
+ end
593
+ end
594
+
292
595
  when 'LIMITS_STATE_COLOR'
293
596
  target_name = event['target_name']
294
597
  packet_name = event['packet_name']
@@ -302,7 +605,10 @@ module OpenC3
302
605
  end
303
606
 
304
607
  when 'LIMITS_SET'
305
- System.limits_set = event['set']
608
+ # Read the current set rather than the event's set. Decoms sharing a process
609
+ # share System.limits_set, so a decom still catching up on old events must
610
+ # not switch the others back to an obsolete set.
611
+ System.limits_set = current_set(scope: scope)
306
612
  end
307
613
  end
308
614
 
@@ -313,7 +619,7 @@ module OpenC3
313
619
  topics = ["#{scope}__openc3_limits_events"]
314
620
  Topic.read_topics(topics, nil, block_ms) do |_topic, _msg_id, event, _redis|
315
621
  event = JSON.parse(event['event'], allow_nan: true, create_additions: true)
316
- process_event(event, telemetry: telemetry)
622
+ process_event(event, scope: scope, telemetry: telemetry)
317
623
  end
318
624
  end
319
625
  end
@@ -78,14 +78,18 @@ module OpenC3
78
78
  end
79
79
  end
80
80
 
81
+ # Write to the target's db_shard where its interface reads commands
81
82
  def self.route_command(packet, target_names, scope:)
83
+ # Write to the target's db_shard where its interface reads commands
82
84
  if packet.identified?
83
85
  topic = "{#{scope}__CMD}TARGET__#{packet.target_name}"
84
- Topic.write_topic(topic, { 'target_name' => packet.target_name, 'cmd_name' => packet.packet_name, 'cmd_buffer' => packet.buffer(false) }, '*', 100)
86
+ db_shard = Store.db_shard_for_target(packet.target_name, scope: scope)
87
+ Topic.write_topic(topic, { 'target_name' => packet.target_name, 'cmd_name' => packet.packet_name, 'cmd_buffer' => packet.buffer(false) }, '*', 100, db_shard: db_shard)
85
88
  elsif target_names.length == 1
86
89
  topic = "{#{scope}__CMD}TARGET__#{target_names[0]}"
90
+ db_shard = Store.db_shard_for_target(target_names[0], scope: scope)
87
91
  # Name the routed target so the interface identifies against it (and sends it as UNKNOWN if that fails)
88
- Topic.write_topic(topic, { 'target_name' => target_names[0], 'cmd_name' => 'UNKNOWN', 'cmd_buffer' => packet.buffer(false) }, '*', 100)
92
+ Topic.write_topic(topic, { 'target_name' => target_names[0], 'cmd_name' => 'UNKNOWN', 'cmd_buffer' => packet.buffer(false) }, '*', 100, db_shard: db_shard)
89
93
  else
90
94
  raise "No route for command: #{packet.target_name ? packet.target_name : 'UNKNOWN'} #{packet.packet_name ? packet.packet_name : 'UNKNOWN'}"
91
95
  end
@@ -17,14 +17,17 @@ module OpenC3
17
17
  class SystemEventsTopic < Topic
18
18
  PRIMARY_KEY = "OPENC3__SYSTEM__EVENTS".freeze
19
19
 
20
- # Collect all unique target db_shards from TargetModel
20
+ # Collect all unique target and interface db_shards. Interfaces read system
21
+ # events on their own db_shard which may not have any targets on it.
21
22
  def self._active_db_shards
22
23
  db_shards = Set.new([0])
23
- # Iterate all scopes to find all target db_shards
24
- Store.scan_each(match: '*__openc3_targets', type: 'hash') do |key|
25
- Store.hgetall(key).each do |_name, json|
26
- parsed = JSON.parse(json, allow_nan: true, create_additions: true)
27
- db_shards << (parsed['db_shard'] || 0).to_i
24
+ # Iterate all scopes to find all target and interface db_shards
25
+ ['*__openc3_targets', '*__openc3_interfaces'].each do |match|
26
+ Store.scan_each(match: match, type: 'hash') do |key|
27
+ Store.hgetall(key).each do |_name, json|
28
+ parsed = JSON.parse(json, allow_nan: true, create_additions: true)
29
+ db_shards << (parsed['db_shard'] || 0).to_i
30
+ end
28
31
  end
29
32
  end
30
33
  db_shards
@@ -16,11 +16,14 @@
16
16
  # if purchased from OpenC3, Inc.
17
17
 
18
18
  require 'openc3/topics/topic'
19
+ require 'openc3/utilities/store_queued'
19
20
  require 'openc3/utilities/open_telemetry'
20
21
 
21
22
  module OpenC3
22
23
  class TelemetryDecomTopic < Topic
23
- def self.write_packet(packet, id: nil, include_limits_states: true, scope:)
24
+ # @param queued [Boolean] Queue the stream and CVT writes to be flushed in a pipelined
25
+ # batch (EphemeralStoreQueued / StoreQueued) rather than writing them directly
26
+ def self.write_packet(packet, id: nil, include_limits_states: true, queued: false, scope:)
24
27
  OpenC3.in_span("write_packet") do
25
28
  # Need to build a JSON hash of the decommutated data
26
29
  # Support "downward typing"
@@ -45,12 +48,18 @@ module OpenC3
45
48
  }
46
49
  msg_hash[:extra] = JSON.generate(packet.extra.as_json, allow_nan: true) if packet.extra
47
50
  db_shard = Store.db_shard_for_target(packet.target_name, scope: scope)
48
- Topic.write_topic("#{scope}__DECOM__{#{packet.target_name}}__#{packet.packet_name}", msg_hash, id, minid: Topic.stream_safety_minid(id), db_shard: db_shard)
51
+ topic = "#{scope}__DECOM__{#{packet.target_name}}__#{packet.packet_name}"
52
+ minid = Topic.stream_safety_minid(id)
53
+ if queued
54
+ EphemeralStoreQueued.instance(db_shard: db_shard).write_topic(topic, msg_hash, id, minid: minid)
55
+ else
56
+ Topic.write_topic(topic, msg_hash, id, minid: minid, db_shard: db_shard)
57
+ end
49
58
 
50
59
  unless packet.stored
51
60
  # Also update the current value table with the latest decommutated data
52
61
  # Pass pre-serialized JSON to avoid calling as_json twice
53
- CvtModel.set_json(json_data, json_safe_hash, target_name: packet.target_name, packet_name: packet.packet_name, scope: scope)
62
+ CvtModel.set_json(json_data, json_safe_hash, target_name: packet.target_name, packet_name: packet.packet_name, queued: queued, scope: scope)
54
63
  end
55
64
  end
56
65
  end
@@ -221,6 +221,14 @@ module OpenC3
221
221
  EphemeralStore.instance(db_shard: db_shard).del(topic)
222
222
  end
223
223
 
224
+ # Extract the target name from the {TARGET} hash tag in a topic like
225
+ # DEFAULT__TELEMETRY__{INST}__HEALTH_STATUS. Returns nil if there is none.
226
+ def self.target_name_from_topic(topic)
227
+ # [^{}] (not [^}]) keeps this linear: target names never contain braces
228
+ match = topic.match(/__\{([^{}]+)\}__/)
229
+ match ? match[1] : nil
230
+ end
231
+
224
232
  # Group topics by db_shard. Each topic's target name is extracted and looked up.
225
233
  # Topics matching target_pattern are db_sharded; others go to db_shard 0.
226
234
  # @param topics [Array<String>] List of topic strings
@@ -231,7 +239,7 @@ module OpenC3
231
239
  groups = {}
232
240
  topics.each do |topic|
233
241
  if topic.include?(target_pattern)
234
- target_name = topic.match(/__\{?([^}_]+)\}?__/)[1] rescue nil
242
+ target_name = target_name_from_topic(topic)
235
243
  # Handle CMD}TARGET__ pattern where target is after TARGET__
236
244
  target_name = topic.split('TARGET__')[1] if target_pattern.include?('TARGET__') && target_name.nil?
237
245
  db_shard = (Store.db_shard_for_target(target_name, scope: scope) || 0).to_i