feat(api): cache device power state on the devices table - #2636
Draft
madhavilosetty-intel wants to merge 1 commit into
Draft
feat(api): cache device power state on the devices table#2636madhavilosetty-intel wants to merge 1 commit into
madhavilosetty-intel wants to merge 1 commit into
Conversation
Add powerstate, ospowersavingstate and powerstateupdatedat columns to the devices table and surface them on the Device model, so GET /api/v1/devices can return last known power state for a whole fleet in a single query instead of one live call per device. Add a targeted updatePowerState to IDeviceTable for both the postgres and mongo providers. It is kept separate from update() so the two writers of this row keep disjoint SET lists and cannot clobber each other. Both providers let DB errors propagate so the caller can tell a missing device from an unreachable database and back off accordingly. Power state is server owned, so the three fields are dropped from the device update request body. Existing deployments need an ALTER TABLE adding the three columns before this version starts, since getById is on the CIRA authentication path. Resolves: #2614
Contributor
There was a problem hiding this comment.
Pull request overview
Adds persisted “last known” device power-state fields to the data model so fleet-wide device listings can include power state without requiring one live AMT call per device.
Changes:
- Extends the
Devicemodel + Postgres device queries to includepowerState,osPowerSavingState, andpowerStateUpdatedAt. - Adds a targeted
updatePowerState(...)writer toIDeviceTablewith Postgres and Mongo implementations (and provider tests). - Prevents PATCH
/api/v1/devicesfrom accepting power-state fields from the request body (server-owned fields).
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| src/routes/devices/update.ts | Drops power-state fields from update payload before persisting device changes. |
| src/routes/devices/update.test.ts | Adds regression test ensuring request body cannot overwrite server-owned power-state fields. |
| src/models/models.ts | Adds cached power-state fields to the Device interface. |
| src/interfaces/IDeviceTable.ts | Adds updatePowerState(...) API to the device table abstraction. |
| src/data/postgres/tables/device.ts | Selects new power-state columns and implements updatePowerState(...) in Postgres provider. |
| src/data/postgres/tables/device.test.ts | Updates expected SELECTs and adds tests for updatePowerState(...) + ensuring update() doesn’t clobber power-state columns. |
| src/data/mongo/collections/device.ts | Implements updatePowerState(...) for Mongo provider. |
| src/data/mongo/collections/device.test.ts | Adds tests for Mongo updatePowerState(...). |
| data/initMPS.sql | Adds the new power-state columns to the Postgres devices table initialization script. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+20
to
+22
| // power state is server-owned, so it never comes from the request body | ||
| const { powerState, osPowerSavingState, powerStateUpdatedAt, ...updatable } = req.body | ||
| device = { ...device, ...updatable } |
Comment on lines
+25
to
+27
| powerState?: number | ||
| osPowerSavingState?: number | ||
| powerStateUpdatedAt?: Date |
Comment on lines
+21
to
+27
| updatePowerState: ( | ||
| guid: string, | ||
| powerState: number, | ||
| osPowerSavingState: number, | ||
| updatedAt: Date, | ||
| tenantId?: string | ||
| ) => Promise<boolean> |
Comment on lines
+23
to
+25
| powerstate integer, | ||
| ospowersavingstate integer, | ||
| powerstateupdatedat timestamp with time zone, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add powerstate, ospowersavingstate and powerstateupdatedat columns to the devices table and surface them on the Device model, so GET /api/v1/devices can return last known power state for a whole fleet in a single query instead of one live call per device.
Add a targeted updatePowerState to IDeviceTable for both the postgres and mongo providers. It is kept separate from update() so the two writers of this row keep disjoint SET lists and cannot clobber each other. Both providers let DB errors propagate so the caller can tell a missing device from an unreachable database and back off accordingly.
Power state is server owned, so the three fields are dropped from the device update request body.
Existing deployments need an ALTER TABLE adding the three columns before this version starts, since getById is on the CIRA authentication path.
Resolves: #2614
PR Checklist
What are you changing?
Anything the reviewer should know when reviewing this PR?
If the there are associated PRs in other repositories, please link them here (i.e. device-management-toolkit/repo#365 )