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

ability to query devices using status values #3467

Merged
merged 2 commits into from
Sep 17, 2024
Merged

Conversation

Baalmart
Copy link
Contributor

@Baalmart Baalmart commented Sep 17, 2024

Description

ability to query devices using status values

Changes Made

  • ability to query devices using status values

Testing

  • Tested locally
  • Tested against staging environment
  • Relevant tests passed: [List test names]

Affected Services

  • Which services were modified:
    • device registry

Endpoints Ready for Testing

  • New endpoints ready for testing:
    • get devices

API Documentation Updated?

  • Yes, API documentation was updated
  • No, API documentation does not need updating

Summary by CodeRabbit

  • New Features
    • Introduced enhanced device filtering based on operational status, allowing users to filter devices more granularly.
    • Added support for multiple statuses in filter requests, improving the flexibility of device management.
    • Added a predefined list of valid device statuses for improved device management and categorization.

Copy link
Contributor

coderabbitai bot commented Sep 17, 2024

Walkthrough

Walkthrough

The changes introduce a new constant, VALID_DEVICE_STATUSES, to the device management system, defining a set of valid statuses for devices. Additionally, the generateFilter utility is enhanced to filter devices based on these statuses, allowing for more precise queries. The implementation supports multiple statuses from a request, ensuring proper formatting and validation against the predefined list. This enhancement expands the functionality of device filtering without altering existing features.

Changes

Files Change Summary
src/device-registry/config/global/static-lists.js Added VALID_DEVICE_STATUSES constant as an array of valid device statuses.
src/device-registry/utils/generate-filter.js Added status parameter to generateFilter function, enabling filtering of devices based on status.

Possibly related PRs

In the realm of devices, a status we seek,
With filters now ready, our queries won't leak.
Multiple statuses, all trimmed and neat,
MongoDB's magic makes filtering sweet!
So here's to the changes, both clever and bright,
In the world of tech, we shine a new light! ✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    -- I pushed a fix in commit <commit_id>, please review it.
    -- Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    -- @coderabbitai generate unit testing code for this file.
    -- @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    -- @coderabbitai generate interesting stats about this repository and render them as a table.
    -- @coderabbitai read src/utils.ts and generate unit testing code.
    -- @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    -- @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.
Early access features: enabled

We are currently testing the following features in early access:

  • OpenAI o1 for code reviews: OpenAI's new o1 model is being tested for generating code suggestions in code reviews.

Note:

  • You can enable or disable early access features from the CodeRabbit UI or by updating the CodeRabbit configuration file.

Copy link

codecov bot commented Sep 17, 2024

Codecov Report

Attention: Patch coverage is 9.09091% with 10 lines in your changes missing coverage. Please review.

Project coverage is 30.20%. Comparing base (eb6aa84) to head (6b1c5e6).
Report is 3 commits behind head on staging.

Files with missing lines Patch % Lines
src/device-registry/utils/generate-filter.js 9.09% 9 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           staging    #3467      +/-   ##
===========================================
- Coverage    30.21%   30.20%   -0.01%     
===========================================
  Files          184      184              
  Lines        24554    24565      +11     
  Branches      3219     3222       +3     
===========================================
+ Hits          7418     7419       +1     
- Misses       17022    17031       +9     
- Partials       114      115       +1     
Files with missing lines Coverage Δ
src/device-registry/config/global/static-lists.js 100.00% <ø> (ø)
src/device-registry/utils/generate-filter.js 39.72% <9.09%> (-0.47%) ⬇️

Copy link
Contributor

Device registry changes in this PR available for preview here

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Outside diff range and nitpick comments (1)
src/device-registry/utils/generate-filter.js (1)

1015-1016: Optimize string replacement for status normalization

The replacement of underscores and dashes with spaces might not cover all scenarios. Consider using a more comprehensive normalization method if necessary.

Tools
GitHub Check: codecov/patch

[warning] 1015-1016: src/device-registry/utils/generate-filter.js#L1015-L1016
Added lines #L1015 - L1016 were not covered by tests

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between eb6aa84 and fa2bef8.

Files selected for processing (1)
  • src/device-registry/utils/generate-filter.js (2 hunks)
Additional context used
GitHub Check: codecov/patch
src/device-registry/utils/generate-filter.js

[warning] 1007-1008: src/device-registry/utils/generate-filter.js#L1007-L1008
Added lines #L1007 - L1008 were not covered by tests


[warning] 1010-1010: src/device-registry/utils/generate-filter.js#L1010
Added line #L1010 was not covered by tests


[warning] 1015-1016: src/device-registry/utils/generate-filter.js#L1015-L1016
Added lines #L1015 - L1016 were not covered by tests


[warning] 1018-1018: src/device-registry/utils/generate-filter.js#L1018
Added line #L1018 was not covered by tests


[warning] 1021-1021: src/device-registry/utils/generate-filter.js#L1021
Added line #L1021 was not covered by tests

Additional comments not posted (2)
src/device-registry/utils/generate-filter.js (2)

905-905: Introduction of 'status' parameter to device filter

The addition of the status parameter enhances the querying capabilities of the devices. Please ensure that this parameter is properly validated and sanitized to handle unexpected inputs gracefully.


1005-1005: Verify the regular expression for splitting status strings

The regular expression used to split the status string may not handle all edge cases, such as statuses with embedded commas or unusual formatting. Please verify that the regex correctly handles all expected input formats.

Comment on lines 992 to 1001
const validStatuses = [
"recalled",
"ready",
"deployed",
"undeployed",
"decommissioned",
"assembly",
"testing",
"not deployed",
];
Copy link
Contributor

Choose a reason for hiding this comment

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

Externalize 'validStatuses' for better maintainability

Currently, the validStatuses array is hardcoded within the function. To improve maintainability and allow for easier updates or reuse, consider moving validStatuses to a constants file or configuration module.

Comment on lines +1003 to +1023
if (status) {
// Split the status string by commas, but not within quotes
const statusArray = status.match(/(".*?"|[^",\s]+)(?=\s*,|\s*$)/g) || [];

const validStatusArray = statusArray
.map((s) => {
// Remove quotes and trim whitespace
s = s
.replace(/^"|"$/g, "")
.trim()
.toLowerCase();
// Replace underscores or dashes with spaces
s = s.replace(/[_-]/g, " ");
return s;
})
.filter((s) => validStatuses.includes(s));

if (validStatusArray.length > 0) {
filter.status = { $in: validStatusArray };
}
}
Copy link
Contributor

@coderabbitai coderabbitai bot Sep 17, 2024

Choose a reason for hiding this comment

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

Add unit tests for 'status' parsing logic

The new logic for parsing the status parameter is crucial for filtering devices accurately. However, static analysis indicates that these lines are not covered by tests. Adding unit tests will ensure the reliability of this functionality and help catch potential issues early.

Would you like assistance in writing these unit tests or should I open a GitHub issue to track this task?

Tools
GitHub Check: codecov/patch

[warning] 1007-1008: src/device-registry/utils/generate-filter.js#L1007-L1008
Added lines #L1007 - L1008 were not covered by tests


[warning] 1010-1010: src/device-registry/utils/generate-filter.js#L1010
Added line #L1010 was not covered by tests


[warning] 1015-1016: src/device-registry/utils/generate-filter.js#L1015-L1016
Added lines #L1015 - L1016 were not covered by tests


[warning] 1018-1018: src/device-registry/utils/generate-filter.js#L1018
Added line #L1018 was not covered by tests


[warning] 1021-1021: src/device-registry/utils/generate-filter.js#L1021
Added line #L1021 was not covered by tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please open a GitHub issue to track this task

Copy link
Contributor

Choose a reason for hiding this comment

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

@Baalmart, I've successfully created the GitHub issue: #3468

 (\_/)
 (•ᴗ•)
 /🍃🌼\ 
Analysis chain

@Baalmart, I've opened a GitHub issue to track this task.

 (\_/)
 (•ᴗ•)
 /🍃🌼\ 
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
gh issue create \
  --title "Add unit tests for 'status' parsing logic in generate-filter.js" \
  --body "We need to add unit tests for the new 'status' parsing logic introduced in PR #3467.

PR URL: https://github.com/airqo-platform/AirQo-api/pull/3467
Comment URL: https://github.com/airqo-platform/AirQo-api/pull/3467#discussion_r1764048444

Requested by @Baalmart." \
  --assignee "Baalmart"

Length of output: 451

Copy link
Contributor

Device registry changes in this PR available for preview here

@Baalmart Baalmart merged commit fae0662 into staging Sep 17, 2024
49 of 50 checks passed
@Baalmart Baalmart deleted the hf-devices-query branch September 17, 2024 21:58
@Baalmart Baalmart mentioned this pull request Sep 17, 2024
2 tasks
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Outside diff range and nitpick comments (1)
src/device-registry/config/global/static-lists.js (1)

11-20: Consider aligning the definition of 'VALID_DEVICE_STATUSES' with 'AQI_CATEGORIES' for consistency.

To maintain a consistent coding style within staticLists, you might define VALID_DEVICE_STATUSES using a string and the .split(',') method, similar to AQI_CATEGORIES.

Apply this diff to align the style:

-  VALID_DEVICE_STATUSES: [
-    "recalled",
-    "ready",
-    "deployed",
-    "undeployed",
-    "decommissioned",
-    "assembly",
-    "testing",
-    "not deployed",
-  ],
+  VALID_DEVICE_STATUSES: "recalled,ready,deployed,undeployed,decommissioned,assembly,testing,not deployed".split(","),
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between fa2bef8 and 6b1c5e6.

Files selected for processing (2)
  • src/device-registry/config/global/static-lists.js (1 hunks)
  • src/device-registry/utils/generate-filter.js (2 hunks)
Additional context used
GitHub Check: codecov/patch
src/device-registry/utils/generate-filter.js

[warning] 998-999: src/device-registry/utils/generate-filter.js#L998-L999
Added lines #L998 - L999 were not covered by tests


[warning] 1001-1001: src/device-registry/utils/generate-filter.js#L1001
Added line #L1001 was not covered by tests


[warning] 1006-1007: src/device-registry/utils/generate-filter.js#L1006-L1007
Added lines #L1006 - L1007 were not covered by tests


[warning] 1009-1009: src/device-registry/utils/generate-filter.js#L1009
Added line #L1009 was not covered by tests


[warning] 1012-1012: src/device-registry/utils/generate-filter.js#L1012
Added line #L1012 was not covered by tests

Additional comments not posted (3)
src/device-registry/config/global/static-lists.js (1)

11-20: Addition of 'VALID_DEVICE_STATUSES' is well-structured and enhances maintainability.

Defining VALID_DEVICE_STATUSES within staticLists provides a centralized list of valid statuses, improving consistency across the codebase.

src/device-registry/utils/generate-filter.js (2)

905-905: Inclusion of 'status' parameter enhances device filtering capabilities.

Adding the status parameter allows users to query devices based on their operational status, improving the flexibility of the API.


992-1014: 'Status' filtering logic is effectively implemented.

The parsing, normalization, and validation of the status parameter are handled appropriately. Utilizing constants.VALID_DEVICE_STATUSES ensures only valid statuses are considered in queries.

Tools
GitHub Check: codecov/patch

[warning] 998-999: src/device-registry/utils/generate-filter.js#L998-L999
Added lines #L998 - L999 were not covered by tests


[warning] 1001-1001: src/device-registry/utils/generate-filter.js#L1001
Added line #L1001 was not covered by tests


[warning] 1006-1007: src/device-registry/utils/generate-filter.js#L1006-L1007
Added lines #L1006 - L1007 were not covered by tests


[warning] 1009-1009: src/device-registry/utils/generate-filter.js#L1009
Added line #L1009 was not covered by tests


[warning] 1012-1012: src/device-registry/utils/generate-filter.js#L1012
Added line #L1012 was not covered by tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant