use of com.serotonin.m2m2.reports.vo.ReportInstance in project ma-modules-public by infiniteautomation.
the class ReportWorkItem method queueReport.
public static void queueReport(String host, int port, ReportVO report) {
LOG.debug("Queuing report with id " + report.getId());
// Verify that the user is not disabled.
User user = UserDao.instance.getUser(report.getUserId());
if (user.isDisabled())
return;
// User is ok. Continue...
ReportWorkItem item = new ReportWorkItem(host, port);
// Create the database record in process.
item.reportConfig = report;
ReportInstance reportInstance = new ReportInstance(report);
item.user = user;
item.reportDao = ReportDao.instance;
item.reportDao.saveReportInstance(reportInstance);
// Start the report work item out of process.
item.reportInstance = reportInstance;
Common.backgroundProcessing.addWorkItem(item);
LOG.debug("Queued report with id " + report.getId() + ", instance id " + reportInstance.getId());
}
use of com.serotonin.m2m2.reports.vo.ReportInstance in project ma-modules-public by infiniteautomation.
the class ReportsDwr method deleteReportInstance.
@DwrPermission(custom = ReportPermissionDefinition.PERMISSION)
public List<ReportInstance> deleteReportInstance(int instanceId) {
User user = Common.getUser();
ReportDao reportDao = ReportDao.instance;
reportDao.deleteReportInstance(instanceId, user.getId());
return getReportInstances(user);
}
use of com.serotonin.m2m2.reports.vo.ReportInstance in project ma-modules-public by infiniteautomation.
the class ReportsDwr method getReportInstances.
/**
* Get report Instances and allow Admin users to get all report instances
* @param user
* @return
*/
private List<ReportInstance> getReportInstances(User user) {
// Allow Admin access to all report instances
List<ReportInstance> result;
if (Permissions.hasAdmin(user))
result = ReportDao.instance.getReportInstances();
else
result = ReportDao.instance.getReportInstances(user.getId());
Translations translations = getTranslations();
UserDao userDao = UserDao.instance;
for (ReportInstance i : result) {
i.setTranslations(translations);
User reportUser = userDao.getUser(i.getUserId());
if (reportUser != null)
i.setUsername(reportUser.getUsername());
else
i.setUsername(Common.translate("reports.validate.userDNE"));
}
return result;
}
use of com.serotonin.m2m2.reports.vo.ReportInstance in project ma-modules-public by infiniteautomation.
the class ReportExportBase method execute.
protected void execute(HttpServletRequest request, HttpServletResponse response, int content) throws IOException {
// Get the report instance id
int instanceId = Integer.parseInt(request.getParameter("instanceId"));
// Get the report instance
ReportDao reportDao = ReportDao.instance;
ReportInstance instance = reportDao.getReportInstance(instanceId);
// Ensure the user is allowed access.
ReportCommon.ensureReportInstancePermission(Common.getUser(request), instance);
// Stream the content.
response.setContentType("text/csv");
Translations translations = Common.getTranslations();
if (content == CONTENT_REPORT) {
ExportCsvStreamer creator = new ExportCsvStreamer(request.getServerName(), request.getLocalPort(), response.getWriter(), translations);
if (Common.databaseProxy.getNoSQLProxy() == null)
reportDao.reportInstanceDataSQL(instanceId, creator);
else
reportDao.reportInstanceDataNoSQL(instanceId, creator);
} else if (content == CONTENT_EVENTS)
new EventCsvStreamer(response.getWriter(), reportDao.getReportInstanceEvents(instanceId), translations);
else if (content == CONTENT_COMMENTS)
new UserCommentCsvStreamer(response.getWriter(), reportDao.getReportInstanceUserComments(instanceId), translations);
}
use of com.serotonin.m2m2.reports.vo.ReportInstance in project ma-modules-public by infiniteautomation.
the class ReportDao method runReportSQL.
/**
* SQL Database Report
* @param instance
* @param points
* @return
*/
public int runReportSQL(final ReportInstance instance, List<PointInfo> points) {
PointValueDao pointValueDao = Common.databaseProxy.newPointValueDao();
int count = 0;
// The timestamp selection code is used multiple times for different tables
String timestampSql;
Object[] timestampParams;
if (instance.isFromInception() && instance.isToNow()) {
timestampSql = "";
timestampParams = new Object[0];
} else if (instance.isFromInception()) {
timestampSql = "and ${field}<?";
timestampParams = new Object[] { instance.getReportEndTime() };
} else if (instance.isToNow()) {
timestampSql = "and ${field}>=?";
timestampParams = new Object[] { instance.getReportStartTime() };
} else {
timestampSql = "and ${field}>=? and ${field}<?";
timestampParams = new Object[] { instance.getReportStartTime(), instance.getReportEndTime() };
}
// For each point.
for (PointInfo pointInfo : points) {
DataPointVO point = pointInfo.getPoint();
int dataType = point.getPointLocator().getDataTypeId();
DataValue startValue = null;
if (!instance.isFromInception()) {
// Get the value just before the start of the report
PointValueTime pvt = pointValueDao.getPointValueBefore(point.getId(), instance.getReportStartTime());
if (pvt != null)
startValue = pvt.getValue();
// Make sure the data types match
if (DataTypes.getDataType(startValue) != dataType)
startValue = null;
}
// Insert the reportInstancePoints record
String name = Functions.truncate(point.getName(), 100);
int reportPointId = doInsert(REPORT_INSTANCE_POINTS_INSERT, new Object[] { instance.getId(), point.getDeviceName(), name, pointInfo.getPoint().getXid(), dataType, DataTypes.valueToString(startValue), SerializationHelper.writeObject(point.getTextRenderer()), pointInfo.getColour(), pointInfo.getWeight(), boolToChar(pointInfo.isConsolidatedChart()), pointInfo.getPlotType() }, new int[] { Types.INTEGER, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.INTEGER, Types.VARCHAR, Types.BLOB, Types.VARCHAR, Types.FLOAT, Types.CHAR, Types.INTEGER });
// Insert the reportInstanceData records
String insertSQL = //
"insert into reportInstanceData " + " select id, " + reportPointId + //
", pointValue, ts from pointValues " + //
" where dataPointId=? and dataType=? " + StringUtils.replaceMacro(timestampSql, "field", "ts");
count += ejt.update(insertSQL, appendParameters(timestampParams, point.getId(), dataType));
// Insert the reportInstanceDataAnnotations records
ejt.update(//
"insert into reportInstanceDataAnnotations " + //
" (pointValueId, reportInstancePointId, textPointValueShort, textPointValueLong, sourceMessage) " + //
" select rd.pointValueId, rd.reportInstancePointId, pva.textPointValueShort, " + //
" pva.textPointValueLong, pva.sourceMessage " + //
" from reportInstanceData rd " + //
" join reportInstancePoints rp on rd.reportInstancePointId = rp.id " + //
" join pointValueAnnotations pva on rd.pointValueId = pva.pointValueId " + " where rp.id = ?", new Object[] { reportPointId });
// Insert the reportInstanceEvents records for the point.
if (instance.getIncludeEvents() != ReportVO.EVENTS_NONE) {
String eventSQL = //
"insert into reportInstanceEvents " + //
" (eventId, reportInstanceId, typeName, subtypeName, typeRef1, typeRef2, activeTs, " + //
" rtnApplicable, rtnTs, rtnCause, alarmLevel, message, ackTs, ackUsername, " + //
" alternateAckSource)" + " select e.id, " + instance.getId() + //
", e.typeName, e.subtypeName, e.typeRef1, " + //
" e.typeRef2, e.activeTs, e.rtnApplicable, e.rtnTs, e.rtnCause, e.alarmLevel, " + //
" e.message, e.ackTs, u.username, e.alternateAckSource " + //
" from events e join userEvents ue on ue.eventId=e.id " + //
" left join users u on e.ackUserId=u.id " + //
" where ue.userId=? " + //
" and e.typeName=? " + " and e.typeRef1=? ";
if (instance.getIncludeEvents() == ReportVO.EVENTS_ALARMS)
eventSQL += "and e.alarmLevel > 0 ";
eventSQL += StringUtils.replaceMacro(timestampSql, "field", "e.activeTs");
ejt.update(eventSQL, appendParameters(timestampParams, instance.getUserId(), EventType.EventTypeNames.DATA_POINT, point.getId()));
}
// Insert the reportInstanceUserComments records for the point.
if (instance.isIncludeUserComments()) {
String commentSQL = //
"insert into reportInstanceUserComments " + //
" (reportInstanceId, username, commentType, typeKey, ts, commentText)" + " select " + instance.getId() + ", u.username, " + UserCommentVO.TYPE_POINT + //
", " + reportPointId + //
", uc.ts, uc.commentText " + //
" from userComments uc " + //
" left join users u on uc.userId=u.id " + " where uc.commentType=" + //
UserCommentVO.TYPE_POINT + " and uc.typeKey=? ";
// Only include comments made in the duration of the report.
commentSQL += StringUtils.replaceMacro(timestampSql, "field", "uc.ts");
ejt.update(commentSQL, appendParameters(timestampParams, point.getId()));
}
}
// Insert the reportInstanceUserComments records for the selected events
if (instance.isIncludeUserComments()) {
String commentSQL = //
"insert into reportInstanceUserComments " + //
" (reportInstanceId, username, commentType, typeKey, ts, commentText)" + " select " + instance.getId() + ", u.username, " + UserCommentVO.TYPE_EVENT + //
", uc.typeKey, " + //
" uc.ts, uc.commentText " + //
" from userComments uc " + //
" left join users u on uc.userId=u.id " + //
" join reportInstanceEvents re on re.eventId=uc.typeKey " + " where uc.commentType=" + //
UserCommentVO.TYPE_EVENT + " and re.reportInstanceId=? ";
ejt.update(commentSQL, new Object[] { instance.getId() });
}
// If the report had undefined start or end times, update them with values from the data.
if (instance.isFromInception() || instance.isToNow()) {
ejt.query(//
"select min(rd.ts), max(rd.ts) " + "from reportInstancePoints rp " + " join reportInstanceData rd on rp.id=rd.reportInstancePointId " + "where rp.reportInstanceId=?", new Object[] { instance.getId() }, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException {
if (instance.isFromInception())
instance.setReportStartTime(rs.getLong(1));
if (instance.isToNow())
instance.setReportEndTime(rs.getLong(2));
}
});
}
return count;
}
Aggregations