-
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
more optimisations/refactoring for the readings job #3608
Conversation
📝 WalkthroughWalkthroughThe changes in the pull request focus on the Changes
Possibly related PRs
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 (
|
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## staging #3608 +/- ##
========================================
Coverage 27.04% 27.05%
========================================
Files 146 146
Lines 21346 21345 -1
Branches 273 273
========================================
Hits 5774 5774
+ Misses 15572 15571 -1
|
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: 3
🧹 Outside diff range and nitpick comments (2)
src/device-registry/bin/jobs/v3-store-readings-job.js (2)
46-49
: Consider adding unit tests forupdateEntityStatus
To ensure the reliability of the
updateEntityStatus
function, it's beneficial to add unit tests covering various scenarios, such as when the entity is found or not found, and verifying the correct calculation ofisOnline
.Would you like assistance in creating unit tests for this function?
🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 46-46: src/device-registry/bin/jobs/v3-store-readings-job.js#L46
Added line #L46 was not covered by tests
159-159
: Consider adding tests for error handling infetchAllData
To improve code reliability, consider adding unit tests that simulate errors during data fetching to ensure the error handling logic works as expected.
Can I assist in creating tests for these error scenarios?
🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 159-159: src/device-registry/bin/jobs/v3-store-readings-job.js#L159
Added line #L159 was not covered by tests
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (1)
- src/device-registry/bin/jobs/v3-store-readings-job.js (3 hunks)
🧰 Additional context used
🪛 GitHub Check: codecov/patch
src/device-registry/bin/jobs/v3-store-readings-job.js
[warning] 33-33: src/device-registry/bin/jobs/v3-store-readings-job.js#L33
Added line #L33 was not covered by tests
[warning] 36-36: src/device-registry/bin/jobs/v3-store-readings-job.js#L36
Added line #L36 was not covered by tests
[warning] 46-46: src/device-registry/bin/jobs/v3-store-readings-job.js#L46
Added line #L46 was not covered by tests
[warning] 51-51: src/device-registry/bin/jobs/v3-store-readings-job.js#L51
Added line #L51 was not covered by tests
[warning] 100-100: src/device-registry/bin/jobs/v3-store-readings-job.js#L100
Added line #L100 was not covered by tests
[warning] 103-104: src/device-registry/bin/jobs/v3-store-readings-job.js#L103-L104
Added lines #L103 - L104 were not covered by tests
[warning] 106-106: src/device-registry/bin/jobs/v3-store-readings-job.js#L106
Added line #L106 was not covered by tests
[warning] 121-121: src/device-registry/bin/jobs/v3-store-readings-job.js#L121
Added line #L121 was not covered by tests
[warning] 123-124: src/device-registry/bin/jobs/v3-store-readings-job.js#L123-L124
Added lines #L123 - L124 were not covered by tests
[warning] 127-128: src/device-registry/bin/jobs/v3-store-readings-job.js#L127-L128
Added lines #L127 - L128 were not covered by tests
[warning] 134-134: src/device-registry/bin/jobs/v3-store-readings-job.js#L134
Added line #L134 was not covered by tests
[warning] 140-140: src/device-registry/bin/jobs/v3-store-readings-job.js#L140
Added line #L140 was not covered by tests
[warning] 142-142: src/device-registry/bin/jobs/v3-store-readings-job.js#L142
Added line #L142 was not covered by tests
[warning] 145-145: src/device-registry/bin/jobs/v3-store-readings-job.js#L145
Added line #L145 was not covered by tests
[warning] 150-151: src/device-registry/bin/jobs/v3-store-readings-job.js#L150-L151
Added lines #L150 - L151 were not covered by tests
[warning] 153-153: src/device-registry/bin/jobs/v3-store-readings-job.js#L153
Added line #L153 was not covered by tests
[warning] 157-157: src/device-registry/bin/jobs/v3-store-readings-job.js#L157
Added line #L157 was not covered by tests
[warning] 159-159: src/device-registry/bin/jobs/v3-store-readings-job.js#L159
Added line #L159 was not covered by tests
🔇 Additional comments (5)
src/device-registry/bin/jobs/v3-store-readings-job.js (5)
33-44
: Implementation ofupdateEntityStatus
function looks goodThe updated
updateEntityStatus
function efficiently calculates theisOnline
status based on the time difference betweencurrentTime
and the providedtime
. The use offindOneAndUpdate
streamlines the update process and improves code readability.🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 33-33: src/device-registry/bin/jobs/v3-store-readings-job.js#L33
Added line #L33 was not covered by tests
[warning] 36-36: src/device-registry/bin/jobs/v3-store-readings-job.js#L36
Added line #L36 was not covered by tests
100-102
: Efficient upsert operation for readingsUsing
updateOne
with{ upsert: true }
in updating readings ensures that documents are either updated or created as needed, which is an efficient approach for maintaining the readings collection.🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 100-100: src/device-registry/bin/jobs/v3-store-readings-job.js#L100
Added line #L100 was not covered by tests
103-107
: Enhance error handling inprocessDocument
While the error is logged when
Promise.all(updatePromises)
fails, consider implementing additional error handling or retries for transient errors to make the processing more robust.🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 103-104: src/device-registry/bin/jobs/v3-store-readings-job.js#L103-L104
Added lines #L103 - L104 were not covered by tests
[warning] 106-106: src/device-registry/bin/jobs/v3-store-readings-job.js#L106
Added line #L106 was not covered by tests
121-123
: Improved control flow withhasMoreData
flagReplacing the infinite
while (true)
loop withwhile (hasMoreData)
enhances the readability and control flow of thefetchAllData
function, making the termination condition explicit.🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 121-121: src/device-registry/bin/jobs/v3-store-readings-job.js#L121
Added line #L121 was not covered by tests
145-155
: Efficient data accumulation infetchAllData
The logic for accumulating data from the database queries is well-implemented, ensuring that all retrieved entities are captured for further processing.
🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 145-145: src/device-registry/bin/jobs/v3-store-readings-job.js#L145
Added line #L145 was not covered by tests
[warning] 150-151: src/device-registry/bin/jobs/v3-store-readings-job.js#L150-L151
Added lines #L150 - L151 were not covered by tests
[warning] 153-153: src/device-registry/bin/jobs/v3-store-readings-job.js#L153
Added line #L153 was not covered by tests
} catch (error) { | ||
logger.error(`Error fetching data: ${error.message}`); |
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.
Prevent potential infinite loop on data fetch errors
If an error occurs during data fetching, hasMoreData
remains true
, which could lead to an infinite loop. To prevent this, consider setting hasMoreData = false
in the catch
block to ensure the loop terminates appropriately.
Apply this diff to address the issue:
} catch (error) {
logger.error(`Error fetching data: ${error.message}`);
+ hasMoreData = false;
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
} catch (error) { | |
logger.error(`Error fetching data: ${error.message}`); | |
} catch (error) { | |
logger.error(`Error fetching data: ${error.message}`); | |
hasMoreData = false; | |
} |
🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 159-159: src/device-registry/bin/jobs/v3-store-readings-job.js#L159
Added line #L159 was not covered by tests
try { | ||
await Promise.all(updatePromises); | ||
} catch (error) { | ||
logger.error(`🐛🐛 Error processing document: ${error.message}`); | ||
} |
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.
🛠️ Refactor suggestion
Avoid swallowing errors silently
In the processDocument
function, errors caught within the inner try-catch
block are logged but not rethrown or handled further. This might cause the batch processing to continue despite critical failures. Consider handling the errors appropriately, possibly by rethrowing them to be caught in the outer scope.
Apply this diff to rethrow the error:
try {
await Promise.all(updatePromises);
} catch (error) {
logger.error(`🐛🐛 Error processing document: ${error.message}`);
+ throw error;
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
try { | |
await Promise.all(updatePromises); | |
} catch (error) { | |
logger.error(`🐛🐛 Error processing document: ${error.message}`); | |
} | |
try { | |
await Promise.all(updatePromises); | |
} catch (error) { | |
logger.error(`🐛🐛 Error processing document: ${error.message}`); | |
throw error; | |
} |
🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 103-104: src/device-registry/bin/jobs/v3-store-readings-job.js#L103-L104
Added lines #L103 - L104 were not covered by tests
[warning] 106-106: src/device-registry/bin/jobs/v3-store-readings-job.js#L106
Added line #L106 was not covered by tests
...filter, | ||
limit: pageSize, | ||
skip: page * pageSize, | ||
}); | ||
|
||
// Check if the response is successful and contains data | ||
if ( | ||
!response.success || | ||
!response.data || | ||
response.data.length === 0 || | ||
response.data[0].data.length === 0 | ||
) { | ||
break; // Exit loop if no more data | ||
} | ||
if ( | ||
!response.success || | ||
!response.data || | ||
response.data.length === 0 || | ||
response.data[0].data.length === 0 | ||
) { | ||
hasMoreData = false; | ||
} else { | ||
allData.push(...response.data[0].data); | ||
} |
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.
Validate response structure when fetching events
In the EventModel
data fetching logic, verify that response.data[0].data
exists before accessing it to prevent potential runtime errors. Adding defensive checks will enhance the robustness of the code.
Apply this diff to add a validation check:
if (
!response.success ||
!response.data ||
response.data.length === 0 ||
- response.data[0].data.length === 0
+ !response.data[0].data ||
+ response.data[0].data.length === 0
) {
hasMoreData = false;
} else {
allData.push(...response.data[0].data);
}
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
...filter, | |
limit: pageSize, | |
skip: page * pageSize, | |
}); | |
// Check if the response is successful and contains data | |
if ( | |
!response.success || | |
!response.data || | |
response.data.length === 0 || | |
response.data[0].data.length === 0 | |
) { | |
break; // Exit loop if no more data | |
} | |
if ( | |
!response.success || | |
!response.data || | |
response.data.length === 0 || | |
response.data[0].data.length === 0 | |
) { | |
hasMoreData = false; | |
} else { | |
allData.push(...response.data[0].data); | |
} | |
...filter, | |
limit: pageSize, | |
skip: page * pageSize, | |
}); | |
if ( | |
!response.success || | |
!response.data || | |
response.data.length === 0 || | |
!response.data[0].data || | |
response.data[0].data.length === 0 | |
) { | |
hasMoreData = false; | |
} else { | |
allData.push(...response.data[0].data); | |
} |
🧰 Tools
🪛 GitHub Check: codecov/patch
[warning] 134-134: src/device-registry/bin/jobs/v3-store-readings-job.js#L134
Added line #L134 was not covered by tests
[warning] 140-140: src/device-registry/bin/jobs/v3-store-readings-job.js#L140
Added line #L140 was not covered by tests
[warning] 142-142: src/device-registry/bin/jobs/v3-store-readings-job.js#L142
Added line #L142 was not covered by tests
Summary by CodeRabbit
New Features
Bug Fixes
Refactor