Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom fragments in alarm creation #1699

Merged

Conversation

PradeepKiruvale
Copy link
Contributor

Signed-off-by: Pradeep Kumar K J pradeepkumar.kj@softwareag.com

Proposed changes

Create an alarm with custom fragments
Supports for both thin-edge devices as well as for the child devices.
For example, if an alarm message is published with a custom fragment as below a child device it must create an alarm with the custom message.

tedge mqtt pub tedge/alarms/critical/myCustomAlarmType/child_device_example '{ "text": "Some test alarm", "someOtherCustomFragment": {"nested":{"value": "extra info"}} }'

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Improvement (general improvements like code refactoring that doesn't explicitly fix a bug or add any new functionality)
  • Documentation Update (if none of the other choices apply)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Paste Link to the issue

#1682

Checklist

  • I have read the CONTRIBUTING doc
  • I have signed the CLA (in all commits with git commit -s)
  • I ran cargo fmt as mentioned in CODING_GUIDELINES
  • I used cargo clippy as mentioned in CODING_GUIDELINES
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Further comments

@reubenmiller
Copy link
Contributor

@PradeepKiruvale Look ok from a non-rust pov. I will let @albinsuresh (or some other rust enabled person) to formally approve to prevent it from being merged prematurely.

crates/core/tedge_api/src/alarm.rs Show resolved Hide resolved
crates/core/tedge_api/src/alarm.rs Outdated Show resolved Hide resolved
pub struct ThinEdgeAlarmData {
pub text: Option<String>,

#[serde(default)]
#[serde(with = "time::serde::rfc3339::option")]
pub time: Option<Timestamp>,

#[serde(flatten)]
pub extras: HashMap<String, Value>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this named extras? We use the term fragment in the documentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, we can rename it as fragments. But it will also contain the child device info as part of the message. Can this child device info also be called a fragment? I am not sure. So, I kept it in line with the events.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the child-device-id is stored as an entry in this extras map, what's the content of the source ? I though it was the id of the child device sending the alarm?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I though it was the id of the child device sending the alarm?

Yes it is. Not just for child devices, but even for the parent device.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed extras as fragments

@@ -35,6 +35,7 @@ tracing = { version = "0.1", features = ["attributes", "log"] }
anyhow = "1.0"
assert-json-diff = "2.0"
assert_matches = "1.5"
maplit = "1.0.2"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine as a dev dependency. But just FYI: you could statically create HashMap using the from method as follows:

let solar_distance = HashMap::from([
    ("Mercury", 0.4),
    ("Venus", 0.7),
    ("Earth", 1.0),
    ("Mars", 1.5),
]);

crates/core/tedge_api/src/alarm.rs Show resolved Hide resolved
@@ -106,10 +126,18 @@ impl ThinEdgeAlarm {
Some(serde_json::from_str(mqtt_payload)?)
};

// The 4th part of the topic name is the event source - if any
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// The 4th part of the topic name is the event source - if any
// The 4th part of the topic name is the alarm source - if any

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

);
output_messages.push(Message::new(&c8y_alarm_topic, smartrest_alarm));
} else {
dbg!(&c8y_alarm);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to remove this at the end.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment on lines 213 to 215
fn update_the_source(
source_name: &str,
) -> Result<Option<HashMap<String, String>>, SMCumulocityMapperError> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name and the type signature of this function hardly convey its purpose.

I would simply return a hash.

Suggested change
fn update_the_source(
source_name: &str,
) -> Result<Option<HashMap<String, String>>, SMCumulocityMapperError> {
fn make_c8y_source_fragment(
source_name: &str,
) -> HashMap<String, String> {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

Comment on lines 226 to 228
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(rename = "externalSource")]
pub source: Option<HashMap<String, String>>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would consider to remove the Option type, using an empty map instead of None.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced the hash map with a SourceInfo struct for the source.

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct C8yCreateAlarm {
#[serde(skip_serializing_if = "Option::is_none")]
pub source: Option<C8yManagedObject>,
#[serde(rename = "externalSource")]
pub source: Option<HashMap<String, String>>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of using a generic Hashmap here, I'd use a struct that replicates externalSource JSON structure:

"externalSource":{
        "externalId":"<child-device-id>",
        "type":"c8y_Serial"
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed

@reubenmiller
Copy link
Contributor

@PradeepKiruvale I found the reason why the alarm is not visible in the Cumulocity UI, the severity fields need to be in uppercase!

I just tested with your version here, and below is the json that I saw was being pushed to the c8y/alarm/alarms/create topic.

{
    "externalSource": {
        "externalId": "TST-extend_pure_loop_child1",
        "type": "c8y_Serial"
    },
    "severity": "critical",    // <<<<<<<<<<<<<<<<< needs to be UPPERCASE
    "type": "myCustomAlarmType",
    "time": "2023-02-01T21:08:14.800229538Z",
    "text": "Some test alarm",
    "someOtherCustomFragment": {
        "nested": {
            "value": "extra info"
        }
    }
}

@reubenmiller reubenmiller added improvement User value theme:telemetry Theme: Telemetry data labels Feb 1, 2023
@PradeepKiruvale
Copy link
Contributor Author

@PradeepKiruvale I found the reason why the alarm is not visible in the Cumulocity UI, the severity fields need to be in uppercase!

I just tested with your version here, and below is the json that I saw was being pushed to the c8y/alarm/alarms/create topic.

{
    "externalSource": {
        "externalId": "TST-extend_pure_loop_child1",
        "type": "c8y_Serial"
    },
    "severity": "critical",    // <<<<<<<<<<<<<<<<< needs to be UPPERCASE
    "type": "myCustomAlarmType",
    "time": "2023-02-01T21:08:14.800229538Z",
    "text": "Some test alarm",
    "someOtherCustomFragment": {
        "nested": {
            "value": "extra info"
        }
    }
}

Good catch, but I remember testing with all CAPS. But still, it was not working then, Now it works, strange. Anyways I have updated the code to address this issue.

@PradeepKiruvale PradeepKiruvale force-pushed the feature/custom-frag-alarm branch from a6b539a to d40ed80 Compare February 2, 2023 03:37
Copy link
Contributor

@didier-wenzek didier-wenzek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved

Signed-off-by: Pradeep Kumar K J <pradeepkumar.kj@softwareag.com>
@PradeepKiruvale PradeepKiruvale force-pushed the feature/custom-frag-alarm branch from e244f63 to d09703f Compare February 2, 2023 11:53
@PradeepKiruvale PradeepKiruvale merged commit 8d4ecbf into thin-edge:main Feb 2, 2023
github-actions bot referenced this pull request in reubenmiller/thin-edge.io Feb 2, 2023
github-actions bot referenced this pull request in reubenmiller/thin-edge.io Feb 2, 2023
Signed-off-by: Reuben Miller <reuben.d.miller@gmail.com>
github-actions bot referenced this pull request in reubenmiller/thin-edge.io Feb 2, 2023
Signed-off-by: Reuben Miller <reuben.d.miller@gmail.com>
github-actions bot referenced this pull request in reubenmiller/thin-edge.io Feb 2, 2023
Signed-off-by: Reuben Miller <reuben.d.miller@gmail.com>
github-actions bot referenced this pull request in reubenmiller/thin-edge.io Feb 3, 2023
Signed-off-by: Reuben Miller <reuben.d.miller@gmail.com>
github-actions bot referenced this pull request in reubenmiller/thin-edge.io Feb 3, 2023
Signed-off-by: Reuben Miller <reuben.d.miller@gmail.com>
github-actions bot referenced this pull request in reubenmiller/thin-edge.io Feb 3, 2023
Signed-off-by: Reuben Miller <reuben.d.miller@gmail.com>
github-actions bot referenced this pull request in reubenmiller/thin-edge.io Feb 3, 2023
Signed-off-by: Reuben Miller <reuben.d.miller@gmail.com>
github-actions bot referenced this pull request in reubenmiller/thin-edge.io Feb 3, 2023
Signed-off-by: Reuben Miller <reuben.d.miller@gmail.com>
github-actions bot referenced this pull request in reubenmiller/thin-edge.io Feb 4, 2023
Signed-off-by: Reuben Miller <reuben.d.miller@gmail.com>
github-actions bot referenced this pull request in reubenmiller/thin-edge.io Feb 4, 2023
Signed-off-by: Reuben Miller <reuben.d.miller@gmail.com>
github-actions bot referenced this pull request in reubenmiller/thin-edge.io Feb 5, 2023
Signed-off-by: Reuben Miller <reuben.d.miller@gmail.com>
github-actions bot referenced this pull request Aug 6, 2023
Signed-off-by: Rina Fujino <18257209+rina23q@users.noreply.github.com>
github-actions bot referenced this pull request Aug 8, 2023
chore: add maintainer scripts from existing debian packages for later comparison
@github-actions github-actions bot mentioned this pull request Dec 18, 2023
11 tasks
github-actions bot referenced this pull request Jan 22, 2024
…attempt

Use JSON over MQTT to listen C8Y operations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement User value theme:telemetry Theme: Telemetry data
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants