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();
}
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();
}
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);
}
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;
}
}
Aggregations