Skip to content

Support custom fragments in alarm creation#1699

Merged
PradeepKiruvale merged 1 commit intothin-edge:mainfrom
PradeepKiruvale:feature/custom-frag-alarm
Feb 2, 2023
Merged

Support custom fragments in alarm creation#1699
PradeepKiruvale merged 1 commit intothin-edge:mainfrom
PradeepKiruvale:feature/custom-frag-alarm

Conversation

@PradeepKiruvale
Copy link
Copy Markdown
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
Copy Markdown
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.

pub time: Option<Timestamp>,

#[serde(flatten)]
pub extras: HashMap<String, Value>,
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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

anyhow = "1.0"
assert-json-diff = "2.0"
assert_matches = "1.5"
maplit = "1.0.2"
Copy link
Copy Markdown
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),
]);

Some(serde_json::from_str(mqtt_payload)?)
};

// The 4th part of the topic name is the event source - if any
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

#[serde(skip_serializing_if = "Option::is_none")]
pub source: Option<C8yManagedObject>,
#[serde(rename = "externalSource")]
pub source: Option<HashMap<String, String>>,
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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>
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