use of com.serotonin.m2m2.reports.ReportDao in project ma-modules-public by infiniteautomation.
the class Upgrade1 method upgrade.
@Override
protected void upgrade() throws Exception {
runScript(new String[] { "alter table reports add xid varchar(50);" });
// Now generate XIDs for all entries in the table
ReportDao dao = ReportDao.instance;
List<ReportVO> reports = dao.getReports();
for (ReportVO report : reports) {
report.setXid(dao.generateUniqueXid());
dao.saveReport(report);
}
// Alter the column back to have no default
Map<String, String[]> scripts = new HashMap<String, String[]>();
scripts.put(DatabaseProxy.DatabaseType.DERBY.name(), new String[] { "alter table reports alter column xid NOT NULL;" });
scripts.put(DatabaseProxy.DatabaseType.MYSQL.name(), new String[] { "ALTER TABLE reports CHANGE COLUMN `xid` `xid` VARCHAR(50) NOT NULL;" });
scripts.put(DatabaseProxy.DatabaseType.MSSQL.name(), new String[] { "alter table reports alter column xid varchar(50) NOT NULL;" });
scripts.put(DatabaseProxy.DatabaseType.H2.name(), new String[] { "alter table reports alter column xid varchar(50) NOT NULL;" });
runScript(scripts);
}
use of com.serotonin.m2m2.reports.ReportDao in project ma-modules-public by infiniteautomation.
the class ReportChartCreator method createContent.
/**
* Uses the given parameters to create the data for the fields of this class. Once the content has been created the
* getters for the fields can be used to retrieve.
*
* @param host - Mango's hostname
* @param port - Mango's port
* @param reportInstance
* @param reportDao
* @param inlinePrefix
* if this is non-null, it implies that the content should be inline.
* @param createExportFile
*/
public void createContent(String host, int port, ReportInstance reportInstance, ReportDao reportDao, String inlinePrefix, boolean createExportFile) {
this.inlinePrefix = inlinePrefix;
reportInstance.setTranslations(translations);
// Use a stream handler to get the report data from the database.
StreamHandler handler = new StreamHandler(host, port, reportInstance.getXidMap(), reportInstance.getReportStartTime(), reportInstance.getReportEndTime(), IMAGE_WIDTH, createExportFile, translations);
// Process the report content with the handler.
if (Common.databaseProxy.getNoSQLProxy() == null)
reportDao.reportInstanceDataSQL(reportInstance.getId(), handler);
else
reportDao.reportInstanceDataNoSQL(reportInstance.getId(), handler);
pointStatistics = handler.getPointStatistics();
devices = handler.getDevices();
pointMap = handler.getStatisticsMap();
UsedImagesDirective inlineImages = new UsedImagesDirective();
SubjectDirective subjectDirective = new SubjectDirective(translations);
// Prepare the model for the content rendering.
Map<String, Object> model = new HashMap<String, Object>();
model.put("fmt", new MessageFormatDirective(translations));
model.put("subject", subjectDirective);
model.put("img", inlineImages);
model.put("instance", reportInstance);
model.put("timezone", timeZone.getID());
model.put("points", pointStatistics);
model.put("inline", inlinePrefix == null ? "" : "cid:");
model.put("devices", devices);
model.put("mapped", pointMap);
model.put("ALPHANUMERIC", DataTypes.ALPHANUMERIC);
model.put("BINARY", DataTypes.BINARY);
model.put("MULTISTATE", DataTypes.MULTISTATE);
model.put("NUMERIC", DataTypes.NUMERIC);
model.put("IMAGE", DataTypes.IMAGE);
// Create the individual point charts
for (PointStatistics pointStat : pointStatistics) {
PointTimeSeriesCollection ptsc = new PointTimeSeriesCollection(timeZone);
if (pointStat.getNumericTimeSeries() != null)
ptsc.addNumericTimeSeries(pointStat.getNumericTimeSeries());
else if (pointStat.getDiscreteTimeSeries() != null)
ptsc.addDiscreteTimeSeries(pointStat.getDiscreteTimeSeries());
if (ptsc.hasData()) {
if (inlinePrefix != null)
model.put("chartName", inlinePrefix + pointStat.getChartName());
pointStat.setImageData(ImageChartUtils.getChartData(ptsc, POINT_IMAGE_WIDTH, POINT_IMAGE_HEIGHT, reportInstance.getReportStartTime(), reportInstance.getReportEndTime()));
}
// in the report I'll add it here while we are already iterating over the points that are included in the report
if (pointStat.getDataType() == DataTypes.IMAGE) {
ValueChangeCounter pointStatisticsGenerator = (ValueChangeCounter) pointStat.getStats();
ImageValue img = (ImageValue) (pointStatisticsGenerator.getLastValue());
if (img != null) {
try {
pointStat.setImageData(img.getImageData());
if (inlinePrefix != null)
model.put("chartName", inlinePrefix + pointStat.getChartName());
else {
// serve up the image using the reportImageChart servlet instead of the imageValueServlet that is used on flipbook page
// The path comes from the servlet path definition in web.xml.
model.put("chartName", IMAGE_SERVLET + pointStat.getChartName());
}
} catch (IOException e) {
LOG.error("failed to retrieve image data", e);
}
}
}
}
// consolidated chart
PointTimeSeriesCollection ptsc = handler.getPointTimeSeriesCollection();
if (ptsc.hasData()) {
if (inlinePrefix != null)
model.put("chartName", inlinePrefix + IMAGE_CONTENT_ID);
else {
chartName = "r" + reportInstance.getId() + ".png";
// The path comes from the servlet path definition in web.xml.
model.put("chartName", IMAGE_SERVLET + chartName);
}
imageData = ImageChartUtils.getChartData(ptsc, true, IMAGE_WIDTH, IMAGE_HEIGHT, reportInstance.getReportStartTime(), reportInstance.getReportEndTime());
}
List<EventInstance> events = null;
if (reportInstance.getIncludeEvents() != ReportVO.EVENTS_NONE) {
events = reportDao.getReportInstanceEvents(reportInstance.getId());
model.put("includeEvents", true);
model.put("events", events);
} else
model.put("includeEvents", false);
List<ReportUserComment> comments = null;
if (reportInstance.isIncludeUserComments()) {
comments = reportDao.getReportInstanceUserComments(reportInstance.getId());
// Only provide the list of point comments to the report. The event comments have already be correlated
// into the events list.
List<ReportUserComment> pointComments = new ArrayList<ReportUserComment>();
for (ReportUserComment c : comments) {
if (c.getCommentType() == UserCommentVO.TYPE_POINT)
pointComments.add(c);
}
model.put("includeUserComments", true);
model.put("userComments", pointComments);
} else
model.put("includeUserComments", false);
// Create the template.
Template ftl;
StringWriter writer = new StringWriter();
FileReader reader = null;
try {
File templateFile = ReportCommon.instance.getTemplateFile(reportInstance.getTemplateFile());
reader = new FileReader(templateFile);
ftl = new Template(reportInstance.getName(), reader, Common.freemarkerConfiguration);
ftl.process(model, writer);
} catch (FileNotFoundException e) {
LOG.error("Unable to find report template file: " + reportInstance.getName());
} catch (IOException e) {
// Couldn't load the template?
throw new ShouldNeverHappenException(e);
} catch (Exception e) {
// Error processing the FTL?
throw new ShouldNeverHappenException(e);
} finally {
if (reader != null)
try {
reader.close();
} catch (IOException e) {
LOG.error("Error closing template file reader: " + e.getMessage(), e);
}
}
// Save the content
html = writer.toString();
subject = subjectDirective.getSubject();
inlineImageList = inlineImages.getImageList();
// Save the export file (if any)
exportFile = handler.exportFile;
if (createExportFile && events != null) {
try {
eventFile = File.createTempFile("tempEventCSV", ".csv");
new EventCsvStreamer(new PrintWriter(new FileWriter(eventFile)), events, translations);
} catch (IOException e) {
LOG.error("Failed to create temp event file", e);
}
}
if (createExportFile && comments != null) {
try {
commentFile = File.createTempFile("tempCommentCSV", ".csv");
new UserCommentCsvStreamer(new PrintWriter(new FileWriter(commentFile)), comments, translations);
} catch (IOException e) {
LOG.error("Failed to create temp comment file", e);
}
}
}
use of com.serotonin.m2m2.reports.ReportDao in project ma-modules-public by infiniteautomation.
the class ReportWorkItem method execute.
@Override
public void execute() {
try {
ReportLicenseChecker.checkLicense();
} catch (LicenseViolatedException e) {
LOG.error("Your core license doesn't permit you to use the reports module.");
reportInstance.setReportStartTime(Common.timer.currentTimeMillis());
reportInstance.setReportEndTime(Common.timer.currentTimeMillis());
reportInstance.setRecordCount(-1);
reportDao.saveReportInstance(reportInstance);
return;
}
LOG.debug("Running report with id " + reportConfig.getId() + ", instance id " + reportInstance.getId());
reportInstance.setRunStartTime(System.currentTimeMillis());
reportDao.saveReportInstance(reportInstance);
Translations translations = Common.getTranslations();
// Create a list of DataPointVOs to which the user has permission.
DataPointDao dataPointDao = DataPointDao.instance;
List<ReportDao.PointInfo> points = new ArrayList<ReportDao.PointInfo>(reportConfig.getPoints().size());
for (ReportPointVO reportPoint : reportConfig.getPoints()) {
DataPointVO point = dataPointDao.getDataPoint(reportPoint.getPointId());
if (point != null && Permissions.hasDataPointReadPermission(user, point)) {
String colour = null;
try {
if (!StringUtils.isBlank(reportPoint.getColour()))
colour = ColorUtils.toHexString(reportPoint.getColour()).substring(1);
} catch (InvalidArgumentException e) {
// Should never happen since the colour would have been validated on save, so just let it go
// as null.
}
points.add(new ReportDao.PointInfo(point, colour, reportPoint.getWeight(), reportPoint.isConsolidatedChart(), reportPoint.getPlotType()));
}
}
int recordCount = 0;
try {
if (!points.isEmpty()) {
if (Common.databaseProxy.getNoSQLProxy() == null)
recordCount = reportDao.runReportSQL(reportInstance, points);
else
recordCount = reportDao.runReportNoSQL(reportInstance, points);
}
} catch (RuntimeException e) {
recordCount = -1;
throw e;
} catch (Throwable e) {
recordCount = -1;
throw new RuntimeException("Report instance failed", e);
} finally {
reportInstance.setRunEndTime(System.currentTimeMillis());
reportInstance.setRecordCount(recordCount);
reportDao.saveReportInstance(reportInstance);
}
if (reportConfig.isEmail()) {
String inlinePrefix = "R" + System.currentTimeMillis() + "-" + reportInstance.getId() + "-";
// TODO should we create different instances of the email based upon language and timezone?
// We are creating an email from the result. Create the content.
final ReportChartCreator creator = new ReportChartCreator(translations, TimeZone.getDefault());
creator.createContent(host, port, reportInstance, reportDao, inlinePrefix, reportConfig.isIncludeData());
// Create the to list
Set<String> addresses = MailingListDao.instance.getRecipientAddresses(reportConfig.getRecipients(), new DateTime(reportInstance.getReportStartTime()));
String[] toAddrs = addresses.toArray(new String[0]);
// Create the email content object.
EmailContent emailContent = new EmailContent(null, creator.getHtml(), Common.UTF8);
// Add the consolidated chart
if (creator.getImageData() != null)
emailContent.addInline(new EmailInline.ByteArrayInline(inlinePrefix + ReportChartCreator.IMAGE_CONTENT_ID, creator.getImageData(), ImageChartUtils.getContentType()));
// Add the point charts
for (PointStatistics pointStatistics : creator.getPointStatistics()) {
if (pointStatistics.getImageData() != null)
emailContent.addInline(new EmailInline.ByteArrayInline(inlinePrefix + pointStatistics.getChartName(), pointStatistics.getImageData(), ImageChartUtils.getContentType()));
}
// Add optional images used by the template.
for (String s : creator.getInlineImageList()) addImage(emailContent, s);
// Check if we need to attach the data.
if (reportConfig.isIncludeData()) {
addFileAttachment(emailContent, reportInstance.getName() + ".csv", creator.getExportFile());
addFileAttachment(emailContent, reportInstance.getName() + "Events.csv", creator.getEventFile());
addFileAttachment(emailContent, reportInstance.getName() + "Comments.csv", creator.getCommentFile());
}
PostEmailRunnable[] postEmail = null;
if (reportConfig.isIncludeData()) {
// See that the temp file(s) gets deleted after the email is sent.
PostEmailRunnable deleteTempFile = new PostEmailRunnable() {
@Override
public void run() {
for (File file : filesToDelete) {
if (!file.delete())
LOG.warn("Temp file " + file.getPath() + " not deleted");
}
}
};
postEmail = new PostEmailRunnable[] { deleteTempFile };
}
try {
TranslatableMessage lm = new TranslatableMessage("ftl.scheduledReport", reportConfig.getName());
String subject = creator.getSubject();
if (subject == null)
subject = lm.translate(translations);
EmailWorkItem.queueEmail(toAddrs, subject, emailContent, postEmail);
} catch (AddressException e) {
LOG.error(e);
}
if (reportConfig.isSchedule()) {
// Delete the report instance.
reportDao.deleteReportInstance(reportInstance.getId(), user.getId());
}
}
LOG.debug("Finished running report with id " + reportConfig.getId() + ", instance id " + reportInstance.getId());
}
use of com.serotonin.m2m2.reports.ReportDao in project ma-modules-public by infiniteautomation.
the class ReportsDwr method init.
@DwrPermission(custom = ReportPermissionDefinition.PERMISSION)
public ProcessResult init() {
ProcessResult response = new ProcessResult();
ReportDao reportDao = ReportDao.instance;
User user = Common.getUser();
response.addData("points", getReadablePoints());
response.addData("mailingLists", MailingListDao.instance.getMailingLists());
if (Permissions.hasAdmin(user)) {
response.addData("users", UserDao.instance.getUsers());
response.addData("reports", reportDao.getReports());
response.addData("instances", getReportInstances(user));
} else {
response.addData("reports", reportDao.getReports(user.getId()));
response.addData("instances", getReportInstances(user));
// Filter User's available to Email on User Permissions
List<User> users = UserDao.instance.getUsers();
List<User> availableForEmail = new ArrayList<User>();
String currentUserPermissions = user.getPermissions();
for (User u : users) {
// Check to see if there are any overlapping privs, if so then add them to the view
Set<String> permissions = Permissions.findMatchingPermissions(u.getPermissions(), currentUserPermissions);
if (permissions.size() > 0)
availableForEmail.add(u);
}
response.addData("users", availableForEmail);
}
response.addData("templates", getTemplateList());
return response;
}
use of com.serotonin.m2m2.reports.ReportDao in project ma-modules-public by infiniteautomation.
the class ReportLaunchServlet method doGet.
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
User user = Common.getUser(request);
if (user != null) {
ReportDao dao = ReportDao.instance;
ReportVO report = null;
int id = getIntRequestParameter(request, "reportId", -1);
if (id != -1)
report = dao.getReport(id);
String xid = request.getParameter("reportXid");
if (xid != null)
report = dao.getReport(xid);
if (report != null && (user.getId() == report.getUserId() || Permissions.hasAdmin(user))) {
ReportJob.scheduleReportJob(request.getServerName(), request.getLocalPort(), report);
try {
response.getWriter().write("Report " + report.getName() + " scheduled");
} catch (Exception e) {
response.setStatus(500);
}
}
}
}
Aggregations