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

Issue 1397 #1398

Merged
merged 19 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src-electron/db/query-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,10 @@ async function insertOrUpdateAttributeState(
)
staticAttribute.defaultValue = featureMapDefaultValue
}
let forcedExternal = await queryUpgrade.getForcedExternalStorage(db)
let forcedExternal = await queryUpgrade.getForcedExternalStorage(
db,
staticAttribute.packageRef
paulr34 marked this conversation as resolved.
Show resolved Hide resolved
)
staticAttribute.storagePolicy =
await queryUpgrade.computeStoragePolicyNewConfig(
db,
Expand Down
2 changes: 1 addition & 1 deletion src-electron/db/query-impexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ WHERE
attribute.reportable = false
}
if (attributeId) {
forcedExternal = await queryUpgrade.getForcedExternalStorage(db)
forcedExternal = await queryUpgrade.getForcedExternalStorage(db, packageIds)
storagePolicy = await queryUpgrade.computeStorageImport(
db,
cluster.name,
Expand Down
77 changes: 61 additions & 16 deletions src-electron/db/query-package.js
paulr34 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -690,23 +690,68 @@ async function getAllPackages(db) {
)
.then((rows) => rows.map(dbMapping.map.package))
}
/**
* Retrieves attribute access interface options from the database.
*
* This function performs a complex query to fetch options related to a specific code and package IDs. It combines results from
* the PACKAGE_OPTION table with those from ATTRIBUTE and CLUSTER tables using a UNION. The purpose is to gather a comprehensive
* list of options that include both direct package options and those inferred from attributes' storage policies and their associated
* clusters. It supports querying for multiple package IDs by ensuring the packageIds parameter is treated as an array, allowing
* for more flexible queries.
*
* @param {Object} db - The database connection object.
* @param {string} code - The option code or storage policy code to query for.
* @param {number|Array<number>} packageIds - The ID(s) of the package(s) to which the options are related. Can be a single ID or an array of IDs.
* @returns {Promise<Array>} A promise that resolves to an array of option objects, each containing the option category, code, and label.
*/
async function getAttributeAccessInterface(db, code, packageIds) {
try {
// Ensure packageIds is always an array
if (!Array.isArray(packageIds)) {
packageIds = [packageIds]
}
paulr34 marked this conversation as resolved.
Show resolved Hide resolved

async function getAttributeAccessInterface(db, code) {
return dbApi
.dbAll(
db,
`SELECT
PACKAGE_REF,
OPTION_CATEGORY,
OPTION_CODE,
OPTION_LABEL
FROM
PACKAGE_OPTION
WHERE
OPTION_CODE = ?`,
[code]
)
.then((rows) => rows.map(dbMapping.map.options))
let packageRefCondition = `po.PACKAGE_REF = ?`
paulr34 marked this conversation as resolved.
Show resolved Hide resolved
let attributePackageRefCondition = `a.PACKAGE_REF = ?`
let queryParams = [code, ...packageIds, code, ...packageIds]

// Since packageIds is now always an array, adjust the query and parameters accordingly
const placeholders = packageIds.map(() => '?').join(', ')
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can do the standard:

IN (${dbApi.toInClause(packageIds)})

packageRefCondition = `po.PACKAGE_REF IN (${placeholders})`
attributePackageRefCondition = `a.PACKAGE_REF IN (${placeholders})`

const extendedQuery = `
SELECT
paulr34 marked this conversation as resolved.
Show resolved Hide resolved
po.OPTION_CATEGORY,
po.OPTION_CODE,
po.OPTION_LABEL
FROM
PACKAGE_OPTION po
WHERE
po.OPTION_CODE = ?
AND ${packageRefCondition}

UNION

SELECT
c.NAME AS OPTION_CATEGORY,
a.STORAGE_POLICY AS OPTION_CODE,
a.NAME AS OPTION_LABEL
FROM
ATTRIBUTE a
LEFT JOIN CLUSTER c ON a.CLUSTER_REF = c.CLUSTER_ID
WHERE
a.STORAGE_POLICY = ?
AND ${attributePackageRefCondition}
`

return dbApi
.dbAll(db, extendedQuery, queryParams)
.then((rows) => rows.map(dbMapping.map.options))
} catch (error) {
console.error('Error fetching attribute access interface:', error)
throw error // Rethrow the error for further handling if necessary
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions src-electron/db/query-zcl.js
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,7 @@ async function selectAttributeByAttributeIdAndClusterRef(
`
SELECT
A.ATTRIBUTE_ID,
A.PACKAGE_REF,
A.CLUSTER_REF,
A.CODE,
A.MANUFACTURER_CODE,
Expand Down
9 changes: 6 additions & 3 deletions src-electron/generator/helper-zcl.js
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,8 @@ async function zcl_attributes(options) {
attributes = await upgrade.computeStoragePolicyForGlobalAttributes(
this.global.db,
this.id,
attributes
attributes,
packageIds
)
} else {
attributes = await queryZcl.selectAllAttributes(this.global.db, packageIds)
Expand Down Expand Up @@ -805,7 +806,8 @@ async function zcl_attributes_client(options) {
clientAttributes = await upgrade.computeStoragePolicyForGlobalAttributes(
this.global.db,
this.id,
clientAttributes
clientAttributes,
packageIds
)
} else {
clientAttributes = await queryZcl.selectAllAttributesBySide(
Expand Down Expand Up @@ -846,7 +848,8 @@ async function zcl_attributes_server(options) {
serverAttributes = await upgrade.computeStoragePolicyForGlobalAttributes(
this.global.db,
this.id,
serverAttributes
serverAttributes,
packageIds
)
} else {
serverAttributes = await queryZcl.selectAllAttributesBySide(
Expand Down
20 changes: 19 additions & 1 deletion src-electron/rest/user-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,30 @@ function httpPostCluster(db) {
}
}
}

/**
* Handles a POST request to retrieve forced external storage options.
*
* This function is designed to be used as a middleware in an Express.js route. It extracts the session ID from the request,
* queries the database for package information associated with that session, and then retrieves forced external storage
* options for the identified package. The results are sent back to the client as a JSON response.
*
* @param {Object} db - The database connection object.
* @returns {Function} An asynchronous function that takes Express.js request and response objects.
*/
function httpPostForcedExternal(db) {
return async (request, response) => {
let forcedExternal = await upgrade.getForcedExternalStorage(db)
let sessionId = request.zapSessionId
let packages = await queryPackage.getPackageSessionPackagePairBySessionId(
db,
sessionId
)
let packageIds = packages.map((pkg) => pkg.id)
let forcedExternal = await upgrade.getForcedExternalStorage(db, packageIds)
response.status(StatusCodes.OK).json(forcedExternal)
}
}

/**
* HTTP POST attribute update
*
Expand Down
Loading
Loading