2 Replies Latest reply on Mar 27, 2015 8:12 AM by jeffreyr RSS
    jeffreyr Explorer

    Persisting an Infotable property

    I had a use case where I needed to add to an Infotable property by parsing the contents of a CSV file. For the rest of the community, I thought it might be useful to share the code that I used to make sure the changes persisted in the Infotable property.

     

    //// Begin example

    // Read in the CSV file using the standard snippet:

    var params = {

                   longitudeField: undefined /* NUMBER /,

                   fieldDelimiter: undefined / STRING /,

                   path: CSVName / CSV filename -- STRING /,

                   dateFormat: "MM/dd/yyyy HH:mm:ss" / STRING /,

                   stringDelimiter: undefined / STRING /,

                   dataShape: "ResultsDS" / DATASHAPENAME /,

                   columnMappings: undefined / STRING /,

                   hasHeader: true / BOOLEAN /,

                   latitudeField: undefined / NUMBER /,

                   fileRepository: "DemoRepository" / THINGNAME /

    };

    // result: INFOTABLE

    var result = Resources["CSVParserFunctions"].ReadCSVFile(params);


    // Now loop through and record to Infotable property:

    var tablelength = result.getRowCount();


    for (var x = 0; x < tablelength; x++) {

        var row = result.getRow(x);

        // ResultsDS entry object

        var newEntry = new Object();

        newEntry.TimeStamp = row.TimeStamp; // DATETIME

        newEntry.Parameter1 = row.Parameter1; // NUMBER

        newEntry.Parameter2 = row.Parameter2; // NUMBER

        newEntry.Parameter3 = row.Parameter3; // NUMBER

        newEntry.Index = row.Index; // STRING - isPrimaryKey = true

        me.MyInfoTableProperty.AddRow(newEntry);

    };


    // The following code will allow the additions to the infotable property to persist across a shutdown.

    var TABLE = me.MyInfoTableProperty;

    / CORRECTION: The following code is not needed when doing it this way:

        TABLE.AddRow({

        TimeStamp : TimeStamp,

        Parameter1 : Parameter1,

        Parameter2 : Parameter2,

        Parameter3 : Parameter3,

        Index: Index

    });

    */

    me.MyInfoTableProperty = TABLE;

     

    //// End example

     

    It works, but if anyone has suggestions to improve it let me know.

      • Persisting an Infotable property
        paic Collaborator

        Thanks Jeffrey,

         

        Indeed similar to our training example, the only way to persist an InfoTable property is to write it out to a variable and then write it back to the property, since the system optimizes InfoTables by generally treating them as rapid access in memory tables.
        Note that this also applies to JSON properties.

        • Persisting an Infotable property
          jeffreyr Explorer

          I just edited my original post with a correction (see comment toward the end of the script). If those lines of code are used at that point and in that manner in the script, the infotable will not persist. One would use that technique only if using .AddRow() directly to the temporary table in-line with the main script body, not after as I originally had it.