-
Notifications
You must be signed in to change notification settings - Fork 22
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
just adding the query parameters for site and device online status #3489
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,6 +113,81 @@ | |
.isIn(constants.NETWORKS) | ||
.withMessage("the tenant value is not among the expected ones"), | ||
]), | ||
oneOf([ | ||
[ | ||
query("id") | ||
.optional() | ||
.notEmpty() | ||
.trim() | ||
.isMongoId() | ||
.withMessage("id must be an object ID") | ||
.bail() | ||
.customSanitizer((value) => { | ||
return ObjectId(value); | ||
}), | ||
query("site_id") | ||
.optional() | ||
.notEmpty() | ||
.trim() | ||
.isMongoId() | ||
.withMessage("site_id must be an object ID") | ||
.bail() | ||
.customSanitizer((value) => { | ||
return ObjectId(value); | ||
}), | ||
query("name") | ||
.optional() | ||
.notEmpty() | ||
.trim(), | ||
query("online_status") | ||
.optional() | ||
.notEmpty() | ||
.withMessage("the online_status should not be empty if provided") | ||
.bail() | ||
.trim() | ||
.toLowerCase() | ||
.isIn(["online", "offline"]) | ||
.withMessage( | ||
"the online_status value is not among the expected ones which include: online, offline" | ||
), | ||
query("last_active_before") | ||
.optional() | ||
.notEmpty() | ||
.withMessage("last_active_before date cannot be empty IF provided") | ||
.bail() | ||
.trim() | ||
.isISO8601({ strict: true, strictSeparator: true }) | ||
.withMessage( | ||
"last_active_before date must be a valid ISO8601 datetime (YYYY-MM-DDTHH:mm:ss.sssZ)." | ||
) | ||
.bail() | ||
.toDate(), | ||
query("last_active_after") | ||
.optional() | ||
.notEmpty() | ||
.withMessage("last_active_after date cannot be empty IF provided") | ||
.bail() | ||
.trim() | ||
.isISO8601({ strict: true, strictSeparator: true }) | ||
.withMessage( | ||
"last_active_after date must be a valid ISO8601 datetime (YYYY-MM-DDTHH:mm:ss.sssZ)." | ||
) | ||
.bail() | ||
.toDate(), | ||
query("last_active") | ||
.optional() | ||
.notEmpty() | ||
.withMessage("last_active date cannot be empty IF provided") | ||
.bail() | ||
.trim() | ||
.isISO8601({ strict: true, strictSeparator: true }) | ||
.withMessage( | ||
"last_active date must be a valid ISO8601 datetime (YYYY-MM-DDTHH:mm:ss.sssZ)." | ||
) | ||
.bail() | ||
.toDate(), | ||
], | ||
]), | ||
Comment on lines
+116
to
+190
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tip Codebase Verification Missing Test Coverage for Custom Sanitizers No test files were found for Analysis chainComprehensive query parameter validation implemented The addition of new query parameters (id, site_id, name, online_status, last_active_before, last_active_after, last_active) with their respective validation logic enhances the API's capability to filter sites based on various criteria. The validation is thorough and well-structured. However, there are a few points to consider:
To improve code organization, consider extracting the date validation logic into a reusable function: function validateDateParameter(field) {
return query(field)
.optional()
.notEmpty()
.withMessage(`${field} date cannot be empty IF provided`)
.bail()
.trim()
.isISO8601({ strict: true, strictSeparator: true })
.withMessage(
`${field} date must be a valid ISO8601 datetime (YYYY-MM-DDTHH:mm:ss.sssZ).`
)
.bail()
.toDate();
}
// Usage
validateDateParameter('last_active_before'),
validateDateParameter('last_active_after'),
validateDateParameter('last_active'), To address the test coverage issue, we can use the following script to locate the relevant test file: This script will help identify if there are any existing tests for the custom sanitizers and where to add them if they're missing. Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Find the test file for src/device-registry/routes/v2/sites.js
test_file=$(find test -name "*sites.test.js" -o -name "*sites.spec.js")
if [ -n "$test_file" ]; then
echo "Test file found: $test_file"
echo "Checking for sanitizer tests:"
grep -n "customSanitizer" "$test_file" || echo "No sanitizer tests found."
else
echo "No test file found for sites.js"
fi
Length of output: 203 Script: #!/bin/bash
# Search for test files matching *sites.test.js or *sites.spec.js across the entire repository
test_files=$(find . -type f \( -name '*sites.test.js' -o -name '*sites.spec.js' \))
if [ -n "$test_files" ]; then
echo "Test files found:"
echo "$test_files"
echo "Checking for sanitizer tests:"
grep -n "customSanitizer" $test_files || echo "No sanitizer tests found."
else
echo "No test files found for sites.js"
fi
Length of output: 179 ToolsGitHub Check: codecov/patch
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please create a new Github issue for this suggestion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Baalmart, the GitHub issue has been created to track this suggestion: Issue #3494. Analysis chain@Baalmart, I'll create a new GitHub issue to track this suggestion. Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
gh issue create \
--title "Add unit tests for custom sanitizers in src/device-registry/routes/v2/sites.js" \
--body $'The custom sanitizers for `id` and `site_id` in `src/device-registry/routes/v2/sites.js` are not currently covered by unit tests. It\'s crucial to add appropriate tests to ensure these sanitizers behave as expected and maintain the integrity of the API.\n\nOriginally suggested in [PR #3489](https://github.com/airqo-platform/AirQo-api/pull/3489) by @Baalmart.\n\nSee the [comment](https://github.com/airqo-platform/AirQo-api/pull/3489#discussion_r1771708562) for more details.' \
--assignee Baalmart
Length of output: 667 |
||
siteController.list | ||
); | ||
router.get( | ||
|
@@ -128,6 +203,81 @@ | |
.isIn(constants.NETWORKS) | ||
.withMessage("the tenant value is not among the expected ones"), | ||
]), | ||
oneOf([ | ||
[ | ||
query("id") | ||
.optional() | ||
.notEmpty() | ||
.trim() | ||
.isMongoId() | ||
.withMessage("id must be an object ID") | ||
.bail() | ||
.customSanitizer((value) => { | ||
return ObjectId(value); | ||
}), | ||
query("site_id") | ||
.optional() | ||
.notEmpty() | ||
.trim() | ||
.isMongoId() | ||
.withMessage("site_id must be an object ID") | ||
.bail() | ||
.customSanitizer((value) => { | ||
return ObjectId(value); | ||
}), | ||
query("name") | ||
.optional() | ||
.notEmpty() | ||
.trim(), | ||
query("online_status") | ||
.optional() | ||
.notEmpty() | ||
.withMessage("the online_status should not be empty if provided") | ||
.bail() | ||
.trim() | ||
.toLowerCase() | ||
.isIn(["online", "offline"]) | ||
.withMessage( | ||
"the online_status value is not among the expected ones which include: online, offline" | ||
), | ||
query("last_active_before") | ||
.optional() | ||
.notEmpty() | ||
.withMessage("last_active_before date cannot be empty IF provided") | ||
.bail() | ||
.trim() | ||
.isISO8601({ strict: true, strictSeparator: true }) | ||
.withMessage( | ||
"last_active_before date must be a valid ISO8601 datetime (YYYY-MM-DDTHH:mm:ss.sssZ)." | ||
) | ||
.bail() | ||
.toDate(), | ||
query("last_active_after") | ||
.optional() | ||
.notEmpty() | ||
.withMessage("last_active_after date cannot be empty IF provided") | ||
.bail() | ||
.trim() | ||
.isISO8601({ strict: true, strictSeparator: true }) | ||
.withMessage( | ||
"last_active_after date must be a valid ISO8601 datetime (YYYY-MM-DDTHH:mm:ss.sssZ)." | ||
) | ||
.bail() | ||
.toDate(), | ||
query("last_active") | ||
.optional() | ||
.notEmpty() | ||
.withMessage("last_active date cannot be empty IF provided") | ||
.bail() | ||
.trim() | ||
.isISO8601({ strict: true, strictSeparator: true }) | ||
.withMessage( | ||
"last_active date must be a valid ISO8601 datetime (YYYY-MM-DDTHH:mm:ss.sssZ)." | ||
) | ||
.bail() | ||
.toDate(), | ||
], | ||
]), | ||
siteController.listSummary | ||
); | ||
router.get("/weather", siteController.listWeatherStations); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -903,6 +903,10 @@ | |
visibility, | ||
deviceName, | ||
status, | ||
online_status, | ||
last_active, | ||
last_active_before, | ||
last_active_after, | ||
} = { ...req.query, ...req.params }; | ||
|
||
const filter = {}; | ||
|
@@ -925,6 +929,32 @@ | |
filter.device_number = parseInt(channel); | ||
} | ||
|
||
if (last_active) { | ||
filter.lastActive = {}; | ||
const start = new Date(last_active); | ||
filter["lastActive"]["$gte"] = start; | ||
} | ||
|
||
if (last_active_after) { | ||
filter.lastActive = {}; | ||
const start = new Date(last_active_after); | ||
filter["lastActive"]["$gte"] = start; | ||
} | ||
Comment on lines
+932
to
+942
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactor suggestion for The implementation of if (last_active || last_active_after) {
filter.lastActive = {};
const start = new Date(last_active || last_active_after);
filter["lastActive"]["$gte"] = start;
} This refactoring reduces code duplication and makes the intent clearer. Additionally, it's important to note that this new code is not covered by tests. To ensure the reliability of these new filters, please add unit tests that cover various scenarios for these parameters. Would you like assistance in generating unit tests for these new filters? ToolsGitHub Check: codecov/patch
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please create a new Github issue for this suggestion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Baalmart, I've created a GitHub issue for this suggestion as requested: #3492. Analysis chain@Baalmart, I'll create a GitHub issue for this suggestion. Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Open a new GitHub issue for the suggestion
gh issue create --title "Refactor filters for 'last_active' and 'last_active_after' in generateFilter utility" --body "The implementation of \`last_active\` and \`last_active_after\` in the \`generateFilter\` utility sets the same filter condition. To improve clarity and maintainability, consider combining these conditions to reduce code duplication and enhance readability.
**Pull Request**: https://github.com/airqo-platform/AirQo-api/pull/3489
**Comment**: https://github.com/airqo-platform/AirQo-api/pull/3489#discussion_r1771708574
**Requested by**: @Baalmart" --assignee Baalmart
Length of output: 654 |
||
|
||
if (last_active_before) { | ||
filter.lastActive = {}; | ||
const start = new Date(last_active_before); | ||
filter["lastActive"]["$lte"] = start; | ||
} | ||
Comment on lines
+944
to
+948
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct implementation of The implementation of the However, as with the previous filters, this new code is not covered by tests. To ensure the reliability of this filter, please add unit tests that cover various scenarios for the Consider adding tests that check:
If you need assistance in writing these tests, please let me know, and I'd be happy to help. ToolsGitHub Check: codecov/patch
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please create a new Github issue for this suggestion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Baalmart, I've successfully updated the GitHub issue with the correct title, including the (_/) Analysis chain@Baalmart, I'll create a GitHub issue for this suggestion. @Baalmart, it appears there was an error in the script due to unescaped backticks, which caused the issue title to be incorrect. I've corrected the script by escaping the backticks to prevent shell interpretation: This script will update the existing issue with the correct title. You can check the updated issue here: #3491. Let me know if there's anything else I can assist with. Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Create a new GitHub issue for adding unit tests for the `last_active_before` filter
gh issue create --title "Add unit tests for `last_active_before` filter in generate-filter.js" \
--body "In PR #3489 (https://github.com/airqo-platform/AirQo-api/pull/3489), new code was added implementing the \`last_active_before\` filter in \`src/device-registry/utils/generate-filter.js\`. This new code is not currently covered by unit tests. To ensure its reliability, unit tests should be added covering various scenarios for the \`last_active_before\` parameter.
Original comment: https://github.com/airqo-platform/AirQo-api/pull/3489#discussion_r1771708575
Requester: @Baalmart" \
--assignee Baalmart
Length of output: 746 Script: #!/bin/bash
# Update the title of the existing issue to include `last_active_before`
gh issue edit 3491 --title "Add unit tests for \\\`last_active_before\\\` filter in generate-filter.js"
Length of output: 157 If you found this review helpful, would you consider giving us a shout-out on X? |
||
|
||
if (online_status) { | ||
if (online_status.toLowerCase() === "online") { | ||
filter["isOnline"] = true; | ||
} else if (online_status.toLowerCase() === "offline") { | ||
filter["isOnline"] = false; | ||
} | ||
} | ||
Comment on lines
+950
to
+956
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Enhance robustness of The implementation of the
if (online_status) {
const status = online_status.toLowerCase();
if (status === "online") {
filter["isOnline"] = true;
} else if (status === "offline") {
filter["isOnline"] = false;
} else {
// Handle unexpected value
console.warn(`Unexpected online_status value: ${online_status}`);
// Optionally, you could throw an error or set a default value
}
}
These enhancements will improve the reliability and maintainability of the Would you like assistance in implementing these improvements or writing the corresponding unit tests? ToolsGitHub Check: codecov/patch
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please create a new Github issue for this suggestion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Baalmart, I've created a new GitHub issue for this suggestion: #3493. Analysis chainScripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Create a new GitHub issue for the suggested improvements
gh issue create \
--title "Enhance robustness of 'online_status' filter" \
--body "There is a suggestion to enhance the robustness of the \`online_status\` filter by handling unexpected values and adding test coverage.
[PR URL](https://github.com/airqo-platform/AirQo-api/pull/3489)
[Comment URL](https://github.com/airqo-platform/AirQo-api/pull/3489#discussion_r1771708577)
Requested by @Baalmart" \
--assignee Baalmart
Length of output: 478 |
||
|
||
if (category) { | ||
filter.category = category; | ||
} | ||
|
@@ -1035,6 +1065,10 @@ | |
network, | ||
group, | ||
google_place_id, | ||
online_status, | ||
last_active, | ||
last_active_before, | ||
last_active_after, | ||
} = { ...req.query, ...req.params, ...req.body }; | ||
const filter = {}; | ||
logText("we are generating the filter man!"); | ||
|
@@ -1078,6 +1112,32 @@ | |
filter["visibility"] = true; | ||
} | ||
|
||
if (last_active) { | ||
filter.lastActive = {}; | ||
const start = new Date(last_active); | ||
filter["lastActive"]["$gte"] = start; | ||
Comment on lines
+1116
to
+1118
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add Test Coverage for New The new code handling the Also applies to: 1122-1124, 1128-1130 ToolsGitHub Check: codecov/patch
|
||
} | ||
|
||
if (last_active_after) { | ||
filter.lastActive = {}; | ||
const start = new Date(last_active_after); | ||
filter["lastActive"]["$gte"] = start; | ||
} | ||
|
||
if (last_active_before) { | ||
filter.lastActive = {}; | ||
const start = new Date(last_active_before); | ||
filter["lastActive"]["$lte"] = start; | ||
} | ||
|
||
if (online_status) { | ||
if (online_status.toLowerCase() === "online") { | ||
filter["isOnline"] = true; | ||
} else if (online_status.toLowerCase() === "offline") { | ||
filter["isOnline"] = false; | ||
} | ||
} | ||
|
||
Comment on lines
+1133
to
+1140
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle Unexpected Values for In the Suggested addition: if (online_status) {
if (online_status.toLowerCase() === "online") {
filter["isOnline"] = true;
} else if (online_status.toLowerCase() === "offline") {
filter["isOnline"] = false;
+ } else {
+ // Handle unexpected value
+ next(new HttpError("Invalid online_status value", httpStatus.BAD_REQUEST));
}
}
ToolsGitHub Check: codecov/patch
|
||
if (site_codes) { | ||
const siteCodesArray = site_codes.toString().split(","); | ||
filter["site_codes"] = { $in: siteCodesArray }; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add Unit Tests for Custom Sanitizers Converting to ObjectId
The custom sanitizers for
id
andsite_id
convert input values toObjectId
. Currently, these lines are not covered by tests. To ensure the sanitization logic works as intended and to prevent potential issues, please consider adding unit tests for these sanitizers.Also applies to: 135-137, 206-208, 216-218
Tools
GitHub Check: codecov/patch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please create a new Github issue for this suggestion
There was a problem hiding this comment.
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 for this suggestion. You can find it here: #3490
Analysis chain
@Baalmart, sure, I'll open a new GitHub issue for this suggestion.
((\
( ^.^)
o_(")(")
Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 622