Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This is good example of how we can Eliminate bottlenecks with IguanaX usePrefix Namespaces for Separating Concerns by building a custom file reader component which has a simple interface for a unique purpose rather than using a general purpose no code solution - see The problem with no-code solutions.

Here’s a scenario:

A large lab needs to have a file reading component which can feed files from different customers based on the prefix of the file, i.e. some files start with JJ99, others XYZ etc. depending on the source.

Let’s do it!

Expand
titleImport Use +FROM URL to import a custom version of the file reader from git@bitbucket.org:interfaceware/file_tutorial.gitcomponent for this tutorial

This component generates some test files to make it easier to learn how to make a custom file reader.

You can useCreate component +FROM URL to import the component.

Copy paste the URL from here:

Code Block
git@bitbucket.org:interfaceware/file_tutorial.git
This component generates some test files to make it easier to learn how to make a custom file reader
Expand
titleClick Edit on the component card, then MAKE A COPY AND EDIT to open the Translator editor

Refer to Edit a Component to see how it's done if you have not done this before.

Customize and Copy and Edit button so you can alter the source code
Expand
titleExamine the Custom Field configurations in the component in config.json

To simplify the tutorial we have set these up a ClientList with the filename prefix Ids. Click on the

See Customizing components to see how done if you have not done this beforeconfig.json and you should see:

Image Added

See Custom Fields to understand how this was created.

Expand
titleClick on MatchRules.lua in the Project Tree to see where we need to alter the code

Navigate to MatchRules.lua in the Project Tree. This is the function we need to alter:

Code Block
languagelua
function MatchFile(FileName)
   return true
end

The file reader component is deliberately simple and uses /wiki/spaces/IXB/pages/3181903910 separation of concerns to make it more obvious how to modify the code to meet new needs. We need this function to return true when the matching criteria are met.

Expand
titleLet's extract the filename using FILfilename() from the FIL Library, so we can look at it:

Change the matching to add in two lines so we can see the filenames we need to filter on.

Code Block
languagelua
function MatchFile(FileName)
   local JustFileName = FILfilename(FileName)
   trace(JustFileName)
   return true
end

These are the concepts we have used:

Expand
titleExamine the custom configuration in the component

To simplify the tutorial we have set these up already. Click on the config.json and you should see:

Image Removed

See Custom Fields to understand how this was created.

Expand
titleExtract Edit MatchRules.lua to extract the prefixes from the custom fields

Now we need Create a second function, APPidList, to extract the fields ClientList prefixes from the custom configuration. So fields and update MatchFile to call APPidList.

You can replace the contents of the MatchRules.lua file with this new contentcode:

Code Block
languagelua
function APPidList()
   local IdList = component.fields()["ClientList"]
   return IdList
end

function MatchFile(FileName)
   local IdList = APPidList()
   trace(IdList)
   local JustFileName = FILfilename(FileName)
   trace(JustFileName)
   return true
end

Concepts used:

/wiki/spaces/IXB/pages/3181903910

So we add one line
Expand
titleBreak the comma delimited string into a list
to get each prefix Id

We can quickly do this by adding one line to AppidList after line 2:

Code Block
languagelua
IdList = IdList:split(",")

You should see something like:

Concepts used:

Expand
titleMake a matching function which takes the prefix and sees if the filename starts with it

So now we make Create a third function, APPmatchPrefix, to determine if the file name matches filename matches the prefix. You can copy the code below:

Code Block
languagelua
function APPmatchPrefix(Prefix, FileName)
   local Part = FileName:sub(1, #Prefix)
   trace(Part, Prefix)
   return (Part == Prefix)
end

And we’ll call it as part of the MatchFile routine. You will need to type in the line as part of Match file to call APPmatchPrefix:See the screenshot below and add a line to MatchFile to call and invoke AppmatchPrefix.

Concepts used:

Expand
titleNow change the matching code in MatchFile() so we go iterate through all the of the prefix list

Let’s modify the code to go through the entire list of prefixes:

Code Block
languagelua
for i = 1, #IdList do
   if APPmatchPrefix(IdList[i], JustFileName) then 
      return true
   end
end
return false -- we didn't match anything

And this is what you should see:

Concepts used:

Expand
titleCommit your changes, alter the component to run on the new commit, and run it!

Commit and Push Changes to Git and go to the component card for Choosing the code to run for your component.

And You’re done! The next steps are how you could further refine the code

Expand
titleMake the configuration more robust by striping trimming the white space from the list ClientListof prefixes

This means refining the APPidList function.

That would involve looping through the list and removing white space from each ID:

Concepts:

Expand
titleCustomize the logging

See Custom Logging

using iguana.log*

Add intuitive Custom Logging for your component to increase visibility into processing and adding meaningful messages to help with troubleshooting.

You can see an example of this in main.lua on line 25:

Code Block
languagelua
iguana.log("Polling every "..Polltime.." seconds");

Try it for yourself!

See Concatenating strings for a quick explanation on the double-dot notation.

See Custom Status
Expand
titleCustomize the status
using component.setStatus{data="Text"}

Add Custom Status for your component to have it display real time updates at the bottom of the component card as its running.

This component already creates custom status on lines 31-36 of main.lua.

Try it for yourself!

There is no limit to how you can streamline the usage of a custom component - if that helps address a bottleneck, then it is well worth it.  

...