If someone has a better question title, i'm all ears eyes...
I just spent a while on this problem and found the issue was my understanding. I had the following code below (note the comment marked by "## <<--").
It basically takes a dictionary (summaryTotal, e.g. data below) which contains a summary of alarms: both total counts and a list of the summarised alarms and some info about them. The actual alarms are within summaryTotal['Alarms']
which is an array of dictionaries. My function filters this full list of alarms by the alarm source
and produces the same format as summaryTotal
, but filtered.
The line of code I was having issue with was this one:
summaryFiltered['Alarms'].append(alarm)
In this line, alarm
is really a reference to an element from the alarmsTotal
list. The alarmsTotal
list itself is a reference to summaryTotal['Alarms']
.
When I used this line of code and appended alarm
, the function at the bottom of the code system.util.jsonEncode
(external function to change a python object into a json encoded python object - not too sure on the details), was always coming up with a 'too many recursive calls' error. When I changed what I was appending by creating essentially a new alarm
dictionary within a new object and set it to the values within the actual alarm
object, then the jsonEncode
function started working and not raising recursion exceptions.
I'd like to be able to explain why that is?
When I'm appending alarm
to my new array, I think I'm appending the reference of the summaryTotal['Alarms']
object to it, for example summaryTotal['Alarms'][20]
... ?
summaryTotal = ['ActiveUnacked':0, 'ActiveAcked': 2, 'ClearUnacked': 23,
'Alarms':
[
{
"name": "Comms Fault",
"eventTime": "Fri Mar 05 12:25:27 ACDT 2021",
"label": "Comms Fault",
"displayPath": "Refrigeration MSB4 MCC PWM Comms Fault",
"source": "FolderA/FolderB/FolderC/MSB4 MCC PWM Comms OK",
"state": "Cleared, Unacknowledged"
},
{
"name": "Comms Fault",
"eventTime": "Fri Mar 05 12:28:46 ACDT 2021",
"label": "Comms Fault",
"displayPath": "Refrigeration MSB4 MCC PWM Comms Fault",
"source": "Folder1/Folder2/Folder3/MSB4 MCC PWM Comms OK",
"state": "Cleared, Unacknowledged"
}
]
]
alarmsTotal = summaryTotal['Alarms']
summaryFiltered = {'ActiveAcked':0, 'ActiveUnacked':0, 'ClearUnacked':0, 'Alarms':[]}
for alarm in alarmsTotal:
if pathFilter in alarm['source']:
alarmInfo = {'name':alarm['name'],
'label':alarm['label'],
'displayPath':alarm['displayPath'],
'source': alarm['source'],
'state': alarm['state'],
'eventTime': alarm['eventTime']
}
summaryFiltered['Alarms'].append(alarm) ## <<-- to fix the code, I added `alarmInfo` above and appended `alarmInfo` instead of `alarm`
if alarm['state'] == 'Cleared, Unacknowledged':
summaryFiltered['ClearUnacked'] += 1
if alarm['state'] == 'Active, Unacknowledged':
summaryFiltered['ActiveUnacked'] += 1
if alarm['state'] == 'Active, Acknowledged':
summaryFiltered['ActiveAcked'] += 1
ret = system.util.jsonEncode(summaryFiltered)