Skip to content

refactor: denormalize currentMode/discovered into device columns - #1211

Open
ShradhaGupta31 wants to merge 2 commits into
mainfrom
denormalize-mode-discovered
Open

refactor: denormalize currentMode/discovered into device columns#1211
ShradhaGupta31 wants to merge 2 commits into
mainfrom
denormalize-mode-discovered

Conversation

@ShradhaGupta31

@ShradhaGupta31 ShradhaGupta31 commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Addresses: #1210

ADR: https://github.com/device-management-toolkit/console/wiki/API-contract-update-&-DB-query-mechanism-to-fetch-Device-Stats

  • Mirror the deviceinfo currentMode and discovered JSON fields into dedicated queryable device columns, synced on every insert/update from the deviceinfo blob (source of truth).
  • Adds nullable columns via migration and wires the write path across sqldb (Postgres/SQLite) and mongo backends.

No API or behaviour change: the columns are written but not yet read.

@ShradhaGupta31
ShradhaGupta31 requested a lite review from Copilot August 20, 2026 14:00

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

This PR implements issue #1210’s first step by denormalizing deviceInfo.currentMode and deviceInfo.discovered into dedicated device-level columns across SQL (Postgres/SQLite) and MongoDB, writing them on every insert/update while keeping the deviceinfo blob as the source of truth.

Changes:

  • Add nullable currentmode (TEXT) and discovered (BOOLEAN) columns via DB migration.
  • Wire SQL insert/update and Mongo update paths to persist the new mirror columns.
  • Update the devices use case to sync mirror fields from DeviceInfo during DTO→entity transforms (but currently introduces compile errors).

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
internal/usecase/sqldb/device.go Persist currentmode/discovered on device insert/update.
internal/usecase/sqldb/device_test.go Extend SQLite test schemas with the new nullable columns.
internal/usecase/nosqldb/mongo/device.go Persist currentmode/discovered in Mongo updates.
internal/usecase/devices/usecase.go Attempt to sync mirror columns from DeviceInfo during transforms (contains build-breaking issues).
internal/usecase/devices/repo_test.go Adjust partial update expectations to include the new discovered mirror field.
internal/entity/device.go Add mirror fields to the device entity model.
internal/app/migrations/20260820000000_migrate_device_mode_discovered.up.sql Add currentmode/discovered columns to devices.
internal/app/migrations/20260820000000_migrate_device_mode_discovered.down.sql Drop currentmode/discovered columns from devices.
Suppressed comments (1)

internal/usecase/devices/usecase.go:350

  • unmarshalDeviceInfo currently unmarshals into an undefined identifier (iDeviceInfonfo) and then returns &info, which remains empty. This is a compile-time error and breaks deviceInfo deserialization.

	var info dto.DeviceInfo
	if err := json.Unmarshal([]byte(raw), &iDeviceInfonfo); err != nil {
		return nil, ErrDeviceUseCase.Wrap("unmarshalDeviceInfo", "failed to unmarshal deviceInfo for device "+guid, err)
	}

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread internal/entity/device.go
Comment thread internal/usecase/devices/usecase.go
@ShradhaGupta31
ShradhaGupta31 force-pushed the denormalize-mode-discovered branch from 6ae7fa9 to 6518745 Compare August 20, 2026 14:32
@codecov

codecov Bot commented Aug 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 50.83%. Comparing base (76d223b) to head (7034e21).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1211      +/-   ##
==========================================
+ Coverage   50.82%   50.83%   +0.01%     
==========================================
  Files         149      149              
  Lines       13873    13877       +4     
==========================================
+ Hits         7051     7055       +4     
  Misses       6218     6218              
  Partials      604      604              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ShradhaGupta31
ShradhaGupta31 force-pushed the denormalize-mode-discovered branch from 6518745 to 25621d1 Compare August 21, 2026 13:20
@ShradhaGupta31
ShradhaGupta31 marked this pull request as ready for review August 21, 2026 13:30
@ShradhaGupta31
ShradhaGupta31 requested a review from a team as a code owner August 21, 2026 13:30
@ShradhaGupta31
ShradhaGupta31 force-pushed the denormalize-mode-discovered branch from 25621d1 to ceb82d9 Compare August 24, 2026 04:52
Comment thread internal/usecase/sqldb/device.go Outdated
Insert("devices").
Columns("guid", "hostname", "tags", "mpsinstance", "connectionstatus", "mpsusername", "tenantid", "friendlyname", "dnssuffix", "deviceinfo", "username", "password", "mpspassword", "mebxpassword", "usetls", "allowselfsigned", "certhash").
Values(d.GUID, d.Hostname, d.Tags, d.MPSInstance, d.ConnectionStatus, d.MPSUsername, d.TenantID, d.FriendlyName, d.DNSSuffix, d.DeviceInfo, d.Username, d.Password, d.MPSPassword, d.MEBXPassword, d.UseTLS, d.AllowSelfSigned, d.CertHash)
Columns("guid", "hostname", "tags", "mpsinstance", "connectionstatus", "mpsusername", "tenantid", "friendlyname", "dnssuffix", "deviceinfo", "username", "password", "mpspassword", "mebxpassword", "usetls", "allowselfsigned", "certhash", "currentmode", "discovered").

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.

currentmode and discovered are not included in the SELECT statement. As a result, DB reads will return empty values even when the data exists in the DB.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is kept intentional. currentmode/discovered are write-only mirror columns & we sync them from the deviceinfo JSON on every insert/update purely so we can filter on them in SQL. On read, the API response sources currentMode/discovered from the deviceinfo blob (which is in the SELECT) via entityToDTO, so consumers still get the real values & the empty entity fields never reach the response. Adding them to the SELECT wouldn't change any output, just duplicate data we already read from deviceinfo.

Please let me know if still should be added.

Comment thread internal/entity/device.go Outdated
CertHash *string `bson:"certhash"`
// Queryable mirrors of the matching deviceinfo JSON fields, synced on every insert/update.
CurrentMode string `bson:"currentmode"`
Discovered *bool `bson:"discovered"`

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.

@ShradhaGupta31 : do we need to denormalize Discovered at this point. Since with CurrentMode we should be able to get activated and discovered devices.
Ex:
There are 10 devices in the DB
6 are activated in either CCM or ACM mode - for these devices CurrentMode would be either acmProvisioned or ccmProvisioned
remaining 4 devices the CurrentMode would be NotProvisioned

Discovered could help only to know whether the device was auto-discovered ( through rpc amtinfo --discover or some other mechanism )

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, your understanding is right. For the current UI, total = activated + discovered and the two tabs are mutually exclusive, so a device that's discovered and then activated moves to the activated tab.

I kept the separate discovered filter for future use as it would filter was this device ever discovered on the network. If we later add UI tabs or need a backend query for all discovered devices, this filter is what we might use.

That said, if you feel it's not needed right now, I'm happy to remove it and derive discovered purely as the complement of activated.

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.

you could remove the discovered filter. When required we can implement

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed discovered denormalisation

Addresses: #1210

- Mirror the deviceinfo currentMode and discovered JSON fields into dedicated
queryable device columns, synced on every insert/update from the deviceinfo
blob (source of truth).
- Adds nullable columns via migration and wires the
write path across sqldb (Postgres/SQLite) and mongo backends.

No API or behaviour change: the columns are written but not yet read.

Signed-off-by: ShradhaGupta31 <shradha.gupta@intel.com>
@ShradhaGupta31
ShradhaGupta31 force-pushed the denormalize-mode-discovered branch from c20cd8b to 7034e21 Compare August 27, 2026 10:37
- Removed discovered feilds denormalisation

Signed-off-by: ShradhaGupta31 <shradha.gupta@intel.com>
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.

4 participants