Search in sources :

Example 1 with ReportInstance

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());
}
Also used : User(com.serotonin.m2m2.vo.User) ReportInstance(com.serotonin.m2m2.reports.vo.ReportInstance)

Example 2 with ReportInstance

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);
}
Also used : User(com.serotonin.m2m2.vo.User) ReportDao(com.serotonin.m2m2.reports.ReportDao) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 3 with ReportInstance

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;
}
Also used : User(com.serotonin.m2m2.vo.User) UserDao(com.serotonin.m2m2.db.dao.UserDao) ReportInstance(com.serotonin.m2m2.reports.vo.ReportInstance) Translations(com.serotonin.m2m2.i18n.Translations)

Example 4 with ReportInstance

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);
}
Also used : UserCommentCsvStreamer(com.serotonin.m2m2.reports.web.UserCommentCsvStreamer) EventCsvStreamer(com.serotonin.m2m2.vo.export.EventCsvStreamer) ExportCsvStreamer(com.serotonin.m2m2.vo.export.ExportCsvStreamer) ReportDao(com.serotonin.m2m2.reports.ReportDao) ReportInstance(com.serotonin.m2m2.reports.vo.ReportInstance) Translations(com.serotonin.m2m2.i18n.Translations)

Example 5 with ReportInstance

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;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) ExportDataValue(com.serotonin.m2m2.vo.export.ExportDataValue) SQLException(java.sql.SQLException) ExportPointInfo(com.serotonin.m2m2.vo.export.ExportPointInfo) PointValueDao(com.serotonin.m2m2.db.dao.PointValueDao) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) ResultSet(java.sql.ResultSet) RowCallbackHandler(org.springframework.jdbc.core.RowCallbackHandler)

Aggregations

ReportInstance (com.serotonin.m2m2.reports.vo.ReportInstance)6 User (com.serotonin.m2m2.vo.User)5 ReportDao (com.serotonin.m2m2.reports.ReportDao)4 HashMap (java.util.HashMap)4 Translations (com.serotonin.m2m2.i18n.Translations)3 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)3 ExportPointInfo (com.serotonin.m2m2.vo.export.ExportPointInfo)3 ArrayList (java.util.ArrayList)3 InvalidArgumentException (com.serotonin.InvalidArgumentException)2 PointValueDao (com.serotonin.m2m2.db.dao.PointValueDao)2 UserDao (com.serotonin.m2m2.db.dao.UserDao)2 NoSQLDao (com.serotonin.m2m2.db.dao.nosql.NoSQLDao)2 AnnotatedPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)2 IdPointValueTime (com.serotonin.m2m2.rt.dataImage.IdPointValueTime)2 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)2 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)2 EventCsvStreamer (com.serotonin.m2m2.vo.export.EventCsvStreamer)2 ExportDataValue (com.serotonin.m2m2.vo.export.ExportDataValue)2 File (java.io.File)2 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)1