Search in sources :

Example 1 with NoSQLDao

use of com.serotonin.m2m2.db.dao.nosql.NoSQLDao in project ma-modules-public by infiniteautomation.

the class ReportDao method runReportNoSQL.

/**
 * Generate a report using the NoSQL DB for point value storage
 * @param instance
 * @param points
 * @return
 */
public int runReportNoSQL(final ReportInstance instance, List<PointInfo> points) {
    PointValueDao pointValueDao = Common.databaseProxy.newPointValueDao();
    final MappedCallbackCounter count = new MappedCallbackCounter();
    final NoSQLDao dao = Common.databaseProxy.getNoSQLProxy().createNoSQLDao(ReportPointValueTimeSerializer.get(), "reports");
    // The timestamp selection code is used multiple times for different tables
    long startTime, endTime;
    String timestampSql;
    Object[] timestampParams;
    if (instance.isFromInception() && instance.isToNow()) {
        timestampSql = "";
        timestampParams = new Object[0];
        startTime = 0l;
        endTime = Common.timer.currentTimeMillis();
    } else if (instance.isFromInception()) {
        timestampSql = "and ${field}<?";
        timestampParams = new Object[] { instance.getReportEndTime() };
        startTime = 0l;
        endTime = instance.getReportEndTime();
    } else if (instance.isToNow()) {
        timestampSql = "and ${field}>=?";
        timestampParams = new Object[] { instance.getReportStartTime() };
        startTime = instance.getReportStartTime();
        endTime = Common.timer.currentTimeMillis();
    } else {
        timestampSql = "and ${field}>=? and ${field}<?";
        timestampParams = new Object[] { instance.getReportStartTime(), instance.getReportEndTime() };
        startTime = instance.getReportStartTime();
        endTime = instance.getReportEndTime();
    }
    // For each point.
    List<Integer> pointIds = new ArrayList<Integer>();
    // Map the pointId to the Report PointId
    final Map<Integer, Integer> pointIdMap = new HashMap<Integer, Integer>();
    // the reports table/data store
    for (PointInfo pointInfo : points) {
        DataPointVO point = pointInfo.getPoint();
        pointIds.add(point.getId());
        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 });
        // Keep the info in the map
        pointIdMap.put(pointInfo.getPoint().getId(), reportPointId);
        // 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()));
        }
    }
    // end for all points
    // Insert the data into the NoSQL DB and track first/last times
    // The series name is reportInstanceId_reportPointId
    final AtomicLong firstPointTime = new AtomicLong(Long.MAX_VALUE);
    final AtomicLong lastPointTime = new AtomicLong(-1l);
    final String reportId = Integer.toString(instance.getId()) + "_";
    pointValueDao.getPointValuesBetween(pointIds, startTime, endTime, new MappedRowCallback<IdPointValueTime>() {

        @Override
        public void row(final IdPointValueTime ipvt, int rowId) {
            dao.storeData(reportId + Integer.toString(pointIdMap.get(ipvt.getId())), ipvt);
            count.increment();
            if (ipvt.getTime() < firstPointTime.get())
                firstPointTime.set(ipvt.getTime());
            if (ipvt.getTime() > lastPointTime.get())
                lastPointTime.set(ipvt.getTime());
        }
    });
    // 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()) {
        if (instance.isFromInception()) {
            if (firstPointTime.get() != Long.MAX_VALUE)
                instance.setReportStartTime(firstPointTime.get());
        }
        if (instance.isToNow()) {
            instance.setReportEndTime(lastPointTime.get());
        }
    }
    return count.getCount();
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) ExportDataValue(com.serotonin.m2m2.vo.export.ExportDataValue) ArrayList(java.util.ArrayList) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) NoSQLDao(com.serotonin.m2m2.db.dao.nosql.NoSQLDao) ExportPointInfo(com.serotonin.m2m2.vo.export.ExportPointInfo) PointValueDao(com.serotonin.m2m2.db.dao.PointValueDao) AtomicLong(java.util.concurrent.atomic.AtomicLong) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime)

Example 2 with NoSQLDao

use of com.serotonin.m2m2.db.dao.nosql.NoSQLDao in project ma-modules-public by infiniteautomation.

the class ReportDao method reportInstanceDataNoSQL.

public void reportInstanceDataNoSQL(int instanceId, final ExportDataStreamHandler handler) {
    // Retrieve point information.
    List<ExportPointInfo> pointInfos = query(REPORT_INSTANCE_POINT_SELECT + "where reportInstanceId=?", new Object[] { instanceId }, new RowMapper<ExportPointInfo>() {

        @Override
        public ExportPointInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
            int i = 0;
            ExportPointInfo rp = new ExportPointInfo();
            rp.setReportPointId(rs.getInt(++i));
            rp.setDeviceName(rs.getString(++i));
            rp.setPointName(rs.getString(++i));
            rp.setXid(rs.getString(++i));
            rp.setDataType(rs.getInt(++i));
            String startValue = rs.getString(++i);
            if (startValue != null)
                rp.setStartValue(DataValue.stringToValue(startValue, rp.getDataType()));
            rp.setTextRenderer((TextRenderer) SerializationHelper.readObjectInContext(rs.getBlob(++i).getBinaryStream()));
            rp.setColour(rs.getString(++i));
            rp.setWeight(rs.getFloat(++i));
            rp.setConsolidatedChart(charToBool(rs.getString(++i)));
            rp.setPlotType(rs.getInt(++i));
            return rp;
        }
    });
    final ExportDataValue edv = new ExportDataValue();
    for (final ExportPointInfo point : pointInfos) {
        if (point.getDataType() == DataTypes.IMAGE) {
            DataPointVO vo = DataPointDao.instance.getByXid(point.getXid());
            if (vo != null)
                point.setDataPointId(vo.getId());
            else
                point.setDataPointId(-1);
        }
        handler.startPoint(point);
        edv.setReportPointId(point.getReportPointId());
        final NoSQLDao dao = Common.databaseProxy.getNoSQLProxy().createNoSQLDao(ReportPointValueTimeSerializer.get(), "reports");
        final String pointStore = instanceId + "_" + point.getReportPointId();
        dao.getData(pointStore, 0, Long.MAX_VALUE, -1, false, new NoSQLQueryCallback() {

            @Override
            public void entry(String storeName, long timestamp, ITime entry) {
                PointValueTime pvt = (PointValueTime) entry;
                edv.setValue(pvt.getValue());
                edv.setTime(pvt.getTime());
                if (pvt instanceof AnnotatedPointValueTime)
                    edv.setAnnotation(((AnnotatedPointValueTime) pvt).getSourceMessage());
                handler.pointData(edv);
            }
        });
    }
    handler.done();
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO) SQLException(java.sql.SQLException) ExportDataValue(com.serotonin.m2m2.vo.export.ExportDataValue) ExportPointInfo(com.serotonin.m2m2.vo.export.ExportPointInfo) NoSQLQueryCallback(com.serotonin.m2m2.db.dao.nosql.NoSQLQueryCallback) NoSQLDao(com.serotonin.m2m2.db.dao.nosql.NoSQLDao) ResultSet(java.sql.ResultSet) IdPointValueTime(com.serotonin.m2m2.rt.dataImage.IdPointValueTime) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) ITime(com.serotonin.m2m2.view.stats.ITime) TextRenderer(com.serotonin.m2m2.view.text.TextRenderer)

Example 3 with NoSQLDao

use of com.serotonin.m2m2.db.dao.nosql.NoSQLDao in project ma-modules-public by infiniteautomation.

the class ReportDao method deleteReportInstance.

/**
 * Delete the instance from the system
 * @param id
 * @param userId
 */
public void deleteReportInstance(int id, int userId) {
    if (Common.databaseProxy.getNoSQLProxy() != null) {
        // Delete the NoSQL Data
        final NoSQLDao dao = Common.databaseProxy.getNoSQLProxy().createNoSQLDao(ReportPointValueTimeSerializer.get(), "reports");
        List<ExportPointInfo> points = this.getReportInstancePoints(id);
        // Drop the series for these
        for (ExportPointInfo point : points) {
            dao.deleteStore(id + "_" + point.getReportPointId());
        }
    }
    int deleted = ejt.update("delete from reportInstances where id=? and userId=?", new Object[] { id, userId });
    instanceCountMonitor.addValue(-deleted);
}
Also used : ExportPointInfo(com.serotonin.m2m2.vo.export.ExportPointInfo) NoSQLDao(com.serotonin.m2m2.db.dao.nosql.NoSQLDao)

Example 4 with NoSQLDao

use of com.serotonin.m2m2.db.dao.nosql.NoSQLDao in project ma-modules-public by infiniteautomation.

the class ReportDao method purgeReportsBefore.

public int purgeReportsBefore(final long time) {
    // Check to see if we are using NoSQL
    if (Common.databaseProxy.getNoSQLProxy() == null) {
        return ejt.update("delete from reportInstances where runStartTime<? and preventPurge=?", new Object[] { time, boolToChar(false) });
    } else {
        // We need to get the report instances to delete first
        List<ReportInstance> instances = query(REPORT_INSTANCE_SELECT + "where runStartTime<? and preventPurge=?", new Object[] { time, boolToChar(false) }, new ReportInstanceRowMapper());
        final NoSQLDao dao = Common.databaseProxy.getNoSQLProxy().createNoSQLDao(ReportPointValueTimeSerializer.get(), "reports");
        for (ReportInstance instance : instances) {
            List<ExportPointInfo> points = this.getReportInstancePoints(instance.getId());
            // Drop the series for these
            for (ExportPointInfo point : points) {
                dao.deleteStore(instance.getId() + "_" + point.getReportPointId());
            }
        }
        int deleted = ejt.update("delete from reportInstances where runStartTime<? and preventPurge=?", new Object[] { time, boolToChar(false) });
        instanceCountMonitor.addValue(-deleted);
        return deleted;
    }
}
Also used : ExportPointInfo(com.serotonin.m2m2.vo.export.ExportPointInfo) ReportInstance(com.serotonin.m2m2.reports.vo.ReportInstance) NoSQLDao(com.serotonin.m2m2.db.dao.nosql.NoSQLDao)

Aggregations

NoSQLDao (com.serotonin.m2m2.db.dao.nosql.NoSQLDao)4 ExportPointInfo (com.serotonin.m2m2.vo.export.ExportPointInfo)4 AnnotatedPointValueTime (com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime)2 IdPointValueTime (com.serotonin.m2m2.rt.dataImage.IdPointValueTime)2 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)2 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)2 ExportDataValue (com.serotonin.m2m2.vo.export.ExportDataValue)2 PointValueDao (com.serotonin.m2m2.db.dao.PointValueDao)1 NoSQLQueryCallback (com.serotonin.m2m2.db.dao.nosql.NoSQLQueryCallback)1 ReportInstance (com.serotonin.m2m2.reports.vo.ReportInstance)1 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)1 ITime (com.serotonin.m2m2.view.stats.ITime)1 TextRenderer (com.serotonin.m2m2.view.text.TextRenderer)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1