Skip to content

feat(api): cache device power state on the devices table - #2636

Draft
madhavilosetty-intel wants to merge 1 commit into
mainfrom
feat/device-power-state-cache
Draft

feat(api): cache device power state on the devices table#2636
madhavilosetty-intel wants to merge 1 commit into
mainfrom
feat/device-power-state-cache

Conversation

@madhavilosetty-intel

Copy link
Copy Markdown
Contributor

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

  • Unit Tests have been added for new changes
  • API tests have been updated if applicable
  • All commented code has been removed
  • If you've added a dependency, you've ensured license is compatible with Apache 2.0 and clearly outlined the added dependency.

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 )

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

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 Device model + Postgres device queries to include powerState, osPowerSavingState, and powerStateUpdatedAt.
  • Adds a targeted updatePowerState(...) writer to IDeviceTable with Postgres and Mongo implementations (and provider tests).
  • Prevents PATCH /api/v1/devices from 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 thread src/models/models.ts
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 thread data/initMPS.sql
Comment on lines +23 to +25
powerstate integer,
ospowersavingstate integer,
powerstateupdatedat timestamp with time zone,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for bulk power state retrieval

2 participants