Question

Add zip file size to data download notication email

  • 24 August 2015
  • 4 replies
  • 2 views

Badge
Hi all

 

I'm trying to add the zip file size to the data download service notification email.  This is the method I'm using: 
  • Workspace writes the data to be downloaded and a json file containing the information elements I will use in the email.
  • Python shutdown script gets the zip file size from the zip file and appends a 'zip_size' element to the json (this doesn't work)
  • workspace published as a data download service with its own success topic and publisher.  The publisher is set to access the info in the modified json file (set as 'Data to Post') when workspace published (this doesn't work)
 I'm not sure if this is possible.  Can anyone suggest any alternatives or ideas?

 

This is my python shutdown script 
 import os import json import fme def sizeof_fmt(num, suffix='B'):     for unit in ['','K','M','G','T','P','E','Z']:         print "unit: {0}    number: {1}".format(unit, num)         if abs(num) < 1024.0:             return "{number:.{digits}f} {u}{suff}".format(number=num, digits=1, u=unit, suff=suffix)         num /= 1024.0     return "{number:.{digits}f} {u}{suff}".format(number=num, digits=1, u="Y", suff=suffix) logger = open(fme.logFileName,'a') # get required FME parameter values zipDir = fme.macroValues['FME_SERVER_DEST_DIR'] logger.write("Zip Directory: {0}".format(zipDir)) print "Zip Directory: {0}".format(zipDir) successJSON = fme.macroValues['success_json'] logger.write("Success Json File: {0}".format(successJSON)) print "Success Json File: {0}".format(successJSON) # get the data download zip file size osSize = sizeof_fmt(os.path.getsize(zipDir)) logger.write("Zip file size: {0}".format(osSize)) print "Zip file size: {0}".format(osSize) # modify the Success JSON with open(successJSON, 'r+') as outfile:     data = json.load(outfile)     # add ZIP_SIZE value     data['zip_size'] = osSize     outfile.seek(0) # reset file position to the beginning     logger.write("Success JSON Content: {0}".format(data))     print data     json.dump(data, outfile, indent=4, sort_keys=True)     logger.write("Update to success JSON done")     print "Update to success JSON done" 
 

 

Note:

 

This is the 'success_json' parameter value from the job log file

 

`--success_json' `$(FME_MF_DIR)SuccessJSON\successJson.txt'

 

This is also from the log file

 

`-TEXTLINE_3_DATASET' `/data/fmeserver/DefaultResults/FME_252F7068_1440392215399_29199_nw/successJson.txt'

 

Is TEXTLINE_3_DATASET a parameter I can access in the shutdown script?  Maybe I'm looking for the output text file in the wrong place.

 

 

Thanks

 

Rob

4 replies

Userlevel 4
Hi

 

 

It's an interesting approach you're trying out. I haven't tried it out, but I suspect, as you're indicating, that maybe you're modifying the wrong json file. The macro FME_MF_DIR refers to the directory where your workspace file is located, which on FME Server would be in the repository. It does seem more plausible that the json data is stored separately for each execution, and not in the repository directory where several parallell runs might fight for the same file.

 

 

You could try modifiying the line:

 

 

successJSON = fme.macroValues['success_json']

 

 

as follows:

 

 

successJSON = fme.macroValues['TEXTLINE_3_DATASET']

 

 

It'd be very interesting to hear if you can make it work!

 

 

David
Badge
Hi all,

 

Here's the solution I've come up with in case anyone wants to do something similar.

 

Firstly I went through the python shutdown script line by line and had to change a couple things.  Firstly, FME had a problem with the function I had defined in my original script.  Not sure why but I took it out and added it to the main body of the script.  The second change was to my successJSON variable value.  It had to be defined as

 

successJSON = os.path.join(zipDir[:-4] + "_nw", fme.macroValues['success_json'])

 

where zipDir = fme.macroValues['FME_SERVER_DEST_DIR']

 

and

 

fme.macroValues['success_json'] is the name of the text file (in my case "Success_JSON.txt"

 

Here's the final script: 
 # -*- coding: utf-8 -*- import fme import os import json # only run if translation was successfull if fme.status: # fme.status true if job ran successfully          logger = open(fme.logFileName,'a')          logger.write("\nimported all required modules\n")              # get required FME parameter value     zipDir = fme.macroValues['FME_SERVER_DEST_DIR']     logger.write("Zip Directory: {0}\n".format(zipDir))          # get the data download zip file size     suffix = "B"     numBytes = os.path.getsize(zipDir)     # get units     for unit in ['','K','M','G','T','P','E','Z']:         print "unit: {0}    number: {1}".format(unit, numBytes)         if abs(numBytes) < 1024.0:             osSize = "{number:.{digits}f} {u}{suff}".format(number=numBytes, digits=1, u=unit, suff=suffix)             break         else:             numBytes /= 1024.0     try:         osSize     except NameError:         osSize = "{number:.{digits}f} {u}{suff}".format(number=numBytes, digits=1, u="Y", suff=suffix)     logger.write("Zip file size: {0}\n".format(osSize))          # get the Success JSON FME parameter value     successJSON = os.path.join(zipDir[:-4] + "_nw", fme.macroValues['success_json'])     logger.write("Success Json File: {0}\n".format(successJSON))          # add zip file size to the success json     with open(successJSON, 'r+') as outfile:         data = json.load(outfile)         # add ZIP_SIZE value         data['zip_size'] = osSize         outfile.seek(0) # reset file position to the beginning         logger.write("Success JSON Content: {0}\n".format(data))         json.dump(data, outfile, indent=4, sort_keys=True)         logger.write("Update to success JSON done\n")          logger.write("All done")     logger.close()
 The script fetches the size of the output zip file and adds it to the success json notification.  When publishing the workspace as a Data Download Service, the appropriate success topic is selected and the text file writer (the one that writes the success json content) is chosen as "Data to Post".

 

This allows you to use any elements in the Success Json that was written + any of the standard email elements available through the Data Download Service (see http://docs.safe.com/fme/2015/html/FME_Server_Documentation/Default.htm#ServerCore/EmailTemplateLanguage.htm).

 

You can then build an email message in your Subscription with the zip file download size like this:

 

<fmeblock type="optional">

 

<b>WARNING:</b> The zip file is approximately <b>{zip_size}</b>.

 

Click the link to download the result:

 

{urlPrefix}{ResultRootDir}/{@getFileName(@zip(OutputLocation))}

 

</fmeblock>

 

Hope this helps,

 

Rob

 

 
Userlevel 4
Hi

 

 

Excellent, thanks for sharing :-)

 

 

David

Hi,

I tried to implement your solution and all went well except that the {zip_size} variable doesn't seem to be available in the email template, although I published the workspace as Data Download service with the appropriate success topic and the text file writer as "Data to Post". I am using FME 2016. Any hint?

Thank you in advance rkay

Reply