Send Deephaven Data by Email
You can send a report with Deephaven tables and charts by email using the reports plugin.
Caution
The query's host must be able to talk to the outgoing SMTP host or SMTP relay on your network.
If you want to use other libraries such as Javax Mail
or Apache Commons Mail (both of which are included in a Deephaven
installation), you can easily create an HTML representation of a Deephaven table using the TableTools.html
method.
StockTrades = db.i("LearnDeephaven", "StockTrades").where("Date = currentDateNy()", "USym in `AAPL`, `GOOG`").lastBy()
StockTradesHtml = html(StockTrades)
If you want to include a Deephaven chart in your email, the chart's image may show as empty or incomplete in the
email if you do not wait for the chart to finish saving. The following overload of save
will block the worker thread
until the chart is fully written to disk:
final String plotFilePath = "/tmp/StockPricePlot.png"
StockTrades = db.i("LearnDeephaven", "StockTrades").where("Date = currentDateNy()", "USym in `AAPL`, `GOOG`")
StockPricePlot = plotBy("Stock Prices", StockTrades, "Timestamp", "Last", "USym")
// Waits for the chart to finish being written to disk, with a timeout of 30 seconds
StockPricePlot.save(plotFilePath, true, 30)
The following query uses the Javax Mail package to send an email with inline tables and charts:
import javax.mail.Address
import javax.mail.Message
import javax.mail.Multipart
import javax.mail.Session
import javax.mail.Transport
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeBodyPart
import javax.mail.internet.MimeMessage
import javax.mail.internet.MimeMultipart
final String sender = "sender@deephaven.io"
final String[] recipients = ["recipient@deephaven.io"] as String[]
final String subject = "Awesome report"
final String plotFilePath = "/tmp/StockPricePlot.png"
StockTrades = db.i("LearnDeephaven", "StockTrades").where("Date = currentDateNy()", "USym in `AAPL`, `GOOG`")
StockPricePlot = plotBy("Stock Prices", StockTrades, "Timestamp", "Last", "USym")
// Waits for the chart to finish being written to disk, with a timeout of 30 seconds.
// The image in your email may be empty or incomplete if you do not wait for the chart to finish being saved
StockPricePlot.save(plotFilePath, true, 30)
tableAsHtml = html(StockTrades)
msg = "<p>Howdy! Below is my table</p>\n" + tableAsHtml + "\n<p> And here is the chart </p>\n<img src=\"cid:image1\" />"
Properties sessionProperties = System.getProperties()
sessionProperties.put("mail.smtp.host", "localhost")
Session session = Session.getDefaultInstance(sessionProperties, null)
Message message = new MimeMessage(session)
message.setFrom(sender==null ? null : new InternetAddress(sender))
message.setSubject(subject)
recipientsAddresses = []
for (int i=0; i<recipients.length; i++){
recipientsAddresses[i]=new InternetAddress(recipients[i])
}
message.addRecipients(Message.RecipientType.TO, recipientsAddresses as Address[])
// creates message part
MimeBodyPart messageBodyPart = new MimeBodyPart()
messageBodyPart.setContent(msg, "text/html")
// creates multi-part
Multipart multipart = new MimeMultipart()
multipart.addBodyPart(messageBodyPart)
// attach images as MimeBodyParts
MimeBodyPart imagePart = new MimeBodyPart()
imagePart.setHeader("Content-ID", "image1")
imagePart.setDisposition(MimeBodyPart.INLINE)
imagePart.attachFile(plotFilePath)
multipart.addBodyPart(imagePart)
message.setContent(multipart)
Transport.send(message)