Converting a dataset into a JSON file format for the flex repeater

Hello,
I have a dataset that i am first pulling from SQL, adding a column from a tag read, and then sorting. I want to get this into the Flex Repeater on perspective to allow it to show a view for every piece of equipment. The dataset is converted into a JSON in script.

However,, when i bind this to the Flex Repeater Instances object - it errors out.
I am thinking there is an issue with how i am converting the dataset into a JSON, any ideas? It doesn't appear to have the right file format for the Flex Repeater - but i am unsure on how to get this format

The data you are giving to the instances prop appears to be coming out as a single dictionary, the instances prop expects an array(list of dictionaries), so you'll need to adjust your transform script to return your results as such.

You appear to be json encoding a dataset in your transform script, which will not return what you are looking for.

I have tried this - but it doesn't work either. It is like i am missing the column data?


image

Post code - not pictures of code! That way we can copy it and edit in our answers. Format it using the </> button.

def transform(self, value, quality, timestamp):
statusCol =
newData =
for row in range(value.getRowCount()):
stationNumberInt = value.getValueAt(row,'station_number')
stationNumberStr = str(stationNumberInt)
tagPath = '[default]ACME/Station '+stationNumberStr+ '/State .value' #Tag path for station
status = system.tag.readBlocking(tagPath)[0].value #
statusInt = int(status)
#status = system.tag.readBlocking(tagPath)
statusCol.append(status)

newData = system.dataset.addColumn(value,0,statusCol,'status',int)
sortedvalue = system.dataset.sort(newData, 'status', 1)
jsonData = system.util.jsonEncode(sortedvalue)
stationnumber = sortedvalue.getColumnAsList(1)


return stationnumber

expected array item format is:

{
  "instanceStyle": {
    "classes": ""
  },
  "instancePosition": {},
  "param1": value,
  "param2": value2
}

if you don't care about the position properties or styles you can omit those.

Looking through your transform, a few things stand out other than the return format.

You have a line where you are building a tag path then immediately using a blocking read call to fetch the status of the machine. Consider instead passing this tag path to the flex instance as a parameter, and then having a custom property use this tag path in an indirect binding to fetch the tag value. Makes your transform simpler and faster.

Or if you must have the status value, consider using list comprehension to build the list of tag paths and use them in a single blocking read call.

def transform(self, value, quality, timestamp):
	import operator

	statusValueTagPaths = ['[default]ACME/Station {0}/State'.format(value.getValueAt(x, 'station_number')) for x in xrange(value.rowCount)]
	statusValues = system.tag.readBlocking(statusValueTagPaths)

	repeaterItems = []
	positionDictionary = {}
	styleDictionary = {"classes": ""}

	for row in xrange(value.rowCount):
		stationNumber = value.getValueAt(row, 'station_number')
		repeaterItems.append({
			"instanceStyle": styleDictionary,
			"instancePosition": positionDictionary,
			"stationNumber": stationNumber,
			"stationTagPath": '[default]ACME/Station {0}/State'.format(stationNumber),
			"status": statusValues[row].value
		})

	repeaterItems.sort(key=operator.itemgetter("status"))

	return repeaterItems

This unfortunately loops over the base dataset twice (once for making tag paths and once for making the repeater items)

This is also complex enough of a transform that the script should be moved to a project level library and then called from the script transform.

@Transistor you need to find a way to make that reminder your signature

2 Likes

Thanks!

That looks like it should do the trick! I will give it a go when I am back at my computer

One question though - what does the “import operator” function do??

Its just a library import. That is just so I can use the operator.itemgetter('key') function to use as the sort key for sorting the final array. It is supposed to be more performant/straightforward than making a lambda call.

Makes sense - trying to work with it a bit, but for whatever reason it isn't reading the status value it - as they are all null, so the sort function doesn't work.
Any ideas?
I have been working with it - but to be honest, don't quite understand why the tag isn't reading - since the path is correct.

Code here: `def transform(self, value, quality, timestamp):
import operator

statusValueTagPaths = ['[default]ACME/Station {0}/State'.format(value.getValueAt(x, 'station_number')) for x in xrange(value.rowCount)]
statusValues = system.tag.readBlocking(statusValueTagPaths)

		
repeaterItems = []
positionDictionary = {}
styleDictionary = {"classes": ""}


for row in xrange(value.rowCount):
	stationNumber = value.getValueAt(row, 'station_number')
	repeaterItems.append({
			"instanceStyle": styleDictionary,
			"instancePosition": positionDictionary,
			"stationNumber": stationNumber,
			"stationTagPath": '[default]ACME/Station {0}/State '.format(stationNumber),
			"status": statusValues[row].value
		})

repeaterItems.sort(key=operator.itemgetter("status"))


return repeaterItems`

You have a space at the very end of the tag path, does your tag actually have a space at the end of its name? That seems like it would be a typo rather than a naming convention.

You can test if the tag path is valid by copying one of them and then setting a custom test property to be a tag binding with the copied path.

Edit:

should be

statusValueTagPaths = ['[default]ACME/Station {0}/State '.format(value.getValueAt(x, 'station_number')) for x in xrange(value.rowCount)]

You only adjusted one of the tag paths to include the space at the end

2 Likes

Wow, i just saw that...
I screwed up when i made the UDT, and didn't catch it till now. Ugh - debating on changing the script or going back and finding all of the references....

Anyways - that you for all of your help - i really appreciate it!

Here is a version that doesn't loop through the dataset twice.

def transform(self, value, quality, timestamp):
	import operator

	statusValueTagPaths = ['[default]ACME/Station {0}/State'.format(row['station_number']) for row in system.dataset.toPyDataSet(value)]
	statusValues = system.tag.readBlocking(statusValueTagPaths)

	repeaterItems = [{
			"instanceStyle": {'classes':''},
			"instancePosition": {},
			"stationNumber": path.split('/')[1][-1:],
			"stationTagPath": path,
			"status": statusValues[index].value
		} for index,path in enumerate(statusValueTagPaths)]

	repeaterItems.sort(key=operator.itemgetter("status"))

	return repeaterItems
1 Like

ACME... this looks like a certification question... what is it with people trying to flout the rules lately?? I do wonder if these certs get thrown out or do they just a slap on the wrist with no consequence?

I sure hope not, I wouldn’t know as I’ve never taken it.

Ugh, almost need to take it so I know.

2 Likes

Yeah, ACME and "Station" are ringing major alarm bells to me, as these were a big part of the practical side. It's been a few years since I did mine though. I've been caught out in some of the recent posts that were taken down, as I didn't recognise them as from the certs. They must have updated them or my memory is failing.

It CLEARLY states it in the cert info though that asking questions on the forum is prohibited

Edit: This is from when I did my cert. Maybe it doesn't mention the forum, but it's pretty darn obvious you shouldn't be using it (or the help from peers, for that matter either)
Maybe this wording has changed and is less explicit??
image

Edit2:
@Mike_Zylla This from a RECENT cert info sheet, and again, clearly states in no uncertain terms where you can get help from... It also states the repercussions of trying to flout the rules. I'm not sure how someone would think it's ok to try to cheat on a certification test? If you get help from anyone else, it's not proving that you're capable; it's proving that the other person is capable

1 Like

Good to know I won't have any issue with that part of the cert then.

The exam says you cannot receive help from the IA Support group, not the forum

Well, as I understand it it won’t matter for much longer as they’re going to phasing out the tests, if I remember correctly.