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

Update Offline Status for Untracked Devices #3620

Merged
merged 3 commits into from
Oct 10, 2024
Merged

Conversation

Baalmart
Copy link
Contributor

@Baalmart Baalmart commented Oct 10, 2024

Description

Enhance Device Status Management: Update Offline Status for Untracked Devices

Changes Made

  • Extracted Active Device IDs: Implemented logic to gather unique device IDs from the fetched measurements.
  • Updated Device Status: Added functionality to mark devices as offline if their IDs are not present in the fetched measurements.
  • Optimised Database Operations: Utilised updateMany to efficiently update multiple device statuses in a single database call.
  • Improved Logging: Enhanced logging for better tracking of device status updates and potential issues during processing.
  • Refactored Code Structure: Organised code for clarity and maintainability, ensuring separation of concerns between processing documents and updating statuses.

Affected Services

  • Which services were modified:
    • Device Registry

API Documentation Updated?

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

Additional Notes

Enhance Device Status Management: Update Offline Status for Untracked Devices

Summary by CodeRabbit

  • New Features

    • Enhanced device status tracking by marking devices without active readings as offline.
    • Added functionality to extract unique device IDs from fetched measurements for accurate updates.
  • Bug Fixes

    • Improved logging messages to reflect successful data insertion and offline device updates.
  • Tests

    • Expanded test cases to ensure devices not present in fetched measurements are marked as offline.
    • Updated tests for handling empty data responses to include log expectations.

Copy link
Contributor

coderabbitai bot commented Oct 10, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes in this pull request focus on enhancing the functionality of the v2-store-readings-job.js file. A new feature has been introduced to extract unique device IDs from fetched measurements, which are then utilized to update device statuses in the database. Specifically, devices not present in the activeDeviceIds set are marked as offline. Additionally, logging messages have been updated to reflect the successful insertion of data and the status updates for offline devices.

Changes

File Path Change Summary
src/device-registry/bin/jobs/v2-store-readings-job.js Enhanced functionality for device status updates; added logic to mark devices offline if not in active readings; updated logging messages.
src/device-registry/bin/jobs/test/ut_v2-store-readings-job.js Added new test cases for marking devices offline; updated existing tests for empty data responses.

Possibly related PRs

Suggested reviewers

  • Codebmk
  • BenjaminSsempala

🎉 In the realm of devices, a change takes flight,
Unique IDs now shine, making status right.
Offline they rest, if readings are few,
Logging the journey, with updates anew.
A job well done, in code we trust,
Enhancing our systems, it's a must! 🚀


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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @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.

Copy link

codecov bot commented Oct 10, 2024

Codecov Report

Attention: Patch coverage is 0% with 4 lines in your changes missing coverage. Please review.

Project coverage is 27.04%. Comparing base (95212e9) to head (d77a0d1).
Report is 42 commits behind head on staging.

Files with missing lines Patch % Lines
.../device-registry/bin/jobs/v2-store-readings-job.js 0.00% 4 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           staging    #3620      +/-   ##
===========================================
- Coverage    27.09%   27.04%   -0.06%     
===========================================
  Files          146      146              
  Lines        21336    21339       +3     
  Branches       273      273              
===========================================
- Hits          5782     5772      -10     
- Misses       15554    15567      +13     
Files with missing lines Coverage Δ
.../device-registry/bin/jobs/v2-store-readings-job.js 18.36% <0.00%> (-0.59%) ⬇️

... and 2 files with indirect coverage changes

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: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between b030cfe and 972c671.

📒 Files selected for processing (1)
  • src/device-registry/bin/jobs/v2-store-readings-job.js (2 hunks)
🧰 Additional context used

Comment on lines 208 to 212
// Update devices that are not in the activeDeviceIds set to offline
await DeviceModel("airqo").updateMany(
{ _id: { $nin: Array.from(activeDeviceIds) } },
{ isOnline: false }
);
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

⚠️ Potential issue

Potential Issue: Devices May Be Incorrectly Marked as Offline

The current logic updates isOnline to false for all devices not present in activeDeviceIds. This may inadvertently mark devices as offline even if they are still active but didn't report in the current batch due to intermittent connectivity or data fetch issues.

Consider modifying the logic to mark devices as offline only if they haven't sent data within the defined INACTIVE_THRESHOLD. This ensures that only truly inactive devices are marked offline.

You might adjust the update query as follows:

- await DeviceModel("airqo").updateMany(
-   { _id: { $nin: Array.from(activeDeviceIds) } },
-   { isOnline: false }
- );
+ const thresholdTime = moment().subtract(INACTIVE_THRESHOLD, 'milliseconds').toDate();
+ await DeviceModel("airqo").updateMany(
+   {
+     _id: { $nin: Array.from(activeDeviceIds) },
+     lastActive: { $lt: thresholdTime }
+   },
+   { isOnline: false }
+ );

This change adds a condition to only update devices whose lastActive timestamp is older than the INACTIVE_THRESHOLD, preventing premature offline status updates.

Committable suggestion was skipped due to low confidence.

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: 0

🧹 Outside diff range and nitpick comments (3)
src/device-registry/bin/jobs/test/ut_v2-store-readings-job.js (3)

137-151: Excellent enhancement to test coverage!

The addition of stubs for the Device Model significantly improves the test's ability to verify offline device updates. This is a valuable improvement to the test suite.

As a minor suggestion, consider extracting the device ID "456" into a constant at the beginning of the test case. This would make the test more maintainable if you need to change the ID in the future.

Here's a small refactor suggestion:

const ACTIVE_DEVICE_ID = "456";
const mockData = [
  { data: [{ site_id: "123", device_id: ACTIVE_DEVICE_ID, time: new Date() }] },
];

// ... rest of the test case ...

expect(deviceUpdateManyStub).to.have.been.calledWith(
  { _id: { $nin: [ACTIVE_DEVICE_ID] } },
  { isOnline: false }
);

This change would make the test more readable and easier to maintain.


158-161: Well-crafted expectation for offline device updates!

This expectation effectively verifies that devices not present in the fetched data are marked as offline. It's a crucial check that ensures the core functionality is working as intended.

To enhance clarity, you might consider adding a brief comment explaining the logic behind the $nin operator. This would make the test more self-documenting for future readers.

Here's a suggestion to improve the comment:

expect(deviceUpdateManyStub).to.have.been.calledWith(
  { _id: { $nin: ["456"] } }, // Update all devices except the one in fetched data
  { isOnline: false }
);

This small change provides more context about the purpose of the $nin operator in this test.


193-223: Excellent addition of a focused test case!

This new test case specifically targeting the offline marking behavior is a valuable addition to the test suite. It ensures that this critical functionality is explicitly verified, which is crucial for maintaining system reliability.

To maintain consistency with the earlier suggestion and improve maintainability, consider extracting the device ID into a constant here as well.

Here's a suggestion to improve consistency and maintainability:

const ACTIVE_DEVICE_ID = "456";
const mockData = [
  { data: [{ site_id: "123", device_id: ACTIVE_DEVICE_ID, time: new Date() }] },
];

// ... rest of the test case ...

expect(deviceUpdateManyStub).to.have.been.calledWith(
  { _id: { $nin: [ACTIVE_DEVICE_ID] } },
  { isOnline: false }
);

This change aligns with the earlier suggestion and makes the test more robust against future changes.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 972c671 and 45b0a60.

📒 Files selected for processing (1)
  • src/device-registry/bin/jobs/test/ut_v2-store-readings-job.js (2 hunks)
🧰 Additional context used
📓 Learnings (1)
📓 Common learnings
Learnt from: Baalmart
PR: airqo-platform/AirQo-api#3611
File: src/device-registry/bin/server.js:27-27
Timestamp: 2024-10-10T06:33:42.428Z
Learning: The `v2-store-readings-job.js` file exists in the repository at `src/device-registry/bin/jobs/v2-store-readings-job.js`.
🔇 Additional comments (1)
src/device-registry/bin/jobs/test/ut_v2-store-readings-job.js (1)

Line range hint 137-223: Commendable enhancement of test coverage!

The changes made to this test file significantly improve the verification of offline device update functionality. The addition of new test cases and expectations demonstrates a thorough approach to ensuring the reliability of this critical feature.

Key strengths of these changes include:

  1. Comprehensive coverage of offline device marking scenarios.
  2. Consistent use of sinon stubs for dependency isolation.
  3. Clear and focused test cases that target specific behaviors.

The minor suggestions provided earlier for extracting constants and improving comments would further enhance the maintainability and readability of these tests.

Overall, these changes represent a valuable contribution to the test suite and will help ensure the robustness of the new-store-readings-job module.

Copy link
Contributor

Device registry changes in this PR available for preview here

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