Wed Aug 15, 2018 1:49 am
Login Register Lost Password? Contact Us


Emailing dataset

Questions around writing code and queries

Mon Oct 19, 2015 9:23 am Change Time Zone

I'm looking to send the results of a workunit (gathering some stats) in an email, as the recipients don't have access to Thor. It's a couple of simple datasets with similar but not identical layouts. The best I can some up with at the moment is to use STD.SendEmailAttachData and to write a function to convert the each of the datasets to CSV or HTML. Is there no way HPCC can do this for me? The code must be there for ECL Watch! If I do have to convert them myself do I have to write a function for each layout, or is there some way I can write a macro function that will do any dataset?
james.wilson
 
Posts: 20
Joined: Fri Nov 14, 2014 3:44 pm

Mon Oct 19, 2015 1:08 pm Change Time Zone

I would use:

Code: Select all
STD.System.Email.SendEmailAttachData 


See https://hpccsystems.com/download/docume ... hData.html

HTH,

Jim
JimD
 
Posts: 132
Joined: Wed May 18, 2011 1:35 pm

Mon Oct 19, 2015 1:24 pm Change Time Zone

Hi Jim

Thanks, yes, that's the function I'm using to send the email, it's getting the contents for the attachment parameter that's the issue. Do I have to write separate code for each dataset to get the results in HTML or CSV format?
james.wilson
 
Posts: 20
Joined: Fri Nov 14, 2014 3:44 pm

Mon Oct 19, 2015 5:31 pm Change Time Zone

James,
Do I have to write separate code for each dataset to get the results in HTML or CSV format?
If you're going to send the data as CSV, then you should just use the SendEmailAttachText() function, since CSV files are just ASCII text anyway.

Whichever function you use, you will need to write a function to get each simple dataset result into the format that can attach to the email. The functions should end up with the entire result dataset formatted into a single string for transmission.

Since you have several different result files to send with different formats for each, I would suggest emailing each separately.

HTH,

Richard
rtaylor
Community Advisory Board Member
Community Advisory Board Member
 
Posts: 1368
Joined: Wed Oct 26, 2011 7:40 pm

Mon Jul 31, 2017 3:29 pm Change Time Zone

After a (very!) long delay I'm looking at this again. I've come up with a collection of macros to convert a dataset to a string containing the CSV data to use in SendEmailAttachText. Here it is:

Code: Select all
SHARED MAC_MakeCsvConcatValue(lay) := MACRO
    #UNIQUENAME(TheList)
    #SET(TheList,'')
    #UNIQUENAME(sep)
    #SET(sep,'')
    #UNIQUENAME(out)
    #EXPORTXML(out, lay)
    #FOR (out)
      #FOR (Field)
        #APPEND(TheList, %'sep'%)
        #APPEND(TheList, 'L.')
        #APPEND(TheList, #TEXT(%{@label}%))
        #SET(sep,' + \',\' + ')
      #END
    #END
    %'TheList'%
ENDMACRO;

SHARED MAC_MakeCsvHeaderList(lay) := MACRO
    #UNIQUENAME(TheList)
    #SET(TheList,'')
    #UNIQUENAME(sep)
    #SET(sep,'')
    #UNIQUENAME(out)
    #EXPORTXML(out, lay)
    #FOR (out)
      #FOR (Field)
        #APPEND(TheList, %'sep'%)
        #APPEND(TheList, #TEXT(%{@label}%))
        #SET(sep,',')
      #END
    #END
    %'TheList'%
ENDMACRO;

EXPORT DatasetAsCsvString(InputDataset, IncludeHeader = TRUE) := FUNCTIONMACRO
  SingleStringLayout := {STRING single_string_field};
  InputLayout := RECORDOF(InputDataset);
  SingleStringLayout MakeSingleString(InputLayout L) := TRANSFORM
    SELF.single_string_field := #EXPAND(MAC_MakeCsvConcatValue(InputLayout));
  END;
  RETURN IF(IncludeHeader, MAC_MakeCsvHeaderList(InputLayout) + '\n', '')
        + UKServices_Utilities.ConcatenateStringFields(PROJECT(InputDataset
                                                            , MakeSingleString(LEFT)
                                                            )
                                                    , single_string_field, '\n');
ENDMACRO;


This works. Woo hoo! However when I tried to put it in to a repository and call it I get the error:
Unknown identifier "MAC_MakeCsvConcatValue"

Here's my test code for calling it:
Code: Select all
Layout := RECORD
  STRING string_field1;
  STRING string_field2;
  INTEGER integer_field;
END;

Ds := DATASET([{'a', 'z', 1}
              , {'b', 'y', 2}
              , {'c', 'x', 3}
              ], Layout);


// x := DatasetAsCsvString(Ds);
// y := DatasetAsCsvString(Ds, FALSE);
x := UKServices_Utilities.DatasetAsCsvString(Ds);
y := UKServices_Utilities.DatasetAsCsvString(Ds, FALSE);

x;
y;


Presumably this is because those aren't visible now they're in the repository. Presumably I could split the two MACROs out in to their own files in the repository but I don't really want to do that as they're so specific to DatasetAsCsvString. I tried putting them in to the FUNCTIONMACRO but couldn't get that to work, can anyone cleverer than me work out how to do this?
james.wilson
 
Posts: 20
Joined: Fri Nov 14, 2014 3:44 pm


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest