Skip to content

Commit 65ab413

Browse files
authored
Merge pull request #2508 from jarhodes314/feat/unix-timestamp-measurements
Add support for parsing unix timestamp measurements
2 parents 2743c64 + bbb68fd commit 65ab413

File tree

50 files changed

+983
-289
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+983
-289
lines changed

Cargo.lock

Lines changed: 20 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ sha256 = "1.1"
132132
shell-words = "1.1"
133133
signal-hook = "0.3"
134134
signal-hook-tokio = { version = "0.3", features = ["futures-v0_3"] }
135+
strum = "0.24"
135136
strum_macros = "0.24"
136137
syn = { version = "2", features = ["full", "extra-traits"] }
137138
tedge-agent = { path = "crates/core/tedge_agent" }
@@ -190,3 +191,7 @@ opt-level = "z"
190191
panic = "abort"
191192
strip = "symbols"
192193
overflow-checks = true
194+
[profile.dev]
195+
codegen-units = 16
196+
opt-level = "z"
197+
strip = "symbols"

crates/common/tedge_config/Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ license = { workspace = true }
88
homepage = { workspace = true }
99
repository = { workspace = true }
1010

11+
[features]
12+
default = []
13+
test = []
14+
1115
[dependencies]
1216
anyhow = { workspace = true }
1317
camino = { workspace = true, features = ["serde", "serde1"] }
@@ -19,9 +23,10 @@ once_cell = { workspace = true }
1923
reqwest = { workspace = true, features = ["rustls-tls-native-roots"] }
2024
serde = { workspace = true, features = ["rc"] }
2125
serde_ignored = { workspace = true }
26+
strum = { workspace = true }
2227
strum_macros = { workspace = true }
2328
tedge_config_macros = { workspace = true }
24-
tedge_utils = { workspace = true }
29+
tedge_utils = { workspace = true, features = ["timestamp"] }
2530
thiserror = { workspace = true }
2631
toml = { workspace = true }
2732
tracing = { workspace = true }
@@ -31,6 +36,7 @@ url = { workspace = true }
3136
[dev-dependencies]
3237
assert_matches = { workspace = true }
3338
figment = { workspace = true, features = ["test"] }
39+
serde_json = { workspace = true }
3440
tedge_test_utils = { workspace = true }
3541
tempfile = { workspace = true }
3642
test-case = { workspace = true }

crates/common/tedge_config/src/tedge_config_cli/figment.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,22 @@ pub fn extract_data<T: DeserializeOwned, Sources: ConfigSources>(
7373
}
7474
}
7575

76+
#[cfg(feature = "test")]
77+
pub fn extract_from_toml_str<T: DeserializeOwned>(toml: &str) -> Result<T, TEdgeConfigError> {
78+
let env = TEdgeEnv::default();
79+
let figment = Figment::new().merge(Toml::string(toml));
80+
81+
let data = extract_exact(&figment, &env);
82+
83+
let warnings = unused_value_warnings::<T>(&figment, &env)
84+
.ok()
85+
.map(UnusedValueWarnings)
86+
.unwrap_or_default();
87+
88+
warnings.emit();
89+
data
90+
}
91+
7692
fn unused_value_warnings<T: DeserializeOwned>(
7793
figment: &Figment,
7894
env: &TEdgeEnv,

crates/common/tedge_config/src/tedge_config_cli/models/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ pub mod port;
77
pub mod seconds;
88
pub mod templates_set;
99

10+
pub use tedge_utils::timestamp;
11+
1012
pub const HTTPS_PORT: u16 = 443;
1113
pub const MQTT_TLS_PORT: u16 = 8883;
1214

crates/common/tedge_config/src/tedge_config_cli/tedge_config.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use super::models::timestamp::TimeFormat;
12
use crate::AutoFlag;
23
use crate::ConnectUrl;
34
use crate::HostPort;
@@ -468,6 +469,12 @@ define_tedge_config! {
468469
#[tedge_config(example = "true")]
469470
#[tedge_config(default(value = true))]
470471
timestamp: bool,
472+
473+
/// The format that will be used by the mapper when sending timestamps to Azure IoT
474+
#[tedge_config(example = "rfc-3339")]
475+
#[tedge_config(example = "unix")]
476+
#[tedge_config(default(variable = "TimeFormat::Unix"))]
477+
timestamp_format: TimeFormat,
471478
},
472479

473480
/// Set of MQTT topics the Azure IoT mapper should subscribe to
@@ -492,6 +499,12 @@ define_tedge_config! {
492499
#[tedge_config(example = "true")]
493500
#[tedge_config(default(value = true))]
494501
timestamp: bool,
502+
503+
/// The format that will be used by the mapper when sending timestamps to AWS IoT
504+
#[tedge_config(example = "rfc-3339")]
505+
#[tedge_config(example = "unix")]
506+
#[tedge_config(default(variable = "TimeFormat::Unix"))]
507+
timestamp_format: TimeFormat,
495508
},
496509

497510
/// Set of MQTT topics the AWS IoT mapper should subscribe to
@@ -735,6 +748,12 @@ define_tedge_config! {
735748
/// The thin-edge.io service's service type
736749
#[tedge_config(rename = "type", example = "systemd", default(value = "service"))]
737750
ty: String,
751+
752+
/// The format that will be used for the timestamp when generating service "up" messages in thin-edge JSON
753+
#[tedge_config(example = "rfc-3339")]
754+
#[tedge_config(example = "unix")]
755+
#[tedge_config(default(variable = "TimeFormat::Unix"))]
756+
timestamp_format: TimeFormat,
738757
},
739758

740759
apt: {

crates/common/tedge_config/src/tedge_config_cli/tedge_config_repository.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ impl TEdgeConfigRepository {
5555
Ok(dto)
5656
}
5757

58+
#[cfg(feature = "test")]
59+
/// A test only method designed for injecting configuration into tests
60+
///
61+
/// ```
62+
/// use tedge_config::TEdgeConfigRepository;
63+
/// let config = TEdgeConfigRepository::load_toml_str("service.ty = \"service\"");
64+
///
65+
/// assert_eq!(&config.service.ty, "service");
66+
/// // Defaults are preserved
67+
/// assert_eq!(config.enable.sudo, true);
68+
/// ```
69+
pub fn load_toml_str(toml: &str) -> TEdgeConfig {
70+
let dto = super::figment::extract_from_toml_str(toml).unwrap();
71+
TEdgeConfig::from_dto(&dto, &TEdgeConfigLocation::default())
72+
}
73+
5874
fn load_dto_with_warnings<Sources: ConfigSources>(
5975
&self,
6076
path: &Utf8Path,

crates/common/tedge_config_macros/impl/src/reader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ fn generate_structs(
124124
.unzip();
125125

126126
Ok(quote! {
127-
#[derive(::doku::Document, ::serde::Serialize, Debug)]
127+
#[derive(::doku::Document, ::serde::Serialize, Debug, Clone)]
128128
#[non_exhaustive]
129129
#[doc = #doc_comment]
130130
pub struct #name {

crates/common/tedge_utils/Cargo.toml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,29 @@ repository = { workspace = true }
1313
# No features on by default
1414
default = []
1515
logging = []
16-
fs-notify = ["strum_macros", "notify", "notify-debouncer-full"]
16+
fs-notify = ["strum", "notify", "notify-debouncer-full"]
17+
timestamp = ["strum", "time", "serde", "serde_json"]
1718

1819
[dependencies]
1920
anyhow = "1.0.71"
21+
doku = { workspace = true }
2022
futures = { workspace = true }
2123
mqtt_channel = { workspace = true }
2224
nix = { workspace = true }
2325
notify = { workspace = true, optional = true }
2426
notify-debouncer-full = { workspace = true, optional = true }
25-
strum_macros = { workspace = true, optional = true }
27+
serde = { workspace = true, optional = true }
28+
serde_json = { workspace = true, optional = true }
29+
strum = { workspace = true, optional = true, features = ["derive"] }
2630
tempfile = { workspace = true }
2731
thiserror = { workspace = true }
32+
time = { workspace = true, features = [
33+
"formatting",
34+
"local-offset",
35+
"parsing",
36+
"serde",
37+
"serde-well-known",
38+
], optional = true }
2839
tokio = { workspace = true, default_features = false, features = [
2940
"fs",
3041
"io-util",

crates/common/tedge_utils/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ pub mod timers;
88
pub mod futures;
99
#[cfg(feature = "fs-notify")]
1010
pub mod notify;
11+
12+
#[cfg(feature = "timestamp")]
13+
pub mod timestamp;

0 commit comments

Comments
 (0)