Alarm Journal Export

I am trying to export the Alarm Journal data into a csv in an efficient manner, as outlined in this post. I'm using Vision.

I went through the javadocs, but I'm unable to get a reference to the model that I can use a getAlarmAt(int row) method. How can I get to that method using my reference to the Alarm Journal table?
alarm_journal= event.source.parent.getComponent('Alarm Journal')

The model is buried deep in the journal component, and I'm not sure if it can be accessed directly.

However, it can be accessed through the journal's subcomponents. I threw this script together on a test button, and it works:

# Returns the first nested component of a given class
# container = The object containing nested components to be recursively searched
# className = the __name__ of the class given as a string
def getComponentOfClass(container, className):
	for component in container.components:
		if component.__class__.__name__ == className:
			return component
		else:
			foundComponent = getComponentOfClass(component, className)
			if foundComponent:
				return foundComponent

# Find the inner table in the alarm journal component
alarmJournal = event.source.parent.getComponent('Alarm Journal')
innerTable = getComponentOfClass(alarmJournal, 'AlarmJournalTable$3')

# Define the parameters needed to convert the table model to an Ignition dataset
data = []
headers = [innerTable.getColumnName(column) for column in xrange(innerTable.columnCount)]

# Extract the row data from the inner table
# If any special filters are needed, do it here in the for loop
for row in xrange(innerTable.rowCount):
	data.append([innerTable.getValueAt(row, column) for column in xrange(innerTable.columnCount)])

# Create the journal data to an Ignition dataset and export it as a CSV
dataset = system.dataset.toDataSet(headers, data)
system.dataset.exportCSV('journalData', True, dataset)

The script simply digs the inner table out, retrieves the data, and converts it to an extractable Ignition dataset.

Out of curiosity. what is the reason for not constructing an alarm query call? It seems like that approach would be simpler.

3 Likes

Thank you for your solution! I was just hoping if I could find a way to directly access the table without having to make the query. The table is already being displayed on the page. Having to do another query seemed redundant, and even your solution also involves constructing the dataset in an O(n) manner. I guess I'll have to do it anyway.

Regardless, thanks a lot! Your solution helps me understand the underlying model, which I could not figure out earlier.

1 Like

Untested, but should work.

from org.apache.commons.lang3.reflect import FieldUtils

alarmJournal = event.source.parent.getComponent('Alarm Journal')

model = FieldUtils.readField(alarmJournal, "model", True)
data = FieldUtils.readField(model, "data", True)
ds = data.getDataset()
4 Likes
`system.dataset.exportCSV('journalData', True, ds)`

:sunglasses::sunglasses::sunglasses: trick
I added the export line of code and tested it, and it works perfectly

4 Likes

Would this work in ignition?

Can you rephrase the question? Basically every piece of code I share on these forums is for use in Ignition.

1 Like

Sorry, I meant to say ignition perspective.

Then no. Perspective's component model is fundamentally different and more 'locked away' from you than Vision's.

Could you describe what you're trying to do (probably in a new forum thread; this post is explicitly about the Vision component) and we can offer advice to help you reach that goal?

Sure! I was trying to do an export of the Alarm Journal in Perspective. It works but I still have some questions about the export. Check this post: The EventState column returned by system.alarm.queryJournal - does it represent the EventType/State or the Event State - Ignition - Inductive Automation Forum

P.S. Apologies for the long title.