Perspective audit log query to database

In perspective at the moment there isn't the possibility to log in the audit the query launched aginst a database.
I've used all named query in my project: in your opinion what is the best approach to log them in the audit using system.util.audit?

Introduce a project library function that "wraps" system.db.runNamedQuery and automatically performs system.util.audit at the same time. Refactor your existing calls to run that script.

1 Like

I configured mi function as follow

def runNamedQueryAudit(path, parameters=None, tx='', getKey=False):
	ret = system.db.runNamedQuery(path, parameters, tx, getKey)
	action = 'query'
	action_target = path + ' - parameters: ' + str(parameters) if parameters is not None else path
	action_value = str(ret)
	system.util.audit(action=action, actionTarget=action_target, actionValue=action_value)
	
	return ret

but I when I call it from outside with

connectionInfo = system.db.getConnectionInfo()
dbName = connectionInfo.getValueAt(0, 'name')
projName = system.util.getProjectName()
txId = system.db.beginNamedQueryTransaction(projName, dbName)
par = {
	'masterRecipeId': 10
}
test.db.runNamedQueryAudit('mr_duplicate/duplicateMrHeader', parameters=par, tx=txId)
system.db.commitTransaction(txId)
system.db.closeTransaction(txId)

have issue at line ret = system.db.runNamedQuery(path, parameters, tx, getKey) of the library, it gave me this error:

com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last):
INFO   | jvm 2    | 2023/05/19 11:37:15 |   File "<function:runAction>", line 16, in runAction
INFO   | jvm 2    | 2023/05/19 11:37:15 |   File "<module:test.db>", line 8, in runNamedQueryAudit
INFO   | jvm 2    | 2023/05/19 11:37:15 | 	at com.inductiveautomation.ignition.common.TypeUtilities.coerce(TypeUtilities.java:1527)
INFO   | jvm 2    | 2023/05/19 11:37:15 | 	at com.inductiveautomation.ignition.common.script.builtin.PyArgumentMap.coerce(PyArgumentMap.java:130)
INFO   | jvm 2    | 2023/05/19 11:37:15 | 	at com.inductiveautomation.ignition.common.script.builtin.PyArgumentMap.interpretPyArgs(PyArgumentMap.java:82)
INFO   | jvm 2    | 2023/05/19 11:37:15 | 	at com.inductiveautomation.ignition.common.script.builtin.PyArgumentMap.interpretPyArgs(PyArgumentMap.java:40)
INFO   | jvm 2    | 2023/05/19 11:37:15 | 	at com.inductiveautomation.ignition.gateway.script.GatewayDBUtilities.interpretWithOptionalProject(GatewayDBUtilities.java:467)
INFO   | jvm 2    | 2023/05/19 11:37:15 | 	at com.inductiveautomation.ignition.gateway.script.GatewayDBUtilities.runNamedQuery(GatewayDBUtilities.java:391)
INFO   | jvm 2    | 2023/05/19 11:37:15 | 	at jdk.internal.reflect.GeneratedMethodAccessor23.invoke(Unknown Source)
INFO   | jvm 2    | 2023/05/19 11:37:15 | 	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
INFO   | jvm 2    | 2023/05/19 11:37:15 | 	at java.base/java.lang.reflect.Method.invoke(Unknown Source)
INFO   | jvm 2    | 2023/05/19 11:37:15 | java.lang.ClassCastException: java.lang.ClassCastException: Cannot coerce value '0aa6c53d-451a-4b6a-aa4b-4098752dd04d' into type: interface java.util.Map

As far as I understand it fail converting to map the parameters argument of my function.
If in my call I use the normal system.db.runNamedQuery function everyting work fine!

Any idea on how to fix it?

Some system functions need additional arguments when called from gateway scope. Running a named query is one of them.

Testing in the script console uses designer scope, which is a variant of Vision Client scope. You are calling from Gateway scope or Perspective scope (a variant of Gateway scope). Check the docs!

Project library scripts do not have their own scope. They run in whatever scope calls them.

(Testing in the script console is never appropriate for gateway or perspective events.)

In the meantime I was figouring it out, the solution I found was in this post

I wasn't testing it from console, but calling it from a button in persepective! In the console give me some other absurd errors by the way!