-
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
Optimize device counting with database aggregation #3540
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## staging #3540 +/- ##
========================================
Coverage 29.88% 29.89%
========================================
Files 184 184
Lines 24699 24692 -7
Branches 3258 3257 -1
========================================
Hits 7382 7382
+ Misses 17195 17188 -7
Partials 122 122
Flags with carried forward coverage won't be shown. Click here to find out more.
|
📝 WalkthroughWalkthroughThe pull request introduces significant updates to the device network status monitoring functionality. It includes a new job script for checking network status, enhancements to logging in an existing function, and a suite of unit tests to ensure the accuracy and reliability of the new job. The changes aim to improve the monitoring process by providing detailed logging and error handling, while also replacing an older job script with a more efficient implementation. Changes
Possibly related issues
Possibly related PRs
Suggested reviewers
Poem
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Device registry changes in this PR available for preview here |
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.
Actionable comments posted: 2
🧹 Outside diff range and nitpick comments (8)
src/device-registry/bin/jobs/check-network-status-job.js (1)
PR does not implement the intended aggregation optimization
The current implementation of
check-network-status-job.js
uses a batching approach withlimit
andskip
to count devices. However, it does not utilize MongoDB's aggregation pipeline as outlined in the PR objectives. Implementing aggregation would optimize performance by handling counting directly within the database.Please consider updating the function to use aggregation pipelines for device counting to align with the PR's goals.
🔗 Analysis chain
Line range hint
1-63
: Discrepancy between PR objectives and implemented changesI noticed that the PR objectives mention optimizing device counting with database aggregation, but the changes in this file don't reflect this optimization. The current implementation uses a batching approach, which is efficient, but it doesn't utilize database aggregation as described in the PR summary.
Could you please clarify if there are additional changes planned or if the PR description needs to be updated to accurately reflect the implemented changes?
To verify if any database aggregation has been implemented elsewhere, we can search for aggregation pipeline usage:
If database aggregation was indeed the intended optimization, consider using MongoDB's aggregation pipeline to count devices and calculate percentages directly in the database. This could potentially improve performance by reducing the amount of data transferred and processed in the application.
It seems there was a syntax error in the previous shell script, preventing it from executing correctly. Let me provide a corrected script to search for MongoDB aggregation pipeline usage in the codebase:
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Search for MongoDB aggregation pipeline usage in the codebase # Test: Look for aggregation pipeline usage rg --type js '\$group|\$sum|\$count|\$project' -g '!node_modules'Length of output: 141
Script:
#!/bin/bash # Description: Search for MongoDB aggregation pipeline usage in the codebase # Test: Look for aggregation pipeline usage rg --type js '\$group|\$sum|\$count|\$project' -g '!node_modules'Length of output: 11936
src/device-registry/bin/jobs/new-check-network-status-job.js (2)
1-10
: Consider removing unused constant BATCH_SIZEThe constant
BATCH_SIZE
is defined on line 10 but doesn't appear to be used in the current implementation. If it's not needed for future functionality, consider removing it to keep the code clean and avoid confusion.If you confirm it's not needed, you can remove it with this change:
-const BATCH_SIZE = 1000; // Define the size of each batch
If it's intended for future use, please add a comment explaining its purpose or create a TODO item for its implementation.
37-60
: Clean up commented code and consider log level consistencyThe logging logic based on the offline percentage is well-implemented and aligns with the PR objective of monitoring network status. However, there's a commented-out
logger.info
call that should be addressed.Please consider one of the following actions:
- If the commented code is no longer needed, remove it entirely:
- // logger.info( - // `✅ Network status is acceptable: ${offlinePercentage.toFixed( - // 2 - // )}% offline` - // );
- If it's intended for future use or debugging, add a comment explaining its purpose:
+ // TODO: Uncomment this block if additional logging is required for acceptable status // logger.info( // `✅ Network status is acceptable: ${offlinePercentage.toFixed( // 2 // )}% offline` // );
Also, consider using
logger.info
instead oflogText
for consistency when logging the acceptable status, similar to how you're usinglogger.warn
for the warning case.🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 38-38: src/device-registry/bin/jobs/new-check-network-status-job.js#L38
Added line #L38 was not covered by tests
[warning] 44-44: src/device-registry/bin/jobs/new-check-network-status-job.js#L44
Added line #L44 was not covered by tests
[warning] 50-50: src/device-registry/bin/jobs/new-check-network-status-job.js#L50
Added line #L50 was not covered by testssrc/device-registry/bin/jobs/test/ut_new-check-network-status-job.js (5)
1-28
: Imports and test setup look good, with a minor suggestion.The imports and test setup are well-structured and appropriate for the testing requirements. The use of stubs for logging and the DeviceModel's aggregate method ensures proper isolation of the tests.
Consider using object destructuring for the Sinon import to make the code more concise:
-const sinon = require("sinon"); +const { stub, restore } = require("sinon");This would allow you to use
stub
andrestore
directly in your code, potentially improving readability.
37-47
: Second test case is well-implemented, with a suggestion for clarity.This test case effectively verifies the behavior when the network status is acceptable. It correctly sets up the scenario and checks the logged message.
To improve clarity, consider adding a comment explaining the calculation of the offline percentage:
// 2 out of 10 devices are offline, which is 20% expect( loggerStub.calledWith("✅ Network status is acceptable: 20.00% offline") ).to.be.true;This would make it easier for other developers to understand the expected behavior at a glance.
49-61
: Third test case is well-implemented, with a suggestion for consistency.This test case effectively verifies the behavior when a high percentage of devices are offline. It correctly sets up the scenario and checks the logged warning message.
For consistency with the previous test case, consider adding a comment explaining the calculation of the offline percentage:
// 4 out of 5 devices are offline, which is 80% expect( loggerStub.calledWith( "⚠️💔😥 More than 60% of devices are offline: 80.00%" ) ).to.be.true;This would maintain consistency in documentation across test cases.
63-72
: Fourth test case is well-implemented, with a suggestion for robustness.This test case effectively verifies the error handling behavior of the checkNetworkStatus function. It correctly simulates a database error and checks that the error message is properly logged.
To make the test more robust, consider adding an assertion to ensure that the error is actually thrown:
it("should handle errors gracefully", async () => { const errorMessage = "Database error"; aggregateStub.throws(new Error(errorMessage)); await expect(checkNetworkStatus()).to.be.rejectedWith(Error); expect( loggerStub.calledWith("Error checking network status: Database error") ).to.be.true; });This additional assertion would ensure that the function not only logs the error but also propagates it, which might be important for error handling at higher levels of the application.
1-73
: Excellent test coverage with a suggestion for an additional edge case.The test suite for the checkNetworkStatus function is comprehensive and well-structured. It covers the main scenarios effectively, including no devices, acceptable status, warning status, and error handling. The use of stubs ensures good isolation of tests, and the assertions are clear and appropriate.
To further enhance the test suite, consider adding an edge case test:
it("should handle edge case with zero total devices", async () => { aggregateStub.returns( Promise.resolve([{ totalDevices: 0, offlineDevicesCount: 0 }]) ); await checkNetworkStatus(); expect( loggerStub.calledWith("No devices found or all devices are offline.") ).to.be.true; });This test would ensure that the function handles the edge case where the total number of devices is zero, which could potentially cause division by zero errors if not handled properly in the main function.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (4)
- src/device-registry/bin/jobs/check-network-status-job.js (1 hunks)
- src/device-registry/bin/jobs/new-check-network-status-job.js (1 hunks)
- src/device-registry/bin/jobs/test/ut_new-check-network-status-job.js (1 hunks)
- src/device-registry/bin/server.js (1 hunks)
🧰 Additional context used
🪛 GitHub Check: codecov/patch
src/device-registry/bin/jobs/new-check-network-status-job.js
[warning] 13-14: src/device-registry/bin/jobs/new-check-network-status-job.js#L13-L14
Added lines #L13 - L14 were not covered by tests
[warning] 29-31: src/device-registry/bin/jobs/new-check-network-status-job.js#L29-L31
Added lines #L29 - L31 were not covered by tests
[warning] 34-35: src/device-registry/bin/jobs/new-check-network-status-job.js#L34-L35
Added lines #L34 - L35 were not covered by tests
[warning] 38-38: src/device-registry/bin/jobs/new-check-network-status-job.js#L38
Added line #L38 was not covered by tests
[warning] 44-44: src/device-registry/bin/jobs/new-check-network-status-job.js#L44
Added line #L44 was not covered by tests
[warning] 50-50: src/device-registry/bin/jobs/new-check-network-status-job.js#L50
Added line #L50 was not covered by tests
[warning] 62-64: src/device-registry/bin/jobs/new-check-network-status-job.js#L62-L64
Added lines #L62 - L64 were not covered by tests
🔇 Additional comments (6)
src/device-registry/bin/jobs/check-network-status-job.js (2)
59-63
: Consider logging consistency and verbosityThe change from
logger.info
tologText
for logging the acceptable network status is noteworthy. While this change might be intentional, it's important to consider the following points:
Consistency: Does this align with the project's logging standards? Are we consistently using
logText
for similar information across the codebase?Log levels:
logger.info
provides a specific log level, which might be useful for log filtering and management. By switching tologText
, we might lose this granularity. Is this intentional?Verbosity: By uncommenting this log, we're increasing the verbosity of the application. Is this desired? Will it provide valuable information in production, or might it lead to log noise?
To check for consistency in logging practices, we can run the following command:
#!/bin/bash # Description: Check for usage of logText and logger.info # Test: Compare usage of logText and logger.info echo "logText usage:" rg --type js 'logText\(' -g '!node_modules' | wc -l echo "logger.info usage:" rg --type js 'logger\.info\(' -g '!node_modules' | wc -lThis will help us understand if the change aligns with the overall logging practices in the project.
Consider adding a comment explaining why this logging method was chosen, especially if it differs from other parts of the codebase.
Line range hint
1-63
: Overall assessment and suggestions for improvementThe
checkNetworkStatus
function is well-implemented with an efficient batching approach. However, there are a few points to consider for further improvement:
Database Aggregation: As mentioned in the PR objectives, consider implementing database aggregation to optimize the device counting process. This could potentially improve performance by offloading more work to the database.
Error Handling: The current error handling is good, but consider adding more specific error types and handling for different scenarios (e.g., database connection issues, timeout errors).
Configuration: The
BATCH_SIZE
and offline percentage threshold (60%) could be moved to a configuration file for easier management and potential dynamic adjustment.Testing: Ensure that unit tests are updated or added to cover the changes in logging behavior and any future optimizations.
To further improve the scalability of this job, consider implementing a distributed locking mechanism to ensure that only one instance of this job runs at a time, especially if you have multiple application servers.
To check if there are any existing tests for this job, we can run:
#!/bin/bash # Description: Search for test files related to check-network-status-job # Test: Look for test files fd -e test.js -e spec.js check-network-statusThis will help us understand if additional tests need to be created or updated.
src/device-registry/bin/jobs/new-check-network-status-job.js (2)
12-26
: Excellent use of database aggregation for efficient device countingThe implementation of
checkNetworkStatus
function, particularly the database query, is well-optimized and aligns perfectly with the PR objective of enhancing the efficiency of device counting. The use of MongoDB's aggregation pipeline to count both total and offline devices in a single query is a great approach.This implementation offers several benefits:
- Reduced database load by performing counts in a single operation.
- Improved scalability as the device count grows.
- Minimized network overhead between the application and the database.
Great job on this optimization!
🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 13-14: src/device-registry/bin/jobs/new-check-network-status-job.js#L13-L14
Added lines #L13 - L14 were not covered by tests
28-35
: Well-structured result handling with a minor suggestionThe handling of query results is well-implemented. I appreciate the check for empty results and the clear calculation of the offline percentage. This aligns well with the PR objective of improving device data processing.
To slightly improve readability, consider using object destructuring directly in the function parameters. Here's a suggestion:
- if (result.length === 0 || result[0].totalDevices === 0) { + if (result.length === 0) { logText("No devices found"); logger.info("No devices found."); return; } - const { totalDevices, offlineDevicesCount } = result[0]; + const [{ totalDevices = 0, offlineDevicesCount = 0 } = {}] = result; + if (totalDevices === 0) { + logText("No devices found"); + logger.info("No devices found."); + return; + }This change combines the null check with the destructuring and provides default values, making the code more concise and robust.
🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 29-31: src/device-registry/bin/jobs/new-check-network-status-job.js#L29-L31
Added lines #L29 - L31 were not covered by tests
[warning] 34-35: src/device-registry/bin/jobs/new-check-network-status-job.js#L34-L35
Added lines #L34 - L35 were not covered by testssrc/device-registry/bin/jobs/test/ut_new-check-network-status-job.js (1)
30-35
: First test case is well-implemented.This test case effectively verifies the behavior when no devices are found. It checks both the logger output and the call to the logText utility, ensuring comprehensive coverage of the expected behavior.
src/device-registry/bin/server.js (1)
Line range hint
1-224
: Overall assessment of server.js changesThe modification to import the new network status check job is the only change in this file. It's a straightforward update that aligns well with the PR's objective of optimizing device counting. The rest of the server configuration and error handling remain intact, which is good for maintaining overall system stability.
As this change is part of a larger optimization effort, it would be beneficial to:
- Ensure that any performance metrics or logging related to the network status check job are updated if necessary.
- Consider adding a comment near the import statement to briefly explain the reason for the new job file (e.g., "Updated for optimized device counting").
Great job on making a focused change to improve system performance!
} catch (error) { | ||
logText(`Error checking network status: ${error.message}`); | ||
logger.error(`Error checking network status: ${error.message}`); | ||
logger.error(`Stack trace: ${error.stack}`); | ||
} | ||
}; | ||
|
||
logText("Network status job is now running....."); | ||
const schedule = "0 */2 * * *"; | ||
cron.schedule(schedule, checkNetworkStatus, { | ||
scheduled: true, | ||
timezone: constants.TIMEZONE, | ||
}); |
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.
💡 Codebase verification
Add Unit Tests for new-check-network-status-job.js
The checkNetworkStatus
function and job scheduling are well-implemented. However, no corresponding test files were found in the repository.
- Suggested Test File Location:
src/device-registry/test/jobs/new-check-network-status-job.test.js
Adding unit tests will enhance the reliability and maintainability of this job by ensuring all scenarios are adequately covered.
🔗 Analysis chain
Robust error handling and scheduling with a note on test coverage
The error handling in the checkNetworkStatus
function is comprehensive, logging both the error message and stack trace. The job scheduling using node-cron is well-implemented, running the check every 2 hours as intended.
The implementation aligns well with the PR objective of introducing a network status check job. However, the static analysis tool indicates that several parts of the code are not covered by tests.
To address this, I recommend adding unit tests for the checkNetworkStatus
function. Here's a shell script to verify the current test coverage:
This script will help identify if a test file exists and suggest creating one if it doesn't. Adding tests will improve the reliability and maintainability of this important job.
Further Verification Needed: Searching for Test Files in Alternative Locations
The initial search for the test file new-check-network-status-job.test.js
in src/device-registry/test
did not yield any results, and the path src/device-registry/test
is not a directory. To ensure thorough verification, we need to search the entire repository for any related test files.
This script will search the entire repository for any test files related to new-check-network-status-job.js
. Please execute it and provide the output to determine if tests exist in alternative locations or need to be created.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Check test coverage for new-check-network-status-job.js
# Test: Search for test file
test_file=$(fd --type file "new-check-network-status-job.test.js" src/device-registry/test)
if [ -z "$test_file" ]; then
echo "No test file found for new-check-network-status-job.js"
else
echo "Test file found: $test_file"
# If a test runner like Jest is available, you could run the tests here
# jest $test_file --coverage
fi
# Suggestion: If no test file is found, create one
if [ -z "$test_file" ]; then
echo "Suggested test file location: src/device-registry/test/jobs/new-check-network-status-job.test.js"
fi
Length of output: 558
Script:
#!/bin/bash
# Description: Search for test files related to new-check-network-status-job.js across the repository
# Search for any files ending with .test.js that include 'new-check-network-status-job' in their name
test_files=$(fd --type file --glob "*new-check-network-status-job*.test.js")
if [ -z "$test_files" ]; then
echo "No test files found for new-check-network-status-job.js in the repository."
echo "Suggested test file location: src/device-registry/test/jobs/new-check-network-status-job.test.js"
else
echo "Test file(s) found:"
echo "$test_files"
# If a test runner like Jest is available, you could run the tests here
# jest $(echo $test_files) --coverage
fi
Length of output: 457
🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 62-64: src/device-registry/bin/jobs/new-check-network-status-job.js#L62-L64
Added lines #L62 - L64 were not covered by tests
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.
this test already exists under src/device-registry/bin/jobs/test/ut_new-check-network-status-job
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, thank you for the clarification. I see that the test for new-check-network-status-job.js
already exists under src/device-registry/bin/jobs/test/ut_new-check-network-status-job
. I'll make sure to consider this in future reviews.
(。•́︿•̀。)
✏️ Learnings added
Learnt from: Baalmart
PR: airqo-platform/AirQo-api#3540
File: src/device-registry/bin/jobs/new-check-network-status-job.js:61-73
Timestamp: 2024-09-28T10:12:09.142Z
Learning: Tests for `new-check-network-status-job.js` are located at `src/device-registry/bin/jobs/test/ut_new-check-network-status-job`.
Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.
@@ -25,7 +25,7 @@ const { logText, logObject } = require("@utils/log"); | |||
const jsonify = require("@utils/jsonify"); | |||
require("@bin/jobs/store-signals-job"); | |||
require("@bin/jobs/new-store-readings-job"); | |||
require("@bin/jobs/check-network-status-job"); | |||
require("@bin/jobs/new-check-network-status-job"); |
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.
💡 Codebase verification
Remaining references to check-network-status-job
detected
The search results show that there are still references to the old job file check-network-status-job
in several files, including:
src/device-registry/bin/jobs/check-network-status-job.js
src/device-registry/bin/jobs/test/ut_check-network-status-job.js
Please update these references to use new-check-network-status-job
to ensure consistency across the codebase.
🔗 Analysis chain
Verify the new network status check job implementation
I see that you've updated the import statement to use a new implementation of the network status check job. This change aligns with the PR objective of optimizing device counting with database aggregation. Well done on taking steps to improve performance!
To ensure a smooth transition, I recommend the following:
- Verify that the new job (
new-check-network-status-job
) implements the optimized database aggregation for device counting as intended. - Update any related documentation or comments that might reference the old job file.
- Double-check the codebase for any other occurrences of the old job file name to ensure consistency.
Let's run a quick check to see if there are any other references to the old job file in the codebase:
This will help us ensure that we've updated all necessary references.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Search for any remaining references to the old job file
rg --type js "check-network-status-job"
Length of output: 666
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.
this is okay since the old file still exists, it was not deleted. So no worries
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, thank you for the clarification. I understand that the old file check-network-status-job.js
still exists and it's acceptable to have references to it. I'll note this for future reviews.
(_/)
(•_•)?
/
✏️ Learnings added
Learnt from: Baalmart
PR: airqo-platform/AirQo-api#3540
File: src/device-registry/bin/server.js:28-28
Timestamp: 2024-09-28T10:12:51.023Z
Learning: The old file `check-network-status-job.js` still exists and it's acceptable to have references to it in the codebase.
Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.
🧠 Learnings used
Learnt from: Baalmart
PR: airqo-platform/AirQo-api#3540
File: src/device-registry/bin/jobs/new-check-network-status-job.js:61-73
Timestamp: 2024-09-28T10:12:09.300Z
Learning: Tests for `new-check-network-status-job.js` are located at `src/device-registry/bin/jobs/test/ut_new-check-network-status-job`.
Description
Optimize device counting with database aggregation
Related Issues
Changes Made
Testing
Affected Services
API Documentation Updated?
Additional Notes
optimize device counting with database aggregation in check-network-status-job.js.
Summary by CodeRabbit
New Features
Bug Fixes
Tests