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

refactor: Extract operation implementation details from CumulocityConverter #3260

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Bravo555
Copy link
Contributor

@Bravo555 Bravo555 commented Nov 22, 2024

TODO

  • cleanup

Proposed changes

This PR introduces an SupportedOperations struct that is used by CumulocityConverter to load and save operations.

It achieves following things:

  • Extract all fs operations and path building from CumulocityConverter into new operations module: The main CumulocityConverter doesn't create any files or symlinks, or doesn't need to create paths, the logic of mapping supported operations to the filesystem is contained in its own module
  • No main device/child device branching in CumulocityConverter: instead of having separate operations: Operations and children: HashMap<Child, Operations>, SupportedOperation handles this distinction, the converter just passes an external id
  • Use BTreeMap for to have only one operation under a given name and to ensure consistent ordering

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

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

Copy link

codecov bot commented Nov 22, 2024

Codecov Report

Attention: Patch coverage is 83.45324% with 46 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/core/c8y_api/src/smartrest/operations.rs 86.63% 17 Missing and 12 partials ⚠️
crates/extensions/c8y_mapper_ext/src/converter.rs 72.13% 6 Missing and 11 partials ⚠️
Additional details and impacted files

📢 Thoughts on this report? Let us know!

@Bravo555 Bravo555 changed the title refactor: Use a BTreeMap for Operations storage refactor: Extract operation implementation details from CumulocityConverter Nov 27, 2024
Using it multiple times will create operations with the same names, but
its impossible to have multiple operation implementations for the same
operation name

Signed-off-by: Marcel Guzik <[email protected]>
Copy link
Contributor

github-actions bot commented Nov 27, 2024

Robot Results

✅ Passed ❌ Failed ⏭️ Skipped Total Pass % ⏱️ Duration
527 0 2 527 100 1h27m28.699284s

let prev_operation = current_operations.remove_operation(&operation.name);
let prev_operation = current_operations.insert_operation(operation);
Copy link
Contributor

Choose a reason for hiding this comment

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

This change seems weird at first ... but is correct!

Previously any operation with the same name was removed before adding a new version of the same operation.

Comment on lines +56 to +57
/// Required because when given an external id of the main device when creating an operation, we want to create it
/// in a main directory, instead of a subdirectory.
Copy link
Contributor

Choose a reason for hiding this comment

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

Okay. It makes sense for this PR focus on code refactoring without any user-visible changes, but this asymmetry between the main device and the child devices makes things more complicated than they should.

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 don't fully get what you mean. Do you mean this PR specifically, or the codebase more generally?

One maybe unnecessary change that I did was instead of having "operations for the main device" in one field and "operations for child devices" in a hashmap, i put it all in a single hashmap, so that CumulocityConverter only passes along the external id and doesn't have to care if a device is a child device or a main device. In retrospect the functions can still take an external id but the operations for the main device can be made a normal field instead of a hashmap entry, since "operations for the main device" is something that always exists and putting it in a hashmap where it can be removed introduces an extra failure mode.

Copy link
Contributor

Choose a reason for hiding this comment

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

My point is in no case to disagree with this PR which is a real improvement reducing the accidental complexity of the c8y mapper.

My comment is coming from the fact that you (correctly) feel the need to add a comment on why we need the main device external id here (while we don't for the child devices).

Comment on lines -811 to +821
EntityType::MainDevice => Ok(self.operations.filter_by_topic(topic, prefix)),
EntityType::ChildDevice => {
if let Some(operations) = self.children.get(device_xid) {
Ok(operations.filter_by_topic(topic, prefix))
} else {
Ok(Vec::new())
}
}
EntityType::MainDevice | EntityType::ChildDevice => Ok(self
.supported_operations
.get_operation_handlers(device_xid, topic, prefix)),
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 one of the benefit of this PR, pushing away the artificial difference that has been introduced between the main and the child devices (see also: #3260 (comment))

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. This is nice clarification of the code base.

@Bravo555 Bravo555 added this pull request to the merge queue Nov 28, 2024
@Bravo555 Bravo555 removed this pull request from the merge queue due to a manual request Nov 28, 2024
github-merge-queue bot pushed a commit that referenced this pull request Nov 28, 2024
refactor: Extract operation implementation details from `CumulocityConverter`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants