openc3 7.5.1 → 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 (169) 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 +93 -41
  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 +10 -0
  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/packet_log_writer.rb +8 -3
  33. data/lib/openc3/microservices/decom_common.rb +4 -1
  34. data/lib/openc3/microservices/decom_microservice.rb +52 -6
  35. data/lib/openc3/microservices/interface_decom_common.rb +13 -0
  36. data/lib/openc3/microservices/interface_microservice.rb +50 -11
  37. data/lib/openc3/microservices/limits_group_microservice.rb +513 -0
  38. data/lib/openc3/microservices/log_microservice.rb +22 -8
  39. data/lib/openc3/microservices/microservice.rb +8 -2
  40. data/lib/openc3/microservices/multi_microservice.rb +76 -22
  41. data/lib/openc3/microservices/queue_microservice.rb +29 -24
  42. data/lib/openc3/migrations/20260704000000_default_bridge.rb +1 -2
  43. data/lib/openc3/migrations/20261103000001_limits_group_backfill.rb +193 -0
  44. data/lib/openc3/models/bridge_model.rb +3 -3
  45. data/lib/openc3/models/cvt_model.rb +3 -1
  46. data/lib/openc3/models/host_interface_microservice_model.rb +4 -4
  47. data/lib/openc3/models/interface_model.rb +27 -7
  48. data/lib/openc3/models/microservice_group.rb +167 -0
  49. data/lib/openc3/models/microservice_model.rb +88 -2
  50. data/lib/openc3/models/panel_model.rb +109 -0
  51. data/lib/openc3/models/plugin_model.rb +1006 -26
  52. data/lib/openc3/models/plugin_store_model.rb +6 -1
  53. data/lib/openc3/models/protected_storage_model.rb +64 -0
  54. data/lib/openc3/models/queue_model.rb +19 -10
  55. data/lib/openc3/models/scope_model.rb +80 -3
  56. data/lib/openc3/models/setting_model.rb +2 -2
  57. data/lib/openc3/models/shared_resource_model.rb +381 -0
  58. data/lib/openc3/models/target_model.rb +324 -68
  59. data/lib/openc3/operators/microservice_operator.rb +76 -9
  60. data/lib/openc3/operators/operator.rb +29 -0
  61. data/lib/openc3/packets/commands.rb +16 -16
  62. data/lib/openc3/packets/limits.rb +2 -0
  63. data/lib/openc3/packets/packet.rb +75 -4
  64. data/lib/openc3/packets/packet_config.rb +152 -13
  65. data/lib/openc3/packets/packet_item.rb +35 -14
  66. data/lib/openc3/packets/packet_item_limits.rb +67 -6
  67. data/lib/openc3/packets/parsers/limits_parser.rb +69 -2
  68. data/lib/openc3/packets/parsers/packet_item_parser.rb +6 -0
  69. data/lib/openc3/packets/parsers/packet_parser.rb +45 -0
  70. data/lib/openc3/packets/parsers/state_parser.rb +1 -1
  71. data/lib/openc3/script/CLAUDE.md +1 -0
  72. data/lib/openc3/script/api_shared.rb +17 -9
  73. data/lib/openc3/script/calendar.rb +18 -6
  74. data/lib/openc3/script/commands.rb +11 -9
  75. data/lib/openc3/script/critical_cmd.rb +22 -2
  76. data/lib/openc3/script/metadata.rb +30 -4
  77. data/lib/openc3/script/plugins.rb +8 -0
  78. data/lib/openc3/script/script.rb +8 -24
  79. data/lib/openc3/script/web_socket_api.rb +23 -17
  80. data/lib/openc3/system/system.rb +9 -3
  81. data/lib/openc3/top_level.rb +17 -0
  82. data/lib/openc3/topics/command_topic.rb +7 -1
  83. data/lib/openc3/topics/decom_interface_topic.rb +9 -3
  84. data/lib/openc3/topics/discovery_topic.rb +84 -0
  85. data/lib/openc3/topics/limits_event_topic.rb +236 -17
  86. data/lib/openc3/topics/telemetry_decom_topic.rb +12 -3
  87. data/lib/openc3/topics/topic.rb +2 -1
  88. data/lib/openc3/utilities/authentication.rb +67 -16
  89. data/lib/openc3/utilities/cli_generator.rb +117 -25
  90. data/lib/openc3/utilities/client_ip_header_filter.rb +38 -0
  91. data/lib/openc3/utilities/config_overlay.rb +24 -1
  92. data/lib/openc3/utilities/cosmos_rails_formatter.rb +18 -10
  93. data/lib/openc3/utilities/hardware_discovery.rb +279 -0
  94. data/lib/openc3/utilities/limits_rollup.rb +81 -0
  95. data/lib/openc3/utilities/local_mode.rb +48 -5
  96. data/lib/openc3/utilities/questdb_client.rb +130 -22
  97. data/lib/openc3/utilities/reingest_job.rb +12 -3
  98. data/lib/openc3/utilities/running_script.rb +206 -49
  99. data/lib/openc3/utilities/script.rb +24 -12
  100. data/lib/openc3/utilities/script_env.rb +164 -0
  101. data/lib/openc3/utilities/script_message_signing.rb +133 -0
  102. data/lib/openc3/utilities/store_autoload.rb +32 -1
  103. data/lib/openc3/utilities/store_queued.rb +5 -1
  104. data/lib/openc3/version.rb +6 -6
  105. data/templates/command_validator/command_validator.py +9 -9
  106. data/templates/{tool_react/pnpm-workspace.yaml → panel/.prettierrc.cjs} +7 -5
  107. data/templates/panel/Rakefile +24 -0
  108. data/templates/panel/package.json +17 -0
  109. data/templates/{tool_angular → panel}/pnpm-workspace.yaml +5 -0
  110. data/templates/panel/src/Panel.js +52 -0
  111. data/templates/panel/src/Panel.vue +60 -0
  112. data/templates/panel/src/panelSdk.js +24 -0
  113. data/templates/panel/vite-Panel.config.js +43 -0
  114. data/templates/tool_vue/package.json +3 -3
  115. data/templates/widget/package.json +2 -2
  116. metadata +24 -56
  117. data/templates/tool_angular/.editorconfig +0 -16
  118. data/templates/tool_angular/.gitignore +0 -44
  119. data/templates/tool_angular/.vscode/extensions.json +0 -4
  120. data/templates/tool_angular/.vscode/launch.json +0 -20
  121. data/templates/tool_angular/.vscode/tasks.json +0 -42
  122. data/templates/tool_angular/angular.json +0 -111
  123. data/templates/tool_angular/extra-webpack.config.js +0 -8
  124. data/templates/tool_angular/package.json +0 -48
  125. data/templates/tool_angular/src/app/app-routing.module.ts +0 -15
  126. data/templates/tool_angular/src/app/app.component.html +0 -22
  127. data/templates/tool_angular/src/app/app.component.scss +0 -18
  128. data/templates/tool_angular/src/app/app.component.spec.ts +0 -29
  129. data/templates/tool_angular/src/app/app.component.ts +0 -52
  130. data/templates/tool_angular/src/app/app.module.ts +0 -30
  131. data/templates/tool_angular/src/app/custom-overlay-container.ts +0 -17
  132. data/templates/tool_angular/src/app/empty-route/empty-route.component.ts +0 -7
  133. data/templates/tool_angular/src/app/openc3-api.d.ts +0 -1
  134. data/templates/tool_angular/src/assets/.gitkeep +0 -0
  135. data/templates/tool_angular/src/environments/environment.prod.ts +0 -3
  136. data/templates/tool_angular/src/environments/environment.ts +0 -16
  137. data/templates/tool_angular/src/favicon.ico +0 -0
  138. data/templates/tool_angular/src/index.html +0 -13
  139. data/templates/tool_angular/src/main.single-spa.ts +0 -40
  140. data/templates/tool_angular/src/single-spa/asset-url.ts +0 -12
  141. data/templates/tool_angular/src/single-spa/single-spa-props.ts +0 -8
  142. data/templates/tool_angular/src/styles.scss +0 -1
  143. data/templates/tool_angular/tsconfig.app.json +0 -13
  144. data/templates/tool_angular/tsconfig.json +0 -33
  145. data/templates/tool_angular/tsconfig.spec.json +0 -14
  146. data/templates/tool_react/.eslintrc +0 -7
  147. data/templates/tool_react/.gitignore +0 -71
  148. data/templates/tool_react/.prettierignore +0 -8
  149. data/templates/tool_react/babel.config.json +0 -29
  150. data/templates/tool_react/jest.config.js +0 -12
  151. data/templates/tool_react/package.json +0 -51
  152. data/templates/tool_react/src/openc3-tool_name.js +0 -24
  153. data/templates/tool_react/src/root.component.js +0 -88
  154. data/templates/tool_react/src/root.component.test.js +0 -9
  155. data/templates/tool_react/webpack.config.js +0 -27
  156. data/templates/tool_svelte/.gitignore +0 -71
  157. data/templates/tool_svelte/.prettierignore +0 -8
  158. data/templates/tool_svelte/.prettierrc.js +0 -5
  159. data/templates/tool_svelte/babel.config.js +0 -12
  160. data/templates/tool_svelte/build/smui.css +0 -1
  161. data/templates/tool_svelte/jest.config.js +0 -9
  162. data/templates/tool_svelte/package.json +0 -49
  163. data/templates/tool_svelte/pnpm-workspace.yaml +0 -20
  164. data/templates/tool_svelte/rollup.config.js +0 -74
  165. data/templates/tool_svelte/src/App.svelte +0 -42
  166. data/templates/tool_svelte/src/App.test.js +0 -9
  167. data/templates/tool_svelte/src/theme/_smui-theme.scss +0 -25
  168. data/templates/tool_svelte/src/tool_name.js +0 -17
  169. data/templates/widget/LICENSE.md +0 -20
@@ -0,0 +1,81 @@
1
+ # encoding: ascii-8bit
2
+
3
+ # Copyright 2026 OpenC3, Inc.
4
+ # All Rights Reserved.
5
+ #
6
+ # This program is distributed in the hope that it will be useful,
7
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
8
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9
+ # See LICENSE.md for more details.
10
+
11
+ # This file may also be used under the terms of a commercial license
12
+ # if purchased from OpenC3, Inc.
13
+
14
+ module OpenC3
15
+ # Reduces item limits states to a single color. Shared by
16
+ # get_overall_limits_state and the limits group rollup so they rank
17
+ # states the same way. Keep in sync with python/openc3/utilities/limits_rollup.py
18
+ module LimitsRollup
19
+ COLORS = {
20
+ 'RED' => 'RED', 'RED_HIGH' => 'RED', 'RED_LOW' => 'RED',
21
+ 'YELLOW' => 'YELLOW', 'YELLOW_HIGH' => 'YELLOW', 'YELLOW_LOW' => 'YELLOW',
22
+ 'GREEN' => 'GREEN', 'GREEN_HIGH' => 'GREEN', 'GREEN_LOW' => 'GREEN',
23
+ 'BLUE' => 'BLUE',
24
+ }.freeze
25
+ # BLUE ranks below GREEN so a rollup is only BLUE when every state is BLUE
26
+ SEVERITY = { 'BLUE' => -1, 'GREEN' => 0, 'YELLOW' => 1, 'RED' => 2 }.freeze
27
+ # A group whose members all have limits disabled
28
+ DISABLED = 'DISABLED'.freeze
29
+ # Whether limits are enabled on a group's members: all, none, or some of them
30
+ ENABLED_TRUE = 'TRUE'.freeze
31
+ ENABLED_FALSE = 'FALSE'.freeze
32
+ ENABLED_PARTIAL = 'PARTIAL'.freeze
33
+
34
+ # @param state [String, nil] An item limits state such as RED_HIGH or BLUE
35
+ # @return [String, nil] 'BLUE', 'GREEN', 'YELLOW' or 'RED', or nil for a missing or unknown state
36
+ def self.color(state)
37
+ COLORS[state]
38
+ end
39
+
40
+ # @param states [Array<String, nil>] Item limits states
41
+ # @return [String, nil] The most severe color, or nil if no state counts
42
+ def self.worst(states)
43
+ states.map { |state| color(state) }.compact.max_by { |color| SEVERITY[color] }
44
+ end
45
+
46
+ # @param enabled [Boolean, String] true or false for an item, or
47
+ # ENABLED_TRUE, ENABLED_FALSE or ENABLED_PARTIAL for a nested group
48
+ # @return [String] ENABLED_TRUE, ENABLED_FALSE or ENABLED_PARTIAL
49
+ def self.enabled_state(enabled)
50
+ case enabled
51
+ when true then ENABLED_TRUE
52
+ when false, nil then ENABLED_FALSE
53
+ else enabled
54
+ end
55
+ end
56
+
57
+ # Roll up a limits group from its members.
58
+ #
59
+ # The group is enabled (ENABLED_TRUE) when every member is, disabled
60
+ # (ENABLED_FALSE) when no member is or the group is empty, and otherwise
61
+ # ENABLED_PARTIAL. A disabled group's state is DISABLED. Otherwise it's the
62
+ # most severe state of the members which aren't disabled, ignoring missing states.
63
+ #
64
+ # @param members [Array<Array(String|nil, Boolean|String)>] [limits_state, enabled]
65
+ # per member. Items pass true or false for enabled, nested groups pass
66
+ # their rolled up [state, enabled].
67
+ # @return [Array(String|nil, String)] [state, enabled] where state is nil
68
+ # when no member which counts has a state
69
+ def self.rollup(members)
70
+ members = members.map { |state, enabled| [state, enabled_state(enabled)] }
71
+ enabled_states = members.map { |_state, enabled| enabled }.uniq
72
+ if enabled_states.empty? or enabled_states == [ENABLED_FALSE]
73
+ return [DISABLED, ENABLED_FALSE]
74
+ end
75
+
76
+ enabled = enabled_states == [ENABLED_TRUE] ? ENABLED_TRUE : ENABLED_PARTIAL
77
+ states = members.reject { |_state, member_enabled| member_enabled == ENABLED_FALSE }.map { |state, _enabled| state }
78
+ return [worst(states), enabled]
79
+ end
80
+ end
81
+ end
@@ -47,6 +47,7 @@ module OpenC3
47
47
 
48
48
  if gems.length == 1 and plugin_instance
49
49
  # If one gem file and plugin_instance.json - Install instance
50
+ restore_local_uploads(full_folder_path, scope: scope_dir.upcase)
50
51
  load_plugin(gems[0], scope: scope_dir.upcase, plugin_hash_file: plugin_instance)
51
52
  elsif gems.length == 1
52
53
  # Else If just gem - Install with default settings
@@ -191,6 +192,7 @@ module OpenC3
191
192
  File.open(File.join(File.dirname(plugin_file_path), 'plugin_instance.json'), 'wb') do |file|
192
193
  file.write(JSON.pretty_generate(plugin_hash, allow_nan: true))
193
194
  end
195
+ save_local_uploads(File.dirname(plugin_file_path), plugin_hash, scope: scope)
194
196
  else
195
197
  # From online install / update
196
198
  # Or init install of container plugin
@@ -219,7 +221,7 @@ module OpenC3
219
221
  if json["name"] == old_plugin_name
220
222
  # Found plugin to update
221
223
  found = true
222
- update_local_plugin_files(full_folder_path, plugin_file_path, plugin_hash, gem_name)
224
+ update_local_plugin_files(full_folder_path, plugin_file_path, plugin_hash, gem_name, scope: scope)
223
225
  end
224
226
  else
225
227
  # New install of same plugin - Leave it alone
@@ -229,7 +231,7 @@ module OpenC3
229
231
  # This shouldn't happen without users using this wrong
230
232
  # We will update
231
233
  found = true
232
- update_local_plugin_files(full_folder_path, plugin_file_path, plugin_hash, gem_name)
234
+ update_local_plugin_files(full_folder_path, plugin_file_path, plugin_hash, gem_name, scope: scope)
233
235
  end
234
236
  end
235
237
  end
@@ -245,13 +247,13 @@ module OpenC3
245
247
  count += 1
246
248
  end
247
249
  full_folder_path = "#{OPENC3_LOCAL_MODE_PATH}/#{scope}/#{folder_name}"
248
- update_local_plugin_files(full_folder_path, plugin_file_path, plugin_hash, gem_name)
250
+ update_local_plugin_files(full_folder_path, plugin_file_path, plugin_hash, gem_name, scope: scope)
249
251
  end
250
252
  end
251
253
  end
252
254
  end
253
255
 
254
- def self.update_local_plugin_files(full_folder_path, plugin_file_path, plugin_hash, gem_name)
256
+ def self.update_local_plugin_files(full_folder_path, plugin_file_path, plugin_hash, gem_name, scope: nil)
255
257
  return if DefaultPlugins::NAMES.include?(gem_name)
256
258
  puts "Updating local plugin files: #{full_folder_path}"
257
259
  FileUtils.mkdir_p(full_folder_path)
@@ -271,11 +273,45 @@ module OpenC3
271
273
  File.open(File.join(full_folder_path, 'plugin_instance.json'), 'wb') do |file|
272
274
  file.write(JSON.pretty_generate(plugin_hash, allow_nan: true))
273
275
  end
276
+ save_local_uploads(full_folder_path, plugin_hash, scope: scope) if scope
274
277
  ensure
275
278
  FileUtils.remove_entry_secure(temp_dir, true)
276
279
  end
277
280
  end
278
281
 
282
+ # Files uploaded for VARIABLE_FILE variables are kept in the bucket, so
283
+ # they're also saved in the local plugin folder (plugin_uploads/<id>/<file>)
284
+ # beside plugin_instance.json. Then a fresh clone, or a bucket that was
285
+ # wiped, can install the plugin again.
286
+ def self.save_local_uploads(full_folder_path, plugin_hash, scope:)
287
+ uploads_path = File.join(full_folder_path, PluginModel::UPLOADS_PATH)
288
+ FileUtils.rm_rf(uploads_path)
289
+ (plugin_hash['variables'] || {}).each do |name, variable|
290
+ next unless variable.is_a?(Hash) and variable['type'] == PluginModel::FILE_VARIABLE_TYPE
291
+ key = PluginModel.upload_key(variable['value'], scope: scope)
292
+ next unless key
293
+ path = File.join(uploads_path, variable['value'])
294
+ FileUtils.mkdir_p(File.dirname(path))
295
+ Bucket.getClient().get_object(bucket: ENV['OPENC3_CONFIG_BUCKET'], key: key, path: path)
296
+ puts "Could not save the file uploaded for VARIABLE_FILE #{name} locally" unless File.exist?(path)
297
+ end
298
+ end
299
+
300
+ # Put the uploads saved in a local plugin folder back in the bucket
301
+ # before the plugin is installed from its plugin_instance.json
302
+ def self.restore_local_uploads(full_folder_path, scope:)
303
+ uploads_path = File.join(full_folder_path, PluginModel::UPLOADS_PATH)
304
+ return unless File.directory?(uploads_path)
305
+ Dir.glob("#{uploads_path}/*/*").each do |path|
306
+ next unless File.file?(path)
307
+ key = PluginModel.upload_key(path.delete_prefix("#{uploads_path}/"), scope: scope)
308
+ next unless key
309
+ File.open(path, 'rb') do |file|
310
+ Bucket.getClient().put_object(bucket: ENV['OPENC3_CONFIG_BUCKET'], key: key, body: file)
311
+ end
312
+ end
313
+ end
314
+
279
315
  def self.remove_local_plugin(plugin_name, scope:)
280
316
  local_plugins = scan_local_mode()
281
317
  scope_local_plugins = local_plugins[scope]
@@ -291,6 +327,7 @@ module OpenC3
291
327
  puts "Removing local plugin files: #{full_folder_path}"
292
328
  File.delete(gems[0])
293
329
  File.delete(plugin_instance)
330
+ FileUtils.rm_rf(File.join(full_folder_path, PluginModel::UPLOADS_PATH))
294
331
  if Dir.empty?(File.dirname(gems[0]))
295
332
  FileUtils.rm_rf(File.dirname(gems[0]))
296
333
  end
@@ -587,8 +624,14 @@ module OpenC3
587
624
  FileUtils.mkdir_p(local_folder_path)
588
625
  Dir.glob(local_folder_path + "/**/*").each do |filename|
589
626
  next if File.directory?(filename)
627
+
590
628
  mod_filename = filename[prefix_length..-1]
591
- local_catalog[mod_filename] = File.size(filename)
629
+ begin
630
+ local_catalog[mod_filename] = File.size(filename)
631
+ rescue Errno::ENOENT
632
+ # Deleted by another request after the glob listed it, so it's no
633
+ # longer part of the catalog
634
+ end
592
635
  end
593
636
  return local_catalog
594
637
  end
@@ -467,18 +467,45 @@ module OpenC3
467
467
  end
468
468
  end
469
469
 
470
- # Build a SQL WHERE clause for PACKET_TIMESECONDS range filtering.
470
+ # Returns the timestamp column that filters and orders data for a time type.
471
+ #
472
+ # @param time_type [Symbol] :PACKET or :RECEIVED
473
+ # @return [String] PACKET_TIMESECONDS or RECEIVED_TIMESECONDS
474
+ def self.time_column(time_type)
475
+ time_type == :RECEIVED ? 'RECEIVED_TIMESECONDS' : 'PACKET_TIMESECONDS'
476
+ end
477
+
478
+ # Build a SQL WHERE clause for time range filtering.
471
479
  #
472
480
  # @param start_time [Integer, String] Start timestamp (nanoseconds)
473
481
  # @param end_time [Integer, String, nil] End timestamp (nanoseconds), or nil for open-ended
474
482
  # @param prefix [String] Table alias prefix (e.g., 'T0.') — default ''
483
+ # @param time_type [Symbol] :PACKET filters on PACKET_TIMESECONDS, :RECEIVED on RECEIVED_TIMESECONDS
484
+ # @param packet_bounds [Array<Integer, nil>, nil] [min, max] PACKET_TIMESECONDS limits (inclusive,
485
+ # either may be nil) added to a :RECEIVED filter so QuestDB can skip partitions. See packet_time_bounds.
475
486
  # @return [String] SQL WHERE clause fragment (includes leading space)
476
- def self.time_where_clause(start_time, end_time, prefix: '')
477
- where = " WHERE #{prefix}PACKET_TIMESECONDS >= #{start_time}"
478
- where += " AND #{prefix}PACKET_TIMESECONDS < #{end_time}" if end_time
487
+ def self.time_where_clause(start_time, end_time, prefix: '', time_type: :PACKET, packet_bounds: nil)
488
+ column = "#{prefix}#{time_column(time_type)}"
489
+ where = " WHERE #{column} >= #{start_time}"
490
+ where += " AND #{column} < #{end_time}" if end_time
491
+ if time_type == :RECEIVED and packet_bounds
492
+ min_time, max_time = packet_bounds
493
+ where += " AND #{prefix}PACKET_TIMESECONDS >= #{Integer(min_time)}" if min_time
494
+ where += " AND #{prefix}PACKET_TIMESECONDS <= #{Integer(max_time)}" if max_time
495
+ end
479
496
  where
480
497
  end
481
498
 
499
+ # PACKET_TIMESECONDS is the designated timestamp so rows already come back in
500
+ # that order. RECEIVED_TIMESECONDS is a regular column and needs an explicit sort,
501
+ # which also keeps LIMIT pagination stable.
502
+ #
503
+ # @param time_type [Symbol] :PACKET or :RECEIVED
504
+ # @return [String] SQL ORDER BY clause (includes leading space) or ''
505
+ def self.time_order_clause(time_type)
506
+ time_type == :RECEIVED ? " ORDER BY RECEIVED_TIMESECONDS, PACKET_TIMESECONDS" : ''
507
+ end
508
+
482
509
  # Fetch a packet definition from TargetModel, returning nil if not found.
483
510
  #
484
511
  # @param target_name [String] Target name
@@ -563,10 +590,11 @@ module OpenC3
563
590
  # @param value_type [Symbol] :RAW or :CONVERTED
564
591
  # @param existing_columns [Set<String>, nil] Column names that actually exist in the
565
592
  # table, used for CONVERTED-to-raw fallback (see build_aggregation_selects).
593
+ # @param time_type [Symbol] :PACKET or :RECEIVED, which timestamp the samples are bucketed by
566
594
  # @return [Array<String>, Boolean] Two-element array: [select_fragments, has_numeric_items]
567
- # select_fragments includes TIMESTAMP_SELECT as the first element.
568
- def self.build_packet_reduced_selects(packet_def, value_type, existing_columns: nil)
569
- selects = [TIMESTAMP_SELECT]
595
+ # select_fragments includes the timestamp_select as the first element.
596
+ def self.build_packet_reduced_selects(packet_def, value_type, existing_columns: nil, time_type: :PACKET)
597
+ selects = [timestamp_select(time_type)]
570
598
  has_items = false
571
599
  return [selects, false] unless packet_def && packet_def['items']
572
600
 
@@ -602,6 +630,19 @@ module OpenC3
602
630
  # PG wire protocol truncates timestamp_ns to microseconds; CAST AS LONG preserves full precision.
603
631
  TIMESTAMP_SELECT = 'CAST(PACKET_TIMESECONDS AS LONG) as PACKET_TIMESECONDS'
604
632
 
633
+ # SQL: nanosecond-precision timestamp select for the given time type.
634
+ #
635
+ # @param time_type [Symbol] :PACKET or :RECEIVED
636
+ # @return [String] SELECT fragment aliased to the timestamp column name
637
+ def self.timestamp_select(time_type)
638
+ column = time_column(time_type)
639
+ "CAST(#{column} AS LONG) as #{column}"
640
+ end
641
+
642
+ # SQL: nanosecond-precision received timestamp for explicit SELECT lists that
643
+ # order by received time. Aliased to avoid colliding with a RECEIVED_TIMESECONDS item column.
644
+ RECEIVED_TIMESTAMP_SELECT = 'CAST(RECEIVED_TIMESECONDS AS LONG) as "__rx_time_ns"'
645
+
605
646
  # SQL: nanosecond-precision timestamps for SELECT * queries (different aliases avoid column name collision).
606
647
  TIMESTAMP_EXTRAS = 'CAST(PACKET_TIMESECONDS AS LONG) as "__pkt_time_ns", CAST(RECEIVED_TIMESECONDS AS LONG) as "__rx_time_ns"'
607
648
 
@@ -623,10 +664,11 @@ module OpenC3
623
664
  # @param table_name [String] Sanitized table name
624
665
  # @param start_time [Integer] Nanosecond start time
625
666
  # @param end_time [Integer, nil] Nanosecond end time
667
+ # @param time_type [Symbol] :PACKET or :RECEIVED, which timestamp the range applies to
626
668
  # @return [Boolean]
627
- def self.table_has_data?(table_name, start_time, end_time, db_shard: 0)
669
+ def self.table_has_data?(table_name, start_time, end_time, db_shard: 0, time_type: :PACKET)
628
670
  query = "SELECT 1 FROM \"#{table_name}\""
629
- query += time_where_clause(start_time, end_time)
671
+ query += time_where_clause(start_time, end_time, time_type: time_type)
630
672
  query += " LIMIT 1"
631
673
  result = query_with_retry(query, max_retries: 1, label: "table_has_data", db_shard: db_shard)
632
674
  result && result.ntuples > 0
@@ -635,6 +677,26 @@ module OpenC3
635
677
  false
636
678
  end
637
679
 
680
+ # Returns the PACKET_TIMESECONDS range of the rows received in a time range.
681
+ # RECEIVED_TIMESECONDS is a regular column, so QuestDB scans every partition to
682
+ # filter on it. Adding these limits to a received-time query (packet_bounds:) lets
683
+ # QuestDB skip the partitions outside them, so paging through the results doesn't
684
+ # scan the whole table for every page.
685
+ #
686
+ # @param table_name [String] Sanitized table name
687
+ # @param start_time [Integer] Nanosecond received start time
688
+ # @param end_time [Integer, nil] Nanosecond received end time
689
+ # @return [Array<Integer>, nil] [min, max] nanosecond packet times, or nil if no rows were received in the range
690
+ def self.packet_time_bounds(table_name, start_time, end_time, db_shard: 0)
691
+ query = "SELECT CAST(min(PACKET_TIMESECONDS) AS LONG) as min_time, CAST(max(PACKET_TIMESECONDS) AS LONG) as max_time FROM \"#{table_name}\""
692
+ query += time_where_clause(start_time, end_time, time_type: :RECEIVED)
693
+ result = query_with_retry(query, label: "packet_time_bounds", db_shard: db_shard)
694
+ return nil if result.nil? or result.ntuples == 0
695
+ row = result[0]
696
+ return nil if row['min_time'].nil? or row['max_time'].nil?
697
+ [row['min_time'].to_i, row['max_time'].to_i]
698
+ end
699
+
638
700
  # Return the telemetry packets which have a table. The TSDB microservice only
639
701
  # creates tables for its initial packets so a packet added later has no table
640
702
  # until it's received. If the table names can't be fetched assume they all exist.
@@ -729,14 +791,21 @@ module OpenC3
729
791
  # @param start_time [Integer] Start timestamp in nanoseconds
730
792
  # @param end_time [Integer, nil] End timestamp in nanoseconds
731
793
  # @param include_received_ts [Boolean] Whether to include RECEIVED_TIMESECONDS
794
+ # @param time_type [Symbol] :PACKET or :RECEIVED, which timestamp filters and orders the rows.
795
+ # :RECEIVED also selects the nanosecond received time as "__rx_time_ns".
796
+ # @param include_times [Boolean] Also select the nanosecond received time as "__rx_time_ns"
797
+ # so decode_item_row can return both the packet and received times
798
+ # @param packet_bounds [Array<Integer, nil>, nil] PACKET_TIMESECONDS limits for :RECEIVED (see time_where_clause)
732
799
  # @return [String] Complete SQL query (without LIMIT clause)
733
- def self.build_item_columns_query(table_name, column_names, start_time, end_time, include_received_ts: false)
800
+ def self.build_item_columns_query(table_name, column_names, start_time, end_time, include_received_ts: false, time_type: :PACKET, include_times: false, packet_bounds: nil)
734
801
  names = column_names.dup
735
802
  names << TIMESTAMP_SELECT
736
803
  names << "RECEIVED_TIMESECONDS" if include_received_ts
804
+ names << RECEIVED_TIMESTAMP_SELECT if time_type == :RECEIVED or include_times
737
805
  names << "COSMOS_EXTRA"
738
806
  query = "SELECT #{names.join(', ')} FROM \"#{table_name}\""
739
- query += time_where_clause(start_time, end_time)
807
+ query += time_where_clause(start_time, end_time, time_type: time_type, packet_bounds: packet_bounds)
808
+ query += time_order_clause(time_type)
740
809
  query
741
810
  end
742
811
 
@@ -745,10 +814,13 @@ module OpenC3
745
814
  # @param table_name [String] Sanitized QuestDB table name
746
815
  # @param start_time [Integer] Start timestamp in nanoseconds
747
816
  # @param end_time [Integer, nil] End timestamp in nanoseconds
817
+ # @param time_type [Symbol] :PACKET or :RECEIVED, which timestamp filters and orders the rows
818
+ # @param packet_bounds [Array<Integer, nil>, nil] PACKET_TIMESECONDS limits for :RECEIVED (see time_where_clause)
748
819
  # @return [String] Complete SQL query (without LIMIT clause)
749
- def self.build_packet_query(table_name, start_time, end_time)
820
+ def self.build_packet_query(table_name, start_time, end_time, time_type: :PACKET, packet_bounds: nil)
750
821
  query = "SELECT *, #{TIMESTAMP_EXTRAS} FROM \"#{table_name}\""
751
- query += time_where_clause(start_time, end_time)
822
+ query += time_where_clause(start_time, end_time, time_type: time_type, packet_bounds: packet_bounds)
823
+ query += time_order_clause(time_type)
752
824
  query
753
825
  end
754
826
 
@@ -759,13 +831,26 @@ module OpenC3
759
831
  # @param start_time [Integer] Start timestamp in nanoseconds
760
832
  # @param end_time [Integer, nil] End timestamp in nanoseconds
761
833
  # @param sample_interval [String] QuestDB SAMPLE BY interval ('1m', '1h', '1d')
834
+ # @param time_type [Symbol] :PACKET or :RECEIVED, which timestamp filters and buckets the rows.
835
+ # select_columns should start with timestamp_select(time_type).
836
+ # @param packet_bounds [Array<Integer, nil>, nil] PACKET_TIMESECONDS limits for :RECEIVED (see time_where_clause)
762
837
  # @return [String] Complete SQL query (without LIMIT clause)
763
- def self.build_reduced_query(table_name, select_columns, start_time, end_time, sample_interval)
764
- query = "SELECT #{select_columns.join(', ')} FROM \"#{table_name}\""
765
- query += time_where_clause(start_time, end_time)
838
+ def self.build_reduced_query(table_name, select_columns, start_time, end_time, sample_interval, time_type: :PACKET, packet_bounds: nil)
839
+ column = time_column(time_type)
840
+ query = "SELECT #{select_columns.join(', ')} FROM "
841
+ if time_type == :RECEIVED
842
+ # SAMPLE BY buckets on the designated timestamp (PACKET_TIMESECONDS), so
843
+ # sort a subquery by received time and declare that as its timestamp
844
+ query += "(SELECT * FROM \"#{table_name}\""
845
+ query += time_where_clause(start_time, end_time, time_type: time_type, packet_bounds: packet_bounds)
846
+ query += " ORDER BY #{column}) timestamp(#{column})"
847
+ else
848
+ query += "\"#{table_name}\""
849
+ query += time_where_clause(start_time, end_time, time_type: time_type, packet_bounds: packet_bounds)
850
+ end
766
851
  query += " SAMPLE BY #{sample_interval}"
767
852
  query += " ALIGN TO CALENDAR"
768
- query += " ORDER BY PACKET_TIMESECONDS"
853
+ query += " ORDER BY #{column}"
769
854
  query
770
855
  end
771
856
 
@@ -779,13 +864,19 @@ module OpenC3
779
864
  # :item_types [Array<Hash>] - type info per position ({ 'data_type' =>, 'array_size' => })
780
865
  # :stored_timestamp_item_keys [Hash] - { item_key => { column: col_name } }
781
866
  # :calculated_positions [Hash] - { local_idx => { source: col_name, format: :seconds/:formatted } }
867
+ # @param time_type [Symbol] :PACKET sets __time to the packet time, :RECEIVED to the
868
+ # received time (requires the "__rx_time_ns" column from build_item_columns_query)
869
+ # @param include_times [Boolean] Also set __packet_time and __received_time (requires
870
+ # build_item_columns_query with include_times: true)
782
871
  # @return [Hash] Entry hash with __type, item_key => value, __time, COSMOS_EXTRA
783
- def self.decode_item_row(row, sql_to_local, meta)
872
+ # and optionally __packet_time and __received_time
873
+ def self.decode_item_row(row, sql_to_local, meta, time_type: :PACKET, include_times: false)
784
874
  num_sql_item_cols = sql_to_local.length
785
875
 
786
876
  entry = { "__type" => "ITEMS" }
787
877
  timestamp_values = {}
788
878
  time_ns = nil
879
+ received_time_ns = nil
789
880
  cosmos_extra = nil
790
881
 
791
882
  values = Array.new(meta[:item_keys].length)
@@ -802,6 +893,8 @@ module OpenC3
802
893
  timestamp_values['PACKET_TIMESECONDS'] = nsec_to_utc_time(time_ns)
803
894
  when 'RECEIVED_TIMESECONDS'
804
895
  timestamp_values['RECEIVED_TIMESECONDS'] = value if value
896
+ when '__rx_time_ns'
897
+ received_time_ns = value.to_i if value
805
898
  when 'COSMOS_EXTRA'
806
899
  cosmos_extra = value
807
900
  # No else because we're only interested in these specific extra columns; others can be ignored
@@ -845,7 +938,12 @@ module OpenC3
845
938
  end
846
939
  end
847
940
 
848
- entry['__time'] = time_ns if time_ns
941
+ entry_time = (time_type == :RECEIVED) ? received_time_ns : time_ns
942
+ entry['__time'] = entry_time if entry_time
943
+ if include_times
944
+ entry['__packet_time'] = time_ns if time_ns
945
+ entry['__received_time'] = received_time_ns if received_time_ns
946
+ end
849
947
  entry['COSMOS_EXTRA'] = cosmos_extra if cosmos_extra
850
948
  entry
851
949
  end
@@ -857,8 +955,11 @@ module OpenC3
857
955
  # @param row [PG::Result row] Single row as iterable [col_name, value] pairs
858
956
  # @param value_type [Symbol] :RAW, :CONVERTED, :FORMATTED
859
957
  # @param packet_def [Hash, nil] Packet definition for type-aware decoding
958
+ # @param time_type [Symbol] :PACKET sets __time to the packet time, :RECEIVED to the received time
959
+ # @param include_times [Boolean] Also set __packet_time and __received_time
860
960
  # @return [Hash] Entry hash with item => value, __time, COSMOS_EXTRA, timestamp entries
861
- def self.decode_packet_row(row, value_type, packet_def)
961
+ # and optionally __packet_time and __received_time
962
+ def self.decode_packet_row(row, value_type, packet_def, time_type: :PACKET, include_times: false)
862
963
  entry = {}
863
964
  item_defs = build_item_defs_map(packet_def)
864
965
 
@@ -878,12 +979,13 @@ module OpenC3
878
979
 
879
980
  if column_name == '__pkt_time_ns'
880
981
  cosmos_timestamp_ns = raw_value.to_i
881
- entry['__time'] = cosmos_timestamp_ns
982
+ entry['__time'] = cosmos_timestamp_ns unless time_type == :RECEIVED
882
983
  next
883
984
  end
884
985
 
885
986
  if column_name == '__rx_time_ns'
886
987
  received_timestamp_ns = raw_value.to_i
988
+ entry['__time'] = received_timestamp_ns if time_type == :RECEIVED
887
989
  next
888
990
  end
889
991
 
@@ -929,11 +1031,17 @@ module OpenC3
929
1031
 
930
1032
  add_timestamp_entries!(entry, cosmos_timestamp_ns, 'PACKET')
931
1033
  add_timestamp_entries!(entry, received_timestamp_ns, 'RECEIVED')
1034
+ if include_times
1035
+ entry['__packet_time'] = cosmos_timestamp_ns if cosmos_timestamp_ns
1036
+ entry['__received_time'] = received_timestamp_ns if received_timestamp_ns
1037
+ end
932
1038
  entry
933
1039
  end
934
1040
 
935
1041
  # Decode a single row from a SAMPLE BY aggregation query.
936
1042
  # All non-timestamp columns are decoded as DOUBLE (aggregation results are always numeric).
1043
+ # The bucket timestamp column is PACKET_TIMESECONDS or RECEIVED_TIMESECONDS
1044
+ # depending on the time type passed to timestamp_select.
937
1045
  #
938
1046
  # @param row [PG::Result row] Single row as iterable [col_name, value] pairs
939
1047
  # @return [Hash] { col_name => decoded_value, '__time' => ns_integer }
@@ -942,7 +1050,7 @@ module OpenC3
942
1050
  row.each do |tuple|
943
1051
  col_name = tuple[0]
944
1052
  value = tuple[1]
945
- if col_name == 'PACKET_TIMESECONDS'
1053
+ if STORED_TIMESTAMP_ITEMS.include?(col_name)
946
1054
  entry['__time'] = value.to_i
947
1055
  else
948
1056
  entry[col_name] = decode_value(value, data_type: 'DOUBLE', array_size: nil)
@@ -196,7 +196,9 @@ module OpenC3
196
196
  reader.each(local_file, false) do |packet|
197
197
  next unless packet.target_name && packet.packet_name
198
198
  cmd_or_tlm = packet.cmd_or_tlm == :CMD ? 'CMD' : 'TLM'
199
- table_names.add("#{@scope}__#{cmd_or_tlm}__#{packet.target_name}__#{packet.packet_name}")
199
+ # Use the same sanitization as the TSDB writer so names match the
200
+ # real tables and can't break out of the quoted ALTER TABLE literal
201
+ table_names.add(QuestDBClient.sanitize_table_name(packet.target_name, packet.packet_name, cmd_or_tlm, scope: @scope))
200
202
  end
201
203
  ids = reader.instance_variable_get(:@target_ids) || []
202
204
  file_versions[local_file] = ids.first ? ids.first.unpack1('H*') : nil
@@ -320,7 +322,7 @@ module OpenC3
320
322
  if already
321
323
  preexisting << table_name
322
324
  else
323
- conn.exec("ALTER TABLE '#{table_name}' DEDUP ENABLE UPSERT KEYS(PACKET_TIMESECONDS)")
325
+ conn.exec("ALTER TABLE #{quote_table_name(table_name)} DEDUP ENABLE UPSERT KEYS(PACKET_TIMESECONDS)")
324
326
  enabled_by_us << table_name
325
327
  end
326
328
  rescue => e
@@ -331,6 +333,13 @@ module OpenC3
331
333
  [enabled_by_us, preexisting]
332
334
  end
333
335
 
336
+ # DDL can't take bind parameters so the table name is interpolated.
337
+ # sanitize_table_name already strips quotes; refuse anything that slipped through.
338
+ def quote_table_name(table_name)
339
+ raise ArgumentError, "Invalid table name: #{table_name}" if table_name.match?(/['"\\]/)
340
+ "'#{table_name}'"
341
+ end
342
+
334
343
  # QuestDB exposes per-table dedup status via tables() function.
335
344
  # Falls back to false (treat as not-enabled, will issue ALTER) on any error.
336
345
  def dedup_already_enabled?(conn, table_name)
@@ -364,7 +373,7 @@ module OpenC3
364
373
  conn = QuestDBClient.connection(db_shard: db_shard)
365
374
  tables.each_with_index do |table_name, i|
366
375
  begin
367
- conn.exec("ALTER TABLE '#{table_name}' DEDUP DISABLE")
376
+ conn.exec("ALTER TABLE #{quote_table_name(table_name)} DEDUP DISABLE")
368
377
  disabled << table_name
369
378
  rescue => e
370
379
  @logger.warn("Failed to disable DEDUP on #{table_name}: #{e.message}")